방치하기

과제 주석 달기 , 그리고 다이어 그램 하고 메모리 본문

카테고리 없음

과제 주석 달기 , 그리고 다이어 그램 하고 메모리

Yi Junho 2009. 7. 23. 17:27
반응형
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
class Product
{
    int price;          // 제품의 가격
    int bonusPoint;     // 제품구매 시 제공하는 보너스점수
 
    Product(int price) {
        this.price = price;
        bonusPoint =(int)(price/10.0);  // 보너스점수는 제품가격의 10%
    }
}
 
class Tv extends Product {
    Tv() {
        // 조상클래스의 생성자 Product(int price)를 호출한다.
        super(100);         // Tv의 가격을 100만원으로 한다.
    }
 
    public String toString() {  // Object클래스의 toString()을 오버라이딩한다.
        return "Tv";
    }
}
 
class Computer extends Product {
    Computer() {
        super(200);
    }
 
    public String toString() {
        return "Computer";
    }
}
 
class Buyer {           // 고객, 물건을 사는 사람
    int money = 1000;       // 소유금액
    int bonusPoint = 0; // 보너스점수
 
    void buy(Product p) {
        if(money < p.price) {
            System.out.println("잔액이 부족하여 물건을 살수 없습니다.");
            return;
        }
 
        money -= p.price;           // 가진 돈에서 구입한 제품의 가격을 뺀다.
        bonusPoint += p.bonusPoint; // 제품의 보너스 점수를 추가한다.
        System.out.println(p + "을/를 구입하셨습니다.");
    }
}
 
class PolyArgumentTest {
    public static void main(String args[]) {
        Buyer b = new Buyer();
        Tv tv = new Tv();
        Computer com = new Computer();
 
        b.buy(tv);
        b.buy(com);
 
        System.out.println("현재 남은 돈은 " + b.money + "만원입니다.");
        System.out.println("현재 보너스점수는 " + b.bonusPoint + "점입니다.");
    }
}
반응형
Comments