admin管理员组

文章数量:1648711

题目描述:

代码:

import java.util.Date;

public class Test07 {

	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:"+account.getBalance()+" "+"monthlyInterest:"
		+account.getMonthlyInterest()+" "+"The date of created account:"+account.getDateCreatd());
	}

}

class Account {
	private int id;
	private double balance;
	private static double annualInterestRate;
	private Date dateCreatd;
	
	//无参构造方法
	Account()
	{
		this.id = 0;
		this.balance = 0;
		this.dateCreatd = new Date();
	}
	
	//有两个参数的构造方法
	Account(int id,double balance)
	{
		this.id = id;
		this.balance = balance;
		this.dateCreatd = new Date();
	}
	
	//id修改器
	public void setId(int id)
	{
		this.id = id;
	}
	
	//balance修改器
	public void setBalance(double balance)
	{
		this.balance = balance;
	}
	
	//annualInterestRate修改器
	public void setAnnualInterestRate(double annualInterestRate)
	{
		Account.annualInterestRate = annualInterestRate / 100;
	}
	
	//id访问器
	public int getId()
	{
		return id;
	}
	
	//balance访问器
	public double getBalance()
	{
		return balance;
	}
	
	//annualInterestRate访问器
	public double getAnnualInterestRate()
	{
		return Account.annualInterestRate;
	}
	
	//dateCreatd访问器
	public Date getDateCreatd()
	{
		return this.dateCreatd;
	}
	
	//返回月利率
	public double getMonthlyInterestRate()
	{
		return Account.annualInterestRate / 12;
	}
	
	//提取money
	public void withDraw(double money)
	{
		this.balance -= money;
		if(this.balance < 0)
			System.out.println("The banlance is insufficient,you already remove"+" "+this.balance);
	}
	
	//存入money
	public void deposit(double money)
	{
		if(money < 0)
			System.out.println("The money of deposit must larger than 0");
		else
			this.balance += money;
	}
	
	//返回月利息
	public double getMonthlyInterest()
	{
		return this.balance * getMonthlyInterestRate();
	}
}

运行结果:

balance:20500.0 monthlyInterest:76.875 The date of created account:Mon Apr 11 23:24:57 CST 2022

本文标签: 账户JavaAccount