(账户类 Account)设计一个名为 Account 的类,它包括:
一个名为 id 的 int 类型私有数据域(默认值为 0)。一个名为 balance 的 double 类型私有数据域(默认值为 0)。一个名为 annualInterestRate 的 double 类型私有数据域存储当前利率(默认值为 0)。假设所有的账户都有相同的利率。一个名为 dateCreated 的 Date 类型的私有数据域,存储账户的开户日期。一个用于创建默认账户的无参构造方法。一个用于创建带特定 id 和初始余额的账户的构造方法。id、balance 和 annualIntersRate 的访问器和修改器。dateCreated 的访问器。一个名为 getMonthlyInterestRate() 的方法,返回月利率。一个名为 withDraw 的方法,从账户提取特定数额。一个名为 deposit 的方法向账户存储特定数额。 画出该类的 UML 图并实现这个类。提示:方法 getMonthlyInterest() 用于返回月利息,而不是利息。月利息是 balance * monthlyInterestRate。monthlyInterestRate 是 annualInterestRate / 12。注意,annualInterestRate 是一个百分数,比如 4.5%。你需要将其除以 100。
编写一个测试程序,创建一个账户 ID 为 1122、余额为 20 000 美元、年利率为 4.5% 的 Account 对象。使用 withDraw 方法取款 2500 美元,使用 deposit 方法存款 3000 美元,然后打印余额、月利息以及这个账户的开户日期。
import java.util.Date;public class PRactice_9_7 { 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() + "/n" + "Monthly Interest Rate: " + account.getMonthlyInterestRate() + "/n" + "Date Created: " + account.getDateCreated()); }}class Account { private int id = 0; private double balance = 0; private static double annualInterestRate = 0; private Date dateCreated; public Account() { dateCreated = new Date(); } public Account(int id, double balance) { this.id = id; this.balance = balance; dateCreated = new Date(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public double getAnnualInterestRate() { return annualInterestRate; } public void setAnnualInterestRate(double annualInterestRate) { this.annualInterestRate = annualInterestRate; } public Date getDateCreated() { return dateCreated; } public double getMonthlyInterestRate() { double monthlyInterestRate = annualInterestRate / 12; return balance * monthlyInterestRate / 100; } public void withDraw(double money) { balance -= money; } public void deposit(double money) { balance += money; }}输出结果为:
Balance: 20500.0 Monthly Interest Rate: 76.875 Date Created: Sat Mar 04 12:37:56 CST 2017
新闻热点
疑难解答