(賬戶類 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
新聞熱點
疑難解答