用java实现银行存取款系统代码
时间: 2023-11-02 10:06:20 浏览: 168
以下是一个简单的Java银行存取款系统的实现代码,仅供参考:
Account.java:
```java
public class Account {
private String name;
private String accountNumber;
private double balance;
public Account(String name, String accountNumber, double balance) {
this.name = name;
this.accountNumber = accountNumber;
this.balance = balance;
}
public String getName() {
return name;
}
public String getAccountNumber() {
return accountNumber;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
} else {
System.out.println("Insufficient balance!");
}
}
public void displayBalance() {
System.out.println("Account balance: " + balance);
}
}
```
Bank.java:
```java
import java.util.ArrayList;
import java.util.List;
public class Bank {
private List<Account> accounts;
public Bank() {
accounts = new ArrayList<>();
}
public void addAccount(Account account) {
accounts.add(account);
}
public void removeAccount(Account account) {
accounts.remove(account);
}
public Account findAccountByName(String name) {
for (Account account : accounts) {
if (account.getName().equals(name)) {
return account;
}
}
return null;
}
public Account findAccountByNumber(String accountNumber) {
for (Account account : accounts) {
if (account.getAccountNumber().equals(accountNumber)) {
return account;
}
}
return null;
}
}
```
UI.java:
```java
import java.util.Scanner;
public class UI {
private Bank bank;
public UI(Bank bank) {
this.bank = bank;
}
public void run() {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the bank!");
while (true) {
System.out.println("Enter account name or number (or 'exit' to quit):");
String input = scanner.nextLine();
if (input.equals("exit")) {
break;
}
Account account;
if (input.matches("\\d+")) {
account = bank.findAccountByNumber(input);
} else {
account = bank.findAccountByName(input);
}
if (account == null) {
System.out.println("Account not found!");
continue;
}
while (true) {
System.out.println("Enter 'd' for deposit, 'w' for withdraw, 'b' for balance (or 'back' to go back):");
String action = scanner.nextLine();
if (action.equals("back")) {
break;
}
switch (action) {
case "d":
System.out.println("Enter deposit amount:");
double depositAmount = Double.parseDouble(scanner.nextLine());
account.deposit(depositAmount);
System.out.println("Deposit successful!");
break;
case "w":
System.out.println("Enter withdraw amount:");
double withdrawAmount = Double.parseDouble(scanner.nextLine());
account.withdraw(withdrawAmount);
System.out.println("Withdrawal successful!");
break;
case "b":
account.displayBalance();
break;
default:
System.out.println("Invalid action!");
}
}
}
System.out.println("Goodbye!");
}
}
```
Main.java:
```java
public class Main {
public static void main(String[] args) {
Bank bank = new Bank();
bank.addAccount(new Account("Alice", "12345", 1000));
bank.addAccount(new Account("Bob", "67890", 500));
UI ui = new UI(bank);
ui.run();
}
}
```
这个简单的实现只是一个基础版本,还有很多可以改进的地方。例如,可以添加密码验证、支持多种货币类型、添加账户余额利息计算等功能。
阅读全文