2017-01-24 42 views
-2

這裏是我的代碼。 主要SavingsDemo.java

public class SavingsDemo 
{ 
    public static void main(String[] args) 
    { 
     // Create a SavingsAccount object with a $100 balance, 
     // 3% interest rate, and a monthly service charge 
     // of $2.50. 
     SavingsAccount savings = 
        new SavingsAccount(100.0, 0.03, 2.50); 

     // Display what we've got. 
     System.out.printf("Balance: $%,.2f\n", 
         savings.getBalance()); 
     System.out.println("Number of deposits: " + 
         savings.getNumDeposits()); 
     System.out.println("Number of withdrawals: " + 
         savings.getNumWithdrawals()); 
     System.out.println(); 

     // Make some deposits. 
     savings.deposit(25.00); 
     savings.deposit(10.00); 
     savings.deposit(35.00); 

     // Display what we've done so far. 
     System.out.printf("Balance: $%,.2f\n", 
         savings.getBalance()); 
     System.out.println("Number of deposits: " + 
         savings.getNumDeposits()); 
     System.out.println("Number of withdrawals: " + 
         savings.getNumWithdrawals()); 
     System.out.println(); 

     // Make some withdrawals. 
     savings.withdraw(100.00); 
     savings.withdraw(50.00); 
     savings.withdraw(10.00); 
     savings.withdraw(1.00); 
     savings.withdraw(1.00); 

     // Display what we've done so far. 
     System.out.printf("Balance: $%,.2f\n", 
         savings.getBalance()); 
     System.out.println("Number of deposits: " + 
         savings.getNumDeposits()); 
     System.out.println("Number of withdrawals: " + 
         savings.getNumWithdrawals()); 
     System.out.println(); 

     // Do the monthly processing. 
     savings.monthlyProcess(); 

     // Display what we've done so far. 
     System.out.printf("Balance: $%,.2f\n", 
         savings.getBalance()); 
     System.out.println("Number of deposits: " + 
         savings.getNumDeposits()); 
     System.out.println("Number of withdrawals: " + 
         savings.getNumWithdrawals()); 
    } 
} 

超類BankAccount.java

public class BankAccount 
{ 
    private double balance;    //The balance in the account 
    private int numDeposits;    //Number of deposits this month 
    private int numWithdrawals;   //Number of withdrawals 
    private double interestRate;   //Annual interest rate 
    private double monthlyServiceCharge; //Monthly service charge 

    /** 
    The constructor 
    Accept arguments for the balance and annual interest rate. 
    @param bal To hold the number of balance. 
    @param intRate To hold the number of annual interest rate. 
    @param mon To hold the number of monthly service charge. 
    */ 
    public BankAccount(double bal, double intRate, double mon) 
    { 
     balance = bal; 
     interestRate = intRate; 
     monthlyServiceCharge = mon; 
    } 

    /** 
    This method to hold the amount of deposit. 
    @param amount The amount of deposit. 
    */ 
    public void deposit(double amount) 
    { 
     balance = balance + amount; 
     numDeposits++; 
    } 

    /** 
    This method to hold the amount of withdrawal. 
    @param amount The amount of withdrawal. 
    */ 
    public void withdraw(double amount) 
    { 
     balance = balance - amount; 
     numWithdrawals++; 
    } 

    /** 
    This method to update the balance by calculating the monthly interest 
    earned by the account. 
    */ 
    private void calcInterest() 
    { 
     double monIntRate; 
     double monInt; 

     monIntRate = (interestRate/12.0); 
     monInt = balance * monIntRate; 
     balance = balance + monInt; 
    } 
    /** 
    This method to calculate the monthly service charge. 
    */ 
    public void monthlyProcess() 
    { 
     balance = balance - monthlyServiceCharge; 
     calcInterest(); 
     numWithdrawals = 0; 
     numDeposits = 0; 
     monthlyServiceCharge = 0; 
    } 

    /** 
    This method to hold the number of monthly service charge. 
    @param amount The number of monthly service charge. 
    */ 
    public void setMonthlyServiceCharges(double amount) 
    { 
     monthlyServiceCharge += amount; 
    } 

    /** 
    This method to return the amount of balance. 
    @return The amount of balance. 
    */ 
    public double getBalance() 
    { 
     return balance; 
    } 

    /** 
    This method to return the number of deposits. 
    @return The number of deposits. 
    */ 
    public int getNumDeposits() 
    { 
     return numDeposits; 
    } 

    /** 
    This method to return the number of withdrawals. 
    @return The number of withdrawals. 
    */ 
    public int getNumWithdrawals() 
    { 
     return numWithdrawals; 
    } 

    /** 
    This method to return the number of annual interest rate. 
    @return The number of annual interest rate. 
    */ 
    public double getInterestRate() 
    { 
     return interestRate; 
    } 

    /** 
    This method to return the amount of monthly service charge. 
    @return The amount of monthly service charge. 
    */ 
    public double getMonthlyServiceCharge() 
    { 
     return monthlyServiceCharge; 
    } 
} 

子類SavingsAccount.java

public class SavingsAccount extends BankAccount 
{ 
    private boolean status; 

    /** 
    The constructor 
    To accept the argument of balance, interest rate and monthly charge. 
    @param bal To hold the amount of balance. 
    @param intRate To hold the number of annual interest rate. 
    @param mon To hold the amount of monthly service charge. 
    */ 
    public SavingsAccount(double bal, double intRate, double mon) 
    { 
     super(bal, intRate, mon); 

     if (bal < 25) 
     { 
      status = false; 
     } 
     else 
     { 
      status = true; 
     } 
    } 

    /** 
    This method to determine whether the account is inactive before withdrawals 
    is made. 
    @param amount The amount of withdrawal. 
    */ 
    public void withdraw(double amount) 
    { 
     if (status = true) 
     { 
      super.withdraw(amount); 
     } 
    } 

    /** 
    This method to determine whether the account is inactive before a 
    deposit is made. 
    @param amount The amount of deposit. 
    */ 
    public void deposit(double amount) 
    { 
     if (status = true) 
     { 
      super.deposit(amount); 
     } 
    } 

    /** 
    This method to check the number of withdrawals. 
    */ 
    public void monthlyProcess() 
    { 
     if (getNumWithdrawals() > 4) 
     { 
      setMonthlyServiceCharges(getNumWithdrawals() - 4); 
     } 
     super.monthlyProcess(); 
     if (getBalance() < 25) 
     { 
      status = false; 
     } 
    } 
} 

如上的問題。我只想做兩次退出,因爲在主程序中,當我退出時,餘額將變成20美元,並且變爲不活動。但不知何故,我的代碼確實撤回了5次。那麼有人可以給我一個想法如何解決這個問題。謝謝!

+2

1)未使用'double' S或'浮動'用於貨幣計算!它們遭受嚴重的舍入誤差,因爲大多數小數不具有有限的二進制表示 - use ['java.math.BigDecimal'](http://docs.oracle.com/javase/8/docs/api/java/數學/ BigDecimal.html)。 2)並非所有問題中的代碼都與您的問題相關。你能創建一個[mcve],以便問題變得更容易閱讀和回答嗎? – dorukayhan

+0

嗨! 在主要課程中,我有5個提款: //提取一些提款。 savings.withdraw(100.00); savings.withdraw(50.00); savings.withdraw(10.00); savings.withdraw(1.00); savings.withdraw(1.00); 我希望程序在第二次撤銷被調用時停止。 –

+0

換句話說, 我得到了我出去放: 「餘額:$ 100.00 的存款數:0 餘額::$ 170.00 的存款數:3 數提款:0 餘額0 提款的次數: $ 8.00 的存款數量:5 餘額::$ 4.51 的存款數:0 提款的次數:3 提款的次數0 ,我希望它喜歡: 餘額:$ 100.00 的存款數:0 號提款:0 餘額:$ 170.00 的存款數:0 餘額::$ 20.00 的存款數目:2 餘額::$ 17.54 的存款數3 提款的次數3 提款的次數: 0 取款次數:0 –

回答

0

變化

public SavingsAccount(double bal, double intRate, double mon) 
    { 
     super(bal, intRate, mon); 

     if (bal < 25) 
     { 
      status = false; 
     } 
     else 
     { 
      status = true; 
     } 
    } 

public void withdraw(double amount) 
    { 
     if (getBalance() > 25) 
     { 
      super.withdraw(amount); 
     } 
    } 

public void withdraw(double amount) 
    { 
     if (status = true) 
     { 
      super.withdraw(amount); 
     } 
    } 

public void withdraw(double amount) 
    { 
     if (getBalance() > 25) 
     { 
      super.withdraw(amount); 
     } 
    } 
相關問題