本專案使用 Java 語言模擬銀行自助終端機 (ATM) 的核心功能,包含開戶、查詢餘額、存款、提款等基本銀行業務操作。透過物件導向設計,展示如何建立一個具備完整業務邏輯的控制台應用程式。
程式啟動後顯示主選單,提供五項功能選項:
系統會驗證使用者輸入,若輸入非 0-4 範圍的數字,會提示「選單不存在」並要求重新輸入。
系統採用檔案儲存資料 (可參考物件序列化/反序列化或 JSON 格式),重新啟動程式時會從檔案讀取資料初始化到記憶體。
package com.bank.unionpay;
public interface Yinhangka {
public void cun(); // 存款
public void qu(); // 提款
public void getyu(); // 查詢餘額
}
public enum Yinhang {
ABC("農行"), ICBC("工商"), CCB("建行"), BOC("中行");
private final String yinhang;
private Yinhang(String yinhang) { this.yinhang = yinhang; }
public String getname() { return this.yinhang; }
}
public abstract class ATM {
private Yinhanglei yhl;
private double yue;
public void yue() { System.out.println("卡上餘額為:" + this.yue); }
public void cun(double cun) {
if (cun < 0) System.out.println("輸入有誤");
else { this.yue += cun; System.out.println("卡上餘額為:" + this.yue); }
}
public void qu(double qu) {
if (qu > this.yue) System.out.println("餘額不足");
else { this.yue -= qu; System.out.println("卡上餘額為:" + this.yue); }
}
public abstract void kaihu(); // 抽象開戶方法
}
public class ATM2 extends ATM {
private Yinhang yhl = Yinhang.ABC;
@Override
public void kaihu() {
Cuxuka cxk = new Cuxuka();
Scanner sc = new Scanner(System.in);
// 輸入姓名
System.out.println("請輸入您的姓名:");
cxk.setName(sc.nextLine());
// 驗證身分證格式 (18位)
while (true) {
System.out.println("請輸入您的身分證號:");
String id = sc.nextLine();
if (id.matches("\\d{15}|\\d{17}[\\dxX]")) {
cxk.setShenfen(id); break;
}
System.out.println("輸入的身分證號不是18位身份號");
}
// 驗證密碼格式 (6位數字)
while (true) {
System.out.println("請輸入您的密碼:");
String pwd = sc.nextLine();
if (pwd.matches("\\d{6}")) {
cxk.setPassword(pwd); break;
}
System.out.println("請輸入正確的6位數密碼");
}
// 產生6位隨機卡號
Random rand = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 6; i++) sb.append(rand.nextInt(9));
cxk.setCard(sb.toString());
// 存入銀行管理容器
Yinhanglei.getYinhangLei().list.add(cxk.getCard());
Yinhanglei.getYinhangLei().map.put(cxk.getCard(), cxk);
System.out.println("您的新卡號為:" + cxk.getCard());
System.out.println("開戶成功,感謝使用");
}
}
本專案完整展示了 Java 物件導向設計在銀行系統模擬中的應用,涵蓋介面定義、抽象類別、繼承與多型、單例模式、列舉類型、例外處理、集合框架運用等核心概念。透過模組化套件設計,達成高內聚低耦合,便於後續擴充其他銀行業務邏輯。