-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoreHandler.java
More file actions
286 lines (241 loc) · 9.58 KB
/
StoreHandler.java
File metadata and controls
286 lines (241 loc) · 9.58 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URL;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Scanner;
/*
Written by: Emil Ljung
Project: Arbetsprov
OS: Windows 7 Ultimate 64-bit, Service Pack 1
Platform: NetBeans IDE 8.1
Java version: Java 8 Update 73
*/
public class StoreHandler implements BookList
{
private ArrayList<Book> inStock;
private ArrayList<Integer> amtBooksInStore; //Amount of the book inStock.get(i) is amtBooks.get(i)
private Cart cart;
private Integer[] searchAmt;
public StoreHandler()
{
this.inStock = new ArrayList<>();
this.amtBooksInStore = new ArrayList<>(); //Remeber to sort this correctly if this.inStock is sorted!
this.cart = new Cart();
}
public enum Status
{
//Used in buy(Book... books)
OK(0), NOT_IN_STOCK(1), DOES_NOT_EXIST(2);
private int value;
private Status(int value)
{
this.value = value;
}
}
@Override
public boolean setup()
{
//Put all books in the URL in the store.
boolean success = true;
try
{
//Read info from this URL
URL url = new URL("http://www.contribe.se/bookstoredata/bookstoredata.txt");
Scanner s = new Scanner(url.openStream());
String[] lineContent;
//Prepare for String -> BigDecimal convertion
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setGroupingSeparator(',');
symbols.setDecimalSeparator('.');
String pattern = "#,##0.0#";
DecimalFormat decimalFormat = new DecimalFormat(pattern, symbols);
decimalFormat.setParseBigDecimal(true);
while(s.hasNextLine())
{
//Split line content and put them in an array
lineContent = s.nextLine().split(";");
try
{
//Add the books to the store
this.amtBooksInStore.add(Integer.parseInt(lineContent[3]));
this.inStock.add(new Book(lineContent[0], lineContent[1], (BigDecimal)decimalFormat.parse(lineContent[2])));
}
catch(ParseException pe)
{ success = false; }
}
}
catch(IOException e)
{ success = false; }
return success;
}
@Override
public void addToStore(Book book, int amount)
{
//Adds a book to the store or increases the amount of a book
boolean addNewBook = true;
int i = 0;
while(i < this.inStock.size() && addNewBook != false)
{
if(this.inStock.get(i).isEqual(book))
{
//Only increase amount that specific book if the book already exists in the store
this.amtBooksInStore.set(i, amount + this.amtBooksInStore.get(i));
System.out.println("\nThe amount of that book is increased.");
addNewBook = false;
}
i++;
}
if(addNewBook == true)
{
//Add a new book to the store
this.amtBooksInStore.add(amount);
this.inStock.add(book);
System.out.println("\nThe book has been added to the store!");
}
}
@Override
public Book[] getAllBooks()
{
//Returns an array of all books in the store (even the ones not in stock)
//Prepare for getAmountArray() in main loop
this.searchAmt = new Integer[this.amtBooksInStore.size()];
this.searchAmt = this.amtBooksInStore.toArray(this.searchAmt);
return this.inStock.toArray(new Book[this.inStock.size()]);
}
//@Override
public boolean removeFromCart(Book book, int amount)
{
//Removes x amount of a specific book from the cart.
//If amount variable equals the amount of that book it is removed from the cart.
boolean success = false;
int i = 0;
while(i < this.cart.getBookArray().length && success == false)
{
if(this.cart.getBookArray()[i].isEqual(book))
{
if(this.cart.getNrOfBooksArray()[i] > amount)
{
//Reduce the amount of that book in the cart with x
System.out.print("\nAmount is reduced from " + this.cart.getNrOfBooksArray()[i]);
this.cart.setNrOfBook(i, this.cart.getNrOfBooksArray()[i] - amount);
System.out.print(" to " + this.cart.getNrOfBooksArray()[i] + "\n");
success = true;
}
else if(this.cart.getNrOfBooksArray()[i] == amount)
{
//Remove the book from the cart
this.cart.remove(i);
System.out.println("\nRemoved from the cart.");
success = true;
}
else
{
//The amount of books the user tries to remove is too high!
System.out.println("\nThe amount of that book in your cart is " + this.cart.getNrOfBooksArray()[i] + "!");
success = true;
}
}
i++;
}
if(success == false)
{
System.out.println("\nThat book doesn't exist in the cart!");
}
return success;
}
@Override
public Book[] list(String searchString)
{
//Returns an array of all books with the title and/or author the user searched for.
ArrayList<Book> list = new ArrayList<>();
ArrayList<Integer> amtList = new ArrayList<>();
for(int i = 0; i < this.inStock.size(); i++)
if(this.inStock.get(i).isEqual(searchString))
{
//Only add books that matches searchString
list.add(this.inStock.get(i));
amtList.add(this.amtBooksInStore.get(i));
}
//Prepare for getAmountArray() in main loop
this.searchAmt = new Integer[amtList.size()];
this.searchAmt = amtList.toArray(this.searchAmt);
//An array of the matched books is returned
return list.toArray(new Book[list.size()]);
}
@Override
public boolean add(Book book, int amount)
{
//Adds x numbers of a book to the cart.
this.cart.add(book, amount);
return true;
}
@Override
public int[] buy(Book... books)
{
//Sets and returns status of all books,
//prints the sum of all OK(0) books and empties the cart.
int[] returnValue = new int[books.length];
BigDecimal total_price = new BigDecimal("0.00");
Integer[] amtBooks = this.cart.getNrOfBooksArray();
//Loop through the cart
for(int i = 0; i < books.length; i++)
{
int k = 0;
boolean breakLoop = false;
//Loop through the book store
while(k < this.inStock.size() && breakLoop == false)
{
if(books[i].isEqual(this.inStock.get(k)))
{
if(amtBooks[i] == 0)
{
//It would be strange to buy 0 books, right?
returnValue[i] = Status.NOT_IN_STOCK.value;
breakLoop = true;
}
else if(this.amtBooksInStore.get(k) >= amtBooks[i])
{
//The amount of books in store is reduced & total_price is increased
returnValue[i] = Status.OK.value;
this.amtBooksInStore.set(k, this.amtBooksInStore.get(k) - amtBooks[i]);
BigDecimal amount = new BigDecimal(amtBooks[i]);
total_price = (total_price.add(amount.multiply(books[i].getPrice())));
breakLoop = true;
}
else
{
//The book is not in stock
returnValue[i] = Status.NOT_IN_STOCK.value;
breakLoop = true;
}
}
k++;
}
if (breakLoop == false)
{
//The book doesn't exist in the store
returnValue[i] = Status.DOES_NOT_EXIST.value;
}
//When the loop is done the cart is will be empty
this.cart.remove(0);
}
System.out.println("\nTotal price for all OK: " + total_price);
//Return an array of statuses of the books which were in the cart.
return returnValue;
}
public Book[] getCartContent()
{
return this.cart.getBookArray();
}
public Integer[] getNrOfBooksInCart()
{
return this.cart.getNrOfBooksArray();
}
public Integer[] getAmountArray()
{
return this.searchAmt;
}
}