题目:
使用编程练习题9.7中创建Account类来模拟一台ATM机。创建一个有10个账户的数组,起id为0,1,...,9,并初始化余额为100美元。系统提示用户输入一个id。如果输入的id不正确,就要求用户输入正确的id。一旦接受一个id,就显示如以下运行示例所示的主菜单。可以选择1来查看当前余额,选择2取钱,选择3存钱,选择4推出主菜单。一旦退出,系统就会提示再次输入id。所以系统一旦启动就不会停止。
例样输出:
Enter an id: 4
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 1
The balance is 100.0Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 2
Enter an amount to withdraw: 3Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 1
The balance is 97.0Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 3
Enter an amount to deposit: 10Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 1
The balance is 107.0Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 4Enter an id:
Account类:
package problem;
import java.util.Date;
public class Account {
//定义四个私有数据域
private int id; //标识账号
private double balance; //余额
private double annualInterestRate; //年利率
private Date dateCreated; //存储账户的开户日期
//创建默认账户的无参构造方法
public Account() {
this(0, 0.0);
this.annualInterestRate = 0.0;
this.dateCreated = new Date();
}
//创建一个具有指定id的初始余额账户balance的构造方法
public Account(int id, double balance) {
this.id = id;
this.balance = balance;
this.dateCreated = new Date();
}
//id balance annualInterestRate的访问器方法get和修改器方法set
public int getId() { //访问器方法get
return id;
}
public void setId(int id) { //修改器方法set
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;
}
// dateCreated的访问器方法
public Date getDateCreated() {
return dateCreated;
}
// getMonthlyInterestRate方法 返回月利率
public double getMonthlyInterestRate() {
return annualInterestRate / 1200;
}
// getMonthlyInterest方法 返回月利息
public double getMonthlyInterest() {
return balance*annualInterestRate / 1200;
}
// withDraw方法 从账户提取指定额度
public void withDraw(double num) {
if (num <= balance) {
balance = balance - num;
}
}
// deposit方法 向账户储存指定额度
public void deposit(double num) {
balance = balance + num;
}
}
AccountTest类:
package problem;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Scanner;
import org.junit.jupiter.api.Test;
class AccountTest {
@Test
void test() {
Scanner input = new Scanner(System.in);
Account[] arr = new Account[10];
for (int i = 1; i <= 9; i++) {
arr[i] = new Account(i, 100);
}
int id;
while (true) {
System.out.print("\nEnter an id: ");
id = input.nextInt();
if(id<1||id>9) {
System.out.println("Your id is wrong, please enter the correct id:");
continue;
}
boolean bool = true;
while (bool) {
System.out.println("\nMain menu\n1: check balance\n2: withdraw\n3: deposit\n4: exit");
System.out.print("Enter a choice: ");
int choice = input.nextInt();
if (choice == 1) {
System.out.print("The balance is "+arr[id].getBalance()+"\n");
}
if (choice == 2){
System.out.print("Enter an amount to withdraw: ");
double withdraw = input.nextDouble();
arr[id].withDraw(withdraw);
}
if (choice == 3) {
System.out.print("Enter an amount to deposit: ");
double deposit = input.nextDouble();
arr[id].deposit(deposit);
}
if (choice == 4) {
bool = false;
}
}
}
}
}