Java Bank计划.如何让客户拥有多个账户?

use*_*293 2 java

我正在java中创建一个银行程序,有5个类:Account,SavingsAccount(继承帐户),CreditAccount(继承帐户),银行,客户.

该程序按原样运行,但我无法弄清楚如何让客户拥有两个或更多帐户.让我们说一个客户想要一个信用账户和一个储蓄账户,或者两个储蓄账户.

谁能给我一些建议?谢谢

银行类:

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;
   }
}
Run Code Online (Sandbox Code Playgroud)

帐户类:

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);
}
Run Code Online (Sandbox Code Playgroud)

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
    }
}
Run Code Online (Sandbox Code Playgroud)

客户类:

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;
    }            
}
Run Code Online (Sandbox Code Playgroud)

主要:

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);
}
Run Code Online (Sandbox Code Playgroud)

eme*_*ava 6

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

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

    private List<Account> accounts;
Run Code Online (Sandbox Code Playgroud)

您的帐户界面可能如下所示

interface Account {

    public void deposit(double amount); 

    public void withdraw(double amount);

    public double getBalance();

    public String getAccountId();
}
Run Code Online (Sandbox Code Playgroud)

您现有的类会被重构

 public abstract AbstractAccount implements Account {
     ....
 }
Run Code Online (Sandbox Code Playgroud)

您的SavingAccount变为

 public SavingAccount extends AbstractAccount {
  ....
 }
Run Code Online (Sandbox Code Playgroud)

我有点担心这个方法在Account类上声明为抽象.为什么在两个不同的类别上实现不同的转移?

 public abstract void transfer(double amount, Account account);
Run Code Online (Sandbox Code Playgroud)

我建议这种方法更多地属于AccountManager类,这将确保从两个帐户中记入/记入正确的金额.

 public void transfer(double amount, Account fromAccount, Account toAccount);
Run Code Online (Sandbox Code Playgroud)

然后,该类可以检查'fromAccount'是否具有可用于在实际传输发生之前作为验证步骤传输的所需资金.