-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStore.java
More file actions
226 lines (200 loc) · 6.14 KB
/
Store.java
File metadata and controls
226 lines (200 loc) · 6.14 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// --== CS400 File Header Information ==--
// Name: Youlin Qu
// Email: yqu39@wisc.edu
// Team: NC
// Back End Developer
// TA: Daniel Finer
// Lecturer: Florian Heimerl
// Notes to Grader: <optional extra notes>
import java.io.File;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
import java.util.Scanner;
/**
* A store class that would work any implemented RBT with methods of adding,
*/
public class Store {
private RedBlackTree<Product> products; // The RBT that contains all the products
private static class Product implements Comparable<Product> {
private String name; // name of product
private double price; // price of the product
private Product next; // linked to a product which has the same price;
public Product(String name, double price) {
this.name = name;
this.price = price;
this.next = null;
}
@Override
public int compareTo(Product o) {
return Double.compare(this.price, o.price);
}
@Override
public String toString() {
String str = "(" + this.name + ": $" + this.price + ")";
Product curr = this;
while (curr.next != null) {
str += "\n(" + curr.next.name + ": $" + curr.next.price + ")";
curr = curr.next;
}
return str;
}
}
public Store() {
products = new RedBlackTree<>();
}
/**
* adding a product to the RBT with its name and price the price of the product should ne unique.
*
* @param name of the product
* @param price of the product
* @throws IllegalArgumentException when the price is duplicated
*/
public void add(String name, double price) {
// if(lookUp(price) != null)
// {
// System.out.println("Trying to add " + new Product(name, price));
// System.out.println("Already existed " + lookUp(price));
// throw new IllegalArgumentException();
// }
// products.insert(new Product(name, price));
Product curr = lookUpHelper(price, products.root);
if (curr != null) {
while (curr.next != null) {
curr = curr.next;
}
curr.next = new Product(name, price);
} else {
products.insert(new Product(name, price));
}
}
/**
* look up the product by its price.
*
* @param price of the product
* @return the String representation of the product
*/
public String lookUp(double price) {
RedBlackTree.Node<Product> pointer = products.root;
return lookUpHelper(price, pointer).toString();
}
/**
* recursive lookup helper method
*
* @param price price of the target
* @param parent current parent of the traversing process
* @return a string representation of the product
*/
private Product lookUpHelper(double price, RedBlackTree.Node<Product> parent) {
if (parent == null)
return null;
if (parent.data.price == price)
return parent.data; // changed == / .equals
Product left = lookUpHelper(price, parent.leftChild);
if (left != null)
return left;
Product right = lookUpHelper(price, parent.rightChild);
if (right != null)
return right;
return null;
}
/**
*
* @param name
* @return
*/
public String lookUp(String name) {
RedBlackTree.Node<Product> pointer = products.root;
return lookUpHelper(name, pointer);
}
/**
* recursive lookup helper method
*
* @param price price of the target
* @param parent current parent of the traversing process
* @return a string representation of the product
*/
private String lookUpHelper(String name, RedBlackTree.Node<Product> parent) {
if(parent == null) return null;
String output = "";
if(parent.data.name.equals(name))
{
Product curr = parent.data;
output += "(" + curr.name + ": $" + curr.price + ")" + "\n"; //changed == / .equals
while(curr.next != null)
{
curr = curr.next;
if(curr.name.equals(name))
{
output += "(" + curr.name + ": $" + curr.price + ")" + "\n";
}
}
}
String left = lookUpHelper(name, parent.leftChild);
if(left != null) output += left;
String right = lookUpHelper(name, parent.rightChild);
if(right != null) output += right;
return output;
}
/**
* lookup the product with highest price
*
* @return a string representation of the product
*/
public String lookMax() {
RedBlackTree.Node<Product> pointer = products.root;
while (pointer.rightChild != null) {
pointer = pointer.rightChild;
}
return pointer.data.toString();
}
/**
* lookup the product with lowest price
*
* @return a string representation of the product
*/
public String lookMin() {
RedBlackTree.Node<Product> pointer = products.root;
while (pointer.leftChild != null) {
pointer = pointer.leftChild;
}
return pointer.data.toString();
}
/**
*
* print "file " + filename + " added" or "file " + filename " adding failed"
*/
public void addFile(String fileName) {
try {
Scanner sc = new Scanner(new File(fileName));
Scanner sc2;
while (sc.hasNext()) {
sc2 = new Scanner(sc.nextLine());
String name = sc2.next();
if (name.equals(""))
break;
double price = Double.parseDouble(sc2.next());
add(name, price);
}
System.out.println("file " + fileName + " added");
} catch (FileNotFoundException fnf) {
System.out.println("File doesn't exist");
System.out.println("file " + fileName + " adding failed");
} catch (NoSuchElementException | NumberFormatException e) {
e.printStackTrace();
System.out.println("Your file contents are not in the right format");
System.out.println("file " + fileName + " adding failed");
}
}
/**
* Clears the store
*
* @return
*/
public void clear() {
this.products = new RedBlackTree<>();
}
@Override
public String toString() {
return products.toString();
}
}