System.out.println not working in my method...?

/**

* This will add a contract to the firm

*/

publicvoid createContract(String contractId,int contractDays, String typeOfContract)

{

for(Accountant anAccountant : accountants)

{

if(getNumOfAccountants() >= 1){

if(anAccountant.getExpertise().equals(typeOfContract)){

if(anAccountant.getIsWorking() ==false){

Contract newContract =new Contract(contractId, contractDays, typeOfContract);

contracts.add(newContract);

System.out.println("A contract has been created, the contracts name is " + contractId);

System.out.println("The price of the contract is " + (anAccountant.getDailyRate()*1.21));

System.out.println("The duration of the contract is " + contractDays +" days");

System.out.println("The type of the contract is " + typeOfContract);

anAccountant.setIsWorking();

}

else{

System.out.println("There are no suitable accountants in the firm to match the requirements");

}

}

}

}

}

I am having trouble with the last else statement, the system.out.println("There are no suitable accountants in the firm to match the requirements");

This isn't getting called if the above "if" statements arn't correct.

For example i want this statement to be printed if there arn't enough accountants in the firm.

Can someone help.

Thanks alot

Message was edited by:

Capsud

[2288 byte] By [Capsuda] at [2007-11-26 22:51:15]
# 1
Fix your brackets.. the else now appears to belong to:anAccountant.getIsWorking() == false
Simeona at 2007-7-10 12:13:08 > top of Java-index,Java Essentials,New To Java...
# 2

/**

* This will add a contract to the firm

*/

public void createContract(String contractId, int contractDays, String typeOfContract)

{

for(Accountant anAccountant : accountants)

{

if(getNumOfAccountants() >= 1)

if(anAccountant.getExpertise().equals(typeOfContract))

if(anAccountant.getIsWorking() == false)

{

Contract newContract = new Contract(contractId, contractDays, typeOfContract);

contracts.add(newContract);

System.out.println("A contract has been created, the contracts name is " + contractId);

System.out.println("The price of the contract is " + (anAccountant.getDailyRate()*1.21));

System.out.println("The duration of the contract is " + contractDays + " days");

System.out.println("The type of the contract is " + typeOfContract);

anAccountant.setIsWorking();

}

else {

System.out.println("There are no suitable accountants in the firm to match the requirements");

}

}

}

Ok that is the code i have now but that statement is still not working.

Capsuda at 2007-7-10 12:13:08 > top of Java-index,Java Essentials,New To Java...
# 3

The text suggests you want to check all accountants and if there are no suitable ones, then print that message. If so, then you can simply use a counter and count the number of suitable accountants by incrementing the counter when you've found a suitable one. Then, after the for-loop, you can display the text if the counter is still 0.

Simeona at 2007-7-10 12:13:08 > top of Java-index,Java Essentials,New To Java...
# 4

a few pointers:

get rid of that nested conditional logic. as demonstrated, it's not particularly clear or readable code. deal with each condition one by one

you don't need to do this:

if(anAccountant.getIsWorking() == false) {

boolean expressions evaluate to true/false already. this will suffice:

if(anAccountant.getIsWorking() ) {

while we're on that subject, why not just call the method isWorking, rather than getIsWorking? naming conventions exist for a reason!

why are you calling getNumOfAccountants in every iteration of your loop? that's not going to change in the space of a few clock cycles, is it?

rather than try to fix this, I strongly suggest you simply throw it out and start again. work out exactly what you want the code to do. this looks like classic guess-driven development

georgemca at 2007-7-10 12:13:08 > top of Java-index,Java Essentials,New To Java...