我已经创建了一个帐户类,但是在进行第一次计算之后,它将继续并将第二行加倍。我是否错过了代码中的任何内容。
public class Account
{
private double balance; //STATE
private double interestRate; //STATE
private double rate;//STATE
public Account()
{
balance = 0;
interestRate = 0;
}
public Account(double amount, double interestRate)
{
balance = amount;
rate = interestRate;
}
public void deposit(double amount)
{
balance=balance+amount;
}
public void withdraw(double amount)
{
balance = balance - amount;
}
public void setInterest(double rate)
{
balance = balance + balance * rate;
//this.setInterst = setInterest;
//setInterest = InterestRate / 12;
}
public double computeInterest(int n)
{
balance=Math.pow(balance*(1+rate),n/12);
return balance;
}
public double getsetInterest()
{
return rate;
}
public double getBalance()
{
return balance;
}
public void close()
{
balance =0;
}
}
public class TestAccountInterest
{
public static void main (String[] args)
{
Account acc1 = new Account(500, 0.1);//0.10);
Account acc2 = new Account(400, 0.2); //0.20);
/*************************************
ACC1 ACCOUNT BELOW
*************************************/
acc1.deposit(500);
acc1.withdraw(300);
acc1.computeInterest(12);
acc1.computeInterest(24);
System.out.println(acc1.computeInterest(12));
/**************************************
ACC2 ACCOUNT BELOW
**************************************/
acc2.withdraw(200);
acc2.deposit(800);
acc2.computeInterest(24);
System.out.println(acc2.computeInterest(24));
}
}
我不知道我错过了什么还是写错了代码。
最佳答案
acc1.computeInterest(12);
acc1.computeInterest(24);
在我看来,您想要的是调用这些函数仅返回计算出的利息,但不应更改余额变量。
只需返回计算值,而不将其保存在@balance变量中即可。
这是我对您问题的解释,您有点含糊。
关于java - 帐户类Java OOP,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42137561/