我正在尝试创建一个银行帐户,而我的addInterest()函数无效。好像它不能识别我的功能。它没有像它应该的那样在平衡上增加利益。我认为计算根本不会发生。我试图修改addInterest()但无济于事。我无法理解什么是错的。任何帮助将不胜感激。
package lab2;
import java.util.*;
import java.io.*;
public class Account {
protected String accName;
protected Double balance;
public Account(String name, double initDeposit) {
accName = name;
balance = initDeposit;
}
public String getAccountName() {
return accName;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
int fee = 6;
if (balance < 100) {
amount += fee;
balance -= amount;
}
else if (amount > balance) {
double limit = -50;
if (balance > limit)
balance = limit;
}
else
balance -= amount;
}
public String toString() {
return accName + ' ' + balance;
}
public static class CurrentAccount extends Account {
final static double DEFAULT_LIMIT = -50;
double limit = DEFAULT_LIMIT;
public CurrentAccount(String name, double i) {
super(name, i);
}
public void withdraw(double amount) {
double fee = 6;
if (balance < 100) {
amount += fee;
balance -= amount;
}
if (amount > balance) {
double res;
res = balance - amount;
if (res > -50)
res = limit;
}
balance -= amount;
}
public void deposit(double amount) {
balance += amount;
}
public String toString() {
return accName + ' ' + balance;
}
}
public static class DepositAccount extends Account {
final static double DEFAULT_INTEREST = 0.04;
double interestRate = DEFAULT_INTEREST;
public DepositAccount(String name, double i) {
super(name, i);
}
public void withdraw(double amount) {
balance -= amount;
}
public void deposit(double amount) {
balance += amount;
addInterest(balance);
}
public double addInterest(double bal) {
return (bal*interestRate)+bal;
}
public String toString() {
return accName + ' ' + balance;
}
}
public static class HigherRateAccount extends Account {
final static double DEFAULT_INTEREST = 0.07;
double interestRate = DEFAULT_INTEREST;
public HigherRateAccount(String name, double i) {
super(name, i);
}
public void withdraw(double amount) {
double fee;
fee = 10;
double feeFixed = amount + fee;
balance += feeFixed;
}
public double addInterest(double bal) {
return (bal*interestRate)+bal;
}
public void deposit(double amount) {
balance += amount;
addInterest(balance);
}
public String toString() {
return accName + ' ' + balance;
}
}
public static void main(String[] args) {
Account arthur = new CurrentAccount("Arthur", 200);
Account brenda = new DepositAccount("Brenda", 70);
Account charlie = new HigherRateAccount("Charlie", 1000);
System.out.println(arthur);
System.out.println(brenda);
System.out.println(charlie);
arthur.withdraw(50);
brenda.withdraw(50);
charlie.deposit(1000);
System.out.println(arthur);
System.out.println(brenda);
System.out.println(charlie);
arthur.withdraw(175);
brenda.deposit(90);
charlie.withdraw(3000);
System.out.println(arthur);
System.out.println(brenda);
System.out.println(charlie);
}
}
答案 0 :(得分:1)
问题在于:
public void deposit(double amount) {
balance += amount;
addInterest(balance);
}
添加兴趣返回一个值,但从不使用此值。你应该做
balance = addInterest(balance);
相反(可能,我认为你已经在addInterest函数中添加了余额)。
答案 1 :(得分:1)
您正在从addInterest
返回一个值,但在调用它的方法中,您没有对返回的数字执行任何操作。你打电话。
因此addInterest
会返回新余额,但该值不会在任何地方分配给余额。