银行存取款Java
介绍
银行存取款是一个非常常见的业务场景,通过编写Java程序,我们可以模拟这个过程。
在本文中,我们将使用Java编写一个简单的银行存取款系统,并通过代码示例来说明。
设计思路
我们的银行存取款系统将包含以下几个核心部分:
-
银行账户类(BankAccount):代表用户的银行账户,包含账户名和余额等信息,提供存款和取款方法。
-
银行类(Bank):代表银行,可以创建和管理多个银行账户。
-
用户界面类(BankUI):提供用户界面,允许用户进行存款和取款操作。
-
主类(Main):程序的入口点,初始化银行和用户界面,并实现用户与银行的交互。
代码示例
银行账户类(BankAccount)
public class BankAccount {
private String accountName;
private double balance;
public BankAccount(String accountName) {
this.accountName = accountName;
this.balance = 0.0;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
} else {
System.out.println("Insufficient balance!");
}
}
}
银行类(Bank)
public class Bank {
private List<BankAccount> accounts;
public Bank() {
accounts = new ArrayList<>();
}
public void createAccount(String accountName) {
BankAccount account = new BankAccount(accountName);
accounts.add(account);
}
public BankAccount getAccount(String accountName) {
for (BankAccount account : accounts) {
if (account.getAccountName().equals(accountName)) {
return account;
}
}
return null;
}
}
用户界面类(BankUI)
import java.util.Scanner;
public class BankUI {
private Bank bank;
public BankUI(Bank bank) {
this.bank = bank;
}
public void start() {
Scanner scanner = new Scanner(System.in);
boolean running = true;
while (running) {
System.out.println("1. Create Account");
System.out.println("2. Deposit");
System.out.println("3. Withdraw");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
System.out.print("Enter account name: ");
String accountName = scanner.nextLine();
bank.createAccount(accountName);
System.out.println("Account created successfully!");
break;
case 2:
System.out.print("Enter account name: ");
accountName = scanner.nextLine();
BankAccount account = bank.getAccount(accountName);
if (account != null) {
System.out.print("Enter amount to deposit: ");
double amount = scanner.nextDouble();
account.deposit(amount);
System.out.println("Amount deposited successfully!");
} else {
System.out.println("Account not found!");
}
scanner.nextLine();
break;
case 3:
System.out.print("Enter account name: ");
accountName = scanner.nextLine();
account = bank.getAccount(accountName);
if (account != null) {
System.out.print("Enter amount to withdraw: ");
double amount = scanner.nextDouble();
account.withdraw(amount);
System.out.println("Amount withdrawn successfully!");
} else {
System.out.println("Account not found!");
}
scanner.nextLine();
break;
case 4:
running = false;
break;
default:
System.out.println("Invalid choice!");
break;
}
}
}
}
主类(Main)
public class Main {
public static void main(String[] args) {
Bank bank = new Bank();
BankUI bankUI = new BankUI(bank);
bankUI.start();
}
}
甘特图
gantt
title 银行存取款项目
dateFormat YYYY-MM-DD
section 设计
银行账户类 :done, 2022-10-01, 1d
银行类 :done, 2022-10-02, 1d
用户界面类 :done, 2022-10-03, 1d
主类 :done, 2022-10-04, 1d
section 编码
银行账户类 :done, 2022-10-05, 2d
银行类 :done, 202