-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTotalPrice.java
More file actions
54 lines (42 loc) · 1.16 KB
/
Copy pathTotalPrice.java
File metadata and controls
54 lines (42 loc) · 1.16 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
package javaExE;
import java.util.ArrayList;
public class TotalPrice {
public static void main(String[] args) {
/* 1. 구매 상품 객체를 생성하세요. */
/* 2. 장바구니에 상품을 담으세요. */
Cart myCart = new Cart();
myCart.add(new Item("스트라이프 셔츠", 49900));
myCart.add(new Item("슬림 면바지", 58900));
myCart.add(new Item("스니커즈", 46900));
// 총합 출력
System.out.printf("총합: %d원", myCart.totalPrice());
}
}
class Cart {
private ArrayList<Item> list;
public Cart() {
this.list = new ArrayList<Item>();
}
public void add(Item item) {
list.add(item);
}
/* 3. 장바구니 총합 메소드를 완성하세요. */
public int totalPrice() {
int sum = 0;
for(int i = 0; i < list.size(); i++) {
sum += list.get(i).getPrice();
}
return sum;
}
}
class Item {
private String name;
private int price;
public Item(String name, int price) {
this.name = name;
this.price = price;
}
public int getPrice() {
return price;
}
}