如果余额低于25美元,如何停止提款

时间:2017-01-24 00:14:21

标签: java

这是我的代码。 主要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());
   }
}

Superclass 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;
    }
}

Subclass 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;
        }
    }
}

如上所述。我想只做两次退出,因为在主程序中当我做2撤销时,余额将变为20 $并且它变为不活动状态。但不知何故,我的代码确实撤了5次。那么有人可以给我一个如何解决这个问题的想法。谢谢!

1 个答案:

答案 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);
        }
    }