Question: I have some issue with getting the first employee's information using both get and set methods. The second, or Bob's comes out just fine but
I have some issue with getting the first employee's information using both get and set methods. The second, or Bob's comes out just fine but Sue isnt so lucky. Please help me fix this!!
package BasePlusCommissionEmployee;
public class CommissionEmployeeTest
{
public static void main(String[] args)
{
CommissionEmployee employee = newCommissionEmployee("Sue", "Jones", "222-22-2222", 10000, .06);
// get commission employee data
System.out.println(
"Employee information obtained by get methods:");
System.out.printf("%n%s %s%n", "First name is",
employee.getFirstName());
System.out.printf("%s %s%n", "Last name is",
employee.getLastName());
System.out.printf("%s %s%n", "Social security number is",
employee.getSocialSecurityNumber());
System.out.printf("%s %.2f%n", "Gross sales is",
employee.getGrossSales());
System.out.printf("%s %.2f%n", "Commission rate is",
employee.getCommissionRate());
/* Change the gross sales and commission rate
using mutator methods setGrossSales and
setCommissionRate methods.*/
employee.setGrossSales(5000);
employee.setCommissionRate(.1);
//Calls the toString method of CommissionEmployee class
System.out.printf("%n%s:%n%n%s%n",
"Updated employee information obtained by toString", employee);
} // end main
private static CommissionEmployee newCommissionEmployee(String string, String string2, String string3, int i,
double d) {
// TODO Auto-generated method stub
return null;
}
} // end class CommissionEmployeeTest
package BasePlusCommissionEmployee;
public class CommissionEmployee extends Employee
{
private double grossSales; // gross weekly sales
private double commissionRate; public CommissionEmployee(String firstName,
String lastName, String socialSecurityNumber,
double grossSales,
double commissionRate)
{
/*Calling super class Employee constructor with three
arguments first name, last name and social security
number. */
super(firstName, lastName, socialSecurityNumber);
// if grossSales is invalid throw exception
if (grossSales < 0.0)
throw new IllegalArgumentException(
"Gross sales must be >= 0.0");
// if commissionRate is invalid throw exception
if (commissionRate <= 0.0 || commissionRate >= 1.0)
throw new IllegalArgumentException(
"Commission rate must be > 0.0 and < 1.0");
this.grossSales = grossSales;
this.commissionRate = commissionRate;
} // end constructor
// set gross sales amount
public void setGrossSales(double grossSales)
{
if (grossSales < 0.0)
throw new IllegalArgumentException(
"Gross sales must be >= 0.0");
this.grossSales = grossSales;
}
// return gross sales amount
public double getGrossSales()
{
return grossSales;
}
// set commission rate
public void setCommissionRate(double commissionRate)
{
if (commissionRate <= 0.0 || commissionRate >= 1.0)
throw new IllegalArgumentException(
"Commission rate must be > 0.0 and < 1.0");
this.commissionRate = commissionRate;
}
// return commission rate
public double getCommissionRate()
{
return commissionRate;
}
// calculate earnings
public double earnings()
{
return commissionRate * grossSales;
}
/* Return String representation of CommissionEmployee
Object indicates that this method overrides a superclass
Method */
@Override
public String toString()
{
/* The method super.toString method that calls the super
Class. Employee toString method that returns the string
representation of Employee class and then sets the gross
sales and commission rate. */
return super.toString()+String.format(
"%n%s: %.2f%n%s: %.2f",
"gross sales", grossSales,
"commission rate", commissionRate);
}
} // end class CommissionEmployee
package BasePlusCommissionEmployee;
public class Employee
{
//instance variables of Employee class
private final String firstName;
private final String lastName;
private final String socialSecurityNumber;
/* Employee constructor that sets the first name,
second name and social security number.*/
public Employee(String firstName,
String lastName, String socialSecurityNumber)
{
this.firstName = firstName;
this.lastName = lastName;
this.socialSecurityNumber = socialSecurityNumber;
}
// return first name
public String getFirstName()
{
return firstName;
}
// return last name
public String getLastName()
{
return lastName;
}
// return social security number
public String getSocialSecurityNumber()
{
return socialSecurityNumber;
}
/*Override the method toString that returns the string
representation of Employee class.*/
@Override
public String toString()
{
return String.format("%s: %s %s%n%s: %s",
"commission employee", firstName, lastName,
"social security number", socialSecurityNumber);
}//end of the class.
}//end of the employee class.
package BasePlusCommissionEmployee;
public class BasePlusCommissionEmployeeTest
{
public static void main(String[] args)
{
// instantiate BasePlusCommissionEmployee object
BasePlusCommissionEmployee employee = new BasePlusCommissionEmployee(
"Bob", "Lewis", "333-33-3333", 5000, .04, 300);
// get base-salaried commission employee data
System.out.println(
"Employee information obtained by get methods:");
System.out.printf("%n%s %s%n", "First name is",
employee.getFirstName());
System.out.printf("%s %s%n", "Last name is",
employee.getLastName());
System.out.printf("%s %s%n", "Social security number is",
employee.getSocialSecurityNumber());
System.out.printf("%s %.2f%n", "Gross sales is",
employee.getGrossSales());
System.out.printf("%s %.2f%n", "Commission rate is",
employee.getCommissionRate());
System.out.printf("%s %.2f%n", "Base salary is",
employee.getBaseSalary());
employee.setBaseSalary(1000);
System.out.printf("%n%s:%n%n%s%n",
"Updated employee information obtained by toString",
employee.toString());
} // end main
} // end class BasePlusCommissionEmployeeTest
package BasePlusCommissionEmployee;
public class BasePlusCommissionEmployee extends CommissionEmployee
{
private double baseSalary; // base salary per week
/* six-argument constructor with first name, last name
,social security number gross sales , commission rate
and base salary */
public BasePlusCommissionEmployee(String firstName,
String lastName, String socialSecurityNumber,
double grossSales, double commissionRate,
double baseSalary)
{
/* Calling super class Employee constructor with three
arguments first name, last name and social security
number. */
super(firstName, lastName, socialSecurityNumber,
grossSales, commissionRate);
// if baseSalary is invalid throw exception
if (baseSalary < 0.0)
throw new IllegalArgumentException(
"Base salary must be >= 0.0");
this.baseSalary = baseSalary;
}
// set base salary
public void setBaseSalary(double baseSalary)
{
if (baseSalary < 0.0)
throw new IllegalArgumentException(
"Base salary must be >= 0.0");
this.baseSalary = baseSalary;
}
// return base salary
public double getBaseSalary()
{
return baseSalary;
}
// calculate earnings
@Override
public double earnings()
{
return getBaseSalary() + super.earnings();
}
/* Return String representation of
BasePlusCommissionEmployee*/
@Override
public String toString()
{
/* The method super.toString method that calls the super
Class CommissionEmployee toString method that in turn
calls the Employee toString method and CommissionEmployee
returns the string representation of CommissionEmployee
class and then sets the gross sales and commission rate.
*/
return String.format("%s %s%n%s: %.2f", "base-salaried",
super.toString(), "base salary", getBaseSalary());
}
} // end class BasePlusCommissionEmployee
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
