2

我试图在提款后更改帐户中的余额,但它只停留在 10.00。我不知道如何在 SavingsAccount 中正确应用会改变它的方法。我试过但没有成功。

import java.util.Date;

public class Account {
    private int id;
    private double balance;
    private double annualInterestRate;
    private Date dateCreated;
    private double monthlyInterestRate;

    public Account() {
        id = 0;
        balance = 0;
        annualInterestRate = 0;
    }

    public Account(int iD, double balancE) {
        id = iD;
        balance = balancE;
    }

    public void setID(int iD) {
        id = iD;
    }

    public int getID() {
        return (id);
    }

    public void setBalance(double balancE) {
        balance = balancE;
    }

    public double getBalance() {
        return (balance);
    }

    public void setAnnualInterestRate(double AIR) {
        annualInterestRate = AIR;
    }

    public double getAnnualInterestRate() {
        return (annualInterestRate);
    }

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }

    public double getMonthlyInterestRate() {
        return ((annualInterestRate / 100) / 12);
    }

    public double getMonthlyInterest() {
        return (balance * monthlyInterestRate);
    }

    public void withdraw(double ammount) {
        balance = balance - ammount;
    }

    public void deposit(double ammount) {
        balance = balance + ammount;
    }
}

public class SavingsAccount extends Account {
    public SavingsAccount(int iD, double balancE) {
        super(iD, balancE);
    }

    @Override
    public void withdraw(double amount) {
        if (amount > getBalance()) {
            System.out.println("Current Balance: " + getBalance() 
                    + "\nThe             withdrawal  cannot be made due to insufficient  funds.");
        }

    }
}



public class Test extends Account {

    public static void main(String[] args) {
        SavingsAccount a1 = new SavingsAccount(1122, 10.00);
        a1.withdraw(5.00);
        a1.deposit(00.00);
        a1.setAnnualInterestRate(4.5);
        Date dat = new Date();

        System.out.println("Balance: " + a1.getBalance() + 
                "\nMonthly Interest: " + a1.getMonthlyInterest() + 
                "\nDate Created: " + dat);

    }
}
4

3 回答 3

2

尝试:

public void deposit(double ammount) {
        balance = balance + ammount;
        setBalance(balance);
    }

您应该在提款和余额结束时调用 setBalance,传入新的余额金额。

于 2015-06-15T18:24:36.720 回答
1

withdraw()中,您只是检查提款金额大于余额的情况,然后打印一条消息。您还必须处理合法提款金额的情况。

@Override
public void withdraw(double amount) {
    if (amount > getBalance()) {
        System.out.println("Current Balance: " + getBalance() 
                + "\nThe             withdrawal  cannot be made due to insufficient  funds.");
    } else {
        super.withdraw(amount);
    }

}
于 2015-06-15T18:26:26.180 回答
0

你必须在你的方法中使用你的 setBalancepublic void withdraw方法。像这样:

public void withdraw(double amount) {
        if (amount > getBalance()) {
            System.out.println("Current Balance: " + getBalance() + "\nThewithdrawal  cannot be made due to insufficient  funds.");
        }
         else
         {
            setBalance(getBalance() - amount);
         }

现在它将为您的balance变量设置您的新值,因为如果您提款,您balance-amount的账户中就会有钱。

我希望它会对你有所帮助!

于 2015-06-15T18:27:19.813 回答