Question
For the SalariedCompany class, write a test class called SalariedCompanyTest that tests the method getNextEmployee(..). Ensure you test it with three different inputs. (ii) Write
For the SalariedCompany class, write a test class called SalariedCompanyTest that tests the method getNextEmployee(..). Ensure you test it with three different inputs. (ii) Write a test class for SalariedEmployee class called SalariedEmployeeTest that tests the toString(..) method. Also test it with three inputs.
----------------------------------------------------------------------------------------------------------------------------------------
SalariedCompany:
import java.util.*; import java.io.*;
public class SalariedCompany extends Company { public static void main (String[ ] args) throws FileNotFoundException { new SalariedCompany().run(); } // method main protected SalariedEmployee getNextEmployee (Scanner sc) { double bonus=0; Scanner lineScanner = new Scanner (sc.nextLine()); String name = lineScanner.next(); double salary = lineScanner.nextDouble(); String b= lineScanner.next(); if (!b.equals("x")) bonus=Double.valueOf(b);
return new SalariedEmployee (name, salary, bonus); } // method getNextEmployee
} // class HourlyCompany
----------------------------------------------------------------------------------------------------------------------------------------
Company:
import java.util.*; // for the Scanner class
import java.io.*; // for the FileNotFoundException class
public class Company {
public static void main (String[ ] args) throws FileNotFoundException { new Company().run(); } // method main public void run() throws FileNotFoundException { final String INPUT_PROMPT = "Please enter the path for the file of employees: "; final String BEST_PAID_MESSAGE = " The best-paid employee (and gross pay) is "; final String NO_INPUT_MESSAGE = " Error: There were no employees scanned in."; String fileName; System.out.print (INPUT_PROMPT); fileName = new Scanner (System.in).nextLine(); Scanner sc = new Scanner (new File (fileName)); FullTimeEmployee bestPaid = findBestPaid (sc); if (bestPaid == null) System.out.println (NO_INPUT_MESSAGE); else System.out.println (BEST_PAID_MESSAGE + bestPaid.toString()); } // method run
public FullTimeEmployee findBestPaid (Scanner sc) { FullTimeEmployee full, bestPaid = new FullTimeEmployee(); while (sc.hasNext()) { full = getNextEmployee (sc); if (full.getGrossPay() > bestPaid.getGrossPay()) bestPaid = full; } //while if (bestPaid.getGrossPay() == 0.00) return null; return bestPaid; } // method findBestPaid
protected /*private*/ FullTimeEmployee getNextEmployee (Scanner sc) { Scanner lineScanner = new Scanner (sc.nextLine()); String name = lineScanner.next(); double grossPay = lineScanner.nextDouble();
return new FullTimeEmployee (name, grossPay); } // method getNextEmployee
} // class Company
----------------------------------------------------------------------------------------------------------------------------------------
SalariedEmployee:
import java.text.DecimalFormat;
public class SalariedEmployee extends FullTimeEmployee implements Employee { // for full-time hourly employees
protected double bonus; protected double salary;
public SalariedEmployee() { bonus=0; salary=0; name=""; grossPay =0; } // default constructor
public SalariedEmployee (String name, double salary, double bonus) { this.name = name; this.bonus=bonus; this.salary=salary; grossPay = bonus+salary; } // 3-parameter constructor
public double getSalary(){ return salary; } public double getBonus() { return bonus; } public double getGrossPay() { return grossPay; } public String toString() { final String SALARIED_STATUS = " SALARIED"; return super.toString() + SALARIED_STATUS; } // method toString
} // class SalariedEmployee
----------------------------------------------------------------------------------------------------------------------------------------
Employee:
import java.text.DecimalFormat;
public interface Employee {
final static DecimalFormat MONEY = new DecimalFormat (" $0.00"); // a class constant used in formatting a value in dollars and cents
String getName();
double getGrossPay();
String toString();
} // interface Employee
----------------------------------------------------------------------------------------------------------------------------------------
FullTimeEmployee:
public class FullTimeEmployee implements Employee { protected /*private*/ String name;
protected /*private*/ double grossPay;
public FullTimeEmployee() { final String EMPTY_STRING = "";
name = EMPTY_STRING; grossPay = 0.00; } // default constructor
public FullTimeEmployee (String name, double grossPay) { this.name = name; this.grossPay = grossPay; } // 2-parameter constructor
public String getName() { return name; } // method getName public double getGrossPay() { return grossPay; } // method getGrossPay
public String toString() { final String EMPLOYMENT_STATUS = " FULL TIME"; return name + MONEY.format (grossPay) + EMPLOYMENT_STATUS; // the format method returns a String representation of grossPay. } // method toString
} // class FullTimeEmployee
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started