用Java写一个ATM柜员模拟项目,需要有的功能有取款、存款、转账、查询余额、历史记录查询、修改密码和退出等功能;其中取款、存款和转账的金额是100的倍数
时间: 2023-12-07 16:03:52 浏览: 129
好的,我可以给你提供一个大概的项目框架,让你参考一下。
首先,你需要创建一个ATM类来处理所有的操作。在ATM类中,你需要定义一些属性,如账户余额、密码等等。你还需要写一些方法实现ATM的各种操作,如取款、存款、转账、查询余额、历史记录查询、修改密码和退出等功能。
下面是一个简单的ATM类示例代码:
```java
import java.util.ArrayList;
import java.util.Scanner;
public class ATM {
private String accountNumber;
private String password;
private double balance;
private ArrayList<String> transactionHistory;
public ATM(String accountNumber, String password, double balance) {
this.accountNumber = accountNumber;
this.password = password;
this.balance = balance;
this.transactionHistory = new ArrayList<>();
}
public void withdraw(double amount) {
if (amount % 100 != 0) {
System.out.println("金额必须是100的倍数!");
return;
}
if (amount > balance) {
System.out.println("余额不足!");
return;
}
balance -= amount;
transactionHistory.add("取款:" + amount);
System.out.println("取款成功!");
}
public void deposit(double amount) {
if (amount % 100 != 0) {
System.out.println("金额必须是100的倍数!");
return;
}
balance += amount;
transactionHistory.add("存款:" + amount);
System.out.println("存款成功!");
}
public void transfer(ATM target, double amount) {
if (amount % 100 != 0) {
System.out.println("金额必须是100的倍数!");
return;
}
if (amount > balance) {
System.out.println("余额不足!");
return;
}
balance -= amount;
target.balance += amount;
transactionHistory.add("转账:" + amount + " 给 " + target.accountNumber);
System.out.println("转账成功!");
}
public void checkBalance() {
System.out.println("账户余额:" + balance);
}
public void checkTransactionHistory() {
System.out.println("交易记录:");
for (String transaction : transactionHistory) {
System.out.println(transaction);
}
}
public void changePassword() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入原密码:");
String oldPassword = scanner.next();
if (!oldPassword.equals(password)) {
System.out.println("原密码输入错误!");
return;
}
System.out.println("请输入新密码:");
String newPassword = scanner.next();
System.out.println("请再次输入新密码:");
String newPassword2 = scanner.next();
if (!newPassword.equals(newPassword2)) {
System.out.println("两次输入的密码不一致!");
return;
}
password = newPassword;
System.out.println("密码修改成功!");
}
public static void main(String[] args) {
ATM atm = new ATM("123456", "123456", 1000);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择操作:");
System.out.println("1.取款 2.存款 3.转账 4.查询余额 5.查询历史记录 6.修改密码 7.退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请输入取款金额:");
double withdrawAmount = scanner.nextDouble();
atm.withdraw(withdrawAmount);
break;
case 2:
System.out.println("请输入存款金额:");
double depositAmount = scanner.nextDouble();
atm.deposit(depositAmount);
break;
case 3:
System.out.println("请输入转账账号:");
String targetAccountNumber = scanner.next();
System.out.println("请输入转账金额:");
double transferAmount = scanner.nextDouble();
ATM target = new ATM(targetAccountNumber, "", 0);
atm.transfer(target, transferAmount);
break;
case 4:
atm.checkBalance();
break;
case 5:
atm.checkTransactionHistory();
break;
case 6:
atm.changePassword();
break;
case 7:
System.out.println("退出系统!");
return;
default:
System.out.println("输入有误,请重新输入!");
}
}
}
}
```
这只是一个简单的示例代码,你可以根据自己的需求进行修改和完善。
阅读全文