close
這邊只做了兩大題,其實後來還有再精簡修改,但沒打上註解
之後有時間再打好放上來
602-1
//設一個類別,來回傳價錢 class Unit{ int cost; Unit(){ cost = 0; } public int cost(){ return this.cost; } } class LCD extends Unit{ //繼承Unit LCD(int i){ if(i == 10){cost = 2000;} //根據i代入值,來條件決定cost的價錢 else if(i == 15){cost = 2500;} else if(i == 17){cost = 3000;} } } class CPU extends Unit{ CPU(double i){ if(i == 1.66){cost = 6000;} else if(i == 2.2){cost = 8000;} else if(i == 2.4){cost = 11000;} } } class HD extends Unit{ HD(int i){ if(i == 120){cost = 2400;} else if(i == 160){cost = 2800;} } } class notebook{ //設定一個類別,來回傳成本、定價 double getCost = 0; double getPrice = 0; public double getCost(){return this.getCost;} public double getPrice(){return this.getPrice;} } class MiniNote extends notebook{ //繼承回傳成本定價的類別 MiniNote(){ //將成本與定價分別加總、乘上倍率 getCost = (new LCD(10).cost + new CPU(1.66).cost + new HD(120).cost)*1.4; getPrice = (new LCD(10).cost + new CPU(1.66).cost + new HD(120).cost)*2; } } class Note15 extends notebook{ Note15(){ getCost = (new LCD(15).cost + new CPU(2.2).cost + new HD(160).cost)*1.4; getPrice = (new LCD(15).cost + new CPU(2.2).cost + new HD(160).cost)*2; } } class JPA06_1 { public static void main(String args[]){ MiniNote mininote = new MiniNote(); System.out.println("MiniNote cost:"+mininote.getCost()+", price:"+mininote.getPrice()); Note15 note15 = new Note15(); System.out.println("Note15 cost:"+note15.getCost()+", price:"+note15.getPrice()); } }
602-2
class Unit{ int cost; Unit(){ cost = 0; } public int cost(){ return cost; } } class LCD extends Unit{ LCD(int i){ if(i == 10){cost = 2000;} else if(i == 15){cost = 2500;} else if(i == 17){cost = 3000;} } } class CPU extends Unit{ CPU(double i){ if(i == 1.66){cost = 6000;} else if(i == 2.2){cost = 8000;} else if(i == 2.4){cost = 11000;} } } class HD extends Unit{ HD(int i ){ if(i == 120){cost = 2400;} else if(i == 160){cost = 2800;} } } class Notebook{ double getCost; double getPrice; Notebook(){getCost = 0;getPrice = 0;} //預設價格為0 public double getCost(){return getCost;} public double getPrice(){return getPrice;} } class MiniNote extends Notebook{ MiniNote(){ //要.cost使用回傳價格的函數 getCost = (new LCD(10).cost + new CPU(1.66).cost + new HD(120).cost)*1.4; getPrice = (new LCD(10).cost + new CPU(1.66).cost + new HD(120).cost)*1.4; } } class Note15 extends Notebook{ Note15(){ getCost = (new LCD(15).cost + new CPU(2.2).cost + new HD(160).cost)*2.2; getPrice = (new LCD(15).cost + new CPU(2.2).cost + new HD(160).cost)*2.2; } } class computer{ //設一個桌上型電腦的類別 double getCost; double getPrice; computer(){getCost = 0;getPrice = 0;} public double getCost(){return getCost;} public double getPrice(){return getPrice*1.8;} //兩種要繼承此類別的零件剛好都要乘1.8倍 } class PC extends computer{ //PC類別繼承桌上型電腦類別 PC(){ getCost = (new CPU(2.4).cost() + new HD(160).cost() + 500); getPrice = (new CPU(2.4).cost() + new HD(160).cost()); } } class MultiPC extends PC{ //超級電腦類別繼承桌上型電腦類別 int NoCPU; int NoHD; MultiPC(int NoCPU,int NoHD){ getCost = (new CPU(2.4).cost()*NoCPU+new HD(160).cost()*NoHD)*1.2; getPrice = (new CPU(2.4).cost()*NoCPU+new HD(160).cost()*NoHD); } } class JPA06_2 { public static void main(String args[]){ PC pc = new PC(); System.out.println("PC cost:"+pc.getCost()+", price:"+pc.getPrice()); MultiPC multipc1 = new MultiPC(2, 4); System.out.println("MultiPC: 2CPU, 4HD, cost:"+multipc1.getCost()+", price:"+multipc1.getPrice()); MultiPC multipc2 = new MultiPC(4, 8); System.out.println("MultiPC: 4CPU, 8HD, cost:"+multipc2.getCost()+", price:"+multipc2.getPrice()); } }
602-3
class Unit{ int cost; Unit(){cost = 0;} public int cost(){ return cost; } } class LCD extends Unit{ LCD(int i){ if(i == 10){cost = 2000;} else if(i == 15){cost = 2500;} else if (i == 17){cost = 3000;} } } class CPU extends Unit{ CPU(double i){ if(i == 1.66){cost = 6000;} else if(i == 2.2){cost = 8000;} else if (i == 2.4){cost = 11000;} } } class HD extends Unit{ HD(int i){ if(i == 120){cost = 2400;} else if(i == 160){cost = 2800;} } } class Notebook{ double getCost; double getPrice; Notebook(){getCost = 0;getPrice=0;} public double getCost(){return getCost;} public double getPrice(){return getPrice;} } class MiniNote extends Notebook{ MiniNote(){ getCost = (new LCD(10).cost + new CPU(1.66).cost + new HD(120).cost)*1.4; getPrice = (new LCD(10).cost + new CPU(1.66).cost + new HD(120).cost)*2; } } class Note15 extends Notebook{ Note15(){ getCost = (new LCD(15).cost + new CPU(2.2).cost + new HD(160).cost)*1.4; getPrice = (new LCD(15).cost + new CPU(2.2).cost + new HD(160).cost)*2; } } class computer{ double getCost; double getPrice; computer(){getCost=0;getPrice=0;} public double getCost(){return getCost;} public double getPrice(){return getPrice*1.8;} } class PC extends computer{ PC(){ getCost = (new CPU(2.4).cost + new HD(160).cost + 500); getPrice = (new CPU(2.4).cost + new HD(160).cost); } } class Multi extends computer{ int Nocpu; int Nohd; Multi(int Nocpu,int Nohd){ getCost = (new CPU(2.4).cost*Nocpu + new HD(160).cost*Nohd)*1.2; getPrice = (new CPU(2.4).cost*Nocpu + new HD(160).cost*Nohd); } } class AllPC{ //建立AllPC類別 double pc; //設定變數,讓AllPC與isExpensive同時取用 double note15; AllPC(double a,double b){ pc =a; note15 = b; } public boolean isExpensive(){ //依照題目要求設為布林值 if(pc>note15) return true; //根據AllPC獲得的值比大小 else return false; } } class JPA06_3 { public static void main(String args[]) { PC pc = new PC(); Note15 note15 = new Note15(); AllPC allpc = new AllPC(pc.getPrice(),note15.getPrice()); //使用AllPC類別,建立物件 if(allpc.isExpensive()) System.out.println("PC is more expensive than Note15"); //使用AllPC類別裡的isExpensive 取得布林值,true的話就打印此行 else System.out.println("Note15 is more expensive than PC"); //false打印此行 } }
602-4
import java.util.*; class Unit{ int cost; Unit(){cost = 0;} public int cost(){ return cost; } } class LCD extends Unit{ LCD(int i){ if(i == 10){cost = 2000;} else if(i == 15){cost = 2500;} else if (i == 17){cost = 3000;} } } class CPU extends Unit{ CPU(double i){ if(i == 1.66){cost = 6000;} else if(i == 2.2){cost = 8000;} else if (i == 2.4){cost = 11000;} } } class HD extends Unit{ HD(int i){ if(i == 120){cost = 2400;} else if(i == 160){cost = 2800;} } } class Notebook{ double getCost; double getPrice; Notebook(){getCost = 0;getPrice=0;} public double getCost(){return getCost;} public double getPrice(){return getPrice;} } class MiniNote extends computer{ MiniNote(){ getCost = (new LCD(10).cost + new CPU(1.66).cost + new HD(120).cost)*1.4; getPrice = (new LCD(10).cost + new CPU(1.66).cost + new HD(120).cost)*2; } } class Note15 extends computer{ Note15(){ getCost = (new LCD(15).cost + new CPU(2.2).cost + new HD(160).cost)*1.4; getPrice = (new LCD(15).cost + new CPU(2.2).cost + new HD(160).cost)*2; } } class computer{ double getCost; double getPrice; computer(){getCost=0;getPrice=0;} public double getCost(){return getCost;} public double getPrice(){return getPrice;} } class PC extends computer{ PC(){ getCost = (new CPU(2.4).cost + new HD(160).cost + 500); getPrice = (new CPU(2.4).cost + new HD(160).cost)*1.8; } } class Multi extends computer{ int Nocpu; int Nohd; Multi(int Nocpu,int Nohd){ getCost = (new CPU(2.4).cost*Nocpu + new HD(160).cost*Nohd)*1.2; getPrice = (new CPU(2.4).cost*Nocpu + new HD(160).cost*Nohd)*1.8; } } class AllPC{ //建立AllPC類別 double pc; //設定變數,讓AllPC與isExpensive同時取用 double note15; AllPC(double a,double b){ pc =a; note15 = b; } public boolean isExpensive(){ //依照題目要求設為布林值 if(pc>note15) return true; //根據AllPC獲得的值比大小 else return false; } } //修改前面的MiniNote 跟 Note15類別,改為繼承computer類別,並將computer原本的定價1.8倍改到PC跟MultiPC中 class Order{ LinkedList<computer> pcs; //使用LinkedList方法,設一個名為pcs的List,表示要收集的元素都是<computer> Order(){ pcs = new LinkedList<computer>(); } public void in(computer i){ //代入變數i ,型態為computer pcs.add(i); //將 i 加入 List } public double revenue(){ //設一個方法,返回總收入 double sum = 0; for(computer x:pcs){ //將pcs訂單中的所有項目迴圈 sum = sum + x.getPrice(); //迴圈加總該項目裡的getPrice()的價錢 } return sum; } } class JPA06_4 { public static void main(String args[]){ Order ord = new Order(); ord.in(new MiniNote()); ord.in(new Note15()); ord.in(new PC()); System.out.println(ord.revenue()); } }
602-5
import java.util.*; class Unit{ int cost; Unit(){cost = 0;} public int cost(){ return cost; } } class LCD extends Unit{ LCD(int i){ if(i == 10){cost = 2000;} else if(i == 15){cost = 2500;} else if (i == 17){cost = 3000;} } } class CPU extends Unit{ CPU(double i){ if(i == 1.66){cost = 6000;} else if(i == 2.2){cost = 8000;} else if (i == 2.4){cost = 11000;} } } class HD extends Unit{ HD(int i){ if(i == 120){cost = 2400;} else if(i == 160){cost = 2800;} } } class Notebook{ double getCost; double getPrice; Notebook(){getCost = 0;getPrice=0;} public double getCost(){return getCost;} public double getPrice(){return getPrice;} } class MiniNote extends computer{ MiniNote(){ getCost = (new LCD(10).cost + new CPU(1.66).cost + new HD(120).cost)*1.4; getPrice = (new LCD(10).cost + new CPU(1.66).cost + new HD(120).cost)*2; } } class Note15 extends computer{ Note15(){ getCost = (new LCD(15).cost + new CPU(2.2).cost + new HD(160).cost)*1.4; getPrice = (new LCD(15).cost + new CPU(2.2).cost + new HD(160).cost)*2; } } class computer{ double getCost; double getPrice; computer(){getCost=0;getPrice=0;} public double getCost(){return getCost;} public double getPrice(){return getPrice;} } class PC extends computer{ PC(){ getCost = (new CPU(2.4).cost + new HD(160).cost + 500); getPrice = (new CPU(2.4).cost + new HD(160).cost)*1.8; } } class Multi extends computer{ int Nocpu; int Nohd; Multi(int Nocpu,int Nohd){ getCost = (new CPU(2.4).cost*Nocpu + new HD(160).cost*Nohd)*1.2; getPrice = (new CPU(2.4).cost*Nocpu + new HD(160).cost*Nohd)*1.8; } } class AllPC{ //建立AllPC類別 double pc; //設定變數,讓AllPC與isExpensive同時取用 double note15; AllPC(double a,double b){ pc =a; note15 = b; } public boolean isExpensive(){ //依照題目要求設為布林值 if(pc>note15) return true; //根據AllPC獲得的值比大小 else return false; } } //修改前面的MiniNote 跟 Note15類別,改為繼承computer類別,並將computer原本的定價1.8倍改到PC跟MultiPC中 class Order{ LinkedList<computer> pcs; //使用LinkedList方法,設一個名為pcs的List,表示要收集的元素都是<computer> Order(){ pcs = new LinkedList<computer>(); } public void in(computer i){ //代入變數i ,型態為computer pcs.add(i); //將 i 加入 List } public double revenue(){ //設一個方法,返回總利潤(總收入-總成本) double sum = 0; for(computer x:pcs){ //將pcs訂單中的所有項目迴圈 sum = sum + (x.getPrice()-x.getCost()); //迴圈加總該項目裡的總利潤 } return sum; } } class JPA06_5 { public static void main(String args[]){ Order ord = new Order(); //建立Order物件 ord.in(new MiniNote()); ord.in(new Note15()); ord.in(new PC()); if(ord.revenue()>20000)System.out.println("This order exceeds 20000:"+ord.revenue()); //例外處理 else System.out.println(ord.revenue()); } }
604-1
class Account{ int money; //存款 String name; //開戶人 double rate; //年利率 Account(){money = 0;} public void deposit(int i){money = money + i;} //存款 public void withdraw(int i){money = money - i;} //提款 public int balance(){return money;} //查詢餘額(回傳存款) public void addInterest(){money = money + (int)(money * rate);}//乘上年利率 } class DepositAccount extends Account{ //定期存款帳戶 DepositAccount(String n,int y){ name = n; if(y == 1){rate = 0.03;} //條件判斷"年期",來選擇"年利率" else if(y == 2){rate = 0.04;} else if(y == 3){rate = 0.05;} } } class FreeAccount extends Account{ //活期存款帳戶 FreeAccount(String n){ name = n; rate = 0.02; //題目提供的年利率 } } class SpecialAccount extends Account{ //優惠存款帳戶(只要該帳戶存款大於10000,基金交易免手續費) SpecialAccount(String n){ name = n; rate = 0.02; } boolean isEmpt(){ //題目要求回傳布林值 if(money > 10000){return true;} //若存款大於10000則回傳true else return false; } } class FundAccount extends Account{ //基金存款帳戶 double unit= 0.0; //基金現額 String name; //開戶人 String fundname; //基金名稱 FreeAccount freeAccount; //活期存款帳戶 SpecialAccount specialAccount; //優惠存款帳戶 FundAccount(String n,String fn,FreeAccount f,SpecialAccount s){ name = n; fundname = fn; freeAccount = f; specialAccount = s; } void buy(int i,int j){ //代入 "買入的基金"、"基金每單位價格" if(specialAccount.isEmpt()){ //檢查優惠存款帳戶餘額是否有超過10000 freeAccount.withdraw(i); //依題目從活期存款帳戶提款 }else freeAccount.withdraw((int)(i*1.02)); //依題目從活期存款帳戶提款 unit = unit + (double)i/j; //計算基金現額(買入基金/基金每單位價格) } public int balance(int j){ //代入基金每單位價格,傳回基金活期餘額 return (int)(unit*j); //基金現額*每單位價格 } } 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()); } }
604-2
class Account{ int money; //存款 String name; //開戶人 double rate; //年利率 Account(){money = 0;} public void deposit(int i){money = money + i;} //存款 public void withdraw(int i){money = money - i;} //提款 public int balance(){return money;} //查詢餘額(回傳存款) public void addInterest(){money = money + (int)(money * rate);}//乘上年利率 } class DepositAccount extends Account{ //定期存款帳戶 DepositAccount(String n,int y){ name = n; if(y == 1){rate = 0.03;} //條件判斷"年期",來選擇"年利率" else if(y == 2){rate = 0.04;} else if(y == 3){rate = 0.05;} } } class FreeAccount extends Account{ //活期存款帳戶 FreeAccount(String n){ name = n; rate = 0.02; //題目提供的年利率 } } class SpecialAccount extends Account{ //優惠存款帳戶(只要該帳戶存款大於10000,基金交易免手續費) SpecialAccount(String n){ name = n; rate = 0.02; } boolean isEmpt(){ //題目要求回傳布林值 if(money > 10000){return true;} //若存款大於10000則回傳true else return false; } } class FundAccount extends Account{ //基金存款帳戶 double unit= 0.0; //基金現額 String name; //開戶人 String fundname; //基金名稱 FreeAccount freeAccount; //活期存款帳戶 SpecialAccount specialAccount; //優惠存款帳戶 FundAccount(String n,String fn,FreeAccount f,SpecialAccount s){ name = n; fundname = fn; freeAccount = f; specialAccount = s; } void buy(int i,int j){ //代入 "買入的基金總值"、"基金每單位價格" if(specialAccount.isEmpt()){ //檢查優惠存款帳戶餘額是否有超過10000 freeAccount.withdraw(i); //依題目從活期存款帳戶提款 }else freeAccount.withdraw((int)(i*1.02)); //依題目從活期存款帳戶提款 unit = unit + (double)i/j; //計算基金現額(買入基金/基金每單位價格),型態須為double,以免之後會有誤差 } public int balance(int j){ //代入基金每單位價格,傳回基金活期餘額 return (int)(unit*j); //基金現額*每單位價格 要先讓unit * j否則會出現誤差 } public double getUnit(){ //回傳基金餘額 return unit; } void sell(double i,int j){ //賣出基金,代入"基金現額","每單位價格" if(specialAccount.isEmpt()){ //檢查優惠存款帳戶餘額是否有超過10000 freeAccount.deposit((int)(i*j)); //依題目到活期存款帳戶存款 }else freeAccount.deposit((int)(i*j*0.98)); //依題目到活期存款帳戶存款 unit = unit - i; } } 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()); } }
604-3
class Account{ int money; //存款 String name; //開戶人 double rate; //年利率 Account(){money = 0;} public void deposit(int i){money = money + i;} //存款 public void withdraw(int i){money = money - i;} //提款 public int balance(){return money;} //查詢餘額(回傳存款) public void addInterest(){money = money + (int)(money * rate);}//乘上年利率 } class DepositAccount extends Account{ //定期存款帳戶 DepositAccount(String n,int y){ name = n; if(y == 1){rate = 0.03;} //條件判斷"年期",來選擇"年利率" else if(y == 2){rate = 0.04;} else if(y == 3){rate = 0.05;} } } class FreeAccount extends Account{ //活期存款帳戶 FreeAccount(String n){ name = n; rate = 0.02; //題目提供的年利率 } } class SpecialAccount extends Account{ //優惠存款帳戶(只要該帳戶存款大於10000,基金交易免手續費) SpecialAccount(String n){ name = n; rate = 0.02; } boolean isEmpt(){ //題目要求回傳布林值 if(money > 10000){return true;} //若存款大於10000則回傳true else return false; } } class FundAccount extends Account{ //基金存款帳戶 double unit= 0.0; //基金現額 String name; //開戶人 String fundname; //基金名稱 FreeAccount freeAccount; //活期存款帳戶 SpecialAccount specialAccount; //優惠存款帳戶 FundAccount(String n,String fn,FreeAccount f,SpecialAccount s){ name = n; fundname = fn; freeAccount = f; specialAccount = s; } void buy(int i,int j){ //代入 "買入的基金總值"、"基金每單位價格" if(specialAccount.isEmpt()){ //檢查優惠存款帳戶餘額是否有超過10000 freeAccount.withdraw(i); //依題目從活期存款帳戶提款 }else freeAccount.withdraw((int)(i*1.02)); //依題目從活期存款帳戶提款 unit = unit + (double)i/j; //計算基金現額(買入基金/基金每單位價格),型態須為double,以免之後會有誤差 } public int balance(int j){ //代入基金每單位價格,傳回基金活期餘額 return (int)(unit*j); //基金現額*每單位價格,要先讓unit*j,否則會出現誤差 } public double getUnit(){ //回傳基金餘額 return unit; } void sell(double i,int j){ //賣出基金,代入"基金現額","每單位價格" if(specialAccount.isEmpt()){ //檢查優惠存款帳戶餘額是否有超過10000 freeAccount.deposit((int)(i*j)); //依題目到活期存款帳戶存款 }else freeAccount.deposit((int)(i*j*0.98)); //依題目到活期存款帳戶存款 unit = unit - i; } } class InternetAccount{ //建立網路銀行帳戶類別 DepositAccount deposit; //設定四個帳戶的變數 FreeAccount free; SpecialAccount special; FundAccount fund; InternetAccount(){} void setDeposit(DepositAccount d){ //分別代入帳戶 deposit = d; } void setFree(FreeAccount f){ free = f; } void setSpecial(SpecialAccount s){ special = s; } void setFund(FundAccount fd){ fund = fd; } public int getTotalBalance(){ //將三種帳戶的餘額相加並返回 return (deposit.balance() + free.balance() + special.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()); } }
604-4
import java.util.*; class Account{ int money; //存款 String name; //開戶人 double rate; //年利率 Account(){money = 0;} public void deposit(int i){money = money + i;} //存款 public void withdraw(int i){money = money - i;} //提款 public int balance(){return money;} //查詢餘額(回傳存款) public void addInterest(){money = money + (int)(money * rate);}//乘上年利率 } class DepositAccount extends Account{ //定期存款帳戶 DepositAccount(String n,int y){ name = n; if(y == 1){rate = 0.03;} //條件判斷"年期",來選擇"年利率" else if(y == 2){rate = 0.04;} else if(y == 3){rate = 0.05;} } } class FreeAccount extends Account{ //活期存款帳戶 FreeAccount(String n){ name = n; rate = 0.02; //題目提供的年利率 } } class SpecialAccount extends Account{ //優惠存款帳戶(只要該帳戶存款大於10000,基金交易免手續費) SpecialAccount(String n){ name = n; rate = 0.02; } boolean isEmpt(){ //題目要求回傳布林值 if(money > 10000){return true;} //若存款大於10000則回傳true else return false; } } class FundAccount{ //基金存款帳戶 double unit= 0.0; //基金現額 String name; //開戶人 String fundname; //基金名稱 FreeAccount freeAccount; //活期存款帳戶 SpecialAccount specialAccount; //優惠存款帳戶 FundAccount(String n,String fn,FreeAccount f,SpecialAccount s){ name = n; fundname = fn; freeAccount = f; specialAccount = s; } void buy(int i,int j){ //代入 "買入的基金總值"、"基金每單位價格" if(specialAccount.isEmpt()){ //檢查優惠存款帳戶餘額是否有超過10000 freeAccount.withdraw(i); //依題目從活期存款帳戶提款 }else freeAccount.withdraw((int)(i*1.02)); //依題目從活期存款帳戶提款 unit = unit + (double)i/j; //計算基金現額(買入基金/基金每單位價格),型態須為double,以免之後會有誤差 } public int balance(int j){ //代入基金每單位價格,傳回基金活期餘額 return (int)(unit*j); //基金現額*每單位價格 } public double getUnit(){ //回傳基金餘額 return unit; } void sell(double i,int j){ //賣出基金,代入"基金現額","每單位價格" if(specialAccount.isEmpt()){ //檢查優惠存款帳戶餘額是否有超過10000 freeAccount.deposit((int)(i*j)); //依題目到活期存款帳戶存款 }else freeAccount.deposit((int)(i*j*0.98)); //依題目到活期存款帳戶存款 unit = unit - i; } } class InternetAccount{ //建立網路銀行帳戶類別 DepositAccount deposit; //設定四個帳戶的變數 FreeAccount free; SpecialAccount special; FundAccount fund; InternetAccount(){} void setDeposit(DepositAccount d){ //分別代入帳戶 deposit = d; } void setFree(FreeAccount f){ free = f; } void setSpecial(SpecialAccount s){ special = s; } void setFund(FundAccount fd){ fund = fd; } public int getTotalBalance(){ //將三種帳戶的餘額相加並返回 return (deposit.balance() + free.balance() + special.balance()); } } class MultiFund{ //設定一個多筆基金類別 HashMap<String ,FundAccount> Multi = new HashMap<String ,FundAccount>(); //依據題目要求,使用HashMap方法(key為基金名稱,value為基金存款帳戶) MultiFund(){} public void addFund(String fundname,FundAccount fd){ //添加基金的方法,(基金名稱,基金存款帳戶) Multi.put(fundname,fd); //將該筆資料放入hashmap中 } public void printEachUnit(){ //迴圈打印hashmap中的資料 for(FundAccount fd:Multi.values()){ //迴圈(基金帳戶類別 基金帳戶:hashmap中的所有值) System.out.println(fd.fundname + ":" + fd.getUnit()); //打印基金帳戶名稱、基金帳戶中的基金現額 } } public int getFundBalance(String fundname,int i){ //輸出該基金名稱的基金餘額,代入(基金名稱,基金現額) return Multi.get(fundname).balance(i); //hashmap.get(基金名稱)取得基金存款帳戶,再使用其中的balance()方法代入基金現額,返回基金餘額 } } 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)); } }
604-5
import java.util.*; class Account{ int money; //存款 String name; //開戶人 double rate; //年利率 Account(){money = 0;} public void deposit(int i){money = money + i;} //存款 //例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理 //例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理 //例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理 public void withdraw(int i) throws Exception{ //聲明會丟出一個名為Exception的關鍵字 if(money < i){ throw new Exception(name + ":提款金額: " + i + "大於存款餘額: " + money); //throw new 創建Exception關鍵字,並附上getMessage的訊息 } else money = money - i; } //提款 //例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理 //例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理 //例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理 public int balance(){return money;} //查詢餘額(回傳存款) public void addInterest(){money = money + (int)(money * rate);}//乘上年利率 } class DepositAccount extends Account{ //定期存款帳戶 DepositAccount(String n,int y){ name = n; if(y == 1){rate = 0.03;} //條件判斷"年期",來選擇"年利率" else if(y == 2){rate = 0.04;} else if(y == 3){rate = 0.05;} } } class FreeAccount extends Account{ //活期存款帳戶 FreeAccount(String n){ name = n; rate = 0.02; //題目提供的年利率 } } class SpecialAccount extends Account{ //優惠存款帳戶(只要該帳戶存款大於10000,基金交易免手續費) SpecialAccount(String n){ name = n; rate = 0.02; } boolean isEmpt(){ //題目要求回傳布林值 if(money > 10000){return true;} //若存款大於10000則回傳true else return false; } } class FundAccount{ //基金存款帳戶 double unit= 0.0; //基金現額 String name; //開戶人 String fundname; //基金名稱 FreeAccount freeAccount; //活期存款帳戶 SpecialAccount specialAccount; //優惠存款帳戶 FundAccount(String n,String fn,FreeAccount f,SpecialAccount s){ name = n; fundname = fn; freeAccount = f; specialAccount = s; } //例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理 //例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理 //例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理 void buy(int i,int j){ //代入 "買入的基金總值"、"基金每單位價格" try{ if(specialAccount.isEmpt()){ //檢查優惠存款帳戶餘額是否有超過10000 freeAccount.withdraw(i); //依題目從活期存款帳戶提款 }else freeAccount.withdraw((int)(i*1.02)); //依題目從活期存款帳戶提款 unit = unit + (double)i/j; //計算基金現額(買入基金/基金每單位價格),型態須為double,以免之後會有誤差 } catch (Exception e){ //捕捉到異常的話,將這個異常命名為e並代入 System.out.println(e.getMessage()); } } //例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理 //例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理 //例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理例外處理 public int balance(int j){ //代入基金每單位價格,傳回基金活期餘額 return (int)(unit*j); //基金現額*每單位價格 } public double getUnit(){ //回傳基金餘額 return unit; } void sell(double i,int j){ //賣出基金,代入"基金現額","每單位價格" if(specialAccount.isEmpt()){ //檢查優惠存款帳戶餘額是否有超過10000 freeAccount.deposit((int)(i*j)); //依題目到活期存款帳戶存款 }else freeAccount.deposit((int)(i*j*0.98)); //依題目到活期存款帳戶存款 unit = unit - i; } } class InternetAccount{ //建立網路銀行帳戶類別 DepositAccount deposit; //設定四個帳戶的變數 FreeAccount free; SpecialAccount special; FundAccount fund; InternetAccount(){} void setDeposit(DepositAccount d){ //分別代入帳戶 deposit = d; } void setFree(FreeAccount f){ free = f; } void setSpecial(SpecialAccount s){ special = s; } void setFund(FundAccount fd){ fund = fd; } public int getTotalBalance(){ //將三種帳戶的餘額相加並返回 return (deposit.balance() + free.balance() + special.balance()); } } class MultiFund{ //設定一個多筆基金類別 HashMap<String ,FundAccount> Multi = new HashMap<String ,FundAccount>(); //依據題目要求,使用HashMap方法(key為基金名稱,value為基金存款帳戶) MultiFund(){} public void addFund(String fundname,FundAccount fd){ //添加基金的方法,(基金名稱,基金存款帳戶) Multi.put(fundname,fd); //將該筆資料放入hashmap中 } public void printEachUnit(){ //迴圈打印hashmap中的資料 for(FundAccount fd:Multi.values()){ //迴圈(基金帳戶類別 基金帳戶:hashmap中的所有值) System.out.println(fd.fundname + ":" + fd.getUnit()); //打印基金帳戶名稱、基金帳戶中的基金現額 } } public int getFundBalance(String fundname,int i){ //輸出該基金名稱的基金餘額,代入(基金名稱,基金現額) return Multi.get(fundname).balance(i); //hashmap.get(基金名稱)取得基金存款帳戶,再使用其中的balance()方法代入基金現額,返回基金餘額 } } 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); fund.buy(15000, 500); try { 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) { //捕捉到異常的話,將這個異常命名為e並代入 System.out.println(e.getMessage()); } } }
全站熱搜
留言列表