Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Help with list manager - methods problem etc

Hi,

Please help!! I have been trying to see a solution to this for days, and every time I think I have a solution something goes wrong.

I am basically trying to write a manager class that will allow a user to add, delete and search for a borrower, as defined in a Borrower and BorrowerList class. The only problem is that I'm really struggling with the implementation of this in the BorrowerListManager class. So far I have the following code,

1. Borrower class,

package videos;

import simplejava.*;

public class Borrower extends Person {//allows use of methods, variables and constructor from Person class
private int membNum;
private int borrowerLimit;

public Borrower(int mn, int limit, String fn, String sn, String tele, String emailAddress) {
super(fn, sn, tele, emailAddress);
membNum = mn;
borrowerLimit = limit;
}

public Borrower(SimpleReader in) {//gets constructor from Person class and adds Borrower specific constructors
super(in); //for membership number and borrower limit
membNum = in.readInt();
borrowerLimit = in.readInt();
}

public int getBorrowerLimit() {
return borrowerLimit;
}

public void setBorrowerLimit(int limit) {
borrowerLimit = limit;
}

public int getMembNum() {
return membNum;
}

public void setMembNum(int mn) {
membNum = mn;
}

public void saveTo(SimpleWriter out) {
out.println(getForename()+" "+getSurname()+" "+getTelephoneNumber()+" "+getEmailAddress()
+" "+membNum+" "+borrowerLimit);
}//saveTo method writes details of the Borrower class to a text file

public String toString() {
return getForename()+" "+getSurname()+" "+getTelephoneNumber()+" "+getEmailAddress()+
" Membership Number = "+membNum+" Borrower Limit = "+borrowerLimit+" "; }
//toString method calling methods from Person class and membership and borrower details
//from within this class
}



2. BorrowerList class,

package videos;

import simplejava.*;

public class BorrowerList {

private int maxBorrowers, borrowerCount;

private Borrower[] borrowerArray;
private int numBorrowers;
Node runner;
Node previous;


public BorrowerList(int max) {
borrowerCount = 0; maxBorrowers=max;
borrowerArray = new Borrower[max];
}

public BorrowerList() {
this(1000000);
}

public BorrowerList(SimpleReader in) {
this(in.readInt());
int entries = in.readInt();
for (int i = 0; i<entries; i++)
add(new Borrower(in));
for (int i=0; i<entries; i--)
delete(new Borrower(in));
}

public int getSize() {
return borrowerCount;
}

public boolean add(Borrower newItem) {
if (borrowerCount<maxBorrowers) {
borrowerArray[borrowerCount] = newItem;
borrowerCount++;
return true;
} else return false;
}

public boolean delete(Borrower oldItem) {
if (borrowerCount<maxBorrowers)
{ borrowerArray[borrowerCount] = oldItem;
borrowerCount--;
return true;
}
else return false;
}

public Borrower getBorrower(int i) {
if (i<0 || i>=borrowerCount)
return null;
else return borrowerArray[i];
}

public Borrower lookUp(int requiredNum) {
boolean found = false;
int next = 0;

while (!found && (next<borrowerCount)) {
if ( borrowerArray[next].getMembNum() == requiredNum)
found = true;
else next++;
}
if (found) return borrowerArray[next];
else return null;
}

public void saveTo(SimpleWriter out) {
int next;
out.println(maxBorrowers+" "+borrowerCount);
for (next=0; next<borrowerCount; next++)
borrowerArray[next].saveTo(out);
}

public String toString() {
int next;
String result = "";
result+= "Borrower Count = "+" "+borrowerCount;
result+= ", Max Borrowers ="+maxBorrowers;
for (next=0; next<borrowerCount; next++)
result+= "\n"+borrowerArray[next];
return result;
}

}



3. BorrowerListManager class,

package videos;


import simplejava.*;

public class BorrowerListManager{

private BorrowerList borrowers;

SimpleWriter screen = new SimpleWriter();
SimpleReader keyboard = new SimpleReader();
BorrowerList newBorrower;

public BorrowerListManager(BorrowerList borrowers) {
this.borrowers=borrowers;
}


public void run() {
BorrowerList borrowerSelected = null;
String nextLine;
char choice;
do
{
String permitted;
if (borrowerSelected == null) {
screen.println("a - add, f - find, q - Quit --> ");
permitted = "AaFfQq";
} else {
screen.println("Current borrower: " + borrowerSelected);
screen.println("a - add, d - delete, f - find, q - Quit --> ");
permitted = "AaDdFfEeQq";
}
nextLine = keyboard.readLine();
choice = nextLine.charAt(0);
if (permitted.indexOf(choice)>=0) {
switch (choice) {
case 'A':
case 'a':
borrowerSelected = addBorrower(newBorrower);
break;
case 'D':
case 'd':
deleteBorrower(borrowerSelected);
borrowerSelected = null;
break;
case 'F':
case 'f':
borrowerSelected = findBorrower();
break;
case 'Q':
case 'q':
break;
}
} else System.out.println("Unrecognised option.");
} while (choice != 'q' && choice != 'Q');
}

private void addBorrower(BorrowerList newBorrower) {

boolean result = borrowers.add(newBorrower);
System.out.println("New borrower added? "+result);

}

private void deleteBorrower(BorrowerList b) {

}

private Borrower findBorrower() {
return borrower;
}
}



Any help or advice would be really appreciated!! :confused:
[7699 byte] By [fulcanelli] at [2007-11-11 7:21:24]
# 1 Re: Help with list manager - methods problem etc
What problems are you having?

Did you notice that you are first adding and then deleting borrowers in any call to your constructor?
nspils at 2007-11-11 22:38:35 >
# 2 Re: Help with list manager - methods problem etc
Hi,

Thanks for the reply. The main problem is in the BorrowerListManager class. I'm trying to call the addBorrower, deleteBorrower, findBorrower methods from within run, but each time I try to call the method I get numerous errors.

Basically, how do I call these methods from within 'public void run'?

Any help appreciated!! :confused:
fulcanelli at 2007-11-11 22:39:41 >