Java代码 使用字符用户界面。当输入给定的卡号和密码(初始卡号为123456,密码是666666)时,系统能登录ATM柜员机系统,用户可以按照以下规则进行: 1、如果三次不能正确输入密码,则退出程序。 2、查询余额:初始余额为10000元 3、ATM取款:每次取款金额为100的倍数,总额不超过5000元,支取金额不允许透支。 4、ATM存款:不能出现负存款。 5、修改密码:新密码长度不小于6位,不允许出现6位完全相同的情况,只有旧密码正确,新密码符合要求才可以成功修改密码。 6、卡号、密码、余额可以写入文件中。
时间: 2024-03-03 10:53:15 浏览: 103
以下是Java代码实现:
```java
import java.io.*;
import java.util.Scanner;
public class ATM {
private static final String CARD_FILE = "card.txt"; // 卡信息文件名
private static final int MAX_TRIES = 3; // 最大尝试次数
private static final int MAX_WITHDRAWAL = 5000; // 最大取款额
private static final int MULTIPLE = 100; // 取款金额倍数
private static final int INITIAL_BALANCE = 10000; // 初始余额
private static final int MIN_PASSWORD_LENGTH = 6; // 密码最小长度
private static final int MAX_SAME_DIGITS = 6; // 最大相同数字个数
private String cardNum; // 卡号
private String password; // 密码
private int balance; // 余额
public ATM(String cardNum, String password, int balance) {
this.cardNum = cardNum;
this.password = password;
this.balance = balance;
}
// 读取卡信息文件
public static ATM readCardFile() throws IOException {
try (Scanner scanner = new Scanner(new File(CARD_FILE))) {
String cardNum = scanner.nextLine();
String password = scanner.nextLine();
int balance = scanner.nextInt();
return new ATM(cardNum, password, balance);
} catch (FileNotFoundException e) {
throw new IOException("Card file not found");
}
}
// 写入卡信息文件
public void writeCardFile() throws IOException {
try (PrintWriter writer = new PrintWriter(new FileWriter(CARD_FILE))) {
writer.println(cardNum);
writer.println(password);
writer.println(balance);
}
}
// 登录
public boolean login() {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < MAX_TRIES; i++) {
System.out.print("Card number: ");
String cardNum = scanner.nextLine();
System.out.print("Password: ");
String password = scanner.nextLine();
if (cardNum.equals(this.cardNum) && password.equals(this.password)) {
return true;
}
System.out.println("Invalid card number or password");
}
return false;
}
// 查询余额
public void queryBalance() {
System.out.println("Balance: " + balance);
}
// 取款
public void withdraw() {
Scanner scanner = new Scanner(System.in);
System.out.print("Amount to withdraw: ");
int amount = scanner.nextInt();
if (amount % MULTIPLE != 0 || amount > balance || amount > MAX_WITHDRAWAL) {
System.out.println("Invalid amount");
return;
}
balance -= amount;
System.out.println("New balance: " + balance);
}
// 存款
public void deposit() {
Scanner scanner = new Scanner(System.in);
System.out.print("Amount to deposit: ");
int amount = scanner.nextInt();
if (amount < 0) {
System.out.println("Invalid amount");
return;
}
balance += amount;
System.out.println("New balance: " + balance);
}
// 修改密码
public void changePassword() {
Scanner scanner = new Scanner(System.in);
System.out.print("Old password: ");
String oldPassword = scanner.nextLine();
if (!oldPassword.equals(password)) {
System.out.println("Invalid password");
return;
}
System.out.print("New password: ");
String newPassword = scanner.nextLine();
if (newPassword.length() < MIN_PASSWORD_LENGTH || countSameDigits(newPassword) > MAX_SAME_DIGITS) {
System.out.println("Invalid password");
return;
}
password = newPassword;
System.out.println("Password changed");
}
// 计算相同数字个数
private int countSameDigits(String s) {
int count = 0;
for (int i = 0; i < s.length() - 1; i++) {
if (s.charAt(i) == s.charAt(i + 1)) {
count++;
}
}
return count;
}
public static void main(String[] args) {
try {
ATM atm = readCardFile();
if (!atm.login()) {
return;
}
while (true) {
System.out.println();
System.out.println("1. Query balance");
System.out.println("2. Withdraw");
System.out.println("3. Deposit");
System.out.println("4. Change password");
System.out.println("5. Exit");
Scanner scanner = new Scanner(System.in);
System.out.print("Choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
atm.queryBalance();
break;
case 2:
atm.withdraw();
break;
case 3:
atm.deposit();
break;
case 4:
atm.changePassword();
break;
case 5:
atm.writeCardFile();
return;
default:
System.out.println("Invalid choice");
break;
}
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
```
注意事项:
1. 卡信息写在文件中,格式为卡号、密码、余额,每行一个。
2. 修改密码时,新密码必须符合要求才能成功修改。
阅读全文