/*
Enter an id: 4
Invalid value.
Enter an id: 978
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 1
The balance is: 100.0
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 2
Enter an amount to withdraw: 33
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 1
The balance is: 67.0
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 3
Enter an amount to deposit: 100
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 1
The balance is: 167.0
Main menu
1: check balance
2: withdraw
3: deposit
4: exit
Enter a choice: 4
Enter an id:
*/
// ATM.java
package Uber;
import java.util.Scanner;
public class ATM {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Account account = new Account();
int number = 0;
while (true) {
System.out.print("Enter an id: ");
String id = input.next();
while (account.checkId(id)) {
showMenu();
number = input.nextInt();
if (number == 1)
System.out.println("The balance is: " + account.getBalance() + "\n");
else if (number == 2) {
System.out.print("Enter an amount to withdraw: ");
if (!account.withdraw(input.nextInt()))
System.out.println("Don't have enough monney in your account.\n");
else
System.out.println();
}
else if (number == 3) {
System.out.print("Enter an amount to deposit: ");
account.deposit(input.nextInt());
}
else if (number == 4)
break;
}
if (number != 4)
System.out.println("Invalid value.\n");
}
}
public static void showMenu() {
System.out.print("Main menu\n1: check balance\n2: withdraw\n3: deposit\n4: exit\nEnter a choice: ");
}
}
// Account.java
package Uber;
public class Account {
private String id;
private double balance;
private double withdraw;
private double deposit;
public Account() {
id = "978332";
balance = 100;
}
public boolean checkId(String id) {
if (this.id.equals(id))
return true;
else
return false;
}
public boolean withdraw(int amount) {
if (balance < amount)
return false;
else {
balance -= amount;
return true;
}
}
public double getBalance() {
return balance;
}
public void deposit(int amount) {
balance += amount;
}
}
ATM系统Java实现
本文介绍了一个简单的ATM系统的Java实现。系统包括基本的账户管理功能,如查询余额、取款、存款等操作。通过使用Scanner类从用户获取输入,并通过自定义的Account类来处理账户相关业务逻辑。
3894

被折叠的 条评论
为什么被折叠?



