银行账户管理系统java
时间: 2023-10-23 15:10:24 浏览: 81
JAVA银行账户管理系统
5星 · 资源好评率100%
银行账户管理系统可以通过Java语言实现。下面是一个简单的Java程序示例,它可以实现银行账户管理系统的基本功能:
```java
import java.util.Scanner;
public class BankAccountManagementSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Bank bank = new Bank();
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. 退出");
int option = scanner.nextInt();
switch (option) {
case 1:
System.out.print("请输入账户名:");
String name = scanner.next();
System.out.print("请输入初始存款金额:");
double balance = scanner.nextDouble();
bank.createAccount(name, balance);
System.out.println("账户创建成功!");
break;
case 2:
System.out.print("请输入账户名:");
name = scanner.next();
System.out.print("请输入存款金额:");
double amount = scanner.nextDouble();
bank.deposit(name, amount);
System.out.println("存款成功!");
break;
case 3:
System.out.print("请输入账户名:");
name = scanner.next();
System.out.print("请输入取款金额:");
amount = scanner.nextDouble();
bank.withdraw(name, amount);
System.out.println("取款成功!");
break;
case 4:
System.out.print("请输入账户名:");
name = scanner.next();
double balance2 = bank.getBalance(name);
System.out.println("账户余额为:" + balance2);
break;
case 5:
System.exit(0);
break;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
}
class Bank {
private Account[] accounts; // 账户数组
private int count; // 账户数量
public Bank() {
accounts = new Account[100];
count = 0;
}
// 创建账户
public void createAccount(String name, double balance) {
Account account = new Account(name, balance);
accounts[count++] = account;
}
// 存款
public void deposit(String name, double amount) {
for (int i = 0; i < count; i++) {
if (accounts[i].getName().equals(name)) {
accounts[i].deposit(amount);
break;
}
}
}
// 取款
public void withdraw(String name, double amount) {
for (int i = 0; i < count; i++) {
if (accounts[i].getName().equals(name)) {
accounts[i].withdraw(amount);
break;
}
}
}
// 查询余额
public double getBalance(String name) {
for (int i = 0; i < count; i++) {
if (accounts[i].getName().equals(name)) {
return accounts[i].getBalance();
}
}
return -1;
}
}
class Account {
private String name; // 账户名
private double balance; // 余额
public Account(String name, double balance) {
this.name = name;
this.balance = balance;
}
// 存款
public void deposit(double amount) {
balance += amount;
}
// 取款
public void withdraw(double amount) {
balance -= amount;
}
public String getName() {
return name;
}
public double getBalance() {
return balance;
}
}
```
该程序使用了三个类:`BankAccountManagementSystem` 类是主类,用于用户交互;`Bank` 类实现了银行的核心业务逻辑;`Account` 类表示一个账户,包含账户名和余额两个属性,并提供了存款、取款等操作。
在 `Bank` 类中,使用了一个数组来存储账户信息,通过遍历数组来查找指定账户,并进行存款、取款等操作。在 `Account` 类中,使用了两个属性来存储账户名和余额,以及相应的方法来进行存款、取款等操作。
该程序只是一个简单的示例,实际应用中还需要考虑并发、安全等问题。
阅读全文