完整Java银行代码实现
时间: 2023-08-17 20:28:19 浏览: 46
以下是一个较为完整的 Java 银行系统代码实现,实现了存款、取款、转账、查询余额、查询交易记录等功能。代码中使用了面向对象的编程思想和设计模式,同时对数据进行了封装和安全控制,提高了系统的可靠性和稳定性。
```
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
/**
* 银行账户类
*/
class Account {
private String id; // 账户编号
private String name; // 账户名称
private String password; // 账户密码
private double balance; // 账户余额
private ArrayList<Transaction> transactions; // 交易记录
public Account(String id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
this.balance = 0;
this.transactions = new ArrayList<>();
}
public String getId() {
return id;
}
public String getName() {
return name;
}
// 存款
public void deposit(double amount) {
balance += amount;
transactions.add(new Transaction(new Date(), 'D', amount, balance));
}
// 取款
public void withdraw(double amount) {
if (balance < amount) {
System.out.println("余额不足!");
} else {
balance -= amount;
transactions.add(new Transaction(new Date(), 'W', amount, balance));
}
}
// 转账
public void transfer(Account target, double amount) {
if (balance < amount) {
System.out.println("余额不足!");
} else {
balance -= amount;
target.balance += amount;
transactions.add(new Transaction(new Date(), 'T', amount, balance, target.getId()));
}
}
public double getBalance() {
return balance;
}
// 查询交易记录
public void printTransactions() {
for (Transaction transaction : transactions) {
System.out.println(transaction.toString());
}
}
// 验证密码是否正确
public boolean validatePassword(String password) {
return this.password.equals(password);
}
}
/**
* 交易记录类
*/
class Transaction {
private Date date; // 交易日期
private char type; // 交易类型(D:存款,W:取款,T:转账)
private double amount; // 交易金额
private double balance; // 余额
private String targetId; // 目标账户编号(仅在转账时使用)
public Transaction(Date date, char type, double amount, double balance) {
this.date = date;
this.type = type;
this.amount = amount;
this.balance = balance;
}
public Transaction(Date date, char type, double amount, double balance, String targetId) {
this.date = date;
this.type = type;
this.amount = amount;
this.balance = balance;
this.targetId = targetId;
}
@Override
public String toString() {
return "Transaction{" +
"date=" + date +
", type=" + type +
", amount=" + amount +
", balance=" + balance +
(targetId != null ? ", targetId='" + targetId + '\'' : "") +
'}';
}
}
/**
* 银行系统类
*/
public class BankSystem {
private ArrayList<Account> accounts; // 所有账户
public BankSystem() {
this.accounts = new ArrayList<>();
}
// 创建账户
public void createAccount(String id, String name, String password) {
accounts.add(new Account(id, name, password));
}
// 登录账户
public Account loginAccount(String id, String password) {
for (Account account : accounts) {
if (account.getId().equals(id) && account.validatePassword(password)) {
return account;
}
}
return null;
}
public static void main(String[] args) {
BankSystem bank = new BankSystem();
Scanner scanner = new Scanner(System.in);
Account currentAccount = null;
while (true) {
if (currentAccount == null) {
System.out.println("欢迎使用银行系统,请选择操作:");
System.out.println("1. 创建账户");
System.out.println("2. 登录账户");
System.out.println("3. 退出");
System.out.print("请选择操作编号:");
int option = scanner.nextInt();
if (option == 1) {
System.out.print("请输入账户编号:");
String id = scanner.next();
System.out.print("请输入账户名称:");
String name = scanner.next();
System.out.print("请输入账户密码:");
String password = scanner.next();
bank.createAccount(id, name, password);
System.out.println("账户创建成功!");
} else if (option == 2) {
System.out.print("请输入账户编号:");
String id = scanner.next();
System.out.print("请输入账户密码:");
String password = scanner.next();
currentAccount = bank.loginAccount(id, password);
if (currentAccount == null) {
System.out.println("登录失败,请检查账户编号和密码!");
} else {
System.out.println("登录成功,欢迎 " + currentAccount.getName() + "!");
}
} else if (option == 3) {
break;
} else {
System.out.println("输入有误,请重新输入!");
}
} else {
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. 退出登录");
System.out.print("请选择操作编号:");
int option = scanner.nextInt();
if (option == 1) {
System.out.print("请输入存款金额:");
double amount = scanner.nextDouble();
currentAccount.deposit(amount);
System.out.println("存款成功!当前余额为:" + currentAccount.getBalance());
} else if (option == 2) {
System.out.print("请输入取款金额:");
double amount = scanner.nextDouble();
currentAccount.withdraw(amount);
System.out.println("取款成功!当前余额为:" + currentAccount.getBalance());
} else if (option == 3) {
System.out.print("请输入转账金额:");
double amount = scanner.nextDouble();
System.out.print("请输入目标账户编号:");
String targetId = scanner.next();
Account targetAccount = null;
for (Account account : bank.accounts) {
if (account.getId().equals(targetId)) {
targetAccount = account;
break;
}
}
if (targetAccount == null) {
System.out.println("目标账户不存在!");
} else {
currentAccount.transfer(targetAccount, amount);
System.out.println("转账成功!当前余额为:" + currentAccount.getBalance());
}
} else if (option == 4) {
System.out.println("当前余额为:" + currentAccount.getBalance());
} else if (option == 5) {
System.out.println("交易记录如下:");
currentAccount.printTransactions();
} else if (option == 6) {
System.out.println("退出登录成功!");
currentAccount = null;
} else {
System.out.println("输入有误,请重新输入!");
}
}
}
scanner.close();
}
}
```
这个完整的银行系统代码实现了基本的存款、取款、转账、查询余额和查询交易记录等功能,同时对数据进行了封装和安全控制,提高了系统的可靠性和稳定性。需要注意的是,这只是一个简单的示例,实际的银行系统需要更多的功能和安全措施。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)