储蓄账户类+如何上交?

时间:2014-10-26 02:44:15

标签: java class object methods

问题所在 设计一个SavingsAccount类,用于存储储蓄账户的年利率和余额。类构造函数应该接受储蓄账户的起始余额。该类还应具有减去提款金额,添加存款金额以及将余额的金额添加到余额的方法。每月利率是年利率除以12。要将每月利息添加到余额,请将月利率乘以余额,并将结果添加到余额中。

在一个程序中测试该类,该程序在一段时间结束时计算储蓄账户的余额。它应该询问用户年度利率,起始余额以及自帐户建立以来经过的月数。然后循环应该每月迭代一次,执行以下操作:

询问用户在月内存入帐户的金额。使用类方法将此金额添加到帐户余额中。 询问用户在月内从帐户中提取的金额。使用类方法从帐户余额中减去此金额。 使用类方法计算月利息。 在最后一次迭代之后,程序应显示期末余额,存款总额,提取总金额以及所获得的总利息。

我一直在第22行的主程序中遇到一个问题,如果我修复了,它会弄乱一堆代码。任何人都知道如何解决这个问题?

import java.text.DecimalFormat;
import java.util.Scanner;
import java.io.*;

public class SavingsAccountMainProgram {
	public static void main(String args[]) {

	   
	    Scanner keyboard = new Scanner(System.in);

	    
	    System.out.print("How much money is in the account?: ");
	    double startingBalance = keyboard.nextDouble();

	
	    System.out.print("Enter the annual interest rate:");
	    double annualInterestRate = keyboard.nextDouble();

	    
	    SavingsAccountClass savingAccountClass = new SavingsAccountClass();
	    SavingsAccount savingsAccountClass = savingAccountClass.new SavingsAccount(
	            startingBalance, annualInterestRate);

	    
	    System.out.print("How long has the account been opened? ");
	    double months = keyboard.nextInt();

	    double montlyDeposit;
	    double monthlyWithdrawl;
	    double interestEarned = 0.0;
	    double totalDeposits = 0;
	    double totalWithdrawn = 0;

	    
	    for (int i = 1; i <= months; i++) {

	        
	        System.out.print("Enter amount deposited for month: " + i + ": ");
	        montlyDeposit = keyboard.nextDouble();
	        totalDeposits += montlyDeposit;

	        
	        savingsAccountClass.deposit(montlyDeposit);

	       
	        System.out.print("Enter amount withdrawn for " + i + ": ");
	        monthlyWithdrawl = keyboard.nextDouble();
	        totalWithdrawn += monthlyWithdrawl;

	       
	        savingsAccountClass.withdraw(monthlyWithdrawl);

	       
	        savingsAccountClass.addInterest();

	       
	        interestEarned += savingsAccountClass.getLastAmountOfInterestEarned();
	    }

	  
	    keyboard.close();

	    
	    DecimalFormat dollar = new DecimalFormat("#,##0.00");

	    
	    System.out.println("Total deposited: $" + dollar.format(totalDeposits));
	    System.out.println("Total withdrawn: $" + dollar.format(totalWithdrawn));
	    System.out.println("Interest earned: $" + dollar.format(interestEarned));
	    System.out.println("Ending balance: $"
	            + dollar.format(savingsAccountClass.getAccountBalance()));
	
}}

这个

的另一个程序

import java.util.Scanner;
import java.io.*;
public class SavingsAccountClass {
	class SavingsAccount {

	    private double accountBalance;
	    private double annualInterestRate;
	    private double lastAmountOfInterestEarned;

	    public SavingsAccount(double balance, double interestRate) {

	        accountBalance = balance;
	        annualInterestRate = interestRate;
	        lastAmountOfInterestEarned = 0.0;
	    }

	    public void withdraw(double withdrawAmount) {
	        accountBalance -= withdrawAmount;
	    }

	    public void deposit(double depositAmount) {
	        accountBalance += depositAmount;
	    }

	    public void addInterest() {

	        // Get the monthly interest rate.
	        double monthlyInterestRate = annualInterestRate / 12;

	        // Calculate the last amount of interest earned.
	        lastAmountOfInterestEarned = monthlyInterestRate * accountBalance;

	        // Add the interest to the balance.
	        accountBalance += lastAmountOfInterestEarned;
	    }

	    public double getAccountBalance() {
	        return accountBalance;
	    }

	    public double getAnnualInterestRate() {
	        return annualInterestRate;
	    }

	    public double getLastAmountOfInterestEarned() {
	        return lastAmountOfInterestEarned;
	    }
	}
}

另外,有人会知道如何提交像老师要求的.class文件吗?

1 个答案:

答案 0 :(得分:0)

不确定为什么你有SavingsAccount作为内部类。出于什么目的?

在这里,我修改了你所分享的内容,我认为它很好地回答了问题陈述

main方法

public static void main (String arg[]) {
    Scanner keyboard = new Scanner(System.in);
    System.out.print("How much money is in the account?: ");
    double startingBalance = keyboard.nextDouble();

    System.out.print("Enter the annual interest rate:");
    double annualInterestRate = keyboard.nextDouble();

    SavingsAccount savingsAccount = new SavingsAccount(startingBalance, annualInterestRate);

    System.out.print("How long has the account been opened? ");
    double months = keyboard.nextInt();

    double montlyDeposit;
    double monthlyWithdrawl;
    double interestEarned = 0.0;
    double totalDeposits = 0;
    double totalWithdrawn = 0;

    for (int i = 1; i <= months; i++) {
        System.out.print("Enter amount deposited for month: " + i + ": ");
        montlyDeposit = keyboard.nextDouble();
        totalDeposits += montlyDeposit;

        savingsAccount.deposit(montlyDeposit);

        System.out.print("Enter amount withdrawn for " + i + ": ");
        monthlyWithdrawl = keyboard.nextDouble();
        totalWithdrawn += monthlyWithdrawl;

        savingsAccount.withdraw(monthlyWithdrawl);

        savingsAccount.addInterest();

        interestEarned += savingsAccount.getLastAmountOfInterestEarned();
    }
    keyboard.close();

    DecimalFormat dollar = new DecimalFormat("#,##0.00");

    System.out.println("Total deposited: $" + dollar.format(totalDeposits));
    System.out.println("Total withdrawn: $" + dollar.format(totalWithdrawn));
    System.out.println("Interest earned: $" + dollar.format(interestEarned));
    System.out.println("Ending balance: $" + dollar.format(savingsAccount.getAccountBalance()));
}

并删除了SavingsAccountClass并将SavingsAccount作为顶级课程

class SavingsAccount {
    .
    .
   //everything as it is
}