帐户类别无法正常运作 [英] Account class not working properly

查看:68
本文介绍了帐户类别无法正常运作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class AccountDriver {
    public static void main(String[] args) {

        // ID, Balance, Annual Interest Rate
        Account number1 = new Account();
        Account number2 = new Account(1122, 20000.00, 0.045);

        // Default account
        System.out.println("The Account ID is:  " + number1.getId());
        System.out.println("The Account Balance is: " + number1.getBalance());
        // System.out.println("The Account Balance is: "+
        // number1.getMontlyInterest());
        System.out.println("");

        // Ask to withdraw 2500
        System.out.println("The Account ID is:  " + number2.getId());
        number2.withdraw(2500.00);
        number2.deposit(3000.00);
        System.out.println("Account Balance is " + number2.getBalance());
        // System.out.println("The montly interest is : "+
        // number2.getMontlyInterest());
        System.out.println("");

    }
}

public class Account {

    private int id = 0;
    private double balance = 0;
    private double annualInterestRate = 0;

    public Account(int id, double balance, double annualInterestRate) {
        this.setId(id);
        this.setBalance(this.balance);
        this.setBalance(annualInterestRate);
    }

    public Account() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public double getAnnualInterestRate() {
        return annualInterestRate;
    }

    public void setAnnualInterestRate(double annualInterestRate) {
        this.annualInterestRate = annualInterestRate;
    }

    public double getMontlyInterest(double montlyInterest) {
        // Given Formula
        // double MontlyInterest= this.balance * get.MontlyInterestRate();
        return montlyInterest;
    }

    public double getMontlyInterestRate(double montlyInterestRate) {
        // Given Formula
        montlyInterestRate = this.annualInterestRate / 12;
        return montlyInterestRate;

    }

    double withdraw(double amount) {
        return balance -= amount;
    }

    double deposit(double amount) {
        return balance += amount;
    }
}

我遇到错误

帐户ID为:0
帐户余额为:0.0

The Account ID is: 0 The Account Balance is: 0.0

帐户ID为:1122
帐户余额为20500.0
线程 main中的异常java.lang.Error:未解决的编译问题:
Account类型的getMontlyInterestRate(double)方法不适用于参数()

The Account ID is: 1122 Account Balance is 20500.0 Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method getMontlyInterestRate(double) in the type Account is not applicable for the arguments ()

at Account.getMontlyInterest(Account.java:41)
at AccountDriver.main(AccountDriver.java:21)


推荐答案

您在代码中犯了2个小错误。

You did 2 small mistakes in your code.

在您的构造函数中,这两行

In your constructor these 2 lines

this.setBalance(this.balance); // this.balance is the instance variable and not the parameter passed
                ^^^^ - this is not required, just use the balance parameter passed.
this.setBalance(annualInterestRate); // you are re-writing the balance with interest rate
     ^^^^^^^^^^ - You need to set annual interest rate and not the balance here.

应该是

this.setBalance(balance); // sets the balance passed to the instance variable balance
this.setAnnualInterestRate(annualInterestRate); // sets the annual interest rate






自设置 annualInterestRate ,您可以通过修改 getMontlyInterestRate 这样的方法来获得每月利率。


Now since the annualInterestRate is set, you can get the monthly interest rate by modifying getMontlyInterestRate method like this.

public double getMontlyInterestRate() {
    // Given Formula
return this.annualInterestRate / 12;
}

您可以通过取消注释 System.out.println 代码。

And you can print your monthly interest rate by uncommenting your System.out.println code.

System.out.println("The montly interest is : "+ number2.getMontlyInterestRate());






每月利息法如下所示:


And the monthly interest method would look like this:

public double getMontlyInterest() { // no parameter required
    // Given Formula
    double MontlyInterest = this.balance * getMontlyInterestRate(); // balance multiplied by monthly interest rate
    return MontlyInterest; // return the value
}

System.out.println("The montly interest is : "+ number2.getMontlyInterest());

这篇关于帐户类别无法正常运作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆