0

我有一个任务,我需要创建一个调用静态方法和静态变量的程序。因此,当我输入利率时,它不会计算利率。

package runsavingsaccount;
import java.util.Scanner;
class SavingsAccount{

    private double balance;
    public static double interestRate = 0;

    SavingsAccount() {
        this.balance = 0.0f;
    }
    
    public static void setInterestRate(double newRate){
        newRate = interestRate;
    }
    
    public static double getInterestRate(){
        return interestRate;
    }
    
    public double getBalance(){
        return balance;
    }
    
    public void deposit(double amount){
        this.balance = balance += amount;
        
    }
    public double withdraw(double amount){
        if(balance >= amount){
            balance -= amount;
        }
        else{
            amount = 0;
        }
        return amount;
    }
    
    public void addInterest(){
        double Interest;
        Interest = balance * interestRate;
        balance = balance + Interest;
        
    }
    
    public static void showBalance(SavingsAccount account){
        account.getBalance();   
    }
}
public class RunSavingsAccount {

    public static void main(String[] args) {
       
        Scanner input = new Scanner(System.in);
        SavingsAccount savings = new SavingsAccount();
        
        double interestRateInput;
        double depositInput;
        
         
        System.out.print("Enter the Interest Rate: ");
        interestRateInput = input.nextDouble(); 
        SavingsAccount.setInterestRate(interestRateInput);
        
        System.out.print("Enter the Amount of Deposit: ");
        depositInput = input.nextDouble();
        savings.deposit(depositInput);
        
        System.out.println("Total Balance: "+ savings.getBalance());
        input.nextLine();
        System.out.print("Press 'D' for another Deposit / Press 'W' for Withdraw ");
        String D = input.nextLine();
        
       
        
        if(D.equals("D")){
            System.out.print("Enter the Amount of Deposit: ");
            depositInput = input.nextDouble();
            savings.deposit(depositInput);
           
            System.out.print("Total Balance: " + savings.getBalance());
        }
        else if(D.equals("W")){
            System.out.print("Enter the Amount of Deposit: ");
            double withdrawInput = input.nextDouble();
            savings.withdraw(withdrawInput);
            
            System.out.print("Remaining Balance: "+ savings.getBalance());
        }
        

    }
    
}

预期输出示例:

Enter interest rate: .10 
Enter the amount of deposit: 500 
Total Balance: 500.0

Press D for another deposit / Press W to withdraw : D
Total Balance: 1650.0

我的输出:

Enter interest rate: .10
Enter the amount of deposit: 500
Total Balance: 500.0

Press D for another deposit / Press W to withdraw : D
Total Balance: 1500.0
4

0 回答 0