Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 银行帐户程序,更改余额_Java_Account_Bank - Fatal编程技术网

Java 银行帐户程序,更改余额

Java 银行帐户程序,更改余额,java,account,bank,Java,Account,Bank,我想在取款后改变账户余额,但它只是停留在10点。我不知道如何在SavingsAccount中正确地应用一种方法来改变它。我试过了,但没有成功 import java.util.Date; public class Account { private int id; private double balance; private double annualInterestRate; private Date dateCreated; private dou

我想在取款后改变账户余额,但它只是停留在10点。我不知道如何在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);

    }
}
尝试:

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

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

@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);
    }

}

您必须在
public void draw
方法中使用setBalance方法。像这样:

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);
         }
现在,它将为您的
余额
变量设置您的新值,因为如果您提取,您的帐户中将有
余额金额
资金


我希望它会对你有帮助

您不会更改
余额
(使用
setBalance
)的值。例如,在这种情况下,余额为10.00,但在我提取5.00并调用getBalance()后,余额应为5.00。但它没有,它仍然给出10.00。
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);
         }