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
版权声明:本文标题:JAVA:(账户类:Account) 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1729495054a1202810.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论