Java Bank程序。如何让客户拥有多个帐户? - 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);
}

参考方案

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

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类上被声明为抽象方法。为什么在两个不同的类上以不同的方式实现传输?

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

我建议此方法应更多地属于AccountManager类,该类将确保从两个帐户中贷记/借记正确的金额。

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

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

Java:找到特定字符并获取子字符串 - java

我有一个字符串4.9.14_05_29_16_21,我只需要获取4.9。数字各不相同,所以我不能简单地获得此char数组的前三个元素。我必须找到最正确的.并将其子字符串化直到那里。我来自Python,因此我将展示Python的实现方法。def foobar(some_string): location = some_string.rfind('.&…

Java Double与BigDecimal - java

我正在查看一些使用双精度变量来存储(360-359.9998779296875)结果为0.0001220703125的代码。 double变量将其存储为-1.220703125E-4。当我使用BigDecimal时,其存储为0.0001220703125。为什么将它双重存储为-1.220703125E-4? 参考方案 我不会在这里提及精度问题,而只会提及数字…

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

在Map中,如果我们使用现有键进行修改,则不会获得ConcurrentModificationException - java

我有以下代码,我希望从情况2的情况下抛出ConcurrentModificationException,但它运行成功。据我所知,如果我对地图中的单个键执行相同的操作,则不会抛出异常,因为here但是当我重现这种具有两个案例的多个密钥的场景时,通过新密钥修改。通过现有密钥进行修改。情况1: Map<String,String> mp = new H…

对象比较是否相等:JAVA - java

public ClassA { private String firstId; private String secondId; public void setFirstId(String firstId) { this.firstId = firstId; } public String getFirstId() { return id; } public…