JavaCard
⑴ java定義一個銀行卡類(BankCard)以及派生類ForeignCard,LimitedCard,成員包括姓名,卡
abstract class BankCard {
protected String name;
protected String cardNo;
protected double money;
public BankCard(String name, String cardNo, double money) {
super();
this.name = name;
this.cardNo = cardNo;
this.money = money;
}
public String getName() {
return name;
}
public String getCardNo() {
return cardNo;
}
public double getMoney() {
return money;
}
public abstract void unputMoney(int someMoney);
public void putMoney(int someMoney) {
this.money += someMoney;
}
}
class ForeignCard extends BankCard {
public ForeignCard(String name, String cardNo, double money) {
super(name, cardNo, money);
}
@Override
public void unputMoney(int someMoney) {
double left = getMoney() - someMoney * 1.01;
if(left >= 0) {
super.money = left;
} else {
throw new RuntimeException("余額不足");
}
}
}
class LimitedCard extends BankCard {
public LimitedCard(String name, String cardNo, double money) {
super(name, cardNo, money);
}
@Override
public void unputMoney(int someMoney) {
if(someMoney > 1000) {
throw new RuntimeException("取款不能超過1000");
}
if(getMoney() < someMoney) {
throw new RuntimeException("余額不足");
}
super.money -= someMoney;
}
}