Solution: Role names
Completion requirements
The UML use case diagram requires double navigation directions:

Role names with double navigation direction
1 import java.util.ArrayList; 2 3 public class Account { 4 private int accountNumber; 5 private double balance; 6 private Customer owner; // account has 1 owner 7 private ArrayList<Customer> authorizedPersons; // account has * authorizedPersons 8 9 public Account(int accountNumber, double balance) 10 { 11 this.accountNumber = accountNumber; 12 this.balance = balance; 13 authorizedPersons = new ArrayList<>(); // creation of object is still nessecary 14 } 15 public void addAuthorizedCustomer(Customer customer) 16 { 17 authorizedPersons.add(customer); 18 } 19 20 public Customer getOwner() { 21 return owner; 22 } 23 public void setOwner(Customer owner) { 24 this.owner = owner; 25 } 26 public int getAccountNumber() { 27 return accountNumber; 28 } 29 30 public ArrayList<Customer> getAuthorizedPersons() { 31 return authorizedPersons; 32 } 33 }
1 import java.util.ArrayList; 2 3 public class Customer { 4 private int CustomerID; 5 private String Name; 6 private ArrayList<Account> ownedAccounts; // customer owns * accounts 7 private ArrayList<Account> authorizedAccounts; // customer owns * authorized accounts 8 9 public Customer(int nr, String name) 10 { 11 CustomerID = nr; 12 Name = name; 13 ownedAccounts = new ArrayList<Account>(); 14 authorizedAccounts = new ArrayList<Account>(); 15 } 16 17 public void addOwnedAccount(Account account) 18 { 19 ownedAccounts.add(account); 20 account.setOwner(this); 21 } 22 public void addAuthorizedAccount(Account account) 23 { 24 authorizedAccounts.add(account); 25 account.addAuthorizedCustomer(this); 26 } 27 28 public int getCustomerID() { 29 return CustomerID; 30 } 31 public String getName() { 32 return Name; 33 } 34 public ArrayList<Account> getOwnedAccounts() { 35 return ownedAccounts; 36 } 37 public ArrayList<Account> getAuthorizedAccounts() { 38 return authorizedAccounts; 39 } 40 }
1 public class Main { 2 3 public static void main(String[] args) { 4 Customer c1 = new Customer(1, "Owner A"); 5 Customer c2 = new Customer(2, "Authorized person B"); 6 Customer c3 = new Customer(3, "Authorized person C"); 7 8 Account account1 = new Account(1, 50); 9 Account account2 = new Account(2, 100); 10 11 c1.addOwnedAccount(account1); 12 c2.addAuthorizedAccount(account1); 13 c3.addAuthorizedAccount(account1); 14 15 c1.addAuthorizedAccount(account2); 16 17 // Navigation from customer to account 18 System.out.println(c1.getName() + " is owner of "); 19 for (Account account: c1.getOwnedAccounts()) 20 { 21 System.out.println("account number " + account.getAccountNumber()); 22 } 23 System.out.println("and an authorized person of "); 24 for (Account account: c1.getAuthorizedAccounts()) 25 { 26 System.out.println("account number " + account.getAccountNumber()); 27 } 28 29 // Navigation from account to customer 30 System.out.println("\nOwner of account number " + account1.getAccountNumber() + " is "); 31 System.out.println(account1.getOwner().getName() + " with customer ID " + account1.getOwner().getCustomerID()); 32 System.out.println("and autorized persons are "); 33 for (Customer customer: account1.getAuthorizedPersons()) 34 { 35 System.out.println(customer.getName() + " with customern ID " + customer.getCustomerID()); 36 } 37 } 38 }
Last modified: Monday, 20 May 2019, 9:01 AM