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

时间:2014-09-20 18:35:37

标签: java

我在java中制作银行程序并有5个类: 帐户,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;
   }
}

帐户类:

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类:( 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
    }
}

客户类:

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;
    }            
}

主:

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);
}

2 个答案:

答案 0 :(得分:6)

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

这样的东西
public class Customer {
    private String firstName;
    private String lastName;
    private String number;

    private List<Account> accounts;

您的帐户界面可能如

interface Account {

    public void deposit(double amount); 

    public void withdraw(double amount);

    public double getBalance();

    public String getAccountId();
}

您现有的课程被重构

 public abstract AbstractAccount implements Account {
     ....
 }

您的SavingAccount变为

 public SavingAccount extends AbstractAccount {
  ....
 }

我有点担心这个方法在Account类上被声明为abstract。为什么转移会在两个不同的类别上以不同的方式实现?

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

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

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

然后,该类可以检查'fromAccount'是否具有可用于在实际转移发生之前作为验证步骤进行转移的资金。

答案 1 :(得分:1)

而不是访问客户的对象构造函数SavingsAccount或CreditAccount, 你可以使用:

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

并将set方法添加到帐户和cAccount:

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

当然你可以像上一条评论一样创建一个列表:

并添加方法:

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