Account class not working properly

2019-09-20 06:45发布

问题:

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

I am getting error

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

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)

回答1:

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.

should be

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

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

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


标签: java class