用Java设计一个ATM取款机取款系统,具备注册,登录,查询,存款和取款基本功能
时间: 2024-10-18 07:20:27 浏览: 24
Swing实现ATM取款机系统(带sql文件)java全套代码,支持换背景图片
5星 · 资源好评率100%
在Java中设计一个简单的ATM取款机模拟系统,我们可以创建几个类来实现这个功能:
1. **Account** 类:表示用户的账户,包含账户号、余额等属性。
```java
public class Account {
private String accountNumber;
private double balance;
// 构造函数,getters和setters
public Account(String accountNumber, double initialBalance) {
this.accountNumber = accountNumber;
this.balance = initialBalance;
}
// 取款方法
public boolean withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
return true;
} else {
System.out.println("Insufficient balance.");
return false;
}
}
// 其他方法...
}
```
2. **User** 类:用户有密码和关联的账户信息。
```java
public class User {
private String username;
private String password;
private Account account;
// 构造函数,getters和setters
public User(String username, String password, Account account) {
this.username = username;
this.password = password;
this.account = account;
}
// 登录方法
public boolean login(String inputPassword) {
if (inputPassword.equals(password)) {
return true;
} else {
return false;
}
}
// 其他方法...
}
```
3. **ATMMachine** 类:用于处理 ATM 的操作。
```java
public class ATMMachine {
private Map<String, User> users;
public ATMMachine() {
users = new HashMap<>();
}
// 注册方法
public void register(String username, String password, Account account) {
users.put(username, new User(username, password, account));
}
// 查询余额方法
public void queryBalance(String username) {
User user = users.get(username);
if (user != null && user.login(username)) {
System.out.println("Your current balance is: " + user.getAccount().getBalance());
} else {
System.out.println("Invalid username or incorrect password.");
}
}
// 存款和取款方法
public void deposit(String username, double amount) {
User user = users.get(username);
if (user != null && user.login(username)) {
user.getAccount().deposit(amount);
System.out.println("Deposit successful. New balance: " + user.getAccount().getBalance());
}
}
// 取款方法
public void withdraw(String username, double amount) {
User user = users.get(username);
if (user != null && user.login(username)) {
boolean result = user.getAccount().withdraw(amount);
if (result) {
System.out.println("Withdrawal successful. New balance: " + user.getAccount().getBalance());
} else {
System.out.println("Withdrawal failed.");
}
} else {
System.out.println("Invalid username or incorrect password.");
}
}
}
```
阅读全文