// 列印出所有基金和擁有單位數 void printEachUnit() { FundAccount fundaccount; for (Iterator iterator = funds.values().iterator(); iterator.hasNext(); ) { fundaccount = (FundAccount) iterator.next(); System.out.println(fundaccount.fundName + " : " + fundaccount.getUnit()); } }
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(); // 增加A基金(因A基金本身就存在,不需再另外建立) multi.addFund("A", fund); // 建立一個B基金 FundAccount fundB = new FundAccount("peter", "B", free, special); // 購買每單位50元的基金,2000元 fundB.buy(2000, 50); // 增加B基金 multi.addFund("B", fundB); // 建立一個C基金 FundAccount fundC = new FundAccount("peter", "C", free, special); // 購買每單位30元的基金,5000元 fundC.buy(5000, 30); multi.addFund("C", fundC); System.out.println("活期餘額:" + free.balance()); // 顯示特定基金的總現值,須傳入基金名稱和單位金額 multi.printEachUnit(); System.out.println("B 基金餘額: " + multi.getFundBalance("B", 100)); }