Solution: Direction of navigation
Completion requirements
1 import java.util.ArrayList; 2 3 public class Customer { 4 5 //fields 6 private String name; 7 private int customerID; 8 private ArrayList<Account> accounts = new ArrayList<Account>(); 9 private static int cntCustomerID = 1000000; 10 11 //constructor 12 public Customer() { 13 customerID = cntCustomerID++; 14 } 15 16 //methods 17 public int getCustomerID() { 18 return customerID; 19 } 20 public void setName(String name) { 21 this.name = name; 22 } 23 public String getName() { 24 return name; 25 } 26 public void addAccount(Account account) { 27 accounts.add(account); 28 } 29 public ArrayList<Account> getAccounts() { 30 return accounts; 31 } 32 }
1 import java.util.ArrayList; 2 3 public class Account { 4 private int accountNumber; 5 private float balance; 6 private ArrayList<Customer> customers = new ArrayList<>(); // an account holds more than one authorized customers 7 8 private static int cntAccountNumber = 10001000; 9 10 public Account() { 11 accountNumber = cntAccountNumber++; 12 } 13 public int getAccountNumber() { 14 return accountNumber; 15 } 16 public void transaction(float sum) { 17 balance += sum; 18 } 19 public float getBalance() { 20 return balance; 21 } 22 23 public ArrayList<Customer> getCustomers() { 24 return customers; 25 } 26 public void addCustomer(Customer customer) { 27 customers.add(customer); 28 } 29 }
1 import java.util.ArrayList; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 Customer customer1 = new Customer(); 7 customer1.setName("Mueller"); 8 Customer customer2 = new Customer(); 9 customer2.setName("Meier"); 10 11 Account account1 = new Account(); 12 Account account2 = new Account(); 13 customer1.addAccount(account1); 14 customer1.addAccount(account2); 15 account1.addCustomer(customer1); 16 account1.addCustomer(customer2); 17 18 account2.addCustomer(customer1); 19 20 account1.transaction(100); 21 account2.transaction(-20); 22 23 System.out.println("customer id="+customer1.getCustomerID()+"\tName="+customer1.getName()+":"); 24 float sum = 0; 25 ArrayList<Account> accounts = customer1.getAccounts(); // navigates from customer to account 26 for (Account account: accounts) { 27 System.out.println("\taccount number="+account.getAccountNumber()+"\tbalance="+account.getBalance()+":"); 28 ArrayList<Customer> customers = account.getCustomers(); // navigates from account to customer 29 for (Customer customer: customers) { 30 System.out.println("\tauthorized customerID="+customer.getCustomerID()+"\tname="+customer.getName()+":"); 31 } 32 sum+=account.getBalance(); 33 } 34 System.out.print("\n\ttotal sum="+sum); 35 } 36 }

Last modified: Saturday, 18 May 2019, 12:50 PM