设计Account1类,包含: ■ 一个名为id 的int 类型的私有数据域(默认值为0),长度为6位。 ■ 一个名为balance的double类型的私有数据域(默认值为0)。 ■ 一个名为annualInterestRate 的double类型的私有数据域存储当前利率(默认值为0)。假设所有的账户都有相同的利率。 ■ 一个名为dateCreated的Date类型的私有数据域存储账户的开户日期。 ■ 一个能创建默认账户的无参构造方法。 ■ 一个能创建带特定id和初始余额的构造方法,初始余额不能为负数。 ■ id、balance和annualInterestRate 的访问器和修改器。 ■ dateCreated的访问器。 ■ 一个名为getMonthlyInterestRate的方法返回月利率。 ■ 一个名为withDraw的方法从账户提取特定金额。 ■ 一个名为deposit的方法向账户存人特定金额。 ■ double类型的数据域保留2位小数。 ■ 成员方法和数据域应进行基本的合理性检查。 设计测试类ATMMachine1: ■ 创建一个有100个账户的数组,其id 为0,1,2,...99, 并初始化收支为1000美元。 ■ 主菜单如下: Main menu 1: check balance 2: withdraw 3: deposit 4: exit
时间: 2024-04-21 09:27:49 浏览: 26
下面是Account1类和ATMMachine1类的代码实现:
```
import java.util.Date;
public class Account1 {
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;
public Account1() {
id = 0;
balance = 0;
annualInterestRate = 0;
dateCreated = new Date();
}
public Account1(int id, double balance) {
this.id = id;
if (balance >= 0) {
this.balance = balance;
} else {
System.out.println("Initial balance cannot be negative.");
this.balance = 0;
}
annualInterestRate = 0;
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() {
return annualInterestRate / 12;
}
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
} else {
System.out.println("Insufficient balance.");
}
}
public void deposit(double amount) {
if (amount >= 0) {
balance += amount;
} else {
System.out.println("Deposit amount cannot be negative.");
}
}
public String toString() {
return "Account ID: " + id + "\nBalance: $" + String.format("%.2f", balance) + "\nAnnual Interest Rate: " + annualInterestRate + "%\nDate Created: " + dateCreated;
}
}
import java.util.Scanner;
public class ATMMachine1 {
public static void main(String[] args) {
Account1[] accounts = new Account1[100];
for (int i = 0; i < accounts.length; i++) {
accounts[i] = new Account1(i, 1000);
}
Scanner input = new Scanner(System.in);
int choice = 0;
while (choice != 4) {
System.out.println("Main menu");
System.out.println("1: check balance");
System.out.println("2: withdraw");
System.out.println("3: deposit");
System.out.println("4: exit");
System.out.print("Enter a choice: ");
choice = input.nextInt();
switch (choice) {
case 1:
System.out.print("Enter an account ID (0-99): ");
int id = input.nextInt();
if (id >= 0 && id <= 99) {
System.out.println(accounts[id]);
} else {
System.out.println("Invalid account ID.");
}
break;
case 2:
System.out.print("Enter an account ID (0-99): ");
id = input.nextInt();
if (id >= 0 && id <= 99) {
System.out.print("Enter an amount to withdraw: ");
double amount = input.nextDouble();
accounts[id].withdraw(amount);
} else {
System.out.println("Invalid account ID.");
}
break;
case 3:
System.out.print("Enter an account ID (0-99): ");
id = input.nextInt();
if (id >= 0 && id <= 99) {
System.out.print("Enter an amount to deposit: ");
double amount = input.nextDouble();
accounts[id].deposit(amount);
} else {
System.out.println("Invalid account ID.");
}
break;
case 4:
System.out.println("Goodbye!");
break;
default:
System.out.println("Invalid choice.");
}
System.out.println();
}
}
}
```
这个程序创建了一个有100个账户的数组,初始收支为1000美元。主菜单提供了4个选项:检查余额、提取金额、存款和退出。用户可以根据需要选择相应的选项,对账户进行操作。每个账户都有一个6位的ID、余额和利率,以及一个开户日期。可以使用访问器和修改器来获取或设置这些值。程序还提供了getMonthlyInterestRate、withdraw和deposit等方法来计算月利率、提取特定金额和存入特定金额。这个程序还对输入进行了错误检查,确保提取金额不超过余额,存款金额不为负数,账户ID在0到99之间等。