-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpos2.java
More file actions
402 lines (329 loc) · 14.2 KB
/
pos2.java
File metadata and controls
402 lines (329 loc) · 14.2 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
//CREATED BY SYNTAXERROR
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
public class Login {
private String cashierName;
private String branchName;
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(r);
//starts everything
private void startSystem() throws IOException{
//get cashier name and branch name
try {
System.out.println("Cashier Name: ");
cashierName = br.readLine();
System.out.println("Branch: ");
branchName = br.readLine();
//while loop is used to keep coming back to main menu
while (true) {
boolean exit = enterMenu();
if(exit)
break;
}
}catch (IOException ioe){
ioe.printStackTrace();
System.exit(1);
}finally {
br.close();
r.close();
}
}
private boolean enterMenu() throws IOException{
//main menu
String choice = null;
System.out.println("══Super-Saving Supermarket══");
System.out.println();
System.out.println("1. New Bill");
System.out.println("2. Pending BIlls");
System.out.println("3. Exit");
//while loop used to keep looping until valid input is given
while (true) {
System.out.println("Choice: ");
//We will keep using replace all to ensure no spaces
try {
choice = br.readLine().replaceAll(" ", "");
}catch(IOException e){
e.printStackTrace();
System.exit(1);
}
if(choice.equals("3"))
return true;
else if(choice.equals("1")){
//leaving this blank means not registered.
System.out.println("Enter Customer Name (Leave Blank if Needed): ");
String customerName = new Scanner(System.in).nextLine();
if(customerName.isBlank()){
customerName = "N/A";
}
//POS class called with a new bill to issue
POS biller = new POS();
biller.startBillingProcess(new Bill(cashierName, branchName, customerName));
break;
}else if(choice.equals("2")){
//this loads a bill using serialization.
System.out.println("Enter ID of Bill to Load: ");
String id = br.readLine();
BillSaver.loadAndContinueBill(id);
break;
}else
System.out.println("Invalid Input");
}
return false;
}
public static void main(String[] args) {
//initialize the grocery data
Groceries.initMap();
try {
new Login().startSystem();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
}
//instances of this class will become the bills of customers.
class Bill implements Serializable{
private String cashier, branch, customer;
//the items will be stored inside an array list of GroceryItems
private final ArrayList<GroceryItem> items = new ArrayList<>();
public Bill(String cashier, String branch, String customer){
this.customer = customer;
this.cashier = cashier;
this.branch = branch;
}
public void addToList(GroceryItem item){
items.add(item);
}
//we optionally added a remove from list items in case the cashier made a mistake
public void removeFromList(){
if(items.isEmpty()){
System.out.println("No Items to Remove!");
}else{
int last = items.size() -1;
GroceryItem itemToRemove = items.get(last);
items.remove(last);
System.out.println("Successfully Removed the Item: "+itemToRemove.getName());
}
}
//this will print the bill in a table, doing necessary calculations for items of customer
public void printBill(){
double netTotal = 0.00;
double unitPriceTotal = 0.00;
System.out.println("══Super-Saving Supermarket══");
System.out.println(" Cashier Name: "+ cashier);
System.out.println(" Branch Name : "+ branch);
System.out.println(" Customer Name: "+ customer);
System.out.println();
System.out.println(" ╔════════════════╦═══════════════╦══════════╦═════════════╦══════════════╗");
System.out.println(" ║ Item Name ║Unit Price (Rs)║ Qty. ║Discount (Rs)║Net Price (Rs)║");
System.out.println(" ╠════════════════╬═══════════════╬══════════╬═════════════╬══════════════╣");
for (GroceryItem item: items) {
double netPrice = item.getPrice()*item.getWeightOrSize()*(1-item.getDiscount());
netTotal += netPrice;
unitPriceTotal += (item.getPrice() * item.getWeightOrSize());
System.out.printf(" ║%16s║%15.2f║%10.2f║%13.2f║%14.2f║%n",
item.getName(),
item.getPrice(),
item.getWeightOrSize(),
item.getDiscount()*item.getPrice()*item.getWeightOrSize(),
netPrice
);
}
System.out.println(" ╚════════════════╩═══════════════╩═════╦════╩═════════════╬══════════════╣");
System.out.println(" ║Tot. Discount (Rs)║Tot. Price(Rs)║");
System.out.println(" ╠══════════════════╬══════════════╣");
System.out.printf( " ║%18.2f║%14.2f║%n",
(unitPriceTotal-netTotal),
netTotal
);
System.out.println(" ╚══════════════════╩══════════════╝");
System.out.println();
LocalDateTime currentDateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = currentDateTime.format(formatter);
System.out.println("Issued Date and Time: " + formattedDateTime);
System.out.println();
System.out.println("Come Back Again!");
System.out.println();
System.out.println();
}
}
//this class deals with retrieving items and adding them to the bill, removing items from bill and saving a bill for later
//or exiting to print the bill
class POS{
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(r);
public void startBillingProcess(Bill bill) {
//here we give a choice to checkout, pause or remove last item
System.out.println("Tip: Enter E to Checkout, P to Pause Temporarily or R to Remove Last Item");
while(true){
try {
System.out.println("Enter Item Code: ");
String choice = br.readLine().replaceAll(" ", "");
if(choice.equals("E")){
bill.printBill();
break;
} else if (choice.equals("P")) {
//the bill will be saved in a serialized file with a file name chosen by the cashier
System.out.println("Enter an ID to Save Bill as (REMEMBER THIS): ");
String id = br.readLine();
int x = BillSaver.saveBill(bill, id);
if(x == -1)
continue;
System.out.println("Saved Successfully with ID "+id);
break;
} else if(choice.equals("R")){
bill.removeFromList();
}else {
GroceryItem nextItem = getItemDetails(choice);
bill.addToList(nextItem);
}
} catch (IOException e) {
System.out.println("An Unexpected Error Occured: "+e.getMessage());
} catch (ItemNotFoundException e){//created and handled the ItemNotFoundException as asked
System.out.println("No Item Found!");
}catch (NumberFormatException nfe){
System.out.println("Enter a Valid Item Code!");
}
}
}
//modified getItemDetails function. it gets the details of an item from the Groceries class as a whole string and splits it and processes it accordingly.
private GroceryItem getItemDetails(String item_code) throws IOException, ItemNotFoundException, NumberFormatException{
int itemCode = Integer.parseInt(item_code);
String detail = Groceries.getStringOfDetails(itemCode);
String[] details = detail.split(",");
String name = details[0], manufacturer = details[1], mfd = details[3], exp = details[4];
double unitPrice = Double.parseDouble(details[2]);
System.out.println("Quantity? ");
double quantity = Double.parseDouble(br.readLine());
double price = unitPrice;
int discount = createDiscount();
System.out.printf ("Name:......... %s\n" +
"Qty:.......... %.2f\n" +
"Price:........ Rs %.2f\n" +
"Manufacturer:. %s\n" +
"MFD:.......... %s\n" +
"EXP:.......... %s\n" +
"Discount:..... %d%%",name,quantity,price,manufacturer,mfd, exp,discount);
System.out.println();
//creates and returns a grocery item.
GroceryItem item = new GroceryItem(itemCode, price, quantity, mfd, exp, manufacturer, name, discount/100.0);
return item;
}
//created a function that gives a 50% chance of having a discount between 1% and 75%
private int createDiscount() {
int chance = new Random().nextInt(1, 101);
int discountChance = 50;
if(chance > 100 - discountChance){
System.out.println("Discount Given!");
return new Random().nextInt(1, 76);
}
return 0;
}
}
//the class for the ItemNotFoundException
class ItemNotFoundException extends Exception{
public ItemNotFoundException(){
super("ITEM NOT FOUND!");
}
}
//this class contains the hardcoded details of the groceries
class Groceries{
private static HashMap<Integer, String> groceryDB = new HashMap<>();
//itemCode, name, manufacturer, size/weight, price, mfd, exp date
//hardcoded items
public static void initMap(){
groceryDB.put(1, "Beans,Alankulama,300.00,2023-03-03,2023-04-03");
groceryDB.put(2, "Carrots,Alankulama,1000.00,2023-03-03,2023-04-03");
}
//return the huge string of details to be processed.
public static String getStringOfDetails(int code) throws ItemNotFoundException {
if(groceryDB.containsKey(code))
return groceryDB.get(code);
else{
throw new ItemNotFoundException();
}
}
}
//this class allows instances of grocery items.
class GroceryItem implements Serializable{
private int itemCode;
private double price;
public double getPrice() {
return price;
}
public double getWeightOrSize() {
return weightOrSize;
}
public String getName() {
return name;
}
public double getDiscount() {
return discount;
}
private double weightOrSize;
private String DOM;
private String DOE;
private String manufacturer;
private String name;
private double discount;
public GroceryItem(int itemCode, double price, double weightOrSize, String DOM, String DOE, String manufacturer, String name, double discount) {
this.itemCode = itemCode;
this.price = price;
this.weightOrSize = weightOrSize;
this.DOM = DOM;
this.DOE = DOE;
this.manufacturer = manufacturer;
this.name = name;
this.discount = discount;
}
}
//the class that saves/loads bills from .ser files.
class BillSaver{
//method to save bill
public static int saveBill(Bill bill, String id){
String path = id+".ser";
try {
//we make sure that a bill has a unique id before saving.
File file = new File(path);
if(file.exists()){
System.out.println("ID TAKEN!");
return -1;
}
new File(path);
FileOutputStream fos = new FileOutputStream(path);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(bill);
os.close();
}catch(IOException ex) {
throw new RuntimeException(ex);
}
return 0;
}
//method to load bill
public static void loadAndContinueBill(String id) {
String path = id+".ser";
Bill billToLoad = null;
try {
FileInputStream fis = new FileInputStream(path);
ObjectInputStream os = new ObjectInputStream(fis);
Object gotIt = os.readObject();
billToLoad = (Bill) gotIt;
} catch (IOException e) {
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
if(billToLoad == null){
System.out.println("No such Bill Found!");
}else{
System.out.println("Successfully Found Bill!");
POS biller = new POS();
biller.startBillingProcess(billToLoad);
//makes sure to delete the serialized files after we are done with it.
File file = new File(path);
file.delete();
}
}
}