这个简单的银行程序是否符合OOP概念(即封装,抽象,多态等)的标准?如果没有,我该怎么做?

我创建了一个简单的银行程序。该程序会询问用户名和起始余额。从那里,用户可以做4件事,检查余额,添加资金,转移资金并退出程序。 Check Balance仅返回用户名以及其帐户中的剩余资金额。添加资金要求用户输入要存入他的帐户的金额。转帐资金要求用户从他的帐户中减少一笔金额。这是一个简单的程序,可帮助您理解对象以及如何操纵它们的字段。您可以尝试重写一种方法来简化该程序的一个功能。

我发布了这段代码,希望引起人们对我的程序是否遵循OOP标准以及对编写方式的任何改进的看法。我正在自我学习Java,并且希望收到任何反馈,以便可以从创建此程序中学习。

import java.util.Scanner;

public class Main {
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        boolean quit = false;
        int choice;
        String accountName;
        double startBalance;

        System.out.printf("%24s\n","BankApp v1.0");
        System.out.println("=====================================");
        System.out.println("Please enter your details below.");
        System.out.println("Account Name: ");
        accountName = scanner.nextLine();
        System.out.println("Starting Balance: ");
        startBalance = scanner.nextDouble();
        Account account = new Account(accountName,startBalance);
        while(!quit){
            printMainMenu();
            choice = scanner.nextInt();
            switch(choice){
                case 1:
                    System.out.printf("Account Name: %s\nAccount Balance: $%.2f\n",account.getAccountName(),
                            account.getAccountBal());
                    break;
                case 2:
                    System.out.println("Enter amount to be added: ");
                    account.addFunds(scanner.nextDouble());
                    break;
                case 3:
                    System.out.println("Enter amount to be transferred: ");
                    account.transferFunds(scanner.nextDouble());
                    break;
                case 4:
                    quit = true;
                    break;
                default:
                    System.out.println("Invalid choice.");
                    break;
            }
        }
        scanner.close();
    }

    public static void printMainMenu(){
        System.out.printf("%24s\n" +
                "=====================================\n" +
                "Please select an option:\n" +
                "1 - Check Balance\n" +
                "2 - Add Funds\n" +
                "3 - Transfer Funds\n" +
                "4 - Exit the program\n" +
                "=====================================\n" +
                "Choice: ","BankApp v1.0");
    }
}

public class Account {
    private String accountName;
    private double accountBal;

    public Account(String accountName, double accountBal) {
        if(accountBal < 0) {
            System.out.println("Starting balance cannot be less than zero.\nBalance set to $0.00");
        }else {
            this.accountName = accountName;
            this.accountBal = accountBal;
            System.out.println("Account initialized.\nBalance set to $" + this.accountBal);
        }
    }

    public String getAccountName() {
        return accountName;
    }

    public double getAccountBal() {
        return accountBal;
    }

    public void transferFunds(double withdrawal){
        if(withdrawal > this.accountBal){
            System.out.printf("Unable to transfer $%.2f. Balance is insufficient.\n",withdrawal);
        }else if(withdrawal < 0){
            System.out.println("Transfer amount must be greater than zero. Transfer failed.");
        }else{
            this.accountBal -= withdrawal;
            System.out.printf("Transfer of $%.2f successful. Your new balance is $%.2f.\n",withdrawal,this.accountBal);
        }
    }

    public void addFunds(double deposit){
        if(deposit < 0){
            System.out.println("Amount deposited must be greater than zero.");
        }else {
            this.accountBal += deposit;
            System.out.printf("Deposit of $%.2f successful. Your new balance is $%.2f.\n",deposit,this.accountBal);
        }
    }
}
评论