-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomer.java
More file actions
97 lines (89 loc) · 2.12 KB
/
Customer.java
File metadata and controls
97 lines (89 loc) · 2.12 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
import java.util.*;
public class Customer
{
static void checkout()
{
Scanner stdin = new Scanner(System.in);
Inventory inv = Inventory.getInstance();
TransactionHistory hist = TransactionHistory.getInstance();
Cart cart = new Cart();
float total = 0f;
char action;
System.out.println(inv);
while (true)
{
System.out.print("(S)can/(P)ay/(C)ancel: ");
action = stdin.nextLine().charAt(0);
action = Character.toUpperCase(action);
switch (action)
{
case 'S':
total += scanItem();
System.out.println(Cart.items);
System.out.println(String.format("Total: %.2f", total));
break;
case 'P':
if(payBill(total)){
for(String item : Cart.items){
hist.items.add(item);
}
hist.totals.add(total);
return;
}
case 'C':
System.out.println("Transaction Canceled");
for(String s : Cart.items){
inv.amounts.set(inv.names.indexOf(s),
inv.amounts.get(inv.names.indexOf(s)) + 1);
}
return;
default:
System.out.println("Not a valid option");
}
}
}
static float scanItem()
{
Scanner stdin = new Scanner(System.in);
Inventory inv = Inventory.getInstance();
int choice;
while (true)
{
try
{
System.out.print("What item to scan: ");
choice = Helpers.extractInt(stdin.nextLine(), inv.names.size());
if (inv.getItemAmount(choice - 1) > 0)
{
Cart.items.add(inv.getItemName(choice - 1));
inv.amounts.set(choice - 1, inv.getItemAmount(choice - 1) - 1);
return inv.getItemPrice(choice - 1);
}
System.out.println("Item out of stock!");
return 0f;
}
catch (IndexOutOfBoundsException e)
{
continue;
}
}
}
static boolean payBill(float total)
{
Scanner stdin = new Scanner(System.in);
System.out.println(String.format("Your total bill is: $%.2f", total));
while (true)
{
System.out.print("How would you like to pay (card/cash): ");
String answer = stdin.nextLine().toUpperCase();
if (answer.equals("CASH"))
{
return Payment.payWithCash(total);
}
else if (answer.equals("CARD"))
{
return Payment.payWithCard(total);
}
}
}
}