| RHS => Allan => Basic Programming => Exercises => BorrowerMenu |
Updated:
02/02/2004 23:36 WebMaster: allanhn@rhs.dk |
BorrowerMenu
The mission
You are to develop a program, which can manage, control and handle collections
of borrowers at the Fantasy Library by using menus.
As the methods for
collections of borrowers already have been implemented it would be nice to
utilize them in a broader sense as well as in a bigger user friendly program
based on menus.
The problem
You are to develop a menu based program, which can handle registrations of
borrowers data. In order to get the required functionality we
have to combine the ideas from two previously made programs: Menu and BorrowerCollectionVector.
Therefore we create a class BorrowerMenu with a private datafield, borrowers, which is a variable (object reference) of the class BorrowerCollectionVector, and methods like menu(), printList(), create(), remove(), find() etc., as described below.
For a start we define a simple version of the class BorrowerMenu:
Data fields:
BorrowerCollectionVector borrowers;Constructor:
BorrowerMenu (BorrowerCollectionVector bc)Methods operating on the data fields:
public void menu() //runs the menu
printList()
create()
find()
remove()
menu()
When the program is executed, the menu is called and the following is printed
on the screen:
![]() |
If key 1 is typed, all borrowers in the Vector will be shown:
No of borrowers : 3
[{111, Klavs, 1955, male, passive}, {333, Susanne, 1958, female, active}, {222, Allan, 1968, male, active}]
If key 2 is typed, the user will be prompted for borrower data, a borrower will be created and added to the borrowers collection.
Similar actions
happens when selecting 3, 4 or 5 (see below).
After each action the menu appears again.
If any other key is typed the user will see a default text such as:
![]() |
If 9 is typed, the program ends.
The actions for each choice are made by the following methods:
private void listBorrowers()
Prints out the number of borrowers and the data of all the borrowers.
private void create()
Reads in the borrower data from the screen. Constructs an object
with these data and adds it to the collection of borrowers by calling
the
method borrowers.add(Borrower).
private void find()
Reads in a cprNo from the screen. Finds the borrower and print out the borrower
data, if any. Otherwise a relevant warning is given.
private void remove()
Reads in a cprNo from the screen. Removes the borrower from the collection
and print out the borrower data, if any. Otherwise a relevant warning is
given.
private void removeBySex()
Reads in a sex from the screen. Removes all borrowers with this sex and print
out their data, if any.
Tip: The method split(int age, String sex) from BorrowerCollectionVector removes all borrowers with the given sex if they are older than age. If you use the age 0 all borrowers should be older! The removed borrowers are returned in a BorrowerCollectionVector which can be printed on screen.
Assignment 1: View: BorrowerMenu
- constructor
The class BorrowerMenu has a private datafield, borrowers, which is a variable
(object reference) of the class BorrowerCollectionVector, and constructor must
initialize this variable. Thus the start of the class resembles the following:
public class BorrowerMenu {
private BorrowerCollectionVector borrowers;
BorrowerMenu(BorrowerCollectionVector bc) {
borrowers = bc;
}//here follows menu() and the other methods
}// BorrowerMenu
Compile!
Assignment 2: View: BorrowerMenu - methods
Extend the class BorrowerMenu with the following methods:
public void menu() //shows the menu
private void listBorrowers() //choice 1
private void create() //choice 2
private void find() //choice 3
private void remove() //choice 4
private void removeBySex() //choice 5
private void bye() //choice 9
private void error() //all other choices
Hint: menu() is very similar to runMenu() in Menu !!
As some of the methods require data entered by the user it would probably be nice to have methods to take care of that. Here is a suggestion to a method that read and returns the cprNo (note that there is no checking of whether the input is valid - if you have time this could be added to the methods):
private String readCprNo() {
String result;
result = JOptionPane.showInputDialog("Enter CprNo :");
return result;
}//readCprNo()
It is a good idea just to program menu(), listBorrowers() and create(), and then do Assignment 3; i.e. the other methods are just empty for a start ! (KISS + one small step at the time...)
Note that all methods except menu() are declared as private - can you see why??
Assignment 3: Application class: BorrowerMenuApp
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 three borrowers:
public class BorrowerMenuApp {
public static void main(String[] args) {
BorrowerCollectionVector borrowers = new BorrowerCollectionVector();//Add three borrowers to the BorrowerCollectionVector
//Make the code yourself!!//Create menu with existing collection:
BorrowerMenu bm = new BorrowerMenu(borrowers);//Start menu:
bm.menu();
//As we use GUI we need to exit:
System.exit(0);
}//main(String[])
}//BorrowerMenuApp
Compile and run!
If you didn't code all methods in Assignment 2 do it now (add one at the time
and don't continue until it works).