-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrarySystem.java
More file actions
299 lines (247 loc) · 9.28 KB
/
LibrarySystem.java
File metadata and controls
299 lines (247 loc) · 9.28 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
package Subject;
import java.util.*;
import java.text.SimpleDateFormat;
class Book {
private String id;
private String name;
private int price;
private String author;
private String description;
private String category;
private String dateIssued;
public Book(String id,
String name,
int price,
String author,
String description,
String category,
String dateIssued) {
this.id = id;
this.name = name;
this.price = price;
this.author = author;
this.description = description;
this.category = category;
this.dateIssued = dateIssued;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
public String getAuthor() {
return author;
}
public String getDescription() {
return description;
}
public String getCategory() {
return category;
}
public String getDateIssued() {
return dateIssued;
}
public String toString() {
return String.format("%s | %s | %d원 | %s | %s | %s | %s\n",
getId(),
getName(),
getPrice(),
getAuthor(),
getDescription(),
getCategory(),
getDateIssued()
);
}
}
class SelectedBook {
private String id;
private int quantity;
private int totalPrice;
public SelectedBook(
String id,
int totalPrice) {
this.id = id;
this.quantity = 1;
this.totalPrice = totalPrice;
}
public String getId() {
return id;
}
public int getTotalPrice() {
return totalPrice;
}
public void setTotalPrice() {
this.totalPrice += (getTotalPrice() / getQuantity());
}
public int getQuantity() {
return quantity;
}
public void setQuantity() {
this.quantity = getQuantity() + 1;
}
public String toString() {
return String.format(" %s | %d | %d원",
getId(),
getQuantity(),
getTotalPrice()
);
}
}
class Cart {
private List<SelectedBook> selectedBooks;
public Cart() {
selectedBooks = new ArrayList<>();
}
public String getCartList() {
StringBuilder cartList = new StringBuilder();
selectedBooks.forEach(selectedBook -> cartList.append(selectedBook.toString()).append("\n"));
return cartList.toString();
}
public void addToCart(SelectedBook selectedBook) {
// 장바구니에 동일한 도서가 있는지 확인
for (SelectedBook book : selectedBooks) {
if (book.getId().equals(selectedBook.getId())) {
book.setTotalPrice();
book.setQuantity();
return;
}
}
// 장바구니에 새 도서 추가
selectedBooks.add(selectedBook);
}
public void removeFromCart(String id) {
// 해당 ID의 책을 제거
selectedBooks.removeIf(selectedBook -> selectedBook.getId().equals(id));
}
public void flushCart() {
selectedBooks.clear();
}
public void printCartList() {
System.out.println("장바구니 상품 목록 : ");
System.out.println(this); // toString 자동 호출
}
public void printReceipt() {
int totalPrice = selectedBooks.stream()
.mapToInt(SelectedBook::getTotalPrice)
.sum();
System.out.printf("총 결재 금액 : %,d원\n", totalPrice);
}
public String toString() {
return String.format("--------------------------------------------------\n 도서 ID | 수량 | 합계 \n%s--------------------------------------------------\n", getCartList());
}
}
public class LibrarySystem {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int menuNumber = 0;
// 프로그램이 실행되면, 보유도서 정보를 적당한 자료구조에 저장
Book[] books = new Book[3];
books[0] = new Book(
"ISBN1234",
"셜롬홈즈",
20000,
"코난 도일",
"그 누구도 뛰어넘지 못했던 추리 소설의 고전",
"추리소설",
"2018/10/08"
);
books[1] = new Book(
"ISBN2345",
"도리안 그레이의 초상",
16000,
"오스카 와일드",
"예술을 위한 예술!",
"고전소설",
"2022/01/22"
);
books[2] = new Book(
"ISBN3456",
"쥐덫",
27000,
"애거서 크리스티",
"폭설 속에 갇힌 몽스웰 여관 네 명의 손님과 주인 부부, 그리고 한 명의 형사",
"추리소설",
"2019/06/10"
);
// 보유도서 출력
for(int i = 0; i < books.length; i++) {
System.out.println(books[i].toString());
}
// 사욛자 정보(이름, 연락처)를 입력 받음
System.out.print("당신의 이름을 입력하세요 : ");
String name = scanner.next();
System.out.print("연락처를 입력하세요 : ");
String phoneNumber = scanner.next();
// 장바구니 생성
Cart cart = new Cart();
while(menuNumber != 7) {
System.out.print(
"******************************************************\n" +
"오늘의 선택, 코난문고\n" +
"영원한 스테디셀러, 명탐정 코난시리즈를 만나보세요~\n" +
"******************************************************\n" +
"1. 고객 정보 확인하기 2. 장바구니 상품 목록 보기\n" +
"3. 장바구니에 항목 추가하기 4. 장바구니의 항목 삭제하기\n" +
"5. 장바구니 비우기 6. 영수증 표시하기 7. 종료\n" +
"메뉴 번호를 선택해주세요\n" +
"******************************************************\n" +
"메뉴 번호를 선택해주세요 "
);
menuNumber = scanner.nextInt();
if(menuNumber == 1) {
System.out.println("현재 고객 정보 : ");
System.out.printf("이름 %s 연락처 %s\n", name, phoneNumber);
} else if(menuNumber == 2) {
// 장바구니 목록 호출하는 메소드 호출
cart.printCartList();
} else if(menuNumber == 3) {
for(int i = 0; i < books.length; i++) {
books[i].toString();
}
System.out.print("장바구니에 추가할 도서의 ID를 입력하세요 : ");
String id = scanner.next();
System.out.print("장바구니에 추가하시겠습니까? Y | N ");
if(scanner.next().trim().toLowerCase().equals("y")) {
int price = Arrays.stream(books)
.filter(book -> book.getId().equals(id))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("해당 ID의 책이 존재하지 않습니다."))
.getPrice();
// 장바구니에 추가하는 메소드 호출
cart.addToCart(new SelectedBook(id, price));
}
} else if(menuNumber == 4) {
cart.printCartList();
System.out.print("장바구니에서 삭제할 도서의 ID를 입력하세요 : ");
String id = scanner.next();
// 장바구니에서 삭제하는 메소드 호출
cart.removeFromCart(id);
System.out.printf("장바구니에서 %s가 삭제되었습니다\n", id);
} else if(menuNumber == 5) {
// 장바구니 비우는 메소드 호출
cart.flushCart();
System.out.println("장바구니를 비웠습니다.");
} else if(menuNumber == 6) {
System.out.print("배송받을 분은 고객정보와 같습니까? "); // y를 받는다는 전제 하
if(scanner.next().equals("y")) {
System.out.print("배송지를 입력해주세요 ");
String destination = scanner.next();
SimpleDateFormat sdf = new SimpleDateFormat("YYYY/MM/dd");
System.out.println("-----------------배송 받을 고객 정보-----------------");
System.out.printf("고객명 : %s 연락처 : %s\n배송지 : %s 발송일 : %s\n",
name,
phoneNumber,
destination,
sdf.format(new Date().getTime())
);
cart.printCartList();
cart.printReceipt();
}
}
}
}
}