-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoffeeSerive.java
More file actions
232 lines (179 loc) · 7.94 KB
/
Copy pathCoffeeSerive.java
File metadata and controls
232 lines (179 loc) · 7.94 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
package ezen_coffee_hsw;
import java.text.DecimalFormat;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
/**
* CoffeeSerive
*/
public class CoffeeSerive {
private boolean reOrder = false;
private int orderNum = 1;
// private static CoffeeSerive cS = new CoffeeSerive();
public CoffeeSerive() {
orderList = new LinkedHashMap<>();
}
// public static CoffeeSerive getInstance() {
// return cS;
// };
// 커피 객체 준비
Coffee coffee = Coffee.getInstance();
// 손님
Customer customer;
// 지연쓰레드 준비
Thread t = new Thread();
// 주문받은 음료 이름과 수량 담을 map 선언
Map<String, Integer> orderList;
Scanner sc = new Scanner(System.in);
public void start() {
System.out.println("\n 어서오세요 Ezen커피샵 입니다 ");
customer = new Customer(orderNum);
coffee.getMenu(); // 메뉴판
// 주문 메소드
order();
// 최종 주문 내역
totalOrder(customer);
try {
System.out.println("기다려주시면 주문하신 음료가 나옵니다 ");
t.sleep(5000);
end();
} catch (Exception e) {
}
}
// 음료 결과
public void end() {
int s = 1;
StringBuffer message = new StringBuffer();
message.append("\n\n ")
.append("+----------------------------------------------------+\n ")
.append("| | \n ")
.append("| " + customer.getOrderName() + "고객님 주문하신 음료 나왔습니다 | " + "\n");
System.out.print(message);
for (Entry<String, Integer> order : customer.getCoffeOrder().entrySet()) {
System.out.printf(" | [%d] %-13s\t: %2d잔 %7s | \n", s, order.getKey(), order.getValue(), "");
s++;
}
System.out.println(" | |");
System.out.println(" +----------------------------------------------------+");
}
// 주문
private void order() {
System.out.println("\n취소를 원하시면 0번을 눌러주세요");
end: while (true) { // 추가 주문할수도 있기 때문에 무한루프
try {
System.out.print("\n원하시는 음료의 번호를 선택해주세요 > ");
String choice = sc.next();
int choiceNum = Integer.parseInt(choice.substring(0, 1)); // 개수 확인
if (choiceNum == 0) {
System.out.println("주문을 취소합니다 ");
System.exit(0);
}
sc.nextLine();
String coffeName = coffee.coffeList.get(choiceNum - 1);
System.out.println("선택하신 음료는 : " + coffeName + "입니다 몇 잔을 주문하시겠습니까?");
int orderCount = sc.nextInt();
sc.nextLine(); // 버퍼 삭제
// 재주문 if문
if (reOrder) {
for (String coff : orderList.keySet()) {
if (coff.equals(coffeName)) {
int addCount = orderList.get(coff).intValue() + orderCount;
orderList.replace(coffeName, addCount);
break;
} else {
orderList.put(coffeName, orderCount);
break;
}
}
// ConcurrentModificationException 오류 해결 -> 내부 반복문이 아닌 외부 반복문으로 수정
// for (Entry<String, Integer> e : orderList.entrySet()) {
// if (e.getKey().equals(coffeName)) {
// int addCount = e.getValue() + orderCount; // 기존 개수에 추가
// orderList.replace(coffeName, addCount);
// break;
// } else {
// orderList.put(coffeName, orderCount);
// }
// }
} else {
orderList.put(coffeName, orderCount); // key : 커피 이름 v = 개수
}
// 추가 주문 메소드
addOrder();
customer.setCoffeOrder(orderList);
break end;
} catch (Exception e) {
System.out.println("\n잘못된 선택입니다");
// e.printStackTrace(); // 에러메시지 테스트 끝나면 주석
continue;
}
}
}
// 추가주문 여부
private void addOrder() {
reOrder = false;
System.out.println("\n주문을 계속하시겠습니까?");
System.out.println("예(Y) / 아니요(N)");
String yesOrNo = sc.next();
sc.nextLine();
// 추가 주문시 메뉴판 보여주고 리스트에 추가
if (yesOrNo.equals("예") || yesOrNo.equalsIgnoreCase("y")) {
coffee.getMenu();
reOrder = true;
order();
} else if (yesOrNo.equals("아니요") || yesOrNo.equalsIgnoreCase("n")) {
return;
}
}
// 최종 주문 결과
private void totalOrder(Customer customer) {
int s = 1;
int totalmoney = 0;
int coffePrice = 0;
DecimalFormat f = new DecimalFormat("###,000원");
String name = customer.getOrderName() + "번 ";
StringBuffer message = new StringBuffer();
message.append("\n\n ")
.append("+----------------------------------------------------+\n ")
.append("| | \n ")
.append("| " + name + "고객님 의 주문 내역 입니다 | " + "\n");
for (Entry<String, Integer> order : customer.getCoffeOrder().entrySet()) {
coffePrice = coffee.menu.get(order.getKey()).intValue()
* customer.getCoffeOrder().get(order.getKey()).intValue();
totalmoney += coffePrice;
String pay = f.format(coffePrice);
message.append(
String.format(" | [%d] %-13s\t: %2d잔 %7s | \n", s, order.getKey(), order.getValue(), pay));
s++;
}
message.append(" | |\n ")
.append("+----------------------------------------------------+ \n")
.append(" ============ 총 결제 금액은 " + f.format(totalmoney) + "입니다 ========== \n");
System.out.println(message);
payment(totalmoney);// 결제 메소드
return;
}
// 결제 메소드
private void payment(int totalmoney) {
System.out.println("\n 결제를 도와드리겠습니다 카드를 넣어주세요 ");
int payResult = customer.getMoney() - totalmoney;
try {
t.sleep(2500);
System.out.println("결제중입니다 .....");
t.sleep(3000);
} catch (InterruptedException e) {
}
if (payResult <= 0) {
System.out.println("잔액이 부족합니다 \n 확인후 다시 주문해주세요 ");
System.exit(0); // 종료
} else {
int paycalc = customer.getMoney() - totalmoney; //
customer.setMoney(paycalc);
System.out.println("결제가 완료 되었습니다 \n ");
System.out.println("이용해주셔서 감사합니다 ");
orderNum++; // 다음 고객 번호 증가
}
return;
}
}