【问题标题】:Bank Account class with deposit and withdraw带存款和取款的银行账户类
【发布时间】:2019-12-03 14:59:19
【问题描述】:

简介是创建一个 ID 为 1122 的 Account 对象,余额为 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("------------------------");
    }

}

【问题讨论】:

  • 这真的可以构建和工作吗?例如"Initial Balance:" + account1.getBalance 不会调用getBalance 函数。
  • 我是编程新手,对我做错了什么有点困惑。它返回 20500 英镑,而不是 20000 英镑的实际初始余额。
  • 你调用 account1.withdraw(2500);和 account1.deposit(3000);两次
  • @Someprogrammerdude 表示您的程序将无法使用 .getBalance 编译 - 可能是错字,我认为您的意思是 .getBalance()

标签: java class


【解决方案1】:

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

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

您调用了两次取款/存款(稍后在您的打印语句中再次调用)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 2012-10-03
    • 1970-01-01
    相关资源
    最近更新 更多