You are on page 1of 3

CS 212 Object-Oriented ProgrammingFall 2013

Lab Assignment 4
You will specifically apply:
arrays
ArrayList with and without Generics
traversing an ArrayList using:
o for loop
o Iterator
o for each loop (in case of Generics)
Implement the following changes within the existing classes.
Please try to adhere to the specified names.
class Account:
The class Account must keep track of the last 5 transactions.

Insert a getter methods for the fields number and client.


Add a new field of type String[] named transactions and a field
transactionNumberof type int.
Modify both constructors: transactionsis instantiated (call to new with
specific syntax for arrays) with an array of length 5. transactionNumber is
initialized to 0.
Implement a method insertTransaction():
o this method receives a parameter of type String
o if transactionNumber<arraylength
the String is written in the array at position
transactionNumber1
o otherwise
in a for loop the elements at index 1 to (arraylength-1) of the
array are copied to the 1st(index 0) to (arraylenth-1)th(index
arraylength 2) position.
the String is written at index arraylength-1 of the array
Implement a method printTransactions: all Strings in the array are printed
using a for each loop
Modify the takeOff() Method. If the money can be taken off,
o the transactionNumber is incremented
o a String transString is created that looks like the following: Take
off:100newBalance:300 (taking the sum as well as the new
balance)
o the method insertTransaction() is called with transString as
parameter
Modify the payIn() method.
o the transactionNumber is incremented
o a String transString is created that looks like the following: PayIn:
100newBalance:400. (taking the sum as well as the new balance)
o the method insertTransaction() is called with transString as
parameter.

CS 212

German Jordanian University

Page 1 of 3

CS 212 Object-Oriented ProgrammingFall 2013

class Client:
The client is now allowed to have more than one account. We use an ArrayList with
Generics.

Modify the field account. Its name is now accounts and it is of the type
ArrayList<Account>
Add a new field accountNumber (of type int).
The ArrayList is instantiated in the constructor. accountNumber is initialized to
1.
Modify the method newAccount(). Its now creates a new account (using
accountNumber) and adds it to accounts. Then accountNumber is
incremented.
Modify the method getAccount(): it has now an int parameter specifying the
index of the account. If an account with this index exists, it is returned.
Otherwise null is returned.
Modify the method printClerkPhoneNumber(): it prints for every account in
accounts:
o the accountNumber
o and the name and phone number of the clerk or no clerk for this
account
Use a for each loop.
Modify the method printBalance(): it prints for every account in accounts:
o the accountNumber
o the balance of this account
Use the Iterator.
Modify the methods payIn(), takeOff()and setClerk(). They receive an
additional parameter denoting the index of the account to be used. If the
index is not valid, the first account (index 0) is used if an account exists at all.

class Clerk:
A clerk should know for which accounts he is responsible. This will be implemented
without Generics.

add a field of type ArrayList called accounts.


theArrayList is instantiated in the constructor.
write a method addAccount() that receives the account the client is now
responsible for
write a method printAllAccountsFor() that prints all accounts (account
number, balance, last transactions and name of the client) the clerk is
responsible for. Use a for loop.

CS 212

German Jordanian University

Page 2 of 3

CS 212 Object-Oriented ProgrammingFall 2013


write a method printAllAccountsIterator() that prints all accounts
(account number, balance, last transactions and name of the client) the clerk
is responsible for. Use the Iterator.
Modification in class Account:

in the method setClerk(): call the method addAccount of the clerk that is now
responsible for the account.
Note: The list of accounts in the Clerk class might contain accounts twice or more
often and also contain accounts, the client is no longer responsible for. We will
repair this in a future lab.

CS 212

German Jordanian University

Page 3 of 3

You might also like