Java--利用介面以及抽象類解決銀行問題。
阿新 • • 發佈:2019-01-08
/* 02.*煙臺大學計算機學院學生 03.*All right reserved. 04.*檔名稱*煙臺大學計算機學院學生 05.*All right reserved. 06.*檔名稱:抽象基類 07.*作者:王洪海 08.*完成日期:2014年11月4日 09.*版本號:v1.0 10.*對任務及求解方法的描述部分:利用介面以及抽象類解決銀行問題 11.*我的程式: 12.*/ package whh; public abstract class Bank { abstract double Income(); } interface Commision { double ticheng(); //存款的金額不同,所獲得的提成利率不同。 } class ChinaBank extends Bank implements Commision{ int savedMoney; //存款 int year; //存款年數(注意型別) double income; //存款利息 double inRate; //存款利率 double commision; public ChinaBank(){ } public ChinaBank(int sm,int y,double r){ savedMoney=sm; year=y; inRate=r; } public double Income(){ return savedMoney*year*inRate; } public double ticheng(){ if(savedMoney>50000) commision=0.005; else commision=0.003; return (savedMoney*commision); } public int getSavedMoney() { return savedMoney; } public void setSavedMoney(int savedMoney) { this.savedMoney = savedMoney; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public double getInRate() { return inRate; } public void setInRate(double inRate) { this.inRate = inRate; } } class ABank extends Bank{ int savedMoney; //存款 double year; //存款年數(注意型別) double inRate; //存款利率 double hRate; //活期利率 public ABank(){ } public ABank(int sm,double y,double r,double h){ savedMoney=sm; year=y; inRate=r; hRate=h; } public double Income(){ return (year-(int)year)*hRate*savedMoney+(int)year*inRate*savedMoney; } public int getSavedMoney() { return savedMoney; } public void setSavedMoney(int savedMoney) { this.savedMoney = savedMoney; } public double getYear() { return year; } public void setYear(double year) { this.year = year; } public double getInRate() { return inRate; } public void setInRate(double inRate) { this.inRate = inRate; } public double gethRate() { return hRate; } public void sethRate(double hRate) { this.hRate = hRate; } } class Test{ public static void main(String []args){ ChinaBank cb = new ChinaBank(60000,5,0.01); System.out.println("中國銀行存"+cb.getSavedMoney()+"元的提成為:"+cb.ticheng()); System.out.println("中國銀行存"+cb.getSavedMoney()+"元錢,"+cb.getYear()+"年的利息為:"+cb.Income()); System.out.println(" "); ABank ab = new ABank(60000,5.2,0.01,0.008); System.out.println("A銀行存"+ab.getSavedMoney()+"元錢,"+ab.getYear()+"年的利息為:"+ab.Income()); } }
執行結果,如下圖: