/****** 檔名:JPA06_1.java 題目:TQC+ JAVA6物件導向程式設計 參考答案 時間:2015/06 作者:fang shi mai ******/ class JPA06_1 { public static void main(String args[]) { DepositAccount deposit = new DepositAccount("peter", 2); deposit.deposit(5000); FreeAccount free = new FreeAccount("peter"); free.deposit(20000); SpecialAccount special = new SpecialAccount("peter"); special.deposit(10000); deposit.addInterest(); free.addInterest(); special.addInterest(); System.out.println("定期存款:" + deposit.balance()); System.out.println("活期存款:" + free.balance()); System.out.println("優惠存款:" + special.balance()); FundAccount fund = new FundAccount("peter", "A", free, special); fund.buy(15000, 500); System.out.println("基金現額:" + fund.balance(300)); System.out.println("活期餘額:" + fund.freeAccount.balance()); } } abstract class Account { String name; double rate; double money; void deposit(double a) { money+=a; } void withdraw(double a) { money-=a; } int balance() { return (int)money; } void addInterest() { money+=money*rate; } } class DepositAccount extends Account { DepositAccount(String a,int b) { name=a; if(b==1) rate=0.03; else if(b==2) rate=0.04; else if(b==3) rate=0.05; } } class FreeAccount extends Account { FreeAccount(String a) { name=a; rate=0.02; } } class SpecialAccount extends Account { SpecialAccount(String a) { name=a; rate=0.02; } boolean isOver() { if(money>10000) return true; else return false; } } class FundAccount extends Account { String fundName; double unit; FreeAccount freeAccount; SpecialAccount specialAccount; FundAccount(String a,String b,FreeAccount c,SpecialAccount d) { name=a; fundName=b; freeAccount=c; specialAccount=d; } void buy(double a,double b) { unit+=a/b; if(specialAccount.isOver()) freeAccount.withdraw(a); else freeAccount.withdraw(a*1.02); } void sell(double a) { } int balance(double a) { return (int)(unit*a); } }
/****** 檔名:JPA06_2.java 題目:TQC+ JAVA6物件導向程式設計 參考答案 時間:2015/06 作者:fang shi mai ******/ class JPA06_2 { public static void main(String args[]) { DepositAccount deposit = new DepositAccount("peter", 2); deposit.deposit(5000); FreeAccount free = new FreeAccount("peter"); free.deposit(20000); SpecialAccount special = new SpecialAccount("peter"); special.deposit(10000); deposit.addInterest(); free.addInterest(); special.addInterest(); FundAccount fund = new FundAccount("peter", "A", free, special); fund.buy(15000, 500); special.withdraw(5000); fund.buy(2000, 300); System.out.println("基金餘額:" + fund.balance(300)); System.out.println("售出前活期餘額:" + fund.freeAccount.balance()); fund.sell(fund.getUnit(), 400); System.out.println("售出後活期餘額:" + fund.freeAccount.balance()); } } abstract class Account { String name; double rate; double money; void deposit(double a) { money+=a; } void withdraw(double a) { money-=a; } int balance() { return (int)money; } void addInterest() { money+=money*rate; } } class DepositAccount extends Account { DepositAccount(String a,int b) { name=a; if(b==1) rate=0.03; else if(b==2) rate=0.04; else if(b==3) rate=0.05; } } class FreeAccount extends Account { FreeAccount(String a) { name=a; rate=0.02; } } class SpecialAccount extends Account { SpecialAccount(String a) { name=a; rate=0.02; } boolean isOver() { if(money>10000) return true; else return false; } } class FundAccount extends Account { String fundName; double unit; FreeAccount freeAccount; SpecialAccount specialAccount; FundAccount(String a,String b,FreeAccount c,SpecialAccount d) { name=a; fundName=b; freeAccount=c; specialAccount=d; } void buy(double a,double b) { unit+=a/b; if(specialAccount.isOver()) freeAccount.withdraw(a); else freeAccount.withdraw(a*1.02); } void sell(double a,double b) { unit-=a; if(specialAccount.isOver()) freeAccount.deposit(a*b); else freeAccount.deposit(a*b*0.98); } int balance(double a) { return (int)(unit*a); } double getUnit() { return unit; } }
/****** 檔名:JPA06_3.java 題目:TQC+ JAVA6物件導向程式設計 參考答案 時間:2015/06 作者:fang shi mai ******/ class JPA06_3 { public static void main(String args[]) { DepositAccount deposit = new DepositAccount("peter", 2); deposit.deposit(5000); FreeAccount free = new FreeAccount("peter"); free.deposit(20000); SpecialAccount special = new SpecialAccount("peter"); special.deposit(10000); deposit.addInterest(); free.addInterest(); special.addInterest(); FundAccount fund = new FundAccount("peter", "A", free, special); fund.buy(15000, 500); special.withdraw(5000); fund.buy(2000, 300); fund.sell(fund.getUnit(), 400); InternetAccount internet = new InternetAccount(); internet.setDeposit(deposit); internet.setFree(free); internet.setSpecial(special); internet.setFund(fund); System.out.println("存款總額:" + internet.getTotalBalance()); } } class InternetAccount { DepositAccount depositAccount; FreeAccount freeAccount; SpecialAccount specialAccount; FundAccount fundAccount; void setDeposit(DepositAccount a) { depositAccount=a; } void setFree(FreeAccount a) { freeAccount=a; } void setSpecial(SpecialAccount a) { specialAccount=a; } void setFund(FundAccount a) { fundAccount=a; } int getTotalBalance() { return depositAccount.balance()+freeAccount.balance()+specialAccount.balance(); } } abstract class Account { String name; double rate; double money; void deposit(double a) { money+=a; } void withdraw(double a) { money-=a; } int balance() { return (int)money; } void addInterest() { money+=money*rate; } } class DepositAccount extends Account { DepositAccount(String a,int b) { name=a; if(b==1) rate=0.03; else if(b==2) rate=0.04; else if(b==3) rate=0.05; } } class FreeAccount extends Account { FreeAccount(String a) { name=a; rate=0.02; } } class SpecialAccount extends Account { SpecialAccount(String a) { name=a; rate=0.02; } boolean isOver() { if(money>10000) return true; else return false; } } class FundAccount extends Account { String fundName; double unit; FreeAccount freeAccount; SpecialAccount specialAccount; FundAccount(String a,String b,FreeAccount c,SpecialAccount d) { name=a; fundName=b; freeAccount=c; specialAccount=d; } void buy(double a,double b) { unit+=a/b; if(specialAccount.isOver()) freeAccount.withdraw(a); else freeAccount.withdraw(a*1.02); } void sell(double a,double b) { unit-=a; if(specialAccount.isOver()) freeAccount.deposit(a*b); else freeAccount.deposit(a*b*0.98); } int balance(double a) { return (int)(unit*a); } double getUnit() { return unit; } }
/****** 檔名:JPA06_4.java 題目:TQC+ JAVA6物件導向程式設計 參考答案 時間:2015/06 作者:fang shi mai ******/ import java.util.HashMap; class JPA06_4 { public static void main(String args[]) { DepositAccount deposit = new DepositAccount("peter", 2); deposit.deposit(5000); FreeAccount free = new FreeAccount("peter"); free.deposit(20000); SpecialAccount special = new SpecialAccount("peter"); special.deposit(10000); deposit.addInterest(); free.addInterest(); special.addInterest(); FundAccount fund = new FundAccount("peter", "A", free, special); fund.buy(15000, 500); special.withdraw(5000); fund.buy(2000, 300); fund.sell(fund.getUnit(), 400); InternetAccount internet = new InternetAccount(); internet.setDeposit(deposit); internet.setFree(free); internet.setSpecial(special); internet.setFund(fund); MultiFund multi = new MultiFund(); multi.addFund("A", fund); FundAccount fundB = new FundAccount("peter", "B", free, special); fundB.buy(2000, 50); multi.addFund("B", fundB); FundAccount fundC = new FundAccount("peter", "C", free, special); fundC.buy(5000, 30); multi.addFund("C", fundC); System.out.println("活期餘額:" + free.balance()); multi.printEachUnit(); System.out.println("B 基金餘額: " + multi.getFundBalance("B", 100)); } } class MultiFund { HashMap<String,FundAccount> hm=new HashMap<String,FundAccount>(); void addFund(String a,FundAccount b) { hm.put(a,b); } void printEachUnit() { for(FundAccount i:hm.values()) { System.out.println(i.fundName+":"+i.getUnit()); } } int getFundBalance(String a,int b) { FundAccount temp=hm.get(a); return temp.balance(b); } } class InternetAccount { DepositAccount depositAccount; FreeAccount freeAccount; SpecialAccount specialAccount; FundAccount fundAccount; void setDeposit(DepositAccount a) { depositAccount=a; } void setFree(FreeAccount a) { freeAccount=a; } void setSpecial(SpecialAccount a) { specialAccount=a; } void setFund(FundAccount a) { fundAccount=a; } int getTotalBalance() { return depositAccount.balance()+freeAccount.balance()+specialAccount.balance(); } } abstract class Account { String name; double rate; double money; void deposit(double a) { money+=a; } void withdraw(double a) { money-=a; } int balance() { return (int)money; } void addInterest() { money+=money*rate; } } class DepositAccount extends Account { DepositAccount(String a,int b) { name=a; if(b==1) rate=0.03; else if(b==2) rate=0.04; else if(b==3) rate=0.05; } } class FreeAccount extends Account { FreeAccount(String a) { name=a; rate=0.02; } } class SpecialAccount extends Account { SpecialAccount(String a) { name=a; rate=0.02; } boolean isOver() { if(money>10000) return true; else return false; } } class FundAccount extends Account { String fundName; double unit; FreeAccount freeAccount; SpecialAccount specialAccount; FundAccount(String a,String b,FreeAccount c,SpecialAccount d) { name=a; fundName=b; freeAccount=c; specialAccount=d; } void buy(double a,double b) { unit+=a/b; if(specialAccount.isOver()) freeAccount.withdraw(a); else freeAccount.withdraw(a*1.02); } void sell(double a,double b) { unit-=a; if(specialAccount.isOver()) freeAccount.deposit(a*b); else freeAccount.deposit(a*b*0.98); } int balance(double a) { return (int)(unit*a); } double getUnit() { return unit; } }
/****** 檔名:JPA06_5.java 題目:TQC+ JAVA6物件導向程式設計 參考答案 時間:2015/06 作者:fang shi mai ******/ import java.util.HashMap; class JPA06_5 { public static void main(String args[]) { DepositAccount deposit = new DepositAccount("peter", 2); deposit.deposit(5000); FreeAccount free = new FreeAccount("peter"); free.deposit(20000); SpecialAccount special = new SpecialAccount("peter"); special.deposit(10000); deposit.addInterest(); free.addInterest(); special.addInterest(); FundAccount fund = new FundAccount("peter", "A", free, special); try { fund.buy(15000, 500); special.withdraw(5000); fund.buy(2000, 300); fund.sell(fund.getUnit(), 400); InternetAccount internet = new InternetAccount(); internet.setDeposit(deposit); internet.setFree(free); internet.setSpecial(special); internet.setFund(fund); MultiFund multi = new MultiFund(); multi.addFund("A", fund); FundAccount fundB = new FundAccount("peter", "B", free, special); fundB.buy(2000, 50); multi.addFund("B", fundB); FundAccount fundC = new FundAccount("peter", "C", free, special); fundC.buy(5000, 30); multi.addFund("C", fundC); fund.buy(14000, 300); } catch(Exception e) { System.out.println(e.getMessage()); } } } class MultiFund { HashMap<String,FundAccount> hm=new HashMap<String,FundAccount>(); void addFund(String a,FundAccount b) { hm.put(a,b); } void printEachUnit() { for(FundAccount i:hm.values()) { System.out.println(i.fundName+":"+i.getUnit()); } } int getFundBalance(String a,int b) { FundAccount temp=hm.get(a); return temp.balance(b); } } class InternetAccount { DepositAccount depositAccount; FreeAccount freeAccount; SpecialAccount specialAccount; FundAccount fundAccount; void setDeposit(DepositAccount a) { depositAccount=a; } void setFree(FreeAccount a) { freeAccount=a; } void setSpecial(SpecialAccount a) { specialAccount=a; } void setFund(FundAccount a) { fundAccount=a; } int getTotalBalance() { return depositAccount.balance()+freeAccount.balance()+specialAccount.balance(); } } abstract class Account { String name; double rate; double money; void deposit(double a) { money+=a; } void withdraw(double a)throws Exception { if(a>money) throw new Exception(name+":提款金額: "+(int)a+" 大於存款餘額: "+(int)money); money-=a; } int balance() { return (int)money; } void addInterest() { money+=money*rate; } } class DepositAccount extends Account { DepositAccount(String a,int b) { name=a; if(b==1) rate=0.03; else if(b==2) rate=0.04; else if(b==3) rate=0.05; } } class FreeAccount extends Account { FreeAccount(String a) { name=a; rate=0.02; } } class SpecialAccount extends Account { SpecialAccount(String a) { name=a; rate=0.02; } boolean isOver() { if(money>10000) return true; else return false; } } class FundAccount extends Account { String fundName; double unit; FreeAccount freeAccount; SpecialAccount specialAccount; FundAccount(String a,String b,FreeAccount c,SpecialAccount d) { name=a; fundName=b; freeAccount=c; specialAccount=d; } void buy(double a,double b)throws Exception { unit+=a/b; if(specialAccount.isOver()) freeAccount.withdraw(a); else freeAccount.withdraw(a*1.02); } void sell(double a,double b) { unit-=a; if(specialAccount.isOver()) freeAccount.deposit(a*b); else freeAccount.deposit(a*b*0.98); } int balance(double a) { return (int)(unit*a); } double getUnit() { return unit; } }
沒有留言:
張貼留言