Reputation: 79
My code won't print out the Paid, Current hours and Total Sales for the commission and I think it might have something to do with the inheritance but I'm not sure. I casted them in another class but I think the problem is here because there was no issues with the casting of my other classes. All of the values return in the program besides those 3 for Commission. The Pay and current hours update for hourly which Commission is inherited from but it doesn't work in the Commission class.
//StaffMember Class
public abstract class StaffMember {
protected String name, address, number;
public StaffMember(String name, String address, String number) {
this.name = name;
this.address = address;
this.number = number;
}
public String toString(){
return "Name: " +name + "\nAddress: " +address + "\nPhone: " +number;
}
public abstract double pay();
}
//Employee Class
public class Employee extends StaffMember{
private String rsiNumber;
protected double payRate;
public Employee(String rsiNumber, double payRate, String name, String address, String number) {
super(name, address, number);
this.rsiNumber = rsiNumber;
this.payRate = payRate;
}
public String toString(){
return super.toString() + "\nRSI Number: " +rsiNumber + String.format("\nPaid: €%,.2f" ,pay());
}
public double pay(){
return payRate;
}
}
//Hourly Class
public class Hourly extends Employee{
private int hours;
public Hourly(String rsiNumber, double payRate, String name, String address, String number) {
super(rsiNumber, payRate, name, address, number);
this.hours = 0;
}
public void addHours(int hours){
this.hours=hours;
}
public double pay(){
int otHours=0;
if(hours>40){
otHours=hours-40;
}
double pay=(hours-otHours)*payRate+(otHours*payRate*1.5);
return pay;
}
public String toString(){
return super.toString() + "\nCurrent Hours: " +hours;
}
}
//Commission Class
public class Commission extends Hourly{
private double totalSales, commissionRate;
public Commission(double commissionrate, String rsiNumber, double payRate, String name, String address, String number) {
super(rsiNumber, payRate, name, address, number);
this.commissionRate = commissionrate;
this.totalSales=0;
}
public void addSales(int sales){
this.totalSales=sales;
}
public double pay(){
return super.pay()+(totalSales*commissionRate);
}
public String toString(){
return super.toString() + "\nTotal Sales: " +totalSales;
}
}
//Casting Method From Another Class
public void processStaff(){
for(int i=0; i<sm.length; i++){
if (sm[i] instanceof Hourly) {
Hourly h = (Hourly) sm[i];
if(h.name.equals("Joanne")){
h.addHours(50);
}
}else if (sm[i] instanceof Executive) {
Executive e = (Executive) sm[i];
if(e.name.equals("John")){
e.awardBonus(500);
}
}else if (sm[i] instanceof Commission) {
Commission c = (Commission) sm[i];
if(c.name.equals("Molly")){
c.addHours(55);
c.addSales(400);
}else if(c.name.equals("Joe")){
c.addHours(45);
c.addSales(950);
}
}
}
}
Here is the output I get when I should be getting values returned and not 0.
Edit: Seem to have found the solution by just putting Commission inside of Hourly since Commission is a type of Hourly.
public void processStaff() {
for (int i = 0; i < sm.length; i++) {
if (sm[i] instanceof Hourly) {
Hourly h = (Hourly) sm[i];
if (h.name.equals("Joanne")) {
h.addHours(50);
} else if (sm[i] instanceof Commission) {
Commission c = (Commission) sm[i];
if (c.name.equals("Molly")) {
c.addHours(55);
c.addSales(400);
} else if (c.name.equals("Joe")) {
c.addHours(45);
c.addSales(950);
}
}
} else if (sm[i] instanceof Executive) {
Executive e = (Executive) sm[i];
if (e.name.equals("John")) {
e.awardBonus(500);
}
}
}
}
Upvotes: 0
Views: 133
Reputation: 5558
if (sm[i] instanceof Hourly) {
/* .... */
}else if (sm[i] instanceof Commission) {
Commission c = (Commission) sm[i];
if(c.name.equals("Molly")){
c.addHours(55);
c.addSales(400);
}else if(c.name.equals("Joe")){
c.addHours(45);
c.addSales(950);
}
}
The first condition is true for Commission
so you never call addHours or addSales on it. Hence nothing to display.
You really should look at the content of the classes with a debugger.
Upvotes: 1