은근한

신용카드 예제 본문

카테고리 없음

신용카드 예제

EsJoo 2014. 5. 23. 12:06

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
package ks.example;
 
public class CreditCard4 {
    public String owner;    //카드 소유자 
    private long number;    //16자리 카드번호
    protected int point;        //카드 포인트
    protected int balance;    //현재까지 사용액
    
    //생성자 구현
    public CreditCard4(String owner) {
        this.owner = owner;
    }
    public CreditCard4(long number) {
        this.number = number;
    }
    public CreditCard4(String owner, long number) {
        this.owner = owner;
        this.number = number;
    }
    
    //카드 사용 메소드
    public void use(int amount) {
            balance += amount;
            System.out.println(owner + " 카드 사용액: " + balance);
        }
    
    //카드 비용 지불 메소드
    public void payBill(int amount) {
            balance -= amount;
            System.out.println(owner + " 지불액: " + amount + ", 지불 잔액: " + balance);
            addPoint(amount);
        }    
    
    //카드 포인트 추가 메소드
    protected void addPoint(int amount) {
            point += amount/1000;        
            System.out.println(owner + " 보너스 포인트: " + point);
        }
        
    //setter & getter
    public String getOwner() {
        return owner;
    }
    public void setOwner(String owner) {
        this.owner = owner;
    }
    public int getPoint() {
        return point;
    }
    public void setPoint(int point) {
        this.point = point;
    }
    public int getBalance() {
        return balance;
    }
    public void setBalance(int balance) {
        this.balance = balance;
    }
    public long getNumber() {
        return number;
    }
    public void setNumber(long number) {
        //this.number = number;
        if (number < 1000_0000_0000_0000L) {
            System.err.println("잘못된 카드 번호입니다.");
            return;
        }
        this.number = number;
    }
}


Colored By Color Scripter //DiscountCard Class
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
package ks.example;
 
public class DiscountCard extends CreditCard4 {
 
    private double discountRate = 0.1;
    
    public DiscountCard(String owner, long number) {
        super(owner, number);
        // TODO Auto-generated constructor stub
    }
 
    public double getDiscountRate() {
        return discountRate;
    }
 
    public void setDiscountRate(double discountRate) {
        this.discountRate = discountRate;
    }
    
    @Override
    public void payBill(int amount) {
        // TODO Auto-generated method stub
        balance -= (amount - amount*discountRate);
        System.out.println(owner + " 지불액: " + amount + ", 지불 잔액: " + balance);
        addPoint(amount);
    }
 
}

Colored By Color Scripter //MileageCard Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package ks.example;
 
public class MileageCard extends CreditCard4 {
 
    int mileage = 0;
    
    public MileageCard(String owner, long number) {
        super(owner, number);
        // TODO Auto-generated constructor stub
    }
    
    @Override
    public void payBill(int amount) {
        // TODO Auto-generated method stub
        super.payBill(amount);
        addMileage(amount);
    }
 
    private void addMileage(int amount){
        mileage += amount/1000;
        System.out.println(owner + " 마일리지: " + mileage);
    }
}

Colored By Color Scripter //PresentCard Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package ks.example;
 
public class PresentCard extends CreditCard4{
    public PresentCard(String owner, long number){
        super(owner,number);
    }    
    
    @Override
    public void payBill(int amount) {
        super.payBill(amount);
        present(amount);
    }
    
    public void present(int point){
        if(point>10000){
            point = point-10000;
            System.out.println("선물이 발송되었습니다.");
        }
        System.out.println("잔여 포인트:"+point);
        
    }
 
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package ks.example;
 
public class CreditCardTest4 {
    
    public static void main(String[] args){
        CreditCard4  card1 = new CreditCard4("김성주",1111);
        System.out.println("card1:"+card1.owner+" "+card1.getNumber());
        card1.payBill(10000);
        
        DiscountCard card2 = new DiscountCard("제이와", 2222);
        System.out.println("card2:"+card2.owner+" "+card2.getNumber());
        card2.payBill(10000);
        
        MileageCard card3 = new MileageCard("이피이",3333);
        System.out.println("card3:"+card3.owner+" "+card3.getNumber());
        card3.payBill(10000);
        
        PresentCard card4 = new PresentCard("seongjoo", 7777);
        System.out.println("card4:"+card4.owner+" "+card4.getNumber());
        card4.payBill(15000);
    }
 
}