使用java语言,设计一个银行账户挂历系统,具体功能为:开户、存款、取款、转账、查询、修改密码、销户
时间: 2023-11-08 10:49:45 浏览: 87
基于java的银行帐目管理系统设计与实现
好的,下面是一个简单的银行账户管理系统的Java代码实现,主要包括如下功能:
1. 开户
2. 存款
3. 取款
4. 转账
5. 查询
6. 修改密码
7. 销户
```java
import java.util.ArrayList;
import java.util.Scanner;
public class BankAccountSystem {
// 定义一个内部类,用于表示银行账户
private static class Account {
private String name; // 账户名
private String password; // 账户密码
private double balance; // 账户余额
public Account(String name, String password, double balance) {
this.name = name;
this.password = password;
this.balance = balance;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
private ArrayList<Account> accountList; // 银行账户列表
public BankAccountSystem() {
accountList = new ArrayList<>();
}
// 开户
public void openAccount() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入账户名:");
String name = scanner.nextLine();
System.out.print("请输入密码:");
String password = scanner.nextLine();
System.out.print("请输入初始存款金额:");
double balance = scanner.nextDouble();
Account account = new Account(name, password, balance);
accountList.add(account);
System.out.println("开户成功!");
}
// 存款
public void deposit() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入账户名:");
String name = scanner.nextLine();
System.out.print("请输入密码:");
String password = scanner.nextLine();
System.out.print("请输入存款金额:");
double amount = scanner.nextDouble();
Account account = findAccount(name, password);
if (account != null) {
account.setBalance(account.getBalance() + amount);
System.out.println("存款成功!");
System.out.println("当前余额为:" + account.getBalance());
} else {
System.out.println("账户名或密码错误!");
}
}
// 取款
public void withdraw() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入账户名:");
String name = scanner.nextLine();
System.out.print("请输入密码:");
String password = scanner.nextLine();
System.out.print("请输入取款金额:");
double amount = scanner.nextDouble();
Account account = findAccount(name, password);
if (account != null) {
if (amount <= account.getBalance()) {
account.setBalance(account.getBalance() - amount);
System.out.println("取款成功!");
System.out.println("当前余额为:" + account.getBalance());
} else {
System.out.println("余额不足!");
}
} else {
System.out.println("账户名或密码错误!");
}
}
// 转账
public void transfer() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入转出账户名:");
String name1 = scanner.nextLine();
System.out.print("请输入转出账户密码:");
String password1 = scanner.nextLine();
System.out.print("请输入转入账户名:");
String name2 = scanner.nextLine();
System.out.print("请输入转账金额:");
double amount = scanner.nextDouble();
Account account1 = findAccount(name1, password1);
Account account2 = findAccount(name2);
if (account1 != null && account2 != null) {
if (amount <= account1.getBalance()) {
account1.setBalance(account1.getBalance() - amount);
account2.setBalance(account2.getBalance() + amount);
System.out.println("转账成功!");
System.out.println(account1.getName() + "当前余额为:" + account1.getBalance());
System.out.println(account2.getName() + "当前余额为:" + account2.getBalance());
} else {
System.out.println("余额不足!");
}
} else {
System.out.println("账户名或密码错误!");
}
}
// 查询
public void query() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入账户名:");
String name = scanner.nextLine();
System.out.print("请输入密码:");
String password = scanner.nextLine();
Account account = findAccount(name, password);
if (account != null) {
System.out.println("账户名:" + account.getName());
System.out.println("当前余额为:" + account.getBalance());
} else {
System.out.println("账户名或密码错误!");
}
}
// 修改密码
public void changePassword() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入账户名:");
String name = scanner.nextLine();
System.out.print("请输入原密码:");
String oldPassword = scanner.nextLine();
System.out.print("请输入新密码:");
String newPassword = scanner.nextLine();
Account account = findAccount(name, oldPassword);
if (account != null) {
account.password = newPassword;
System.out.println("密码修改成功!");
} else {
System.out.println("账户名或密码错误!");
}
}
// 销户
public void closeAccount() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入账户名:");
String name = scanner.nextLine();
System.out.print("请输入密码:");
String password = scanner.nextLine();
Account account = findAccount(name, password);
if (account != null) {
accountList.remove(account);
System.out.println("销户成功!");
} else {
System.out.println("账户名或密码错误!");
}
}
// 根据账户名和密码查找账户
private Account findAccount(String name, String password) {
for (Account account : accountList) {
if (account.getName().equals(name) && account.getPassword().equals(password)) {
return account;
}
}
return null;
}
// 根据账户名查找账户
private Account findAccount(String name) {
for (Account account : accountList) {
if (account.getName().equals(name)) {
return account;
}
}
return null;
}
// 启动银行账户管理系统
public void start() {
Scanner scanner = new Scanner(System.in);
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("0. 退出");
System.out.print("请选择操作:");
int choice = scanner.nextInt();
switch (choice) {
case 1:
openAccount();
break;
case 2:
deposit();
break;
case 3:
withdraw();
break;
case 4:
transfer();
break;
case 5:
query();
break;
case 6:
changePassword();
break;
case 7:
closeAccount();
break;
case 0:
System.out.println("谢谢使用!");
System.exit(0);
default:
System.out.println("无效操作!");
}
System.out.println();
}
}
public static void main(String[] args) {
BankAccountSystem bankAccountSystem = new BankAccountSystem();
bankAccountSystem.start();
}
}
```
阅读全文