設計說明:
1. 該銀行為服務客戶增設網路銀行帳戶,網路銀行帳戶含有某客戶之所有帳戶的資料。
2. 請為Peter開立一網路銀行帳戶,並撰寫能夠計算Peter所有存款帳戶(不含基金)的總餘額的方法。
參考程式碼:
TQC+ Java 試題總整理
- class Account{
- String name;
- double APR;
- int Balance;
- Account(){Balance = 0;}
- public void deposit(int i){
- Balance+=i;
- }
- public void withdraw(int i){
- 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){
- if (specialAccount.isEmpt()){
- freeAccount.withdraw(i);
- }
- else{
- freeAccount.withdraw((int)(i*1.02));
- }
- unit+=1.0*i/j;
- }
- 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();
- }
- }
- public class JPA604_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());
- }
- }
TQC+ Java 試題總整理
聲明:
這裡的範例程式碼皆由本人親自編輯,歡迎轉載本教學,但請註明本網站,尊重一下作者的心血
沒有留言:
張貼留言
歡迎留言,較舊文章需要留言審核看不到自己的留言是正常的。
若長時間無回應請使用以下聯絡方式:
填寫表單:https://forms.gle/hxxX9n4tATcFnhnk8
寄信到:happyplayblogs@gmail.com