admin管理员组文章数量:1647971
设计一个名为 Account 的类,该类包含:
- 为账号定义一个名为 id 的int类型私有数据域(默认值为0)标识账号。
- 为账号定义一个名为 balance 的double类型私有数据域(默认值为0)表示余额。
- 一个名为 annualInterestRate 的double类型私有数据域存储当前利率(默认值为0)。假设所有的账户都有相同的利率。
- 一个名为 dateCreated 的Date类型的私有数据域,存储账户的开户日期。
- 一个用于创建默认账户的无参构造方法。
- 一个用于创建具有指定 id 和初始余额的账户的构造方法。
- id、balance 和 annualInterest 的访问器方法和修改器方法。
- dateCrated 的访问器方法。
- 一个名为 getMonthlyInterestRate() 的方法、返回月利率。
- 一个名为 getMonthlyInterest() 的方法、返回月利息。
- 一个名为 withDraw 的方法,从账户提取指定额度。
- 一个名为 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());
}
}
注:测试程序中的输出语句可根据个人爱好自己编写
版权声明:本文标题:9.7 Account 类 《Java语言程序设计与数据结构(基础篇)》梁勇 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1729495444a1202853.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论