-
Notifications
You must be signed in to change notification settings - Fork 1
/
Manager
153 lines (142 loc) · 5.27 KB
/
Manager
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Manager;
import Entity.Fruit;
import Entity.Order;
import java.util.ArrayList;
import java.util.Hashtable;
/**
*
* @author Quang Hiep
*/
public class Manager {
//display menu
static int menu() {
//loop until user want to exit
System.out.println("1. Create Fruit");
System.out.println("2. View orders");
System.out.println("3. Shopping (for buyer)");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice = Validation.checkInputIntLimit(1, 4);
return choice;
}
//allow user create fruit
static void createFruit(ArrayList<Fruit> lf) {
//loop until user don't want to create fruit
while (true) {
System.out.print("Enter fruit id: ");
String fruitId = Validation.checkInputString();
//check id exist
if (!Validation.checkIdExist(lf, fruitId)) {
System.out.println("Id exist");
return;
}
System.out.print("Enter fruit name: ");
String fruitName = Validation.checkInputString();
System.out.print("Enter price: ");
double price = Validation.checkInputDouble();
System.out.print("Enter quantity: ");
int quantity = Validation.checkInputInt();
System.out.print("Enter origin: ");
String origin = Validation.checkInputString();
lf.add(new Fruit(fruitId, fruitName, price, quantity, origin));
//check user want to continue or not
if (!Validation.checkInputYN()) {
return;
}
}
}
//allow user show view order
static void viewOrder(Hashtable<String, ArrayList<Order>> ht) {
for (String name : ht.keySet()) {
System.out.println("Customer: " + name);
ArrayList<Order> lo = ht.get(name);
displayListOrder(lo);
}
}
//allow user buy items
static void shopping(ArrayList<Fruit> lf, Hashtable<String, ArrayList<Order>> ht) {
//check list empty user can't buy
if (lf.isEmpty()) {
System.out.println("No have item.");
return;
}
//loop until user don't want to buy continue
ArrayList<Order> lo = new ArrayList<>();
while (true) {
displayListFruit(lf);
System.out.print("Enter item: ");
int item = Validation.checkInputIntLimit(1, lf.size());
Fruit fruit = getFruitByItem(lf, item);
System.out.print("Enter quantity: ");
int quantity = Validation.checkInputIntLimit(1, fruit.getQuantity());
fruit.setQuantity(fruit.getQuantity() - quantity);
//check item exist or not
if (!Validation.checkItemExist(lo, fruit.getFruitId())) {
updateOrder(lo, fruit.getFruitId(), quantity);
} else {
lo.add(new Order(fruit.getFruitId(), fruit.getFruitName(),
quantity, fruit.getPrice()));
}
if (!Validation.checkInputYN()) {
break;
}
}
displayListOrder(lo);
System.out.print("Enter name: ");
String name = Validation.checkInputString();
ht.put(name, lo);
System.out.println("Add successfull");
}
//display list fruit in shop
static void displayListFruit(ArrayList<Fruit> lf) {
int countItem = 1;
System.out.printf("%-10s%-20s%-20s%-15s\n", "Item", "Fruit name", "Origin", "Price");
for (Fruit fruit : lf) {
//check shop have item or not
if (fruit.getQuantity() != 0) {
System.out.printf("%-10d%-20s%-20s%-15.0f$\n", countItem++,
fruit.getFruitName(), fruit.getOrigin(), fruit.getPrice());
}
}
}
//get fruint user want to by
static Fruit getFruitByItem(ArrayList<Fruit> lf, int item) {
int countItem = 1;
for (Fruit fruit : lf) {
//check shop have item or not
if (fruit.getQuantity() != 0) {
countItem++;
}
if (countItem - 1 == item) {
return fruit;
}
}
return null;
}
//display list order
static void displayListOrder(ArrayList<Order> lo) {
double total = 0;
System.out.printf("%15s%15s%15s%15s\n", "Product", "Quantity", "Price", "Amount");
for (Order order : lo) {
System.out.printf("%15s%15d%15.0f$%15.0f$\n", order.getFruitName(),
order.getQuantity(), order.getPrice(),
order.getPrice() * order.getQuantity());
total += order.getPrice() * order.getQuantity();
}
System.out.println("Total: " + total);
}
//if order exist then update order
static void updateOrder(ArrayList<Order> lo, String id, int quantity) {
for (Order order : lo) {
if (order.getFruitId().equalsIgnoreCase(id)) {
order.setQuantity(order.getQuantity() + quantity);
return;
}
}
}
}