帮我写一个关于银行账户管理系统的Java代码,至少要有用户模块包括:登录模块、主菜单模块、存款模块、取款模块、转账模块和查询模块以及退出功能,管理员模块包括:添加用户功能、删除用户功能、查询所有用户基本信息功能、打印用户清单功能以及退出功能。
时间: 2023-11-12 19:09:43 浏览: 71
好的,我可以帮您写一个简单的银行账户管理系统的Java代码,代码如下:
```
import java.util.Scanner;
public class BankAccountManagementSystem {
static int usersCount = 0; // 用户总数
static User[] users = new User[100]; // 最多100个用户
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("欢迎使用银行账户管理系统!");
System.out.println("请选择您的身份:");
System.out.println("1. 用户");
System.out.println("2. 管理员");
System.out.println("3. 退出");
int choice = scanner.nextInt();
if (choice == 1) {
userMenu(scanner);
} else if (choice == 2) {
adminMenu(scanner);
} else if (choice == 3) {
System.out.println("感谢使用银行账户管理系统!");
break;
} else {
System.out.println("无效的选择,请重新选择!");
}
}
}
public static void userMenu(Scanner scanner) {
User currentUser = login(scanner);
if (currentUser == null) {
System.out.println("登录失败!");
return;
}
while (true) {
System.out.println("欢迎使用银行账户管理系统," + currentUser.name + "!");
System.out.println("请选择您要的操作:");
System.out.println("1. 存款");
System.out.println("2. 取款");
System.out.println("3. 转账");
System.out.println("4. 查询余额");
System.out.println("5. 退出");
int choice = scanner.nextInt();
if (choice == 1) {
System.out.println("请输入存款金额:");
double amount = scanner.nextDouble();
currentUser.deposit(amount);
System.out.println("存款成功!");
} else if (choice == 2) {
System.out.println("请输入取款金额:");
double amount = scanner.nextDouble();
if (currentUser.withdraw(amount)) {
System.out.println("取款成功!");
} else {
System.out.println("余额不足,取款失败!");
}
} else if (choice == 3) {
System.out.println("请输入转账金额:");
double amount = scanner.nextDouble();
System.out.println("请输入对方账号:");
String targetAccount = scanner.next();
User targetUser = findUserByAccount(targetAccount);
if (targetUser == null) {
System.out.println("对方账号不存在,转账失败!");
} else {
if (currentUser.transfer(targetUser, amount)) {
System.out.println("转账成功!");
} else {
System.out.println("余额不足,转账失败!");
}
}
} else if (choice == 4) {
System.out.println("您的余额为:" + currentUser.balance);
} else if (choice == 5) {
System.out.println("感谢使用银行账户管理系统," + currentUser.name + "!");
break;
} else {
System.out.println("无效的选择,请重新选择!");
}
}
}
public static void adminMenu(Scanner scanner) {
if (!adminLogin(scanner)) {
System.out.println("登录失败!");
return;
}
while (true) {
System.out.println("欢迎使用银行账户管理系统,管理员!");
System.out.println("请选择您要的操作:");
System.out.println("1. 添加用户");
System.out.println("2. 删除用户");
System.out.println("3. 查询所有用户基本信息");
System.out.println("4. 打印用户清单");
System.out.println("5. 退出");
int choice = scanner.nextInt();
if (choice == 1) {
addUser(scanner);
System.out.println("添加用户成功!");
} else if (choice == 2) {
deleteUser(scanner);
System.out.println("删除用户成功!");
} else if (choice == 3) {
listAllUsers();
} else if (choice == 4) {
printUserList();
} else if (choice == 5) {
System.out.println("感谢使用银行账户管理系统,管理员!");
break;
} else {
System.out.println("无效的选择,请重新选择!");
}
}
}
// 登录
public static User login(Scanner scanner) {
System.out.println("请输入账号:");
String account = scanner.next();
System.out.println("请输入密码:");
String password = scanner.next();
for (int i = 0; i < usersCount; i++) {
if (users[i].account.equals(account) && users[i].password.equals(password)) {
return users[i];
}
}
return null;
}
// 管理员登录
public static boolean adminLogin(Scanner scanner) {
System.out.println("请输入管理员账号:");
String account = scanner.next();
System.out.println("请输入管理员密码:");
String password = scanner.next();
return account.equals("admin") && password.equals("admin");
}
// 根据账号查找用户
public static User findUserByAccount(String account) {
for (int i = 0; i < usersCount; i++) {
if (users[i].account.equals(account)) {
return users[i];
}
}
return null;
}
// 添加用户
public static void addUser(Scanner scanner) {
System.out.println("请输入用户姓名:");
String name = scanner.next();
System.out.println("请输入账号:");
String account = scanner.next();
System.out.println("请输入密码:");
String password = scanner.next();
User newUser = new User(name, account, password);
users[usersCount++] = newUser;
}
// 删除用户
public static void deleteUser(Scanner scanner) {
System.out.println("请输入要删除的用户账号:");
String account = scanner.next();
for (int i = 0; i < usersCount; i++) {
if (users[i].account.equals(account)) {
for (int j = i; j < usersCount - 1; j++) {
users[j] = users[j + 1];
}
usersCount--;
break;
}
}
}
// 查询所有用户基本信息
public static void listAllUsers() {
for (int i = 0; i < usersCount; i++) {
System.out.println("姓名:" + users[i].name + ",账号:" + users[i].account);
}
}
// 打印用户清单
public static void printUserList() {
System.out.println("姓名\t账号\t密码\t余额");
for (int i = 0; i < usersCount; i++) {
System.out.println(users[i].name + "\t" + users[i].account + "\t" + users[i].password + "\t" + users[i].balance);
}
}
}
class User {
String name; // 姓名
String account; // 账号
String password; // 密码
double balance = 0.0; // 余额
public User(String name, String account, String password) {
this.name = name;
this.account = account;
this.password = password;
}
// 存款
public void deposit(double amount) {
balance += amount;
}
// 取款
public boolean withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
return true;
} else {
return false;
}
}
// 转账
public boolean transfer(User targetUser, double amount) {
if (balance >= amount) {
balance -= amount;
targetUser.balance += amount;
return true;
} else {
return false;
}
}
}
```
您可以将代码保存为 `BankAccountManagementSystem.java` 文件,然后在命令行中编译并运行该程序,即可体验银行账户管理系统。
阅读全文