gpt4 book ai didi

java - 具有存款和取款功能的银行账户类别

转载 作者:行者123 更新时间:2023-12-02 01:12:31 25 4
gpt4 key购买 nike

简述是创建一个账户对象,ID为1122,余额为20000英镑,年利率为4.5%,提款方式为2500英镑,存款方式为3000英镑,并打印余额、月息和日期。该帐户已创建。

我已经编写了以下代码,但在主要方法中,初始余额是错误的,应该是 20000 英镑,实际上是 20500 英镑,提款和存款也是错误的。金额应为取款 = 17,500 英镑,存款 = 20,500 英镑。关于如何解决这个问题有什么建议吗?

package Account;
import java.util.Date;

class Account {
private int id;
private double balance; //balance for account
private double annualInterestRate; //store interest rate
private Date dateCreated; //store date account created

// no arg constructor for default account
Account() {
id = 0;
balance = 0.0;
annualInterestRate = 0.0;
}
//constructor with specfic id and initial balance
Account (int newId, double newBalance) {
id = newId;
balance = newBalance;
}
//
Account (int newId, double newBalance, double newAnnualInterestRate) {
id = newId;
balance = newBalance;
annualInterestRate = newAnnualInterestRate;
}
//accessor and mutator methods
public int getId() {
return id;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setId (int newId) {
id = newId;
}
public void setBalance (double newBalance) {
balance = newBalance;
}
public void setAnnualInterestRate (double newAnnualInterestRate) {
annualInterestRate = newAnnualInterestRate;
}
public void seDateCreated (Date newDateCreated) {
dateCreated = newDateCreated;
}
//Method for monthly interest
double getMonthlyInterestRate() {
return annualInterestRate/12;
}
//Define method withdraw
double withdraw (double amount) {
return balance -= amount;
}
//Define method deposit
double deposit(double amount) {
return balance += amount;
}

public static void main(String[] args) {
java.util.Date dateCreated = new java.util.Date();
Account account1 = new Account (1122, 20000, 0.045); //
//account1.withdraw(2500);
//account1.deposit(3000);
System.out.println("----------------------------------------");
System.out.println(" Welcome to your online account!");
System.out.println("Please find your account details below");
System.out.println("----------------------------------------");

System.out.println("Account ID:" + " " + account1.id);
System.out.println("Initial Balance:" + account1.getBalance());

System.out.println("Balance after Withdraw:" + " " + account1.withdraw(2500));
System.out.println("Balance after deposit" + " " + account1.deposit(3000));


System.out.println("Interest Rate:" + " " + account1.getAnnualInterestRate());

System.out.println("Monthly Interest:" + " " + "£" + account1.getMonthlyInterestRate());


System.out.println("Date Account was Created:" + " " + dateCreated);



System.out.println("------------------------");
System.out.println("The Process is complete");
System.out.println("------------------------");
}

}

最佳答案

您需要注释掉/删除以下行:

  public static void main(String[] args) {
java.util.Date dateCreated = new java.util.Date();
Account account1 = new Account (1122, 20000, 0.045); //
//account1.withdraw(2500);
//account1.deposit(3000);

您调用提款/存款两次(稍后在打印报表中再次调用)。

关于java - 具有存款和取款功能的银行账户类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59159893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com