-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSellers.java
More file actions
154 lines (137 loc) · 6.24 KB
/
Sellers.java
File metadata and controls
154 lines (137 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import java.util.*;
import java.io.*;
import java.net.*;
/**
* Sellers
*
* Sellers allows the seller to add products to a store (with addProduct),
* delete products from a store (deleteProduct), get a String with all
* the products in products.txt (getProducts), add a store to the stores.txt file
* (addStore), and get a String with all the stores in stores.txt (getStores)
*
* addProduct - adds a product to the products.txt file with its basic characteristics,
* such as the seller's name, the store it is sold at, the name of the product,
* a description of the product, the quantity available of the product,
* and the price of the product
*
* deleteProduct - deletes a product from a store and updates the products.txt file
* to match the change. Any product that does not match the product parameter will be
* added to the allProducts ArrayList. The products.txt file updates to match allProducts.
*
* getProducts - returns a string with all the products in products.txt
*
* addStore - adds a store to the stores.txt file, along with the username of the seller
* that created the store.
*
* getStores - returns a string with all the stores in stores.txt
*/
public class Sellers extends Selection{
public String username;
public String password;
//Constructor for the Sellers
public Sellers(String username, String password) {
this.username = username;
this.password = password;
}
//This method adds a new product to a store based on the seller's username,
//the name of the store, a description of the product, the product quantity,
//and the price of the product.
public void addProduct(String product, String store, String description,
int quantity, double price) throws IOException {
PrintWriter pw = new PrintWriter(new FileWriter("products.txt", true), true);
pw.write(username + ";" + store + ";" + product + ";" + description + ";" + quantity + ";" + price);
pw.println();
pw.close();
}
//This method deletes products the seller wants to get rid of, and updates the
//products.txt file to only include the remaining, viable, products.
public void deleteProduct(String product) throws IOException {
ArrayList<String> allProducts = new ArrayList<String>();
BufferedReader br = new BufferedReader(new FileReader("products.txt"));
String line = br.readLine();
//Read in the contents of the products.txt file
String useless = "";
while (line != null) {
String[] splitLine = line.split(";");
if (splitLine[0].equals(username) && splitLine[2].equals(product)) {
useless += line;
} else {
System.out.println("That product does not exist");
allProducts.add(line);
}
//The product is added to the list of allProducts if there is not a match
//between the username and the product name. Since this method is meant to
//delete the existing products, any product that hasn't been selected for deletion
//would be part of the remaining allProducts list, and ultimately the products.txt file.
line = br.readLine();
}
PrintWriter pw = new PrintWriter(new FileWriter("products.txt"), true);
for (String allProduct : allProducts) {
pw.print(allProduct + "\n");
//This adds the products that haven't been deleted to the products.txt file.
}
pw.close();
}
//This method returns String with all the products in products.txt
public String getProducts() throws IOException {
StringBuilder allProducts = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader("products.txt"));
String line = br.readLine();
int counter = 0;
while (line != null) {
String[] splitLine = line.split(";");
if (splitLine[0].equals(username) && counter > 0) {
allProducts.append(", ").append(splitLine[2]);
//This creates a comma separated StringBuilder with the products in products.txt
} else {
allProducts.append(splitLine[2]);
counter++;
//This adds the product (3rd element of the products.txt line)
//to the allProducts StringBuilder.
}
line = br.readLine();
}
return allProducts.toString();
}
//This method lets the Seller add a store to the stores.txt file
public void addStore(String store) throws IOException {
PrintWriter pw = new PrintWriter(new FileWriter("stores.txt", true), true);
pw.write(username + ";" + store);
pw.println();
pw.close();
}
//This method returns a String with all the stores from stores.txt
public String getStores() throws IOException {
ArrayList<String> sellerStores = new ArrayList<String>();
String allStores = "";
//This string contains the names of all the stores
BufferedReader br = new BufferedReader(new FileReader("stores.txt"));
String line = br.readLine();
int counter = 0;
while (line != null) {
String[] splitLine = line.split(";");
if (splitLine[0].equals(username) && counter > 0) {
sellerStores.add(", " + splitLine[1]);
//This creates a comma separated ArrayList with the stores in stores.txt as its indexes
} else if (splitLine[0].equals(username)) {
sellerStores.add(splitLine[1]);
counter++;
//This adds the store (2nd element of the stores.txt line)
//to the sellerStores ArrayList.
}
line = br.readLine();
}
for (int i = 0; i < sellerStores.size(); i++) {
allStores = allStores + sellerStores.get(i);
}
//This loop will add all the stores from the sellerStores ArrayList into the string
//allStores. allStores contains the names of all the stores.
return allStores;
}
//A getter that relays the username
public String getUsername() {
return username;
}
//Note that this method is called using HashMaps
//end of the class
}