用java实现atm机功能
时间: 2024-02-05 10:13:41 浏览: 133
实现ATM机功能需要以下步骤:
1. 创建一个用户类,包含用户名、密码、余额等信息。
2. 创建一个ATM类,包含以下功能:
a. 登录功能:输入用户名和密码,验证用户信息是否正确。
b. 查询余额功能:查询当前用户的余额。
c. 存款功能:输入存款金额,将金额加入当前用户的余额中。
d. 取款功能:输入取款金额,将金额从当前用户的余额中减去。
e. 转账功能:输入转账金额和对方账户名,将金额从当前用户的余额中减去,加入对方账户的余额中。
f. 修改密码功能:输入原密码和新密码,修改当前用户的密码。
g. 退出功能:退出ATM机操作。
下面是一个简单的Java代码实现:
```java
import java.util.Scanner;
public class ATM {
private static User[] users = new User[10]; // 最多支持10个用户
private static int userCount = 0; // 用户数
private static User currentUser = null; // 当前用户
public static void main(String[] args) {
initUsers(); // 初始化用户数据
System.out.println("欢迎使用ATM机!");
login(); // 登录
System.out.println("登录成功,当前余额:" + currentUser.getBalance());
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
System.out.println("1. 查询余额");
System.out.println("2. 存款");
System.out.println("3. 取款");
System.out.println("4. 转账");
System.out.println("5. 修改密码");
System.out.println("6. 退出");
int choice = scanner.nextInt();
if (choice == 1) {
queryBalance();
} else if (choice == 2) {
deposit();
} else if (choice == 3) {
withdraw();
} else if (choice == 4) {
transfer();
} else if (choice == 5) {
changePassword();
} else if (choice == 6) {
System.out.println("退出成功,欢迎下次使用!");
break;
} else {
System.out.println("请选择正确的操作!");
}
}
}
// 初始化用户数据
private static void initUsers() {
users[0] = new User("张三", "123456", 1000);
users[1] = new User("李四", "123456", 2000);
userCount = 2;
}
// 登录
private static void login() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请输入用户名:");
String username = scanner.nextLine();
System.out.println("请输入密码:");
String password = scanner.nextLine();
for (int i = 0; i < userCount; i++) {
if (users[i].getUsername().equals(username) && users[i].getPassword().equals(password)) {
currentUser = users[i];
return;
}
}
System.out.println("用户名或密码错误,请重新输入!");
}
}
// 查询余额
private static void queryBalance() {
System.out.println("当前余额:" + currentUser.getBalance());
}
// 存款
private static void deposit() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入存款金额:");
double amount = scanner.nextDouble();
if (amount < 0) {
System.out.println("存款金额不能为负数!");
return;
}
currentUser.setBalance(currentUser.getBalance() + amount);
System.out.println("存款成功,当前余额:" + currentUser.getBalance());
}
// 取款
private static void withdraw() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入取款金额:");
double amount = scanner.nextDouble();
if (amount < 0) {
System.out.println("取款金额不能为负数!");
return;
}
if (amount > currentUser.getBalance()) {
System.out.println("余额不足!");
return;
}
currentUser.setBalance(currentUser.getBalance() - amount);
System.out.println("取款成功,当前余额:" + currentUser.getBalance());
}
// 转账
private static void transfer() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入转账金额:");
double amount = scanner.nextDouble();
if (amount < 0) {
System.out.println("转账金额不能为负数!");
return;
}
if (amount > currentUser.getBalance()) {
System.out.println("余额不足!");
return;
}
System.out.println("请输入对方账户名:");
String username = scanner.nextLine();
User otherUser = null;
for (int i = 0; i < userCount; i++) {
if (users[i].getUsername().equals(username)) {
otherUser = users[i];
break;
}
}
if (otherUser == null) {
System.out.println("对方账户不存在!");
return;
}
currentUser.setBalance(currentUser.getBalance() - amount);
otherUser.setBalance(otherUser.getBalance() + amount);
System.out.println("转账成功,当前余额:" + currentUser.getBalance());
}
// 修改密码
private static void changePassword() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入原密码:");
String oldPassword = scanner.nextLine();
if (!currentUser.getPassword().equals(oldPassword)) {
System.out.println("原密码错误!");
return;
}
System.out.println("请输入新密码:");
String newPassword = scanner.nextLine();
currentUser.setPassword(newPassword);
System.out.println("修改密码成功!");
}
}
class User {
private String username; // 用户名
private String password; // 密码
private double balance; // 余额
public User(String username, String password, double balance) {
this.username = username;
this.password = password;
this.balance = balance;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public double getBalance() {
return balance;
}
public void setPassword(String password) {
this.password = password;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
```
阅读全文