admin管理员组

文章数量:1647971

设计一个名为 Account 的类,该类包含:

  1. 为账号定义一个名为 id 的int类型私有数据域(默认值为0)标识账号。
  2. 为账号定义一个名为 balance 的double类型私有数据域(默认值为0)表示余额。
  3. 一个名为 annualInterestRate 的double类型私有数据域存储当前利率(默认值为0)。假设所有的账户都有相同的利率。
  4. 一个名为 dateCreated 的Date类型的私有数据域,存储账户的开户日期。
  5. 一个用于创建默认账户的无参构造方法。
  6. 一个用于创建具有指定 id 和初始余额的账户的构造方法。
  7. id、balance 和 annualInterest 的访问器方法和修改器方法。
  8. dateCrated 的访问器方法。
  9. 一个名为 getMonthlyInterestRate() 的方法、返回月利率。
  10. 一个名为 getMonthlyInterest() 的方法、返回月利息。
  11. 一个名为 withDraw 的方法,从账户提取指定额度。
  12. 一个名为 deposit 的方法向账户存储指定额度

实现这个类。

提示:方法 getMonthlyInterest() 用于返回月利息,而不是利率。月利息是 balance*monthlyInterestRate。monthlyInterestRate 是 annualInterestRate/12。注意,annualInterestRate 是一个百分数,比如4.5%。你需要将其除以100。

        编写一个测试程序,创建一个账户ID为1122、余额为20 000美元、年利率为4.5%的Account对象。使用 withDraw 方法取款2500美元,使用 deposit 方法存款3000美元,然后打印余额、月利息以及这个账户的开户日期。

     首先是 Account 类:

public class Account {
	//数据域:
	private int id;           //标识账号
	private double balance;   //余额
	private double annualInterestRate; //当前利率
	private Date dateCreated  ;   //账户开户日期
	
	//用于创建账户的构造方法:
	public Account() {  
		//无参构造方法
		 dateCreated = new Date();
	}
	public  Account(int id,double balance) {
		//有参构造方法
		dateCreated = new Date();
		this.id = id;
		this.balance = balance;
	}
	
	
	//访问器方法和修改器方法:getter、setter
	    //id
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	    //balance
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	    //annualInterestRate
	public double getAnnualInterestRate() {
		return annualInterestRate;
	}
	public void setAnnualInterestRate(double annualInterestRate) {
		this.annualInterestRate = annualInterestRate;
	}
	
	//dateCreated的访问器方法:
	public Date getDateCreated() {
		return dateCreated;
	}
	
	//方法:
	    //返回月利率
	public double getMonthlyInterestRate(double annualInterestRate) {
		double monthlyInterestRate = annualInterestRate/12;
		return monthlyInterestRate;
	}
	    //返回月利息
    public double getMonthlyInterest(double balance,double monthlyInterestRate) {
    	double monthlyInterest = this.balance * monthlyInterestRate;
    	return monthlyInterest;
		
	}
        //从账户提取指定额度
   public void withdraw(double money) {
    	if (money<=this.balance) {
    		this.balance =this.balance -money;
    		System.out.println("您已成功提取金额:"+ money);	
		}
    	else 
		System.out.println("您当前余额不足!");
	}
        //向账户存储指定额度
    public void deposit(double money) {
    	this.balance = this.balance + money;
    	System.out.println("您已成功存储金额: "+ money);
    	
	}
	
}

       然后是测试程序: 

public class AccountTest {

	public static void main(String[] args) {
		Account user = new Account (1122,20000);
		user.setAnnualInterestRate( 0.045);
		
		user.withdraw (2500);
		user.deposit(3000);
		System.out.println("您当前余额为: "+ user.getBalance());
		double monthlyInterestRate = user.getMonthlyInterestRate(0.045);
		System.out.println("月利息为: "+user.getMonthlyInterest(user.getBalance(), monthlyInterestRate));
		System.out.println(user.getDateCreated());
	}

}

注:测试程序中的输出语句可根据个人爱好自己编写

本文标签: 数据结构语言程序设计基础Account梁勇