Home » Category » Java Essentials

Java Essentials: " instanceof " problem

300| Mon, 24 Sep 2007 19:21:00 GMT| jp_traininga| Comments (7)

I am working on an employee inheritance problem.

Employee is the top class,

Labourer extends Employee

Foreman extends Labourer

Manager extends Employee

Executive extend Manager

I am working on the statistics part of the problem and have used the following code to get the total wages of all labourers.

public static void labStats(Employees [] emps, int size)

{

int x,noL=0,total=0;

for ( x= 0 ; x < size; x ++)

{

if ( emps[x] instanceof Labourers)

{

noL++;

total = total + ( (Labourers) emps[x] ).getWages();

}

}

p("\n\t\t\t" + noL + " Labourers");

p("\n\t\t\tThe total of wages paid to labourers is ?" + total);

p("\n\t\t\tThe average paid to each labourer is ?" + total/noL);

p("\n\n");

}

When I do this I am also getting the wages of the foremen added in on the count, now I know that they are also a labourer but as regards to the compiling of statistics they are a foreman.

Is there any way I can count only those who are labourers?

Keywords & Tags: instanceof, java, essentials

URL: http://java.itags.org/java-essentials/5/
 
«« Prev - Next »» 7 helpful answers below.
You could tryif(emps[x].getClass().getName().equals("Labourer")) {//}

floundera | Fri, 13 Jul 2007 21:05:00 GMT |

thanks mate thats done the trick :-)

jp_traininga | Fri, 13 Jul 2007 21:05:00 GMT |

Or use:if (( emps[x] instanceof Labourers) && (!(emps[x] instanceof Foreman)))

mlrona | Fri, 13 Jul 2007 21:05:00 GMT |

Am I the only one who is going to mention that the fact they have to do this seems to indicate some kind of inherent design flaw?

kablaira | Fri, 13 Jul 2007 21:05:00 GMT |

> Am I the only one who is going to mention that the> fact they have to do this seems to indicate some kind> of inherent design flaw?No, just the fastest.

jverda | Fri, 13 Jul 2007 21:05:00 GMT |

I'm not sure I think it's a good idea to use inheritance to model these relations. But if you're stuck with that, you could always use the visitor pattern

http://en.wikipedia.org/wiki/Visitor_pattern

public interface Visitor {

void visit(Labourer lab);

void visit(Foreman fm);

void visit(Manager mgr);

void visit(Executive exc);

}

public abstract class Employee {

public abstract int getWages();

public abstract void accept(Visitor v);

}

public class Labourer extends Employee {

public void accept(Visitor v) {

v.visit(this);

}

}

public class Foreman extends Labourer {

public void accept(Visitor v) {

v.visit(this);

}

}

public class Manager extends Employee {

public void accept(Visitor v) {

v.visit(this);

}

}

public class Executive extends Manager {

public void accept(Visitor v) {

v.visit(this);

}

}

public class LabourerStatsCollector {

private int numberOf;

private int totalWages;

public void collect(Employee[] employees) {

Visitor v = new VisitorImpl();

for (Employee e : employees) {

e.accept(v);

}

System.out.println("Number of labourers: " + numberOf);

System.out.println("Total wages: " + totalWages);

}

private class VisitorImpl implements Visitor {

public void visit(Labourer lab) {

++numberOf;

totalWages += lab.getWages();

}

public void visit(Foreman fm) {

// Not interested in foremen

}

public void visit(Manager mgr) {

// Not interested in managers

}

public void visit(Executive exc) {

// Not interested in executives

}

}

}

// To test:

public static void main(String[] args) {

Employee[] employees = ...

new LabourerStatsCollector().collect(employees);

}

torgila | Fri, 13 Jul 2007 21:05:00 GMT |

> >

> public static void labStats(Employees [] emps, int size)

>

>

Btw, in Java it's unnecessary to pass in the length of the array to the method. You can always find out the length by

int numberOfElements = emps.length;

torgila | Fri, 13 Jul 2007 21:05:00 GMT |

Java Essentials Hot Answers

Java Essentials New questions

Java Essentials Related Categories