Java简单的会计程序

时间:2014-02-10 01:48:46

标签: java account

import java.util.Date;
public class Exercise08_07 {
public static void main (String[] args) {
Account account = new Account(1122, 20000);
account.setAnnualInterestRate(4.5);

account.withdraw(2500);
account.deposit(3000);
System.out.println("Balance is " + account.getBalance());
System.out.println("Monthly interest is " +
  account.getMonthlyInterest());
  System.out.println("This account was created at " +
  account.getDateCreated());
  }
}

class Account 
{
//define variables 
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;

//a no-arg constructor that creates a default account.

public Account()

{
id = 0;
balance = 0.0;
annualInterestRate = 0.0;
}
 //constructor creates an account with the specified id and initial balance
  public Account(int id, double balance){
  this.id = id;
  this.balance = balance;

}
  public Account (int newId, double newBalance, double newAnnualInterestRate)
  { 
      id = newId;
      balance = newBalance;
      annualInterestRate = newAnnualInterestRate;

  }
  //accessor and mutator methods for id, balance, and annualInterestRate
  public int getId(){
       return id;
  }
  public double getBalance(){
  return balance;
  }
  public double getAnnualInterestRate(){
      return annualInterestRate;

  }
  public void setId(int id){
      this.id = id;
  }
  public void setBalance(double balance){
      this.balance=balance;
  }
  public void setAnnualInterestRate(double annualInteresteRate){
      this.annualInterestRate = annualInterestRate;
  }
      //accessor method for dateCreated
  public void setDateCreated(Date newDateCreated){
      dateCreated = newDateCreated;
  }
     //method named getMonthlyInterestRate()that returns the monthly interest rate.
  double getMonthlyInterest(){
      return annualInterestRate/12;      
  }
  Date getDateCreated(){
      return dateCreated;
  }
  //method named withdraw that withdraws a specified amount from the account.
  double withdraw (double amount){
      return balance -= amount;
  }
  //method named deposit that deposits a specified amount to the account
  double deposit (double amount){
      return balance += amount;
  }
  }

这是我第一次在这里发帖提问,不知道如何在这里发布代码,netbean没有显示任何错误,但输出不正确,我不确定代码出错的地方,我是Java的新手请帮我解决这个问题,谢谢! 输出:

balance is 20500.0

monthly interest is 0.0

this account was created at null

I am not sure why the output is incorrect, can anyone help please?

4 个答案:

答案 0 :(得分:2)

有拼写错误(输入参数annualInterest * e * Rate的额外'e'):

public void setAnnualInterestRate(double annualInteresteRate){
      this.annualInterestRate = annualInterestRate;
}

因此,您要将this.annualInterestRate设置为自身,在构造函数中将其设置为0.0。应该是:

public void setAnnualInterestRate(double annualInterestRate){
          this.annualInterestRate = annualInterestRate;
}

您也永远不会将创建日期分配给dateCreated,因此为null。我建议在构造函数中设置它(因为这将在您创建新的Account对象时调用。)

答案 1 :(得分:0)

annualInterestRate = 0.0;

然后你按月计算利息除以12。

0 /除0以外的任何东西都是0。

那是

的原因

答案 2 :(得分:0)

您的构造函数设置不正确。

public Account()
public Account(int id, double balance)
public Account (int newId, double newBalance, double newAnnualInterestRate)

所有这些都缺少日期初始化。

this.dateCreated = new Date(); // Add this to each constructor

你也缺乏计算每月产生的利息的方法,也需要写出来。 getMonthlyInterest()与getMonthlyInterestRate()不同,这是你目前正在做的事情。

在您的教科书中应该有一个较早的作业,告诉您如何计算每年利率的月利息(再次不是利率,金额)。

答案 3 :(得分:0)

给定程序的输出是正确的。请参阅下面的每个输出的说明。

每月利息为0.0

下面的setter方法是在不知不觉中编写的,它将类实例变量赋值给它自己,它应该将setter中传递的值作为参数赋值。因此需要更改该值赋值。

setter参数中的变量名是= annualInteresteRate,而=之后的变量名是 年利率。

public void setAnnualInterestRate(double annualInteresteRate){
      this.annualInterestRate = annualInterestRate;
  }

此帐户是在null

创建的

这里它试图从Account类访问数据值,因为日期从未设置为Account类,它返回为null。您需要从

分配创建日期或更改声明
      private Date dateCreated;

private Date dateCreated = new Date();

它将在对象创建期间初始化日期。