首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >程序中存款和取款方式的问题

程序中存款和取款方式的问题
EN

Stack Overflow用户
提问于 2021-05-26 04:53:32
回答 1查看 207关注 0票数 0

在我运行SpecialSavingsDriver时,我需要帮助找出为什么输出中没有我的利息和存款数字:

代码语言:javascript
复制
Saver 1
New Monthly Savings Balance After Interest: 27958.33
Saver 2
New Monthly Savings Balance After Interest: 4933.33
Saver 1
New Monthly Savings Balance After Interest: 49240.28
Saver 2
New Monthly Savings Balance After Interest: 6431.11

我确实让它在我的SavingsAccountClass中正确地添加了兴趣。我在SpecialSavings类中的退出方法似乎也不起作用。任何援助都将得到极大的支持。以下是我正在努力完成的任务的概要:

第1部分

创建一个类SavingsAccount。使用静态类变量来存储每个保护程序的annualInterestRate。该类的每个对象都包含一个私有实例变量savingsBalance,指示保存程序当前存款的数量。提供方法calculateMonthlyInterest,通过将余额乘以annualInterestRate除以12计算月利息;此利息应添加到savingsBalance中。提供一个静态方法modifyInterestRate,将annualInterestRate设置为新值。编写一个驱动程序来测试类SavingsAccount。实例化两个不同的savingsAccount对象,saver1和saver2,余额分别为2000.00美元和3000.00美元。将annualInterestRate设置为4%,然后计算每月利息,并为每个储户打印新的余额。然后将annualInterestRate设置为5%,计算下一个月的利息,并为每个储户打印新的余额。

第2部分

编写另一个类SpecialSavings,将SavingsAccount扩展到对余额超过10K的账户支付10%的利息。还提供了存取款的方法。编写一个驱动程序来测试类SpecialSavings。实例化两个不同的savingsAccount对象,saver1和saver2,余额分别为2000.00美元和3000.00美元。进行一些存款和取款,并显示每个帐户的余额和利息。

再次感谢你!

代码语言:javascript
复制
public class SavingsAccountDriver {

    public static void main(String[] args) {
        SavingsAccount saver1 = new SavingsAccount();
        SavingsAccount saver2 = new SavingsAccount();
        
        saver1.setsavingsBalance(2000);
        saver2.setsavingsBalance(3000);
        
        SavingsAccount.modifyInterestRate(0.04);
        
        saver1.calculateMonthlyInterest();
        saver2.calculateMonthlyInterest();
        
        System.out.printf("\nSaver 1\nNew Monthly Savings Balance After Interest: %.2f",saver1.getsavingsBalance());
        System.out.printf("\nSaver 2\nNew Monthly Savings Balance After Interest: %.2f",saver2.getsavingsBalance());
        
        SavingsAccount.modifyInterestRate(0.05);
        
        saver1.calculateMonthlyInterest();
        saver2.calculateMonthlyInterest();

        System.out.printf("\nSaver 1\nNew Monthly Savings Balance After Interest: %.2f",saver1.getsavingsBalance());
        System.out.printf("\nSaver 2\nNew Monthly Savings Balance After Interest: %.2f",saver2.getsavingsBalance());
    }
代码语言:javascript
复制
public class SavingsAccount {
    
    public static double annualInterestRate;
    public double savingsBalance;
    
    /*
     * Constructor
     * */
    public SavingsAccount() {
        annualInterestRate = 0;
        savingsBalance = 0;
    }
    /*
     * Overloaded
     * */
    public SavingsAccount(double savingsBalance) {
        this.savingsBalance = savingsBalance;
    }
    /*
     * Calculates Monthly Interest from savings balance and interest rate
     * */
    public void calculateMonthlyInterest() {
        savingsBalance += savingsBalance*annualInterestRate/12;
    }
    /*
     * Interest Rate modifier
     * */
    public static void modifyInterestRate(double newInterestRate) {
        annualInterestRate = newInterestRate;
    }
    /*
     * Returns new Savings Balance
     * */
    public double getsavingsBalance() {
        return savingsBalance;
    }   
    /*
     * Sets new savings balance after interest
     * */
    public void setsavingsBalance(double savingsBalance) {
        this.savingsBalance = savingsBalance;
    }

    /*
     * Returns Interest Rate
     * */
    public double getannualInterestRate() {
        return annualInterestRate;
    }
}
代码语言:javascript
复制
public class SpecialSavings extends SavingsAccount {
    
    public SpecialSavings() {
        annualInterestRate = 0;
        savingsBalance = 0;
    }   
    
    public SpecialSavings(double savingsBalance) {
        super(savingsBalance);
    }
    
    public void calculateMonthlyInterest() {
        if(getsavingsBalance()>10000) {
            modifyInterestRate(10);
        }else {
            modifyInterestRate(4);
        }
        super.calculateMonthlyInterest();
    }
    
    public void deposit(double amount) {
        savingsBalance+=amount;
    }
    
    public void withdraw(double amount) {
        savingsBalance-=amount;
    }
    
}
代码语言:javascript
复制
public class SpecialSavingsDriver {

    public static void main(String[] args) {
        SpecialSavings saver1 = new SpecialSavings();
        SpecialSavings saver2 = new SpecialSavings();
        
        saver1.setsavingsBalance(2000);
        saver2.setsavingsBalance(3000);
        
        SavingsAccount.modifyInterestRate(0.04);
        
        saver1.deposit(13000);
        saver1.deposit(250);
        saver2.deposit(350);
        saver2.deposit(350);
        
        saver1.calculateMonthlyInterest();
        saver2.calculateMonthlyInterest();
        
        System.out.printf("\nSaver 1\nNew Monthly Savings Balance After Interest: %.2f",saver1.getsavingsBalance());
        System.out.printf("\nSaver 2\nNew Monthly Savings Balance After Interest: %.2f",saver2.getsavingsBalance());
        
        saver1.withdraw(1000);
        saver1.withdraw(100);
        saver2.withdraw(100);
        saver2.withdraw(10);
        
        saver1.calculateMonthlyInterest();
        saver2.calculateMonthlyInterest();
        
        System.out.printf("\nSaver 1\nNew Monthly Savings Balance After Interest: %.2f",saver1.getsavingsBalance());
        System.out.printf("\nSaver 2\nNew Monthly Savings Balance After Interest: %.2f",saver2.getsavingsBalance());

    }

}
EN

回答 1

Stack Overflow用户

发布于 2021-05-26 05:02:30

我觉得这句话有问题。

savingsBalance*annualInterestRate/12;

它会这样执行:-

代码语言:javascript
复制
(savingsBalance*annualInterestRate)/12;
= (multipliedValue)/12;
= result

我希望你不要这样,为了解决这个问题,你应该用括号

代码语言:javascript
复制
savingsBalance*(annualInterestRate/12);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67698718

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档