Skip to main content
eick-at moodle
  • Home
  • Calendar
  • More
You are currently using guest access
Log in
eick-at moodle
Home Calendar
Expand all Collapse all
  1. OOP with JAVA
  2. Inheritance
  3. Task: Pocket money account

Task: Pocket money account

Completion requirements
Opened: Sunday, 12 May 2019, 12:00 AM
Due: Sunday, 12 May 2019, 12:00 AM

You are working for a financial institution and want to revise the following program:

 1 public class Account {
 2     private int accountNumber;
 3     private double balance;
 4     private Customer customer;  // holds only one owner of account
 5 
 6     private static int cntAccountNumber = 100000;
 7 
 8     public Account() {
 9         accountNumber = ++cntAccountNumber;
10         balance = 0;
11     }
12     public Account(double amount, Customer customer) {
13         this();
14         this.customer = customer;
15         this.customer.addAccount(this);
16         balance = amount;
17     }
18     
19     public int getAccountNumber() {
20         return accountNumber;
21     }
22     public int transaction(double amount) {
23         balance += amount;
24         return 0;
25     }
26     public double getBalance() {
27         return balance;
28     }
29     public Customer getCustomer() {
30         return customer;
31     }
32     public void setCustomer(Customer customer) {
33         this.customer = customer;
34     }
35 }

 1 import java.util.ArrayList;
 2 
 3 public class Customer {
 4 
 5     private String name;
 6     private int customerID;
 7     private ArrayList<Account> accounts = new ArrayList<>();     // List with accounts
 8     private static int cntCustomerID = 400000;
 9 
10     Customer() {                 // no-arg constructor
11         customerID = ++cntCustomerID;
12         name = "noname";
13     }
14     Customer(String name) {      // parameterised constructor
15         this();                  // invokes no-arg constructor Customer()
16         this.name = name;
17     }
18 
19     public int getCustomerID() {
20         return customerID;
21     }
22     public void setName(String name) {
23         this.name = name;
24     }
25     public String getName() {
26         return name;
27     }
28     public void addAccount(Account account) {
29         accounts.add(account);
30     }
31     public ArrayList<Account> getAccounts() {
32         return accounts;
33     }
34 }

 1 import java.util.ArrayList;
 2 import java.util.Arrays;
 3 
 4 public class Main {
 5 
 6     public static void main(String[] args) {
 7         ArrayList<Customer> customers = new ArrayList<>(Arrays.asList(new Customer("Father"), new Customer("Child")));
 8         ArrayList<Account> accounts = new ArrayList<>(Arrays.asList(new Account(50, customers.get(0)))));
 9 
10         for (Customer customer : customers) {
11             System.out.println("customerID=" + customer.getCustomerID() + "\tName=" + customer.getName() + ":");
12             ArrayList<Account> accountsOfCustomer = customer.getAccounts();  // navigates from customer to account
13             for (Account account : accountsOfCustomer) {
14                 System.out.println("\taccount number=" + account.getAccountNumber() + "\tbalance=" + account.getBalance() + ":");
15                 if (account.transaction(-100) == 0)
16                     System.out.println("Transaction OK");
17                 else
18                     System.out.println("Transaction not allowed. Balance would be negative.");
19             }
20         }
21     }
22 }

The marketing department now wants to introduce pocket money accounts in order to bind potential customers early. Pocket money accounts differ from the previous bank accounts by they can not be overdrawn and there must be a parent or guardian 2nd customer.

Expand the program that pocket money accounts can also be managed. Draw the UML class diagram!



You are currently using guest access (Log in)
Data retention summary
Get the mobile app
Powered by Moodle