CreditCard , DiscountCard, MileageCard, PresentCard 클래스를 사용한 상속 예제 입니다.
CreditCard 가 상위 클래스가 되고 Discount, Mileage, Present 클래스는 하위 클래스가 됩니다.
package ks.example.creditcard4;
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;
}
}
package ks.example.creditcard4;
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);
}
}
package ks.example.creditcard4;
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);
}
}
package ks.example.creditcard4;
public class PresentCard extends CreditCard4{
public PresentCard(String owner, long number) {
super(owner, number);
// TODO Auto-generated constructor stub
}
//카드 비용 지불 메소드
public void payBill(int amount) {
super.payBill(amount);
if(amount >= 10000) System.out.println("선물이 발송 되었습니다.");
}
}
package ks.example.creditcard4;
public class creditcardtest {
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("이민호",4444);
System.out.println("card4 : "+ card4.owner + " " + card4.getNumber());
card4.payBill(10000);
}
}
출력결과는 아래와 같습니다