PDA

View Full Version : Help fixing a method() needed!


gak
12-30-2006, 03:29 PM
Hi,

I'm new to java and would really appreciate some advice on the following:

I've made 3 java objects that work together - a Worker object, a Job object and a Rosta object.

All the workers are referenced in an ArrayList in the Rosta object.
All the jobs are also referenced in an ArrayList in the Rosta object.
Jobs are allocated to Workers, who then store a reference of to their allocated jobs in their own ArrayList.

My allocateJob() method looks like this:

public void allocateJob(Job j) {
willDo.add(j); //-------willDo is the name of the workers' ArrayList of jobs
}
All of the above is working ok, but I want to set individual maximum ammounts to be allocated for each worker.

I then added a maxJobs attribute to each Worker with appropriate get/set accessor methods and an isBusy() method which looks like this:

public boolean isBusy() {
if(willDo.size() == this.getMaxJobs()) return true;
else return false;
}

This parts compile and work ok, but I now want to change my allocateJob() method to use the isBusy() method, so that a Job will only be allocated to a Worker who isn't busy!

I tried this, but it doesn't work when I run it:

public void allocateJob(Job j) {
if(this.isBusy() == true) {
willDo.add(j);
} else if (this.isBusy() == false) {
System.out.println("Helper is currently busy");
}
}

If anyone could give me any help or advice on what I've done wrong, I'd be very grateful!

Many thanks,

Gak

tricolaire
12-30-2006, 05:31 PM
what do you mean when you say it doesn't work?

gak
12-30-2006, 05:50 PM
Hi,

Thanks for replying!

What I wanted the method to do is only allocate the Job if the Worker isn't busy, and if the worker is busy, print to the screen.

Basically, I don't want a Worker to be allocated any Jobs if the maxJobs figure has been reached.

However, the method continues to allocate Jobs even if the Worker is busy, and never prints to the screen.

If I call the isBusy() method on a worker who's maxJobs figure has been reached, it will return true.

I noticed after I had posted that I'd put the true and false values in the wrong place - I have since changed them but they still don't work:

public void allocateJob(Job j) {
if(this.isBusy() == false) {
willDo.add(j);
} else if (this.isBusy() == true) {
System.out.println("Helper is currently busy");
}
}


Thanks

Gak

brad211987
12-31-2006, 03:05 AM
I would like to see the entire class that these methods are in, but I would try getting rid of the "this" reference in "this.isBusy" because it is not needed. If that doesnt change anything, post the error message, along with the class here so we can get a better idea of what the problem is.