Xây dựng hệ thống quản lý tài khoản ngân hàng bằng ngôn ngữ Java

Hệ thống quản lý tài khoản ngân hàng là một bài toán lập trình hướng đối tượng (OOP) điển hình, giúp người học nắm vững các kỹ thuật xử lý dữ liệu, quản lý trạng thái đối tượng và logic nghiệp vụ. Dưới đây là hướng dẫn triển khai một ứng dụng quản lý ngân hàng cơ bản với các tính năng: mở tài khoản, nạp/rút tiền (có hỗ trợ ngoại tệ), chuyển khoản liên ngân hàng và quản lý lịch sử giao dịch.

1. Kiến trúc thực thể của hệ thống

Đầu tiên, chúng ta cần định nghĩa các lớp dữ liệu cơ bản để lưu trữ thông tin khách hàng và lịch sử biến động số dư.

import java.util.*;

// Lớp đại diện cho người dùng
class AccountUser {
    String fullName;
    String accountID;
    String pinCode;
    double balance;
    Date createdAt;
    String membershipType;

    AccountUser(String fullName, String accountID, String pinCode, double balance, Date createdAt) {
        this.fullName = fullName;
        this.accountID = accountID;
        this.pinCode = pinCode;
        this.balance = balance;
        this.createdAt = createdAt;
        
        // Phân loại khách hàng dựa trên số dư ban đầu
        if (balance > 100000) {
            this.membershipType = "VIP";
        } else if (balance > 20000) {
            this.membershipType = "DOANH_NGHIEP";
        } else {
            this.membershipType = "CA_NHAN";
        }
    }
}

// Lớp lưu trữ lịch sử giao dịch
class HistoryLog {
    String accountID;
    String action;
    double amount;
    Date timestamp;
    String note;

    HistoryLog(String accountID, String action, double amount, Date timestamp, String note) {
        this.accountID = accountID;
        this.action = action;
        this.amount = amount;
        this.timestamp = timestamp;
        this.note = note;
    }

    @Override
    public String toString() {
        return String.format("[%s] Tài khoản: %s | Hành động: %s | Số tiền: %.2f | Ghi chú: %s", 
                timestamp, accountID, action, amount, note);
    }
}

2. Triển khai các chức năng nghiệp vụ

Lớp BankManager sẽ đóng vai trò điều khiển chính, chứa danh sách tài khoản và các phương thức xử lý logic.

class BankManager {
    private Map<String, AccountUser> userMap = new HashMap<>();
    private List<HistoryLog> logs = new ArrayList<>();

    // Chức năng mở tài khoản mới
    public void registerAccount(String name, String id, String pin, double initialAmount) {
        if (userMap.containsKey(id)) {
            System.out.println("Lỗi: Mã tài khoản đã tồn tại trên hệ thống.");
            return;
        }
        AccountUser newUser = new AccountUser(name, id, pin, initialAmount, new Date());
        userMap.put(id, newUser);
        logs.add(new HistoryLog(id, "MO_TAI_KHOAN", initialAmount, new Date(), "Khởi tạo thành công"));
        System.out.println("Đăng ký thành công khách hàng: " + name + " - Loại: " + newUser.membershipType);
    }

    // Chức năng nạp tiền
    public void addFunds(String id, double amount) {
        AccountUser user = userMap.get(id);
        if (user != null) {
            user.balance += amount;
            logs.add(new HistoryLog(id, "NAP_TIEN", amount, new Date(), "Nạp tiền mặt"));
            System.out.println("Giao dịch thành công. Số dư mới: " + user.balance);
        }
    }

    // Chức năng rút tiền với quy đổi ngoại tệ
    public void withdrawFunds(String id, double amount, String currency) {
        AccountUser user = userMap.get(id);
        if (user == null) return;

        double finalAmount = amount;
        // Logic quy đổi đơn giản (Tỉ giá giả định)
        if (currency.equalsIgnoreCase("USD")) {
            finalAmount = amount * 24000; 
        } else if (currency.equalsIgnoreCase("EUR")) {
            finalAmount = amount * 26000;
        }

        if (user.balance >= finalAmount) {
            user.balance -= finalAmount;
            logs.add(new HistoryLog(id, "RUT_TIEN", finalAmount, new Date(), "Rút bằng " + currency));
            System.out.println("Đã rút: " + finalAmount + " VND. Số dư còn lại: " + user.balance);
        } else {
            System.out.println("Tài khoản không đủ số dư.");
        }
    }

    // Chức năng chuyển khoản
    public void executeTransfer(String senderId, String receiverId, double amount, String pin) {
        AccountUser sender = userMap.get(senderId);
        AccountUser receiver = userMap.get(receiverId);

        if (sender == null || receiver == null || !sender.pinCode.equals(pin)) {
            System.out.println("Xác thực thất bại hoặc tài khoản không tồn tại.");
            return;
        }

        double fee = 0;
        // Kiểm tra chuyển khoản liên ngân hàng (Giả định mã bắt đầu khác nhau là khác ngân hàng)
        boolean isExternal = !senderId.substring(0, 2).equals(receiverId.substring(0, 2));
        if (isExternal) {
            fee = amount * 0.01; // Phí 1% cho liên ngân hàng
        }

        if (sender.balance >= (amount + fee)) {
            sender.balance -= (amount + fee);
            receiver.balance += amount;
            logs.add(new HistoryLog(senderId, "CHUYEN_KHOAN", amount, new Date(), "Đến: " + receiverId + " | Phí: " + fee));
            System.out.println("Chuyển tiền hoàn tất.");
        } else {
            System.out.println("Số dư không đủ chi trả cả phí giao dịch.");
        }
    }

    // Truy vấn thông tin
    public void showAccountInfo(String id, String pin) {
        AccountUser user = userMap.get(id);
        if (user != null && user.pinCode.equals(pin)) {
            System.out.println("--- THÔNG TIN TÀI KHOẢN ---");
            System.out.println("Chủ thẻ: " + user.fullName);
            System.out.println("Số dư: " + user.balance + " VND");
            System.out.println("Hạng: " + user.membershipType);
            System.out.println("Lịch sử giao dịch:");
            for (HistoryLog log : logs) {
                if (log.accountID.equals(id)) {
                    System.out.println(log);
                }
            }
        }
    }
}

3. Giao diện điều khiển ứng dụng

Sử dụng Scanner để tạo menu tương tác cho phép người dùng thực hiện các thao tác từ bàn phím.

public class BankingApp {
    public static void main(String[] args) {
        BankManager bank = new BankManager();
        Scanner sc = new Scanner(System.in);
        boolean isRunning = true;

        while (isRunning) {
            System.out.println("\n--- HỆ THỐNG NGÂN HÀNG ---");
            System.out.println("1. Mở tài khoản");
            System.out.println("2. Nạp tiền");
            System.out.println("3. Rút tiền");
            System.out.println("4. Chuyển khoản");
            System.out.println("5. Kiểm tra số dư");
            System.out.println("6. Thoát");
            System.out.print("Chọn chức năng: ");
            
            int cmd = sc.nextInt();
            switch (cmd) {
                case 1:
                    System.out.print("Tên khách hàng: ");
                    String name = sc.next();
                    System.out.print("Mã số (Ví dụ: 10... cho nội bộ): ");
                    String id = sc.next();
                    System.out.print("Mật khẩu: ");
                    String pin = sc.next();
                    System.out.print("Số dư ban đầu: ");
                    double init = sc.nextDouble();
                    bank.registerAccount(name, id, pin, init);
                    break;
                case 2:
                    System.out.print("Nhập mã TK: ");
                    String depositId = sc.next();
                    System.out.print("Số tiền nạp: ");
                    double money = sc.nextDouble();
                    bank.addFunds(depositId, money);
                    break;
                case 3:
                    System.out.print("Nhập mã TK: ");
                    String wId = sc.next();
                    System.out.print("Số tiền: ");
                    double wAmount = sc.nextDouble();
                    System.out.print("Loại tiền (VND/USD/EUR): ");
                    String cur = sc.next();
                    bank.withdrawFunds(wId, wAmount, cur);
                    break;
                case 4:
                    System.out.print("TK nguồn: ");
                    String from = sc.next();
                    System.out.print("TK đích: ");
                    String to = sc.next();
                    System.out.print("Số tiền: ");
                    double amount = sc.nextDouble();
                    System.out.print("Mật khẩu: ");
                    String p = sc.next();
                    bank.executeTransfer(from, to, amount, p);
                    break;
                case 5:
                    System.out.print("Nhập mã TK: ");
                    String checkId = sc.next();
                    System.out.print("Mật khẩu: ");
                    String checkPin = sc.next();
                    bank.showAccountInfo(checkId, checkPin);
                    break;
                case 6:
                    isRunning = false;
                    System.out.println("Cảm ơn quý khách!");
                    break;
                default:
                    System.out.println("Lựa chọn không hợp lệ.");
            }
        }
        sc.close();
    }
}

Hệ thống trên cung cấp một khung sườn vững chắc cho các ứng dụng quản lý tài chính phức tạp hơn. Bạn có thể mở rộng bằng cách thêm tính năng tính lãi suất hàng tháng, lưu trữ dữ liệu vào cơ sở dữ liệu SQL, hoặc xây dựng giao diện đồ họa (GUI) bằng Java Swing hoặc JavaFX.

Thẻ: Java oop BankingSystem map list

Đăng vào ngày 21 tháng 6 lúc 06:32