I want to get discount from the getter in the employee class which extends person class. The discount varies upon hourlyWorked. I have set up parameters. I can get the discount when i use setMethods. e.g if i use
e.setHourlyWorked(56);
System.out.println(e.getHourlyWorked());
System.out.println(e.getDiscount());
I get the result as expected.
But i cant get e.getDiscount());
from the parameter itself i.e
Employee e= new Employee(23, 22,"Nab","Kar","000");
System.out.println(e.getHourlyWorked());
System.out.println(e.getDiscount());
I get 0 as discount.
Person class
public class Person {
//private fields
private String firstName;
private String surname;
private String phoneNumber;
private int discount;
public Person(){
//Default Constructors
firstName="John";
surname="SMITH";
phoneNumber="0000";
}//constructors with all the inputs
public Person(String fn, String sn, String pn){
firstName=fn;
surname=sn;
phoneNumber=pn;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String fn) {
firstName=fn;
}
public String getSurname() {
return surname;
}
public void setSurname(String sn) {
surname = sn;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String pn) {
phoneNumber = pn;
}
@Override
public String toString(){
return getFirstName()+" "+getSurname()+" "+getPhoneNumber();
}
Employee class
package lollyShopSystem;
public class Employee extends Person {
private double hourlyWorked;
private double wage;
private int dis;
public Employee(){
//Default Constructors
hourlyWorked=30;
wage=34;
}
public Employee(double hourlyWorked, double wage, String fn, String sn, String pn) {
super(fn, sn, pn);
this.hourlyWorked = hourlyWorked;
this.wage = wage;
}
public double getHourlyWorked() {
return this.hourlyWorked;
}
public void setHourlyWorked(double hourlyWorked) {
this.hourlyWorked = hourlyWorked;
if (this.hourlyWorked<=20){
this.dis=5;
}
else if (this.hourlyWorked>20 && this.hourlyWorked<=30){
this.dis=10;
}
else {
this.dis=15;
}
}
public double getWage() {
return this.wage;
}
public void setWage(double wage) {
this.wage = wage;
}
public int getDiscount(){
return this.dis;
}
}
Assigning hourlyWorked
using the constructor does not change the discount. You can replace the assignment of the field with the setHourlyWorked
function instead.
public Employee(double hourlyWorked, double wage, String fn, String sn, String pn) {
super(fn, sn, pn);
setHourlyWorked(hourlyWorked);
this.wage = wage;
}