設計說明:
1. 請使用Exception撰寫一功能:當任何一個帳戶之餘額低於0時, 不可提款,且印出警告信息。
2. Peter再次購買A基金14000元, 價格為每單位300元。
參考程式碼:
TQC+ Java 試題總整理
- import java.util.*;
- class Account{
- String name;
- double APR;
- int Balance;
- Account(){Balance = 0;}
- public void deposit(int i){
- Balance+=i;
- }
- public void withdraw(int i) throws Exception{
- if(Balance < i)
- throw new Exception(name + ":提款金額: " + i + " 大於存款餘額: " + Balance);
- else
- Balance-=i;
- }
- public int balance(){
- return Balance;
- }
- public void addInterest(){
- Balance+=Balance*APR;
- }
- }
- class DepositAccount extends Account{
- DepositAccount(String n,int i){
- name=n;
- if(i==1) APR=0.03;
- else if(i==2) APR=0.04;
- else if(i==3) APR=0.05;
- }
- }
- class FreeAccount extends Account{
- FreeAccount(String n){
- name=n;
- APR=0.02;
- }
- }
- class SpecialAccount extends Account{
- SpecialAccount(String n){
- name=n;
- APR=0.02;
- }
- boolean isEmpt(){
- return Balance>10000;
- }
- }
- class FundAccount{
- String name,type;
- double unit = 0.0;
- FreeAccount freeAccount;
- SpecialAccount specialAccount;
- FundAccount(String n,String t,FreeAccount f,SpecialAccount s){
- name=n;
- type=t;
- freeAccount=f;
- specialAccount=s;
- }
- public void buy(int i,int j){
- try{
- if (specialAccount.isEmpt()){
- freeAccount.withdraw(i);
- }
- else{
- freeAccount.withdraw((int)(i*1.02));
- }
- unit+=1.0*i/j;
- }
- catch(Exception e){
- System.out.println(e.getMessage());
- }
- }
- public int balance(int j){
- return (int)(unit*j);
- }
- double getUnit() {
- return unit;
- }
- public void sell(double i,int j){
- freeAccount.deposit((int)(i*j*0.98));
- unit -= i;
- }
- }
- class InternetAccount{
- DepositAccount depositAccount;
- FreeAccount freeAccount;
- SpecialAccount specialAccount;
- FundAccount fundAccount;
- InternetAccount(){
- }
- public void setDeposit(DepositAccount deposit){
- depositAccount=deposit;
- }
- public void setFree(FreeAccount setFree){
- freeAccount=setFree;
- }
- public void setSpecial(SpecialAccount setSpecial){
- specialAccount=setSpecial;
- }
- public void setFund(FundAccount setFund){
- fundAccount=setFund;
- }
- public int getTotalBalance(){
- return depositAccount.balance()+freeAccount.balance()+specialAccount.balance();
- }
- }
- class MultiFund{
- HashMap<String,FundAccount> Multi;
- MultiFund(){
- Multi = new HashMap<String, FundAccount>();
- }
- public void addFund(String s,FundAccount Fund){
- Multi.put(s, Fund);
- }
- public void printEachUnit(){
- for(FundAccount f : Multi.values()) {
- System.out.println(f.type + ":" + f.getUnit());
- }
- }
- public int getFundBalance(String s,int i){
- return (int)(i * Multi.get(s).getUnit());
- }
- }
- public class JPA604_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) {
- System.out.println(e.getMessage());
- }
- }
- }
TQC+ Java 試題總整理
聲明:
這裡的範例程式碼皆由本人親自編輯,歡迎轉載本教學,但請註明本網站,尊重一下作者的心血
沒有留言:
張貼留言
歡迎留言,較舊文章需要留言審核看不到自己的留言是正常的。
若長時間無回應請使用以下聯絡方式:
填寫表單:https://forms.gle/hxxX9n4tATcFnhnk8
寄信到:happyplayblogs@gmail.com