使用java代码实现一个简单的ATM系统,详解
时间: 2024-05-14 10:13:50 浏览: 120
好的,关于你的问题,我可以给你一个简单的ATM系统的Java代码实现的流程。
1. 首先,创建一个账户类来表示每个账户。这个类应该有一个账号(account number)、密码(password)、余额(balance)等属性,以及相关的getter和setter方法。
2. 创建一个ATM类来代表整个自动取款机系统。这个类应该有账户列表(account list),以及相关的维护账户列表的方法,例如添加账户(add account)、删除账户(delete account)、验证账户(validate account)等等。
3. 当用户登录时,ATM应该提示用户输入账户号和密码。ATM需要验证账户号和密码是否匹配,并显示余额信息。
4. 当用户决定取款时,ATM应提示用户输入取款金额并验证余额是否充足。如果余额足够,则更新账户余额信息,否则提示用户余额不足。
5. 当用户决定存款时,ATM应提示用户输入存款金额并更新账户余额信息。
6. 最后,ATM应该提供一个退出该系统的选项。
下面是一个简单的ATM系统Java代码实现的例子:
```
class Account {
private int accountNumber;
private int password;
private double balance;
public Account(int accountNumber, int password, double balance) {
this.accountNumber = accountNumber;
this.password = password;
this.balance = balance;
}
public int getAccountNumber() {
return accountNumber;
}
public int getPassword() {
return password;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
class ATM {
private ArrayList<Account> accountList;
public ATM() {
accountList = new ArrayList<Account>();
}
public void addAccount(Account account) {
accountList.add(account);
}
public void deleteAccount(Account account) {
accountList.remove(account);
}
public boolean validateAccount(int accountNumber, int password) {
for (Account account : accountList) {
if (account.getAccountNumber() == accountNumber && account.getPassword() == password) {
return true;
}
}
return false;
}
public double showBalance(int accountNumber) {
for (Account account : accountList) {
if (account.getAccountNumber() == accountNumber) {
return account.getBalance();
}
}
return -1;
}
public boolean withdraw(int accountNumber, double amount) {
for (Account account : accountList) {
if (account.getAccountNumber() == accountNumber) {
if (account.getBalance() >= amount) {
account.setBalance(account.getBalance() - amount);
return true;
} else {
return false;
}
}
}
return false;
}
public void deposit(int accountNumber, double amount) {
for (Account account : accountList) {
if (account.getAccountNumber() == accountNumber) {
account.setBalance(account.getBalance() + amount);
}
}
}
}
class Main {
public static void main(String[] args) {
ATM atm = new ATM();
atm.addAccount(new Account(12345, 1234, 500.0));
atm.addAccount(new Account(67890, 5678, 1000.0));
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请输入账户编号:");
int accountNumber = scanner.nextInt();
System.out.println("请输入密码:");
int password = scanner.nextInt();
if (atm.validateAccount(accountNumber, password)) {
System.out.println("余额为:" + atm.showBalance(accountNumber));
System.out.println("请选择操作:\n1.取款\n2.存款\n3.退出");
int option = scanner.nextInt();
switch (option) {
case 1:
System.out.println("请输入取款金额:");
double withdrawAmount = scanner.nextDouble();
if (atm.withdraw(accountNumber, withdrawAmount)) {
System.out.println("取款成功!");
System.out.println("余额为:" + atm.showBalance(accountNumber));
} else {
System.out.println("余额不足!");
}
break;
case 2:
System.out.println("请输入存款金额:");
double depositAmount = scanner.nextDouble();
atm.deposit(accountNumber, depositAmount);
System.out.println("存款成功!");
System.out.println("余额为:" + atm.showBalance(accountNumber));
break;
case 3:
System.exit(0);
default:
System.out.println("输入错误!");
break;
}
} else {
System.out.println("账号或密码错误!");
}
}
}
}
```
以上就是一个简单的ATM系统的Java代码实现,希望能帮到你!
阅读全文