gpt4 book ai didi

java - addInterest() 无法识别

转载 作者:行者123 更新时间:2023-12-02 04:25:58 25 4
gpt4 key购买 nike

我正在尝试创建一个银行帐户,但我的 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);
}


}

最佳答案

问题出在这里:

    public void deposit(double amount) {
balance += amount;
addInterest(balance);
}

添加利息返回一个值,但该值从未被使用。你应该这样做

 balance = addInterest(balance);

相反(可能,我认为您已经在 addInterest 函数中添加了余额)。

关于java - addInterest() 无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32160642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com