Github 備份:LINK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ JAVA6 - 604_1 */ | |
class Account{ | |
String name;//開戶人 | |
double rate;//年利率 | |
int balance;//帳戶餘額 | |
//建構子,設定名字和利率 | |
Account(String s,double d){ | |
name = s; | |
rate = d; | |
} | |
//設定利率 | |
void setRate(double d){ | |
rate = d; | |
} | |
//存款 | |
void deposit(int i){ | |
balance+=i; | |
} | |
//提款 | |
void withdraw(int i){ | |
balance-=i; | |
} | |
//餘額查詢 | |
int balance(){ | |
return balance; | |
} | |
//加計利息,將利息利率整體+1 | |
void addInterest(){ | |
balance*=rate+1; | |
} | |
} | |
//定期存款戶方法,繼承Account的所有成員及方法 | |
class DepositAccount extends Account{ | |
DepositAccount(String s,int i){ | |
super(s,0.0);//使用父親的建構子,初始化姓名和年利率 | |
double d =0.0; | |
switch(i){ | |
case 1: | |
d=0.03; | |
break; | |
case 2: | |
d=0.04; | |
break; | |
case 3: | |
d=0.05; | |
break; | |
} | |
setRate(d);//設定年利率 | |
} | |
} | |
//活期存款戶方法,繼承Account的所有成員及方法 | |
class FreeAccount extends Account{ | |
FreeAccount(String s){ | |
super(s,0.02);/*使用父親的建構子,初始化姓名和年利率*/} | |
} | |
//優惠存款戶方法,繼承Account的所有成員及方法 | |
class SpecialAccount extends Account{ | |
SpecialAccount(String s){ | |
super(s,0.02); | |
} | |
//判斷買基金是否免手續費方法,大於10000則回傳true | |
boolean isEmpt(){ | |
return balance>10000; | |
} | |
} | |
//基金存款戶方法,繼承Account的所有成員及方法 | |
class FundAccount extends Account{ | |
String fundName;//基金名稱 | |
FreeAccount freeAccount;//活期存款戶 | |
SpecialAccount specialaccount;//優惠存款戶 | |
double unit;//購買基金單位(基金儲存方式是以股為單位,而不是以金額,單位金額隨時都有可能漲跌) | |
//建構子,初始化參數,從外部傳入"開戶人"、"基金名稱"、"活期存款戶物件"、"優惠存款戶物件",四個參數 | |
FundAccount(String s,String s1,FreeAccount f,SpecialAccount sa){ | |
super(s,0.0); | |
fundName = s1; | |
freeAccount = f; | |
specialaccount =sa; | |
} | |
//購買基金方法 | |
void buy(int i,int j){//i購買金額,j單位金額 | |
//利用優惠存款檢查是否餘額大於10000再給予優惠 | |
if(specialaccount.isEmpt()) | |
//直接提款 | |
freeAccount.withdraw(i); | |
else | |
//多提出手續費2%扣除 | |
freeAccount.withdraw((int)(i*1.02)); | |
//購買單位=購買金額/每單位金額 | |
unit+=(double)i/(double)j; | |
} | |
void sell(double d,int i){//d基金單位,每單位金額 | |
//利用優惠存款檢查是否餘額大於10000再給予優惠 | |
if(specialaccount.isEmpt()) | |
//直接存款 | |
freeAccount.deposit((int)(d*i)); | |
else | |
//扣除手續費2%在進行存款 | |
freeAccount.deposit((int)(d*i*0.98)); | |
unit-=d; | |
} | |
//將單位基金金額傳入,乘上持有單位數,回傳總基金現額 | |
int balance(int i){ | |
return (int)(unit*i); | |
} | |
//取得持有多少單位基金 | |
double getUnit(){ | |
return unit; | |
} | |
} | |
class JPA06_1 { | |
public static void main(String args[]) { | |
//為Peter開個定期帳戶,兩年期的 | |
DepositAccount deposit = new DepositAccount("peter", 2); | |
//存款5000元 | |
deposit.deposit(5000); | |
//為Peter開個活期帳戶 | |
FreeAccount free = new FreeAccount("peter"); | |
//存款20000 | |
free.deposit(20000); | |
//為Peter開個優惠帳戶 | |
SpecialAccount special = new SpecialAccount("peter"); | |
//存款10000 | |
special.deposit(10000); | |
//利用加計利息增加帳戶餘額 | |
deposit.addInterest(); | |
free.addInterest(); | |
special.addInterest(); | |
//顯示個帳戶的餘額 | |
System.out.println("定期存款:" + deposit.balance()); | |
System.out.println("活期存款:" + free.balance()); | |
System.out.println("優惠存款:" + special.balance()); | |
//為Peter建立一個基金帳戶,名稱為"A" | |
FundAccount fund = new FundAccount("peter", "A", free, special); | |
//購入15000元的基金,且每單位為500元 | |
fund.buy(15000, 500); | |
System.out.println("基金現額:" + fund.balance(300)); | |
System.out.println("活期餘額:" + fund.freeAccount.balance()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ JAVA6 - 604_2 */ | |
class Account{ | |
String name; | |
double rate; | |
int balance; | |
Account(String s,double d){ | |
name = s; | |
rate = d; | |
} | |
void setRate(double d){ | |
rate = d; | |
} | |
void deposit(int i){ | |
balance+=i; | |
} | |
void withdraw(int i){ | |
balance-=i; | |
} | |
int balance(){ | |
return balance; | |
} | |
void addInterest(){ | |
balance*=rate+1; | |
} | |
} | |
class DepositAccount extends Account{ | |
DepositAccount(String s,int i){ | |
super(s,0.0); | |
double d =0.0; | |
switch(i){ | |
case 1: | |
d=0.03; | |
break; | |
case 2: | |
d=0.04; | |
break; | |
case 3: | |
d=0.05; | |
break; | |
} | |
super.setRate(d); | |
} | |
} | |
class FreeAccount extends Account{ | |
FreeAccount(String s){ | |
super(s,0.02); | |
} | |
} | |
class SpecialAccount extends Account{ | |
SpecialAccount(String s){ | |
super(s,0.02); | |
} | |
boolean isEmpt(){ | |
return balance>10000; | |
} | |
} | |
class FundAccount extends Account{ | |
String fundName; | |
FreeAccount freeAccount; | |
SpecialAccount specialaccount; | |
double unit; | |
FundAccount(String s,String s1,FreeAccount f,SpecialAccount sa){ | |
super(s,0.0); | |
fundName = s1; | |
freeAccount = f; | |
specialaccount =sa; | |
} | |
void buy(int i,int j){ | |
if(specialaccount.isEmpt()) | |
freeAccount.withdraw(i); | |
else | |
freeAccount.withdraw((int)(i*1.02)); | |
unit+=(double)i/(double)j; | |
} | |
void sell(double d,int i){ | |
if(specialaccount.isEmpt()) | |
freeAccount.deposit((int)(d*i)); | |
else | |
freeAccount.deposit((int)(d*i*0.98)); | |
unit-=d; | |
} | |
int balance(int i){ | |
return (int)(unit*i); | |
} | |
double getUnit(){ | |
return unit; | |
} | |
} | |
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); | |
//從優惠帳戶中提款5000元 | |
special.withdraw(5000); | |
//再買入2000元的基金,以每單位300元購入 | |
fund.buy(2000, 300); | |
System.out.println("基金餘額:" + fund.balance(300)); | |
System.out.println("售出前活期餘額:" + fund.freeAccount.balance()); | |
//賣出全部的股,以每單位400元賣出 | |
fund.sell(fund.getUnit(), 400); | |
//這邊的fund.freeAccount.balance(),是透過fund裡面的freeAccount的balance來取出餘額的,因為fund本身的balance沒有儲存金額進去 | |
System.out.println("售出後活期餘額:" + fund.freeAccount.balance()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ JAVA6 - 604_3 */ | |
class Account{ | |
String name; | |
double rate; | |
int balance; | |
Account(String s,double d){ | |
name = s; | |
rate = d; | |
} | |
void setRate(double d){ | |
rate = d; | |
} | |
void deposit(int i){ | |
balance+=i; | |
} | |
void withdraw(int i){ | |
balance-=i; | |
} | |
int balance(){ | |
return balance; | |
} | |
void addInterest(){ | |
balance*=rate+1; | |
} | |
} | |
class DepositAccount extends Account{ | |
DepositAccount(String s,int i){ | |
super(s,0.0); | |
double d =0.0; | |
switch(i){ | |
case 1: | |
d=0.03;break; | |
case 2: | |
d=0.04;break; | |
case 3: | |
d=0.05;break; | |
} | |
super.setRate(d); | |
} | |
} | |
class FreeAccount extends Account{ | |
FreeAccount(String s){ | |
super(s,0.02); | |
} | |
} | |
class SpecialAccount extends Account{ | |
SpecialAccount(String s){ | |
super(s,0.02); | |
} | |
boolean isEmpt(){ | |
return balance>10000; | |
} | |
} | |
class FundAccount extends Account{ | |
String fundName; | |
FreeAccount freeAccount; | |
SpecialAccount specialaccount; | |
double unit; | |
FundAccount(String s,String s1,FreeAccount f,SpecialAccount sa){ | |
super(s,0.0); | |
fundName = s1; | |
freeAccount = f; | |
specialaccount =sa; | |
} | |
void buy(int i,int j){ | |
if(specialaccount.isEmpt()) | |
freeAccount.withdraw(i); | |
else | |
freeAccount.withdraw((int)(i*1.02)); | |
unit+=(double)i/(double)j; | |
} | |
void sell(double d,int i){ | |
if(specialaccount.isEmpt()) | |
freeAccount.deposit((int)(d*i)); | |
else | |
freeAccount.deposit((int)(d*i*0.98)); | |
unit-=d; | |
} | |
int balance(int i){ | |
return (int)(unit*i); | |
} | |
double getUnit(){ | |
return unit; | |
} | |
} | |
//建立網路帳戶方法 | |
class InternetAccount{ | |
DepositAccount deposit; | |
FreeAccount free; | |
SpecialAccount specisl; | |
FundAccount fund; | |
InternetAccount(){} | |
//從外部傳入定期存款戶物件 | |
void setDeposit(DepositAccount d){ | |
deposit = d; | |
} | |
//從外部傳入活期存款戶物件 | |
void setFree(FreeAccount f){ | |
free=f; | |
} | |
//從外部傳入優惠存款戶物件 | |
void setSpecial(SpecialAccount s){ | |
specisl=s; | |
} | |
void setFund(FundAccount ff){ | |
fund=ff; | |
} | |
int getTotalBalance(){ | |
return deposit.balance+free.balance+specisl.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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ JAVA6 - 604_4 */ | |
import java.util.HashMap; | |
import java.util.Iterator; | |
class Account{ | |
String name; | |
double rate; | |
int balance; | |
Account(String s,double d){ | |
name = s; | |
rate = d; | |
} | |
void setRate(double d){ | |
rate = d; | |
} | |
void deposit(int i){ | |
balance+=i; | |
} | |
void withdraw(int i){ | |
balance-=i; | |
} | |
int balance(){ | |
return balance; | |
} | |
void addInterest(){ | |
balance*=rate+1; | |
} | |
} | |
class DepositAccount extends Account{ | |
DepositAccount(String s,int i){ | |
super(s,0.0); | |
double d =0.0; | |
switch(i){ | |
case 1: | |
d=0.03;break; | |
case 2: | |
d=0.04;break; | |
case 3: | |
d=0.05;break; | |
} | |
super.setRate(d); | |
} | |
} | |
class FreeAccount extends Account{ | |
FreeAccount(String s){ | |
super(s,0.02); | |
} | |
} | |
class SpecialAccount extends Account{ | |
SpecialAccount(String s){ | |
super(s,0.02); | |
} | |
boolean isEmpt(){ | |
return balance>10000; | |
} | |
} | |
class FundAccount extends Account{ | |
String fundName; | |
FreeAccount freeAccount; | |
SpecialAccount specialaccount; | |
double unit; | |
FundAccount(String s,String s1,FreeAccount f,SpecialAccount sa){ | |
super(s,0.0); | |
fundName = s1; | |
freeAccount = f; | |
specialaccount =sa; | |
} | |
void buy(int i,int j){ | |
if(specialaccount.isEmpt()) | |
freeAccount.withdraw(i); | |
else | |
freeAccount.withdraw((int)(i*1.02)); | |
unit+=(double)i/(double)j; | |
} | |
void sell(double d,int i){ | |
if(specialaccount.isEmpt()) | |
freeAccount.deposit((int)(d*i)); | |
else | |
freeAccount.deposit((int)(d*i*0.98)); | |
unit-=d; | |
} | |
int balance(int i){ | |
return (int)(unit*i); | |
} | |
double getUnit(){ | |
return unit; | |
} | |
} | |
class InternetAccount{ | |
DepositAccount deposit; | |
FreeAccount free; | |
SpecialAccount specisl; | |
FundAccount fund; | |
InternetAccount(){} | |
void setDeposit(DepositAccount d){ | |
deposit = d; | |
} | |
void setFree(FreeAccount f){ | |
free=f; | |
} | |
void setSpecial(SpecialAccount s){ | |
specisl=s; | |
} | |
void setFund(FundAccount ff){ | |
fund=ff; | |
} | |
int getTotalBalance(){ | |
return deposit.balance+free.balance+specisl.balance; | |
} | |
} | |
//建立一個多基金的方法 | |
class MultiFund{ | |
HashMap funds; | |
//初始化建構子,使其產生一個HashMap | |
MultiFund(){funds = new HashMap();} | |
//增加新的基金方法 | |
void addFund(String s, FundAccount fundaccount){ | |
funds.put(s, fundaccount); | |
} | |
//列印出所有基金和擁有單位數 | |
void printEachUnit(){ | |
FundAccount fundaccount ; | |
for(Iterator iterator = funds.values().iterator();iterator.hasNext();){ | |
fundaccount = (FundAccount)iterator.next(); | |
System.out.println(fundaccount.fundName+" : "+fundaccount.getUnit()); | |
} | |
} | |
//建立一個方法,傳入基金名稱、傳入單位金額 | |
int getFundBalance(String s ,int i){ | |
return ((FundAccount)funds.get(s)).balance(i); | |
} | |
} | |
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(); | |
//增加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)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ JAVA6 - 604_5 */ | |
import java.util.HashMap; | |
import java.util.Iterator; | |
class Account{ | |
String name; | |
double rate; | |
int balance; | |
Account(String s,double d){ | |
name = s; | |
rate = d; | |
} | |
void setRate(double d){ | |
rate = d; | |
} | |
void deposit(int i){ | |
balance+=i; | |
} | |
void withdraw(int i) throws Exception { | |
//設定當提款金額大於存款金額時,丟出錯誤訊息 | |
if(balance<i){ | |
throw new Exception(name+"提款金額: "+i+" 大於存款金額: "+balance); | |
} else { | |
balance-=i; | |
return; | |
} | |
} | |
int balance(){ | |
return balance; | |
} | |
void addInterest(){ | |
balance*=rate+1; | |
} | |
} | |
class DepositAccount extends Account{ | |
DepositAccount(String s,int i){ | |
super(s,0.0); | |
double d =0.0; | |
switch(i){ | |
case 1: | |
d=0.03; | |
break; | |
case 2: | |
d=0.04; | |
break; | |
case 3: | |
d=0.05; | |
break; | |
} | |
super.setRate(d); | |
} | |
} | |
class FreeAccount extends Account{ | |
FreeAccount(String s){ | |
super(s,0.02); | |
} | |
} | |
class SpecialAccount extends Account{ | |
SpecialAccount(String s){ | |
super(s,0.02); | |
} | |
boolean isEmpt(){ | |
return balance>10000; | |
} | |
} | |
class FundAccount extends Account{ | |
String fundName; | |
FreeAccount freeAccount; | |
SpecialAccount specialaccount; | |
double unit; | |
FundAccount(String s,String s1,FreeAccount f,SpecialAccount sa){ | |
super(s,0.0); | |
fundName = s1; | |
freeAccount = f; | |
specialaccount =sa; | |
} | |
void buy(int i,int j){ | |
//此處也有使用到提款功能,也須將try catch崁入 | |
try{ | |
if(specialaccount.isEmpt()) | |
freeAccount.withdraw(i); | |
else | |
freeAccount.withdraw((int)(i*1.02)); | |
unit+=(double)i/(double)j; | |
} catch(Exception ex) { | |
System.out.println(ex.getMessage()); | |
} | |
} | |
void sell(double d,int i){ | |
if(specialaccount.isEmpt()) | |
freeAccount.deposit((int)(d*i)); | |
else | |
freeAccount.deposit((int)(d*i*0.98)); | |
unit-=d; | |
} | |
int balance(int i){ | |
return (int)(unit*i); | |
} | |
double getUnit(){ | |
return unit; | |
} | |
} | |
class InternetAccount{ | |
DepositAccount deposit; | |
FreeAccount free; | |
SpecialAccount specisl; | |
FundAccount fund; | |
InternetAccount(){} | |
void setDeposit(DepositAccount d){deposit = d;} | |
void setFree(FreeAccount f){free=f;} | |
void setSpecial(SpecialAccount s){specisl=s;} | |
void setFund(FundAccount ff){fund=ff;} | |
int getTotalBalance(){ | |
return deposit.balance+free.balance+specisl.balance; | |
} | |
} | |
class MultiFund{ | |
HashMap funds; | |
MultiFund(){ | |
funds = new HashMap(); | |
} | |
void addFund(String s, FundAccount fundaccount){ | |
funds.put(s, fundaccount); | |
} | |
void printEachUnit(){ | |
FundAccount fundaccount; | |
for(Iterator iterator = funds.values().iterator();iterator.hasNext();System.out.println(fundaccount.fundName+" : "+fundaccount.getUnit())) | |
fundaccount = (FundAccount)iterator.next(); | |
} | |
int getFundBalance(String s ,int i){ | |
return ((FundAccount)funds.get(s)).balance(i); | |
} | |
} | |
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 catch,確保可以抓取錯誤提款訊息 | |
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()); | |
} | |
} | |
} |
TQC+ JAVA6 試題總覽:LINK
Github 備份:LINK
本篇教學的程式碼皆由筆者編輯,歡迎轉貼本教學,但請全文轉貼,謝啦~
不好意思 關於這題我想問個笨笨的問題
回覆刪除關於void addInterest(){balance*=rate+1;}
balance 不是 int 嗎 ... int * double = double
為什麼程式不會壞掉... 把一個double 存進一個int裡面@@
如果不會壞掉 沒有小數點沒關係嗎...我對銀行之類實在沒概念
很蠢的問題 不過我真的很想知道QAQ
balance沒有小數點的原因是像我們自己的銀行存款是沒有小數點的~
刪除而利率是有小數點的,rate+1是因為把自己的本金算進去在算的
也可以這樣表達balance = balance + balance*rate
而double存到int的話,在JAVA中有兩種轉型,「自動轉型」和「強制轉型」
可以參考這篇:http://x.co/7VGDq