用java实现一个简单的ATM系统详解和代码是什么
时间: 2023-06-07 10:07:44 浏览: 252
好的,我明白了您的问题。以下是用Java实现简单ATM系统的伪代码和部分Java代码:
伪代码:
1. 定义账户类Account,包含账户名、账户余额、账户密码等属性和相关操作方法。
2. 定义ATM机类ATM,包含插卡、输入密码、查询余额、取款、存款、转账等功能。
3. 在ATM类中创建Account对象,并通过账户名和密码验证登录。
4. 实现相关功能时,需要对Account对象的余额进行修改,并记录相关日志。
Java代码:
1. Account类
```
public class Account {
private String accountNumber;
private double balance;
private String password;
//构造函数
public Account(String accountNumber, double balance, String password) {
this.accountNumber = accountNumber;
this.balance = balance;
this.password = password;
}
//设置账户名和密码
public void setAccountNumberAndPassword(String accountNumber, String password) {
this.accountNumber = accountNumber;
this.password = password;
}
//查询余额
public double getBalance() {
return this.balance;
}
//取款
public boolean withdraw(double amount) {
if (this.balance >= amount) {
this.balance -= amount;
return true;
} else {
return false;
}
}
//存款
public void deposit(double amount) {
this.balance += amount;
}
//转账
public boolean transfer(Account account, double amount) {
if (this.balance >= amount) {
this.balance -= amount;
account.deposit(amount);
return true;
} else {
return false;
}
}
}
```
2. ATM类
```
import java.util.Scanner;
public class ATM {
private Account account; //ATM机存储的账户对象
private boolean userAuthenticated = false; //记录用户是否已登录状态
private static final int BALANCE_INQUIRY = 1; //查询余额
private static final int WITHDRAWAL = 2; //取款
private static final int DEPOSIT = 3; //存款
private static final int TRANSFER = 4; //转账
private static final int EXIT = 5; //退出ATM机
//ATM机构造函数,初始化Account对象
public ATM() {
this.account = new Account("123456", 5000.0, "1234");
}
//开始ATM机操作,要求用户插卡并输入正确密码
public void run() {
while (true) {
if (userAuthenticated) {
performTransaction();
userAuthenticated = false; //用户退出登录状态
}
else {
System.out.println("Welcome! Please insert your card and enter your password.");
authenticateUser(); //用户登录
}
}
}
//用户登录输入密码验证
private void authenticateUser() {
Scanner input = new Scanner(System.in);
System.out.println("Please enter your account number:");
String accountNumber = input.nextLine();
System.out.println("Please enter your password:");
String password = input.nextLine();
if (accountNumber.equals(account.getAccountNumber()) && password.equals(account.getPassword())) {
userAuthenticated = true; //登录成功
} else {
System.out.println("Invalid account number or password. Please try again.");
}
}
//ATM机基本操作
private void performTransaction() {
int choice = 0;
Scanner input = new Scanner(System.in);
do {
System.out.println("Please select an option:");
System.out.println("1 - View balance");
System.out.println("2 - Withdraw cash");
System.out.println("3 - Deposit funds");
System.out.println("4 - Transfer cash");
System.out.println("5 - Exit");
choice = input.nextInt();
switch (choice) {
case BALANCE_INQUIRY:
System.out.println("Your balance is " + account.getBalance());
break;
case WITHDRAWAL:
System.out.println("Please enter withdrawal amount:");
double amount = input.nextDouble();
if (account.withdraw(amount)) {
System.out.println("Please take your cash.");
} else {
System.out.println("Insufficient funds.");
}
break;
case DEPOSIT:
System.out.println("Please enter deposit amount:");
amount = input.nextDouble();
account.deposit(amount);
System.out.println("Deposit successful. Your new balance is " + account.getBalance());
break;
case TRANSFER:
System.out.println("Please enter the recipient account number:");
String recipientAccountNumber = input.nextLine();
System.out.println("Please enter transfer amount:");
amount = input.nextDouble();
Account recipientAccount = new Account(recipientAccountNumber, 0, "");
if (account.transfer(recipientAccount, amount)) {
System.out.println("Transfer successful. Your new balance is " + account.getBalance());
System.out.println("Recipient's new balance is " + recipientAccount.getBalance());
} else {
System.out.println("Insufficient funds.");
}
break;
case EXIT:
System.out.println("Thank you for using this ATM machine!");
break;
default:
System.out.println("Invalid choice. Please try again.");
break;
}
} while (choice != EXIT);
}
}
```
3. 主函数
```
public class Main {
public static void main(String[] args) {
ATM atm = new ATM();
atm.run();
}
}
```
以上是一个简单的ATM系统的Java实现,仅供参考。
阅读全文