TQC+ 物件導向程式語言 Java 6 604 銀行理財帳戶
題目前後相關,請閱讀完這個題組的五個題目之後再作答,每題答案皆能獨立執行。請將需要重複或共同使用的程式片段撰寫成函式,以供在類別中呼叫使用,避免在不同的類別中重複寫相同的程式碼,否則將酌量扣分。
題目一:
題目說明:
請開啟檔案 JPD06_1.java,設計「銀行理財帳戶」程式。銀行共設有四種帳戶,需計算目前存款,請依下列題意完成作答。將 JPD06_1.java 內的 class JPD06_1 修改為 class JPA06_1,將檔案另存為 JPA06_1.java 後編譯為 JPA06_1.class,所有題目中有使用到的類別也請編譯後一併儲存。
設計說明:
(1) 銀行共設有四種帳戶,分別是「定期存款」帳戶、「活期存款」帳戶、「優惠存款」帳戶及「基金存款」帳戶。前三種帳戶都有開戶人、年利率及帳戶餘額的資料。
(2) 每個帳戶都可以存款 (deposit)、提款 (withdraw)、查詢餘額 (balance)、加計利息 (addInterest)。
(3) 定期存款帳戶 (Deposit Account) 的年利率分 1、2、3 年期,各為 3%、4%、 5%。
(4) 活期存款帳戶 (FreeAccount) 的年利率為 2%。
(5) 優惠存款帳戶 (SpecialAccount) 的利率與活期存款同為 2%,但優惠存款帳戶餘額若保持在 10000 元以上則買賣基金可免手續費 (傳回布林值代表可/不可)。
(6) 基金存款帳戶 (FundAccount) 的必要資料有:開戶人、基金名稱、單位數、一個活期存款戶、一個優惠存款戶。買入的手續費為買入金額的 2%,賣出的手續費為賣出金額的 2%。基金存款帳戶可以買、賣、 查調餘額,這幾個功能均需傳入基金目前的價格。買賣基金由活期存款帳戶轉帳。
(7) 請先為 Peter 開設一個定期存款帳戶 (2年期,存入5000 元)、活期存款帳戶 (存入 20000 元)、優惠存款帳戶 (存入 10000 元)。並加總一年的利息後,再查詢各帳戶的餘額,依序列出「定期存款」、「活期存款」、「優惠存款」目前的餘額。
(8) 接著再為 Peter 新開設一個基金存款帳戶,並買入 A 基金 15000 元,價格為每單位 500 元。於三天後該基金跌價為每單位 300 元,請計算 目前基金的現額,及 Peter 活期存款帳戶的餘額,請接續 2.(7) 列出「基全現額」以及「活期餘額」。
執行結果參考畫面:
程式碼:
class Account {
String name;
int amount;
double rate = 0.02;
Account(String name) {
this.name = name;
}
void deposit(int money) {
amount += money;
}
void withdraw(int money) {
amount -= money;
}
int balance() {
return amount;
}
void addInterest() {
amount = (int) (amount * (1 + rate));
}
}
class DepositAccount extends Account { // 定期存款帳戶
DepositAccount(String name, int year) {
super(name);
if(year == 1)
rate = 0.03;
else if(year == 2)
rate = 0.04;
else if(year == 3)
rate = 0.05;
}
}
class FreeAccount extends Account { // 活期存款帳戶
FreeAccount(String name) {
super(name);
}
}
class SpecialAccount extends Account { // 優惠存款帳戶
SpecialAccount(String name) {
super(name);
}
boolean isFreeCharge() {
return amount >= 10000;
}
}
class FundAccount extends Account { // 基金存款帳戶
String fundName;
FreeAccount freeAccount;
SpecialAccount specialAccount;
double unit = 0;
FundAccount(String name, String fundName, FreeAccount freeAccount, SpecialAccount specialAccount) {
super(name);
this.fundName = fundName;
this.freeAccount = freeAccount;
this.specialAccount = specialAccount;
}
void buy(int money, int unitPrice) {
unit += (double)money / unitPrice;
if(specialAccount.isFreeCharge())
freeAccount.withdraw(money);
else
freeAccount.withdraw((int) (money * 1.02)); // 因為優惠存款帳戶的餘額,不到一萬塊,所以要付手續費,手續費為買入金額的 2%。
} // 買入金額的 2% 為 money * 0.02,因為代入的參數還要加上買入的金額,所以是「money + money * 0.02」,可以再簡化成 money * 1.02。
int balance(int unitPrice) {
return (int) (unit * unitPrice);
}
}
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());
}
}
題目二:
題目說明:
請開啟 JPD06_2.java,Peter 想要再次購買基金,請依下列題意完成作答。將 JPD06_2.java 内的 class JPD06_2 修改為 class JPA06_2,將檔案另存為 JPA06_2.java 後編譯為 JPA06_2.class,所有題目中有使用到的類別也請編譯後一併儲存。
設計說明:
(1) Peter 由優惠存款帳戶提款 5000 元。並再次購買 A 基金 2000 元,價格為每單位 300 元。
(2) 請列出該【基金餘額】,以及 Peter 活期存款帳戶【售出前活期餘額】。
(3) 接著 Peter 將其基金全數賣出,賣價為每單位 400 元。
(4) 請再次查詢 Peter 之活期存款帳戶的餘額。
執行結果參考畫面:
程式碼:
class Account {
String name;
int amount;
double rate = 0.02;
Account(String name) {
this.name = name;
}
void deposit(int money) {
amount += money;
}
void withdraw(int money) {
amount -= money;
}
int balance() {
return amount;
}
void addInterest() {
amount = (int) (amount * (1 + rate));
}
}
class DepositAccount extends Account { // 定期存款帳戶
DepositAccount(String name, int year) {
super(name);
if(year == 1)
rate = 0.03;
else if(year == 2)
rate = 0.04;
else if(year == 3)
rate = 0.05;
}
}
class FreeAccount extends Account { // 活期存款帳戶
FreeAccount(String name) {
super(name);
}
}
class SpecialAccount extends Account { // 優惠存款帳戶
SpecialAccount(String name) {
super(name);
}
boolean isFreeCharge() {
return amount >= 10000;
}
}
class FundAccount extends Account { // 基金存款帳戶
String fundName;
FreeAccount freeAccount;
SpecialAccount specialAccount;
double unit = 0;
FundAccount(String name, String fundName, FreeAccount freeAccount, SpecialAccount specialAccount) {
super(name);
this.fundName = fundName;
this.freeAccount = freeAccount;
this.specialAccount = specialAccount;
}
void buy(int money, int unitPrice) {
unit += (double)money / unitPrice;
if(specialAccount.isFreeCharge())
freeAccount.withdraw(money);
else
freeAccount.withdraw((int) (money * 1.02)); // 因為優惠存款帳戶的餘額,不到一萬塊,所以要付手續費,手續費為買入金額的 2%。
} // 買入金額的 2% 為 money * 0.02,因為代入的參數還要加上買入的金額,所以是「money + money * 0.02」,可以再簡化成 money * 1.02。
int balance(int unitPrice) {
return (int) (unit * unitPrice);
}
double getUnit() {
return unit;
}
void sell(double unit, int unitPrice) {
this.unit -= unit;
if(specialAccount.isFreeCharge())
freeAccount.deposit((int) (unit * unitPrice));
else
freeAccount.deposit((int) (unit * unitPrice * 0.98)); // 賣出也一樣要手續費 2%,所以我們會拿到的只有,賣出金額的 98%。
}
}
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());
}
}
題目三:
題目說明:
請開啟 JPD06_3.java,該銀行為服務客戶增設網路銀行帳戶,請依下列題意完成作答。將 JPD06_3.java 内的 class JPD06_3 修改為 class JPA06_3,將檔案另存為 JPA06_3.java 後編譯為 JPA06_3.class,所有題目中有使用到的類別也請編譯後一併儲存。
設計說明:
(1) 該銀行為服務客戶增設網路銀行帳戶,網路銀行帳戶含有某客戶之所有帳戶的資料。
(2) 請為 Peter 開立一個網路銀行帳戶,並撰寫能夠計算 Peter 所有存款帳戶 (不含基金) 的總餘額的方法。
執行結果參考畫面:
程式碼:
class Account {
String name;
int amount;
double rate = 0.02;
Account(String name) {
this.name = name;
}
void deposit(int money) {
amount += money;
}
void withdraw(int money) {
amount -= money;
}
int balance() {
return amount;
}
void addInterest() {
amount = (int) (amount * (1 + rate));
}
}
class DepositAccount extends Account { // 定期存款帳戶
DepositAccount(String name, int year) {
super(name);
if(year == 1)
rate = 0.03;
else if(year == 2)
rate = 0.04;
else if(year == 3)
rate = 0.05;
}
}
class FreeAccount extends Account { // 活期存款帳戶
FreeAccount(String name) {
super(name);
}
}
class SpecialAccount extends Account { // 優惠存款帳戶
SpecialAccount(String name) {
super(name);
}
boolean isFreeCharge() {
return amount >= 10000;
}
}
class FundAccount extends Account { // 基金存款帳戶
String fundName;
FreeAccount freeAccount;
SpecialAccount specialAccount;
double unit = 0;
FundAccount(String name, String fundName, FreeAccount freeAccount, SpecialAccount specialAccount) {
super(name);
this.fundName = fundName;
this.freeAccount = freeAccount;
this.specialAccount = specialAccount;
}
void buy(int money, int unitPrice) {
unit += (double)money / unitPrice;
if(specialAccount.isFreeCharge())
freeAccount.withdraw(money);
else
freeAccount.withdraw((int) (money * 1.02)); // 因為優惠存款帳戶的餘額,不到一萬塊,所以要付手續費,手續費為買入金額的 2%。
} // 買入金額的 2% 為 money * 0.02,因為代入的參數還要加上買入的金額,所以是「money + money * 0.02」,可以再簡化成 money * 1.02。
int balance(int unitPrice) {
return (int) (unit * unitPrice);
}
double getUnit() {
return unit;
}
void sell(double unit, int unitPrice) {
this.unit -= unit;
if(specialAccount.isFreeCharge())
freeAccount.deposit((int) (unit * unitPrice));
else
freeAccount.deposit((int) (unit * unitPrice * 0.98)); // 賣出也一樣要手續費 2%,所以我們會拿到的只有,賣出金額的 98%。
}
}
class InternetAccount {
DepositAccount depositAccount;
FreeAccount freeAccount;
SpecialAccount specialAccount;
FundAccount fundAccount;
void setDeposit(DepositAccount depositAccount) {
this.depositAccount = depositAccount;
}
void setFree(FreeAccount freeAccount) {
this.freeAccount = freeAccount;
}
void setSpecial(SpecialAccount specialAccount) {
this.specialAccount = specialAccount;
}
void setFund(FundAccount fundAccount) {
this.fundAccount = fundAccount;
}
int getTotalBalance() {
return depositAccount.balance() + freeAccount.balance() + specialAccount.balance();
}
}
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());
}
}
題目四:
題目說明:
請開啟 JPD06_4.java,Prter 想要購買多筆基金,請依下列題意完成作答。將 JPD06_4.java 内的 class JPD06_4 修改為 class JPA06_4,將檔案另存為 JPA06_4.java 後編譯為 JPA06_4.class,所有題目中有使用到的類別也請編譯後一併儲存。
設計說明:
(1) Peter 決定買入 B 基金 2000 元,買價每單位 50 元:C 基金 5000 元,買價每單位 30 元。
(2) 請查詢 Peter 之活期存款帳戶的餘額及 A、B、C 三個基金的單位數。
(3) B 基金的價格現在漲到 100 元,Peter 想要查詢 B 基金的餘額。請使用 HashMap 為 Peter 建立一 MultiFund (多筆基金) 類別,HashMap 的 key 值為基金名稱 (String),value 値為 FundAccount,使其代入 B 基金後,可輸出目前 B 基金的金額。
執行結果參考畫面:
程式碼:
import java.util.HashMap;
class Account {
String name;
int amount;
double rate = 0.02;
Account(String name) {
this.name = name;
}
void deposit(int money) {
amount += money;
}
void withdraw(int money) {
amount -= money;
}
int balance() {
return amount;
}
void addInterest() {
amount = (int) (amount * (1 + rate));
}
}
class DepositAccount extends Account { // 定期存款帳戶
DepositAccount(String name, int year) {
super(name);
if(year == 1)
rate = 0.03;
else if(year == 2)
rate = 0.04;
else if(year == 3)
rate = 0.05;
}
}
class FreeAccount extends Account { // 活期存款帳戶
FreeAccount(String name) {
super(name);
}
}
class SpecialAccount extends Account { // 優惠存款帳戶
SpecialAccount(String name) {
super(name);
}
boolean isFreeCharge() {
return amount >= 10000;
}
}
class FundAccount extends Account { // 基金存款帳戶
String fundName;
FreeAccount freeAccount;
SpecialAccount specialAccount;
double unit = 0;
FundAccount(String name, String fundName, FreeAccount freeAccount, SpecialAccount specialAccount) {
super(name);
this.fundName = fundName;
this.freeAccount = freeAccount;
this.specialAccount = specialAccount;
}
void buy(int money, int unitPrice) {
unit += (double)money / unitPrice;
if(specialAccount.isFreeCharge())
freeAccount.withdraw(money);
else
freeAccount.withdraw((int) (money * 1.02)); // 因為優惠存款帳戶的餘額,不到一萬塊,所以要付手續費,手續費為買入金額的 2%。
} // 買入金額的 2% 為 money * 0.02,因為代入的參數還要加上買入的金額,所以是「money + money * 0.02」,可以再簡化成 money * 1.02。
int balance(int unitPrice) {
return (int) (unit * unitPrice);
}
double getUnit() {
return unit;
}
void sell(double unit, int unitPrice) {
this.unit -= unit;
if(specialAccount.isFreeCharge())
freeAccount.deposit((int) (unit * unitPrice));
else
freeAccount.deposit((int) (unit * unitPrice * 0.98)); // 賣出也一樣要手續費 2%,所以我們會拿到的只有,賣出金額的 98%。
}
}
class InternetAccount {
DepositAccount depositAccount;
FreeAccount freeAccount;
SpecialAccount specialAccount;
FundAccount fundAccount;
void setDeposit(DepositAccount depositAccount) {
this.depositAccount = depositAccount;
}
void setFree(FreeAccount freeAccount) {
this.freeAccount = freeAccount;
}
void setSpecial(SpecialAccount specialAccount) {
this.specialAccount = specialAccount;
}
void setFund(FundAccount fundAccount) {
this.fundAccount = fundAccount;
}
int getTotalBalance() {
return depositAccount.balance() + freeAccount.balance() + specialAccount.balance();
}
}
class MultiFund {
HashMap hashmap = new HashMap();
void addFund(String fundName, FundAccount fundAccount) {
hashmap.put(fundName, fundAccount);
}
void printEachUnit() {
FundAccount fundAccount;
for(Object key : hashmap.keySet()) {
fundAccount = (FundAccount)hashmap.get(key);
System.out.println(fundAccount.fundName + ":" + fundAccount.getUnit());
}
}
int getFundBalance(String fundName, int unitPrice) {
FundAccount fundAccount = (FundAccount)hashmap.get(fundName);
return (int) (fundAccount.getUnit() * unitPrice);
}
}
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));
}
}
題目五:
題目說明:
請開啟 JPD06_5.java,接續題目四的基金購買,請依下列題意完成作答。將 JPD06_5.java 内的 class JPD06_5 修改為 class JPA06_5,將檔案另存為 JPA06_5.java 後編譯為 JPA06_5.class,所有題目中有使用到的類別也請編譯後一併儲存。
設計說明:
(1) 請使用 Exception 撰寫一個功能:當任何一個帳戶之餘額低於 0 時,不可提款,且印出警告信息。
(2) Peter 再次購買 A 基金 14000 元,價格為每單位 300 元。
執行結果參考畫面:
程式碼:
import java.util.HashMap;
class Account {
String name;
int amount;
double rate = 0.02;
Account(String name) {
this.name = name;
}
void deposit(int money) {
amount += money;
}
void withdraw(int money) throws Exception {
// amount -= money;
if(amount > money)
amount -= money;
else
throw new Exception(name + ":提款金額: " + money + " 大於存款餘額: " + amount);
}
int balance() {
return amount;
}
void addInterest() {
amount = (int) (amount * (1 + rate));
}
}
class DepositAccount extends Account { // 定期存款帳戶
DepositAccount(String name, int year) {
super(name);
if(year == 1)
rate = 0.03;
else if(year == 2)
rate = 0.04;
else if(year == 3)
rate = 0.05;
}
}
class FreeAccount extends Account { // 活期存款帳戶
FreeAccount(String name) {
super(name);
}
}
class SpecialAccount extends Account { // 優惠存款帳戶
SpecialAccount(String name) {
super(name);
}
boolean isFreeCharge() {
return amount >= 10000;
}
}
class FundAccount extends Account { // 基金存款帳戶
String fundName;
FreeAccount freeAccount;
SpecialAccount specialAccount;
double unit = 0;
FundAccount(String name, String fundName, FreeAccount freeAccount, SpecialAccount specialAccount) {
super(name);
this.fundName = fundName;
this.freeAccount = freeAccount;
this.specialAccount = specialAccount;
}
void buy(int money, int unitPrice) throws Exception {
unit += (double)money / unitPrice;
if(specialAccount.isFreeCharge())
freeAccount.withdraw(money);
else
freeAccount.withdraw((int) (money * 1.02)); // 因為優惠存款帳戶的餘額,不到一萬塊,所以要付手續費,手續費為買入金額的 2%。
} // 買入金額的 2% 為 money * 0.02,因為代入的參數還要加上買入的金額,所以是「money + money * 0.02」,可以再簡化成 money * 1.02。
int balance(int unitPrice) {
return (int) (unit * unitPrice);
}
double getUnit() {
return unit;
}
void sell(double unit, int unitPrice) {
this.unit -= unit;
if(specialAccount.isFreeCharge())
freeAccount.deposit((int) (unit * unitPrice));
else
freeAccount.deposit((int) (unit * unitPrice * 0.98)); // 賣出也一樣要手續費 2%,所以我們會拿到的只有,賣出金額的 98%。
}
}
class InternetAccount {
DepositAccount depositAccount;
FreeAccount freeAccount;
SpecialAccount specialAccount;
FundAccount fundAccount;
void setDeposit(DepositAccount depositAccount) {
this.depositAccount = depositAccount;
}
void setFree(FreeAccount freeAccount) {
this.freeAccount = freeAccount;
}
void setSpecial(SpecialAccount specialAccount) {
this.specialAccount = specialAccount;
}
void setFund(FundAccount fundAccount) {
this.fundAccount = fundAccount;
}
int getTotalBalance() {
return depositAccount.balance() + freeAccount.balance() + specialAccount.balance();
}
}
class MultiFund {
HashMap hashmap = new HashMap();
void addFund(String fundName, FundAccount fundAccount) {
hashmap.put(fundName, fundAccount);
}
void printEachUnit() {
FundAccount fundAccount;
for(Object key : hashmap.keySet()) {
fundAccount = (FundAccount)hashmap.get(key);
System.out.println(fundAccount.fundName + ":" + fundAccount.getUnit());
}
}
int getFundBalance(String fundName, int unitPrice) {
FundAccount fundAccount = (FundAccount)hashmap.get(fundName);
return (int) (fundAccount.getUnit() * unitPrice);
}
}
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());
}
}
}