publicclass accounts {
// Create ten accountsprivate Account[] accounts = new Account[10];
publicaccounts() {
for (int i = 0; i < accounts.length; i++) {
accounts[i] = new Account();
accounts[i].setId(i);
accounts[i].setBalance(100);
}
continueATM:while (true) {
System.out.print("Enter an id: ");
id = input.nextInt();
if (id < 1 || id > 10) {
System.out.println("Please enter a correct id");
}
while (true) {
int choice = getAChoice();
switch (choice) {
case1:
System.out.println("The balance is " +
accounts[id].getBalance());
break;
case2:
withdraw();
break;
case3:
deposit();
break;
case4:
// You can rewrite the code without using the continue. // To do so, introduce a boolean variable to control one // customer sessioncontinue continueATM;
}
}
}
}
privatestatic java.util.Scanner input = new java.util.Scanner(
System.in);
int id;
publicstaticvoidmain(String[] args) {
new accounts();
}
publicvoidwithdraw() {
System.out.print("Enter an amount to withdraw: ");
int amount = input.nextInt();
if (amount <= accounts[id].getBalance()) {
accounts[id].withdraw(amount);
}
else {
System.out.print("The amount is too large, ignored");
}
}
publicvoiddeposit() {
System.out.print("Enter an amount to deposit: ");
int amount = input.nextInt();
if (amount >= 0) {
accounts[id].deposit(amount);
}
else {
System.out.print("The amount is negative, ignored");
}
}
publicintgetAChoice() {
int choice;
while (true) {
System.out.println("\nMain 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();
if (choice < 1 || choice > 4) {
System.out.println("Wrond choice, try again: ");
}
else {
break;
}
}
return choice;
}
}
## 题目要求 ##使用Account类来模拟一台ATM机,创建一个有10个账户的数组,其ID从0到9,并初始化收支为100美元。系统会提示用户输入一个 ID 如果输入的ID不正确,就要求用户输入正确的ID,一旦接收一个ID就显示如结果所示的主菜单,而且系统会再次提示输入ID所以,系统一旦启动,就不会停止。## Account类 ##class Account { private int id