RHS =>  Allan =>  Basic Programming => Exercises => BorrowerArray Updated:  02/02/2004 23:36
WebMaster: allanhn@rhs.dk

BorrowerArray

You must mail me your solution before Monday the 1st of December - if you're not completely finished then mail me what you have!!

This exercise is no. 3 in the story of Fantasy Library. The previous exercises were:
   Fantasy Library No. 1: Book
   Fantasy Library No. 2: LibraryHits
If you have not done them yet it is about time……..

The mission
You are to develop a program, which can handle collections of borrowers at the Fantasy Library.
Due to the requirement specification the following use cases has been identified for the functionality of
Borrower handling:

No
Use case name
Description
 
1 Borrower Create/Register Register a new borrower.  
2 Borrower Find/Read Search and find a borrower with specific Id.  
3 Borrower Update Search and update a borrower with new data  
4 Borrower Delete Search and delete a borrower with specific Id.  

The Collection Management Component (CMC) holds the class BorrowerCollection as shown below:

 

In the programming phase we start to follow this design and in later Library exercises we shall add more Model- and Collection-classes due more complexity.
For a start we shall just look at the essential functionality of the BorrowerCollection class, as described above.

The objective
We want a program, which can register and keep records of borrowers.

The problem
You are to develop a program, which can handle registrations of the personal borrower data.
For a start we define a simple version of the class Borrower:

Data fields:
String cprNo (format ddmmyy-xxxx)
String name (full name)
int year (year of birth)
String sex (male or female)
String state (status of the borrower: active, passive, illegal)

Constructors:
Borrower(String cprNo, String name)
Borrower(String cprNo, String name, int year, String sex)
Borrower(String cprNo, String name, int year, String sex, String state)

Methods operating on the data fields:
void setCprNo(String cprNo)
String getCprNo()
void setName(String name)
String getName()
void setYear(int year)
int getYear()
void setSex(String sex)
String getSex()
void setState(String state)
String getState()
String toString()

Assignment 1: Worker class: Borrower
First create a class Borrower with the above mentioned data fields, constructors and methods.
Note, from the figure, that the state field per default must be initialized to "active", and year must be initialized to 9999.
Compile it.

Tip: Very similar to Person class !! So copy, paste and change from the Person class!


Assignment 2: Application class: BorrowerApp
Now write an application class BorrowerApp using objects of the class Borrower with the usual main method.
The BorrowerApp class must show the construction of two borrowers:

borrower1:  150561-7777, Michael, 1961, male
borrower2:  your own data

Also show use of the method setName() to change the name and finally print out the borrowers data using the toString() method.

Tip: Very similar to PersonApp class !!
Compile and run!


Assignment 3: Application class: BorrowerApp
Create (in main) a while-loop so you can read in several borrowers data, construct an object, borrower, with these data and print out the data.

Tip: Use a boolean variable to control the loop (initialize it to true). After reading in values for a borrower (and constructing the object and printing it) ask the user if there is more data. Assign the answer (as boolean value) to the variable - when the user chooses that there is no more data the loop should stop.


Assignment 4: Function class: BorrowerCollection

The borrowers must be recorded in a BorrowerCollection class encapsulating an array of Borrower objects using the following template

Data fields:
Borrower[] data (holds the borrowers)
int freeIndex (holds the next free index-value)

Constructors:
BorrowerCollection(int noOfBorrowers)

Methods operating on the data fields:
boolean add(Borrower borrower)
Borrower get(int index)
Borrower remove(int index)
int size()
etc. etc.

This will now be described in more details.

Create the class BorrowerCollection with the following constructor and methods:

  BorrowerCollection(int noOfBorrowers) the constructor declaring the data array with the size noOfBorrowers and initializing freeIndex to 0
     
  boolean add(Borrower borrower) tries to adds borrower to the array, at the index freeIndex.
Remember to increase freeIndex !
returns true if the borrower has been inserted successfully
returns false otherwise (why would happen do you think??)
     
  String toString() returns a String with all borrower data separated by line breaks ("\n")

Tip: Rather similar to Hits class but add is new.
Compile it!


Assignment 5: New Application class: BorrowerCollectionApp
Now create a new application class BorrowerCollectionApp with the usual main() method.
Start by copying BorrowerApp to the class and change it so it:

Remark
You have now a simple program , which are to be expanded with more methods and later on use a menu.

Assignment 6: size and number of borrowers
In the BorrowerCollection class declare two methods

In the BorrowerCollectionApp class show the use of these methods, e.g. by printing the two numbers.

 

Assignment 7: getBorrower(int index)
In the BorrowerCollection class declare a new method, public Borrower get(int index), which returns the object reference (i.e. the Borrower object), at the given parameter index.
The method should return null if no object exists at that index or index is out of range.
In the BorrowerCollectionApp class show the use of this method and print a borrowers data which you got via the method.

 

Assignment 8: getBorrower(String cprNo)
In the BorrowerCollection class declare a new method, public Borrower get(String cprNo), which returns the object reference (i.e. the Borrower object), with the given parameter cprNo.
The method should return null if no object exists with the given parameter cprNo.
In the BorrowerCollectionApp class show the use of this method and print the borrowers data.

 

Assignment 9: removeBorrower(int index)
In the BorrowerCollection class declare a new method, public Borrower remove(int index), which returns the object reference (i.e. the Borrower object) at the given parameter index.
The method must also remove the object by setting the reference to null - remember to close the "gap" where you remove a borower (no "holes" are allowed in the array).
The method should return null if no object exists at that index or index is out of range.
In the BorrowerCollectionApp class show the use of this method and print the borrowers data.

 

Assignment 10: remove Borrower(int cprNo)
In the BorrowerCollection class declare a new method, public Borrower remove(String cprNo), which returns the object reference ( i.e. the Borrower object) with the given parameter cprNo.
The method must also remove the object by setting the reference to null - remember to close the "gap" where you remove a borower (no "holes" are allowed in the array).
The method should return null if no object exists with the given parameter cprNo.
In the BorrowerCollectionApp class show the use of this method and print the borrowers data.

 

Assignment 11: extendBorrowers Optional!!
In the BorrowerCollection class declare a new method, public void extend(), which extends the arraysize with 10 extra free slots. The extension is done by the declaration and construction of a new and bigger array followed by transferring the original data to this array.
In the BorrowerCollectionApp class show the use of this method.

 

Assignment 12: Extra optional
Think about how the extend() method can be used automatically when new borrowers are inserted.
In the BorrowerCollectionApp class show the use of this method.

 

Help to those who need it
The BorrowerApp class must resemble the following template:

public class BorrowerApp {

  public static void main(String[] args) {
    Borrower borrower1 = new Borrower("150561-7777","Michael", 1961,"male");

    System.out.println(borrower1.getName());
    borrower1.setName("Michael Claudius");
     
    System.out.println();
    System.out.println(borrower1);
  }// main

}// Borrower


The BorrowerCollection class must resemble the following template:

public class BorrowerCollection {
   int freeIndex = 0;
   Borrower[] data;

}