-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomers.java
More file actions
201 lines (172 loc) · 7.59 KB
/
Customers.java
File metadata and controls
201 lines (172 loc) · 7.59 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import java.util.ArrayList;
import java.io.*;
/**
* Customers
*
* Customers can view the overall marketplace listing products for sale,
* use viewMarketPlace to search for specific products using terms that match
* the name, store, or description, and sort the marketplace on price or quantity available.
* Using sortByQuantity and sortByPrice
* Customers can purchase items from the product page and
* review a history of their previously purchased items.
*/
public class Customers extends Selection {
public String username;
public String password;
//Constructor for Customers
public Customers(String username, String password) {
this.username = username;
this.password = password;
}
//Adds to purchase history when item is purchased
public void updatePurchaseHistory(String product, String store, String description,
int quantity, double price) throws IOException {
//Spaced this way just in case Vocareum throws a fit about excessive characters
PrintWriter w = new PrintWriter(new FileWriter("purchased.txt", true), true);
w.write(username + ";" + store + ";" + product + ";" + description + ";" + quantity + ";" + price);
w.println();
w.close();
}
// Return purchase history so customer can view it
public String getPurchaseHistory() throws IOException {
StringBuilder purchaseHistory = null;
BufferedReader r = new BufferedReader(new FileReader("purchased.txt"));
String line = r.readLine();
while (line != null) {
String [] arr = line.split(";");
if (arr[0].equals(username) && purchaseHistory != null) {
//makes sure that the username is already in the purchased.txt history
purchaseHistory.append("\n").append(arr[2] + ", " + arr[5] + ", " + arr[4]);
} else if (arr[0].equals(username)) {
//the String purchaseHistory can't be null for this
purchaseHistory = new StringBuilder(arr[2] + ", " + arr[5] + ", " + arr[4]);
}
line = r.readLine();
}
r.close();
assert purchaseHistory != null;
//Done to prevent a null pointer exception on the toString method
return purchaseHistory.toString();
}
// Search method using product name
public String searchProductName(ArrayList<String> list, String product) throws IOException {
StringBuilder foundProducts = null;
for (String s : list) {
String[] splitLine = s.split(";");
if (splitLine[2].equals(product)) {
if (foundProducts != null) {
foundProducts.append(", ").append(s);
} else {
foundProducts = new StringBuilder(s);
//in case the products didn't already exist
}
}
}
assert foundProducts != null;
return foundProducts.toString();
//Done to avoid possible NullPointerException error
//Note that this method is never used
}
// Search method using the store
public String searchProductStore(ArrayList<String> list, String store) throws IOException {
StringBuilder foundProducts = null;
for (String s : list) {
String[] splitLine = s.split(";");
if (splitLine[1].equals(store)) {
if (foundProducts != null) {
foundProducts.append(", ").append(s);
} else {
foundProducts = new StringBuilder(s);
//Creates the foundProducts StringBuilder since it didn't already exist
}
}
}
assert foundProducts != null;
return foundProducts.toString();
//To avoid possible NullPointerException error
//Note that this method is never used
}
// Search method using product description
public String searchProductDescription(ArrayList<String> list, String description) throws IOException {
StringBuilder foundProducts = null;
for (String s : list) {
String[] splitLine = s.split(";");
if (splitLine[3].equals(description)) {
if (foundProducts != null) {
foundProducts.append(", ").append(s);
} else {
foundProducts = new StringBuilder(s);
//creates the foundProducts StringBuilder since it didn't already exist
}
}
}
assert foundProducts != null;
return foundProducts.toString();
//To avoid possible NullPointerException error
//Note that this method is never used
}
// Return an array list after sorting the products by quantity.
// Will return full lines from product.txt
public ArrayList<String> sortByQuantity(ArrayList<String> list) throws IOException {
ArrayList<String> newMarket = null;
// Initializes the newMarket String ArrayList as null
String[] splitLine = list.get(0).split(";");
int quantity = Integer.parseInt(splitLine[4]);
newMarket.add(list.get(0));
boolean added = false;
for (int j = 1; j < list.size(); j++) {
for (int i = 0; i < newMarket.size(); i++) {
String[] splitProductLine = newMarket.get(i).split(";");
int currentQuantity = Integer.parseInt(splitProductLine[4]);
if (quantity > currentQuantity) {
newMarket.add(i, list.get(j));
added = true;
//This checks to make sure that the quantity of next products is
//greater than the existing quantity
}
}
if (!added) {
newMarket.add(list.get(j));
//This keeps the current quantity as the j position of the newMarket row
}
added = false;
//The new quantity wasn't greater so the order wasn't changed
}
return newMarket;
}
//Return an array list after sorting the products by price.
//This will return full lines from product.txt
public ArrayList<String> sortByPrice(ArrayList<String> list) throws IOException {
ArrayList<String> newMarket = null;
//Initializes the newMarket String ArrayList as null
String[] splitLine = list.get(0).split(";");
int price = Integer.parseInt(splitLine[5]);
newMarket.add(list.get(0));
boolean added = false;
for (int j = 1; j < list.size(); j++) {
for (int i = 0; i < newMarket.size(); i++) {
String[] splitProductLine = newMarket.get(i).split(";");
int currentPrice = Integer.parseInt(splitProductLine[5]);
if (price > currentPrice) {
newMarket.add(i, list.get(j));
added = true;
//This checks to make sure that the price of next products is
//greater than the existing price
}
}
if (!added) {
newMarket.add(list.get(j));
//This keeps the current price as the j position of the newMarket row
}
added = false;
//The new price wasn't greater so the order wasn't changed
}
return newMarket;
}
//This returns customer's username
public String getUsername() {
return username;
}
//Note that this method is called using HashMaps
//end of the class
}