สร้างแอปพลิเคชันคอนโซลระบบธนาคารใน Java

Feb 19 2023
ปรับปรุงความรู้ OOP ของคุณผ่านโครงการที่ใช้งานได้จริงในภาษาจาวา วันนี้เราจะพัฒนา java application อย่างง่ายสำหรับระบบธนาคาร

ปรับปรุงความรู้ OOP ของคุณผ่านโครงการที่ใช้งานได้จริงในภาษาจาวา

ภาพถ่ายโดย Tima Miroshnichenko จาก Pexels

วันนี้เราจะพัฒนา java application อย่างง่ายสำหรับระบบธนาคาร เราจะใช้เวลามากขึ้นในการทำความเข้าใจแนวคิดการเขียนโปรแกรมเชิงวัตถุ

ในการเริ่มต้น เราจะต้องติดตั้งสภาพแวดล้อมจาวาบนคอมพิวเตอร์ โดยเฉพาะอย่างยิ่งจาวา 11 หากคุณยังไม่ได้ติดตั้งจาวาบนสภาพแวดล้อมภายในเครื่องของคุณ โปรดทำตามขั้นตอนเหล่านี้เพื่อเตรียมพร้อม

ต่อไป เราจะเริ่มด้วยรายละเอียดการทำงานของคอนโซลแอ็พพลิเคชัน

ฟังก์ชันการทำงาน:

  • การสร้างบัญชี
  • เข้าสู่ระบบ, ออกจากระบบ,
  • แสดงธุรกรรม 5 รายการล่าสุด
  • ฝากเงิน
  • แสดงข้อมูลผู้ใช้ปัจจุบัน
  • มรดก
  • ความหลากหลาย
  • การห่อหุ้ม

เริ่มพัฒนาแอปพลิเคชันของคุณ

สร้างโปรเจ็กต์จาวาใหม่ใน Eclipse หรือ Intellij

มากำหนดอินเทอร์เฟซใหม่ที่ชื่อว่าบัญชีออมทรัพย์

public interface SavingsAccount {

    void deposit(double amount, Date date);
}

ดังนั้น การใช้งานเมธอดสามารถพบได้ในคลาสลูกค้าโดยการแทนที่เมธอดด้วยชื่อและพารามิเตอร์เดียวกัน เมื่อคุณแทนที่เมธอดจากอินเทอร์เฟซพาเรนต์ในคลาสย่อย ดังตัวอย่างที่กำหนด บริบทได้รับจากข้อเท็จจริงที่ว่าในการเพิ่มเงินในบัญชีปัจจุบัน เราต้องการลูกค้าก่อน

มากำหนดคลาสลูกค้า ของเรากันเถอะ

public class Customer extends Person implements SavingsAccount {

    private String username;

    private String password;

    private double balance;

    private ArrayList<String> transactions = new ArrayList<>(5);

    public Customer(String firstName, String lastName, String address, String phone, String username, String password, double balance, ArrayList<String> transactions, Date date) {
        super(firstName, lastName, address, phone);
        this.username = username;
        this.password = password;
        this.balance = balance;
        addTransaction(String.format("Initial deposit - " + NumberFormat.getCurrencyInstance().format(balance) + " as on " + "%1$tD" + " at " + "%1$tT.", date));
    }

    private void addTransaction(String message) {

        transactions.add(0, message);
        if (transactions.size() > 5) {
            transactions.remove(5);
            transactions.trimToSize();
        }
    }

//Getter Setter
   
    public ArrayList<String> getTransactions() {
        return transactions;
    }

    @Override
    public void deposit(double amount, Date date) {

        balance += amount;
        addTransaction(String.format(NumberFormat.getCurrencyInstance().format(amount) + " credited to your account. Balance - " + NumberFormat.getCurrencyInstance().format(balance) + " as on " + "%1$tD" + " at " + "%1$tT.", date));
    }

    @Override
    public String toString() {
        return "Customer{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", balance=" + balance +
                ", transactions=" + transactions +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Customer customer = (Customer) o;

        if (Double.compare(customer.getBalance(), getBalance()) != 0) return false;
        if (getUsername() != null ? !getUsername().equals(customer.getUsername()) : customer.getUsername() != null)
            return false;
        if (getPassword() != null ? !getPassword().equals(customer.getPassword()) : customer.getPassword() != null)
            return false;
        return getTransactions() != null ? getTransactions().equals(customer.getTransactions()) : customer.getTransactions() == null;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = getUsername() != null ? getUsername().hashCode() : 0;
        result = 31 * result + (getPassword() != null ? getPassword().hashCode() : 0);
        temp = Double.doubleToLongBits(getBalance());
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        result = 31 * result + (getTransactions() != null ? getTransactions().hashCode() : 0);
        return result;
    }

ด้วยการใช้อินเทอร์เฟซ SavingsAccountเราต้องแทนที่วิธีการฝากเงินในคลาสลูกค้า เนื่องจากเราจะเขียนการใช้งานเมธอดในคลาสนั้น

นอกจากนี้ รายการธุรกรรมจะถูกเตรียมใช้งานเพื่อแสดงธุรกรรม 5 รายการล่าสุด วิธี การaddTransactionถูกเรียกในตัวสร้างซึ่งแสดงวันที่ของการเปลี่ยนแปลงและธุรกรรมที่ดำเนินการ

private void addTransaction(String message) {

        transactions.add(0, message);
        if (transactions.size() > 5) {
            transactions.remove(5);
            transactions.trimToSize();
        }
    }

public class Person {

    private String firstName;

    private String lastName;

    private String address;

    private String phone;

    public Person() {}

    public Person(String firstName, String lastName, String address, String phone) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.address = address;
        this.phone = phone;
    }

//Getters Setters

 @Override
    public String toString() {
        return "Person{" +
                "firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", address='" + address + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }

@Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Person person = (Person) o;

        if (getFirstName() != null ? !getFirstName().equals(person.getFirstName()) : person.getFirstName() != null)
            return false;
        if (getLastName() != null ? !getLastName().equals(person.getLastName()) : person.getLastName() != null)
            return false;
        if (getAddress() != null ? !getAddress().equals(person.getAddress()) : person.getAddress() != null)
            return false;
        return getPhone() != null ? getPhone().equals(person.getPhone()) : person.getPhone() == null;
    }

    @Override
    public int hashCode() {
        int result = getFirstName() != null ? getFirstName().hashCode() : 0;
        result = 31 * result + (getLastName() != null ? getLastName().hashCode() : 0);
        result = 31 * result + (getAddress() != null ? getAddress().hashCode() : 0);
        result = 31 * result + (getPhone() != null ? getPhone().hashCode() : 0);
        return result;
    }

ในที่สุด คลาสหลักของเราชื่อคลาสBank

นี่คือคลาสหลักที่เราเริ่มต้นแอปพลิเคชันและโต้ตอบกับฟังก์ชันการทำงานของทุกคลาส

public class Bank {

    private static double amount = 0;
    Map<String, Customer> customerMap;

    Bank() {
        customerMap = new HashMap<String, Customer>();
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        Customer customer;
        Bank bank = new Bank();
        int choice;
        outer:
        while (true) {

            System.out.println("\n-------------------");
            System.out.println("BANK    OF     JAVA");
            System.out.println("-------------------\n");
            System.out.println("1. Registrar cont.");
            System.out.println("2. Login.");
            System.out.println("3. Exit.");
            System.out.print("\nEnter your choice : ");
            choice = sc.nextInt();
            sc.nextLine();
            switch (choice) {
                case 1:
                    System.out.print("Enter First Name : ");
                    String firstName = sc.nextLine();
                    System.out.print("Enter Last Name : ");
                    String lastName = sc.nextLine();
                    System.out.print("Enter Address : ");
                    String address = sc.nextLine();
                    System.out.print("Enter contact number : ");
                    String phone = sc.nextLine();
                    System.out.println("Set Username : ");
                    String username = sc.next();
                    while (bank.customerMap.containsKey(username)) {
                        System.out.println("Username already exists. Set again : ");
                        username = sc.next();
                    }
                    System.out.println("Set a password:");
                    String password = sc.next();
                    sc.nextLine();

                    customer = new Customer(firstName, lastName, address, phone, username, password, new Date());
                    bank.customerMap.put(username, customer);
                    break;

                case 2:
                    System.out.println("Enter username : ");
                    username = sc.next();
                    sc.nextLine();
                    System.out.println("Enter password : ");
                    password = sc.next();
                    sc.nextLine();
                    if (bank.customerMap.containsKey(username)) {
                        customer = bank.customerMap.get(username);
                        if (customer.getPassword().equals(password)) {
                            while (true) {
                                System.out.println("\n-------------------");
                                System.out.println("W  E  L  C  O  M  E");
                                System.out.println("-------------------\n");
                                System.out.println("1. Deposit.");
                                System.out.println("2. Transfer.");
                                System.out.println("3. Last 5 transactions.");
                                System.out.println("4. User information.");
                                System.out.println("5. Log out.");
                                System.out.print("\nEnter your choice : ");
                                choice = sc.nextInt();
                                sc.nextLine();
                                switch (choice) {
                                    case 1:
                                        System.out.print("Enter amount : ");
                                        while (!sc.hasNextDouble()) {
                                            System.out.println("Invalid amount. Enter again :");
                                            sc.nextLine();
                                        }
                                        amount = sc.nextDouble();
                                        sc.nextLine();
                                        customer.deposit(amount, new Date());
                                        break;

                                    case 2:
                                        System.out.print("Enter beneficiary username : ");
                                        username = sc.next();
                                        sc.nextLine();
                                        System.out.println("Enter amount : ");
                                        while (!sc.hasNextDouble()) {
                                            System.out.println("Invalid amount. Enter again :");
                                            sc.nextLine();
                                        }
                                        amount = sc.nextDouble();
                                        sc.nextLine();
                                        if (amount > 300) {
                                            System.out.println("Transfer limit exceeded. Contact bank manager.");
                                            break;
                                        }
                                        if (bank.customerMap.containsKey(username)) {
                                            Customer payee = bank.customerMap.get(username); //Todo: check
                                            payee.deposit(amount, new Date());
                                            customer.withdraw(amount, new Date());
                                        } else {
                                            System.out.println("Username doesn't exist.");
                                        }
                                        break;

                                    case 3:
                                        for (String transactions : customer.getTransactions()) {
                                            System.out.println(transactions);
                                        }
                                        break;

                                    case 4:
                                        System.out.println("Titularul de cont cu numele: " + customer.getFirstName());
                                        System.out.println("Titularul de cont cu prenumele : " + customer.getLastName());
                                        System.out.println("Titularul de cont cu numele de utilizator : " + customer.getUsername());
                                        System.out.println("Titularul de cont cu addresa : " + customer.getAddress());
                                        System.out.println("Titularul de cont cu numarul de telefon : " + customer.getPhone());
                                        break;
                                    case 5:
                                        continue outer;
                                    default:
                                        System.out.println("Wrong choice !");
                                }
                            }
                        } else {
                            System.out.println("Wrong username/password.");
                        }
                    } else {
                        System.out.println("Wrong username/password.");
                    }
                    break;

                case 3:
                    System.out.println("\nThank you for choosing Bank Of Java.");
                    System.exit(1);
                    break;
                default:
                    System.out.println("Wrong choice !");
            }}}}

ด้านล่างเรามีตัวสร้างใหม่ที่บันทึกแผนที่ ข้อมูลลูกค้า Map.putใช้เพื่อบันทึกหรืออัปเดตข้อมูลลูกค้า

customer = new Customer(firstName, lastName, address, phone, username, password, new Date());
bank.customerMap.put(username, customer);

กรณีที่ 1: เราเพิ่มเงินในบัญชีกระแสรายวัน

กรณีที่ 2: เราแสดงธุรกรรม 5 รายการล่าสุด

กรณีที่ 3: เราแสดงข้อมูลของลูกค้าจากแผนที่ในคอนโซล

กรณีที่ 4: เราปิดเมนู

ซอร์สโค้ดที่นี่

ความคิดสุดท้าย

ฉันหวังว่าตัวอย่างนี้จะเป็นประโยชน์ในการช่วยให้คุณคุ้นเคยกับการใช้แนวคิด OOP ใน Java ดังนั้น การฝึกแบบฝึกหัดเป็นสิ่งสำคัญที่จะช่วยให้คุณคิดอย่างมีเหตุผล

คอยติดตามข้อมูลเพิ่มเติมเกี่ยวกับหัวข้อนี้!

ขอบคุณที่อ่าน ขอให้มีความสุขกับการเรียนรู้

สนับสนุนสิ่งพิมพ์ของเราโดยติดตาม

วิธีเรียนรู้ Spring Boot ใน 2 เดือน Intellij IDEA ทำให้พีซีของคุณร้อนเกินไป ใช้ Eclipse แทน