java银行系统包括开户,存款,取款,转账,查询,修改密码,销户代码
时间: 2024-06-08 14:12:29 浏览: 135
java 银行系统 登陆 开户 存取款及其他
以下是一个简单的 Java 银行系统的代码实现,包括开户,存款,取款,转账,查询,修改密码,销户功能。
```java
import java.util.ArrayList;
import java.util.Scanner;
public class BankSystem {
static Scanner scanner = new Scanner(System.in);
static ArrayList<Account> accounts = new ArrayList<>();
public static void main(String[] args) {
while (true) {
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.println("7. 销户");
System.out.println("8. 退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
newAccount();
break;
case 2:
deposit();
break;
case 3:
withdraw();
break;
case 4:
transfer();
break;
case 5:
query();
break;
case 6:
changePassword();
break;
case 7:
deleteAccount();
break;
case 8:
return;
default:
System.out.println("输入错误,请重新输入");
break;
}
}
}
// 开户
static void newAccount() {
System.out.println("请输入账号:");
String account = scanner.next();
System.out.println("请输入密码:");
String password = scanner.next();
System.out.println("请输入初始存款金额:");
double balance = scanner.nextDouble();
accounts.add(new Account(account, password, balance));
System.out.println("开户成功");
}
// 存款
static void deposit() {
System.out.println("请输入账号:");
String account = scanner.next();
System.out.println("请输入密码:");
String password = scanner.next();
Account acc = getAccount(account, password);
if (acc != null) {
System.out.println("请输入存款金额:");
double amount = scanner.nextDouble();
acc.deposit(amount);
System.out.println("存款成功");
}
}
// 取款
static void withdraw() {
System.out.println("请输入账号:");
String account = scanner.next();
System.out.println("请输入密码:");
String password = scanner.next();
Account acc = getAccount(account, password);
if (acc != null) {
System.out.println("请输入取款金额:");
double amount = scanner.nextDouble();
if (acc.withdraw(amount)) {
System.out.println("取款成功");
} else {
System.out.println("余额不足,取款失败");
}
}
}
// 转账
static void transfer() {
System.out.println("请输入源账号:");
String account1 = scanner.next();
System.out.println("请输入密码:");
String password1 = scanner.next();
Account acc1 = getAccount(account1, password1);
if (acc1 == null) {
return;
}
System.out.println("请输入目标账号:");
String account2 = scanner.next();
Account acc2 = getAccount(account2);
if (acc2 == null) {
return;
}
System.out.println("请输入转账金额:");
double amount = scanner.nextDouble();
if (acc1.withdraw(amount)) {
acc2.deposit(amount);
System.out.println("转账成功");
} else {
System.out.println("余额不足,转账失败");
}
}
// 查询
static void query() {
System.out.println("请输入账号:");
String account = scanner.next();
System.out.println("请输入密码:");
String password = scanner.next();
Account acc = getAccount(account, password);
if (acc != null) {
System.out.println("余额:" + acc.getBalance());
}
}
// 修改密码
static void changePassword() {
System.out.println("请输入账号:");
String account = scanner.next();
System.out.println("请输入旧密码:");
String oldPassword = scanner.next();
Account acc = getAccount(account, oldPassword);
if (acc != null) {
System.out.println("请输入新密码:");
String newPassword = scanner.next();
acc.setPassword(newPassword);
System.out.println("修改密码成功");
}
}
// 销户
static void deleteAccount() {
System.out.println("请输入账号:");
String account = scanner.next();
System.out.println("请输入密码:");
String password = scanner.next();
Account acc = getAccount(account, password);
if (acc != null) {
accounts.remove(acc);
System.out.println("销户成功");
}
}
// 根据账号和密码查找账户
static Account getAccount(String account, String password) {
for (Account acc : accounts) {
if (acc.getAccount().equals(account) && acc.getPassword().equals(password)) {
return acc;
}
}
System.out.println("账号或密码错误");
return null;
}
// 根据账号查找账户
static Account getAccount(String account) {
for (Account acc : accounts) {
if (acc.getAccount().equals(account)) {
return acc;
}
}
System.out.println("账号不存在");
return null;
}
}
// 账户类
class Account {
private String account;
private String password;
private double balance;
public Account(String account, String password, double balance) {
this.account = account;
this.password = password;
this.balance = balance;
}
public String getAccount() {
return account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
balance += amount;
}
public boolean withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
return true;
} else {
return false;
}
}
}
```
阅读全文