Java 银行计划。如何让客户拥有多个账户?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25952086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:35:04  来源:igfitidea点击:

Java Bank program. How to let customer have multiple accounts?

java

提问by user2939293

I'm making a bank program in java and have 5 classes: Account, SavingsAccount (inherits Account), CreditAccount (inherits Account), Bank, Customer.

我正在用 java 制作一个银行程序,有 5 个类:帐户、储蓄帐户(继承帐户)、信用帐户(继承帐户)、银行、客户。

The program is working as it is, but I can't figure out how to make a customer have two or more accounts. Lets say that one customer wants both a credit account AND a savings account, or maybe two savings accounts.

该程序按原样运行,但我不知道如何让客户拥有两个或多个帐户。假设一个客户想要一个信用账户和一个储蓄账户,或者可能是两个储蓄账户。

Can anyone give me some suggestions? Thank you

谁能给我一些建议?谢谢

Bank class:

银行类:

public class Bank {
    String bankName;
    private Customer[] customers = new Customer[100];

    Bank(String bankName) {
        this.bankName = bankName;
    }

    public Customer[] getCustomer() {
        return customers;
    }  

   public String getBankname() {
       return bankName;
   }
}

Account class:

账户类:

public abstract class Account {
    protected double balance = 0;
    protected String accountId;

   public Account() {}  //Defaultkonstruktor

   public Account(double bal, String id) {   //Konstruktor
       if (balance >= 0) {
           balance = bal;
       }
       else {
           balance = 0;
       }
       accountId = id;
   }

   public abstract void deposit(double amount); 

   public abstract void withdraw(double amount);

   public abstract double getBalance();

   public abstract String getAccountId();

   public abstract void transfer(double amount, Account account);
}

SavingsAccount class: (CreditAccount class is similar)

SavingsAccount 类:(CreditAccount 类类似)

public class SavingsAccount extends Account{ 
    private double interest = 2.9;

    public SavingsAccount() {      //Konstruktor
        super();
    }

    public SavingsAccount(double balance, String id) {   //Konstruktor
        super(bal,id);
    }

    public void setInterest(Customer customer) {
      //code
    }

    public void setBalance(double balance) {
       //code
    }

    @Override
    public void deposit(double amount) {
       //code
    }

    @Override
    public void withdraw(double amount) {
       //code
    }

    @Override
    public double getBalance(){
       //code
    }

    @Override
    public String getAccountId(){
       //code
    }

    @Override
    public void transfer(double amount, Account account) {
       //code
    }

    public void setInterest(double interest){
       //code
    }

    public double getInterest(){
      //code
    }
}

Customer class:

客户类别:

public class Customer {
    private String firstName;
    private String lastName;
    private String number;

    private SavingsAccount account = new SavingsAccount();
    private CreditAccount cAccount = new CreditAccount();

    Customer(String firstName, String lastName, String number, SavingsAccount account) {   
        this.firstName = firstName;
        this.lastName = lastName;
        this.number = number;
        this.account = account;
     }

    Customer(String firstName, String lastName, String number, CreditAccount cAccount) {   
        this.firstName = firstName;
        this.lastName = lastName;
        this.number = number;
        this.cAccount = cAccount;
    }

    public SavingsAccount getAccount() {
        return account;
    }

    public CreditAccount getCreditAccount() {
        return cAccount;
    }            
}

Main:

主要的:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int choice;
    int numberOfCustomers = 0;
    boolean endProgram = false;
    String bankName;   
    System.out.print("Name of bank: ");
    bankName = input.next();
    Bank bank = new Bank(bankName);
    String accountId;

    SavingsAccount acc = new SavingsAccount();  
    Customer[] customer = bank.getCustomer();            

    do {        
        System.out.println("    " + bank.getBankname() + "\n");
        System.out.println("    1. See balance                          ");
        System.out.println("    2. Withdraw                             ");
        System.out.println("    3. Deposit                              ");
        System.out.println("    4. Transfer                             ");
        System.out.println("    5. Add interest                         ");
        System.out.println("    6. Add new customer                     ");
        System.out.println("    7. Show customers                       ");
        System.out.println("    8. Change interest                      ");
        System.out.println("    0. Exit                                 ");            

        choice = input.nextInt();

        switch(choice) {

            case 1:  
                //code                        
                break;


            case 2: 
                //code
                break;


            case 3: 
                //code
                break;


            case 4: 
                //code
                break;                   


            case 5: 
                //code
                break;


            case 6: //Add customer                   
                System.out.println("Choose account: ");
                System.out.println("1. Savings account");
                System.out.println("2. Credit account");
                choice = input.nextInt();
                switch(choice) {

                    case 1:     //Create savings account
                        System.out.print("Enter amount to deposit: ");
                        double amount = input.nextDouble();
                        System.out.println("Account number is: " + numberOfCustomers);
                        SavingsAccount savingsAccount = new SavingsAccount(amount, String.valueOf(numberOfCustomers));                    
                        System.out.print("First name: ");
                        String firstName = input.next();
                        System.out.print("Last name: ");
                        String lastName = input.next();
                        System.out.print("Customer number: ");
                        String pnumber = input.next();

                        Customer newCustomer = new Customer(firstName, lastName, pnumber, savingsAccount);
                        customer[numberOfCustomers] = newCustomer;
                        numberOfCustomers++;              

                        break;

                    case 2:     //Create credit account
                        System.out.print("Enter amount to deposit: ");
                        double amount = input.nextDouble();
                        System.out.println("Account number is: " + numberOfCustomers);
                        CreditAccount creditAccount = new CreditAccount(amount, String.valueOf(numberOfCustomers));                    
                        System.out.print("First name: ");
                        String firstName = input.next();
                        System.out.print("Last name: ");
                        String lastName = input.next();
                        System.out.print("Customer number: ");
                        String pnumber = input.next();

                        Customer newCustomer = new Customer(firstName, lastName, pnumber, creditAccount);
                        customer[numberOfCustomers] = newCustomer;
                        numberOfCustomers++;                    

                        break;                            
                }
                break;


            case 7: 
                //code
                break;


            case 8: 
                //code                    
                break;  


            case 0: 
                //code
                break;
        }
    } while (!endProgram);
}

回答by emeraldjava

First off rename Account to AbstractAccount and create an Account interface, your Credit and Saving accounts are concrete implementations. Then in your Customer class declare a variable accounts which is a List accounts. Something like

首先将 Account 重命名为 AbstractAccount 并创建一个 Account 接口,您的 Credit 和 Saving 帐户是具体实现。然后在您的 Customer 类中声明一个变量帐户,它是一个列表帐户。就像是

public class Customer {
    private String firstName;
    private String lastName;
    private String number;

    private List<Account> accounts;

Your Account interface might look like

您的帐户界面可能看起来像

interface Account {

    public void deposit(double amount); 

    public void withdraw(double amount);

    public double getBalance();

    public String getAccountId();
}

Your existing class gets refactored

您现有的类被重构

 public abstract AbstractAccount implements Account {
     ....
 }

Your SavingAccount becomes

您的 SavingAccount 变为

 public SavingAccount extends AbstractAccount {
  ....
 }

I'd be a little worried that this method is declared as abstract on the Account class. Why would a transfer be implemented differently on two different classes?

我有点担心这个方法在 Account 类上被声明为抽象的。为什么在两个不同的类上会以不同的方式实现传输?

 public abstract void transfer(double amount, Account account);

I suggest this method would be belong more to an AccountManager class which would ensure the correct amount is credited/debited from two accounts.

我建议这个方法更多地属于 AccountManager 类,它可以确保从两个帐户中贷记/借记正确的金额。

 public void transfer(double amount, Account fromAccount, Account toAccount);

This class could then check that the 'fromAccount' has the required funds available to transfer as a validation step before the actual transfer happens.

然后,此类可以检查“fromAccount”是否具有可用于转移的所需资金,作为在实际转移发生之前的验证步骤。

回答by tomer

Instead of get to Customer's object constructor SavingsAccount or CreditAccount, you may use:

您可以使用:

Customer(String firstName, String lastName, String number) {    
...
}

And add set method to account and cAccount:

并向 account 和 cAccount 添加 set 方法:

Public void setAccount(SavingAccount account) {
    This.account = account;
}

of course you could create a list like in the previous comment:

当然,您可以像之前的评论一样创建一个列表:

and add method:

并添加方法:

public void addAccount(Account account) {
    accounts.add(account);
}