I have 3 classes. helpSystem, Volunteer and Person. I want to be able to
assign a Volunteer to a Person. I have a method to do this in my Volunteer
class but I need to be able to do it from my helpSystem class as I am using
ArrayLists but im having trouble with it (for the past week!).
//FROM THE HELPSYSTEM CLASS
public void assignVolunteer(String volunteersName, String personsName)
{
if (Volunteer.withPerson(volunteersName))
{
Iterator it = help.iterator();
while (it.hasNext())
{
Person person = (Person) it.next();
if (person.getName().equals(personsName))
{
if(person.withPerson())
{
System.out.println (volunteersName +" is already assigned
to a someone");
return;
}
}
else
{
person.assignPerson (getVolunteer (volunteersName));
System.out.println (personsName + " assigned to " +
volunteersName);
return;
}
}
}
//FROM THE VOLUNTEER CLASS
public void assignToPerson(Person person)
{
withPerson = true;
this.person = person;
}
Mark Thomas - 08 Jan 2006 21:48 GMT
> I have 3 classes. helpSystem, Volunteer and Person. I want to be able to
> assign a Volunteer to a Person. I have a method to do this in my Volunteer
[quoted text clipped - 35 lines]
> this.person = person;
> }
You're going to have to teach yourself to use your debugger, and step
through your code line by line to work out for yourself what you're
doing wrong. Small fragments of code like this do not make sense, even
if you've started sticking to the naming conventions we suggested (
for example, a static withPerson() method on the Volunteer class doesn't
make sense).
Once you've got a particular question to ask, come back and we'll try to
help, but just saying you're having trouble doesn't give us a starting
point.
Mark
pit.grinja@gmx.de - 08 Jan 2006 22:51 GMT
Hi ste,
> I have 3 classes. helpSystem, Volunteer and Person. I want to be able to
> assign a Volunteer to a Person. I have a method to do this in my Volunteer
> class but I need to be able to do it from my helpSystem class as I am using
> ArrayLists but im having trouble with it (for the past week!).
<looooooong code passage removed>
I have always found it much easier to learn the basic from examples, so
here´s my attempt to implement the classes that you need. Some things
are dirty (some classes members are public, and setters and getters
have not been included) but you should be able to correct that. And you
could easily double the lines with System.out.printlns, e. g. when you
try to assign a volunteer to a person and one of them is not stored in
the respective list. Note that I am using java 1.4 and thus cannot use
generics.
HTH, Piet
import java.util.*;
public class HelpSystem{
public ArrayList volunteers = new ArrayList();
public ArrayList persons = new ArrayList();
public Volunteer addVolunteer(String name,int number){
Volunteer v = new Volunteer(name,number);
volunteers.add(v);
return v;
}
public Person addPerson(String name,int number){
Person p = new Person(name,number);
persons.add(p);
return p;
}
public void assignVolunteer(String volunteersName, String
personsName){
boolean personFound = false;
boolean volunteerFound = false;
Volunteer volunteer = null;
Person person = null;
Iterator iterPersons = persons.iterator();
while(iterPersons.hasNext()){
Person tempPerson = (Person)(iterPersons.next());
if (tempPerson.getName().equals(personsName) ){
System.out.println("Person found! "+tempPerson.getName());
personFound = true;
person = tempPerson;
break;
}
}
Iterator iterVolunteers = volunteers.iterator();
while(iterVolunteers.hasNext()){
Volunteer tempVolunteer = (Volunteer)(iterVolunteers.next());
if (tempVolunteer.getName().equals(volunteersName) ){
System.out.println("Volunteer found! "+tempVolunteer.getName());
volunteerFound = true;
volunteer = tempVolunteer;
break;
}
}
if (personFound && volunteerFound){
if (volunteer.isAssigned){
System.out.println("Volunteer "+volunteersName+" already assigned
to "+volunteer.person.getName());
}
else {
volunteer.isAssigned = true;
volunteer.person = person;
person.volunteer = volunteer;
};
}
}
public static void main(String[] args){
HelpSystem hs = new HelpSystem();
Person mrbean = hs.addPerson("Mr. Bean",1);
Person donald = hs.addPerson("Donald Duck",1);
Volunteer batman = hs.addVolunteer("Batman",2);
hs.assignVolunteer("Batman","Mr. Bean");
System.out.println("Person assigned to Batman:
"+batman.person.getName());
hs.assignVolunteer("Batman","Donald Duck");
System.out.println("Person assigned to Batman:
"+batman.person.getName());
System.out.println("Person to help Mr. Bean:
"+mrbean.volunteer.getName());
}
}
class Volunteer {
private String name;
private int number;
public boolean isAssigned = false;
public Person person;
Volunteer(String name,int number){
this.name = name;
this.number = number;
}
public String getName(){
return this.name;
}
public int getNumber(){
return this.number;
}
public boolean equals(Object o){
if (o.toString().equals(this.name)) return true;
return false;
}
}
class Person {
private String name;
private int number;
public boolean hasVolunteer;
public Volunteer volunteer;
Person(String name,int number){
this.name = name;
this.number = number;
}
public String getName(){
return this.name;
}
public int getNumber(){
return this.number;
}
public boolean equals(Object o){
if (o.toString().equals(this.name)) return true;
return false;
}
}
Roedy Green - 08 Jan 2006 23:07 GMT
>I have 3 classes. helpSystem, Volunteer and Person. I want to be able to
>assign a Volunteer to a Person. I have a method to do this in my Volunteer
>class but I need to be able to do it from my helpSystem class as I am using
>ArrayLists but im having trouble with it (for the past week!).
To connect a Kennel class with a the Dog class, all the Dogs in the
kennel, you need a Collection reference of some sort in the Kennel.
Then you add the Dog objects to it, which creates a list of references
to the Dogs.
You can then enumerate all dogs in the class, add, remove etc, using
the methods of the Collection.
See http://mindprod.com/jgloss/collection.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.