In: Computer Science
JAVA
Given the company and Employee interface, create a
findLeastPaid() method (that will do the opposite of whats
below). instead of finding the the best paid convert it
find the lowest paid person.
write a Java program that reads in a file of full time employees
with each line of
input containing the name of the employee (String) and gross Salary
(double) and stores
them in an array list. Then the arraylist of stored employees, find
and print the
less paid employee (name) and his/her pay. If there is more than
one such, print any one
lesst paid employee information.
---------------------------------------------------------------------------------
import java.util.*; // for the Scanner class
import java.io.*;
public class Company
{
public static void main (String[ ] args) throws
FileNotFoundException
{
new Company().run();
} // method main
/**
* Determines and prints out the best paid of the full-time
employees
* scanned in from a specified file.
*
*/
public void run() throws FileNotFoundException // see Section 2.3
{
final String INPUT_PROMPT = "Please enter the path for the file of
employees: ";
final String BEST_PAID_MESSAGE =
"\n\nThe best-paid employee (and gross pay) is ";
final String NO_INPUT_MESSAGE =
"\n\nError: 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
/**
* Returns the best paid of all the full-time employees scanned
in.
*
* @param sc – the Scanner object used to scan in the
employees.
*
* @return the best paid of all the full-time employees scanned
in,
* or null there were no employees scanned in.
*
*/
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
/**
* Returns the next full-time employee from the file scanned by a
specified Scanner
* object.
*
* @param sc – the Scanner object over the file.
*
* @return the next full-time employee scanned in from sc.
*
*/
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
---------------------------------------------------------------------------------
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
/**
* Returns this Employee object’s name.
*
* @return this Employee object’s name.
*
*/
String getName();
/**
* Returns this Employee object’s gross pay.
*
* @return this Employee object’s gross pay.
*
*/
double getGrossPay();
/**
* Returns a String representation of this Employee object with the
name
* followed by a space followed by a dollar sign followed by the
gross
* weekly pay, with two fractional digits (rounded).
*
* @return a String representation of this Employee object.
*
*/
String toString();
} // interface Employee
---------------------------------------------------------------------------------
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
/**
* Initializes this FullTimeEmployee object's name and gross pay
from a
* a specified name and gross pay.
*
* @param name - the specified name.
* @param grossPay - the specified gross pay.
*
*/
public FullTimeEmployee (String name, double grossPay)
{
this.name = name;
this.grossPay = grossPay;
} // 2-parameter constructor
/**
* Returns the name of this FullTimeEmployee object.
*
* @return the name of this FullTimeEmployee object.
*
*/
public String getName()
{
return name;
} // method getName
/**
* Returns the gross pay of this FullTimeEmployee object.
*
* @return the gross pay of this FullTimeEmployee object.
*
*/
public double getGrossPay()
{
return grossPay;
} // method getGrossPay
/**
* Returns a String representation of this FullTimeEmployee object
with the
* name followed by a space followed by a dollar sign followed by
the gross
* weekly pay, with two fractional digits (rounded), followed by "
FULL TIME".
*
* @return a String representation of this FullTimeEmployee
object.
*
*/
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
Answer:
I have modified the Company.java class to include the method findLeastPaid() and the call this method to find the least paid employee. The modified code is given below:
package emp;
import java.util.*; // for the Scanner class
import java.io.*;
public class Company
{
public static void main (String[ ] args) throws
FileNotFoundException
{
new Company().run();
} // method main
/**
* Determines and prints out the best paid of the
full-time employees
* scanned in from a specified file.
*
*/
public void run() throws FileNotFoundException // see
Section 2.3
{
final String INPUT_PROMPT = "Please
enter the path for the file of employees: ";
final String BEST_PAID_MESSAGE
=
"\n\nThe best-paid employee (and
gross pay) is ";
final String LEAST_PAID_MESSAGE
=
"\n\nThe least-paid employee (and gross pay) is
";
final String NO_INPUT_MESSAGE
=
"\n\nError: 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 leastPaid =
findLeastPaid (sc);
if (leastPaid == null)
System.out.println (NO_INPUT_MESSAGE);
else
System.out.println (LEAST_PAID_MESSAGE +
leastPaid.toString());
} // method run
/**
* Returns the best paid of all the full-time employees
scanned in.
*
* @param sc – the Scanner object used to scan in the
employees.
*
* @return the best paid of all the full-time employees
scanned in,
* or null there were no employees scanned in.
*
*/
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
/**
* Returns the best paid of all the full-time employees
scanned in.
*
* @param sc – the Scanner object used to scan in the
employees.
*
* @return the best paid of all the full-time employees
scanned in,
* or null there were no employees scanned in.
*
*/
public FullTimeEmployee findLeastPaid (Scanner
sc)
{
ArrayList<FullTimeEmployee>
employees = new ArrayList<FullTimeEmployee>();
while(sc.hasNext()) {
employees.add(new
FullTimeEmployee(sc.next(),sc.nextDouble()));
}
FullTimeEmployee leastPaid =
employees.get(0);
double leastAmount =
leastPaid.grossPay;
for(int
i=1;i<employees.size();i++) {
if(employees.get(i).grossPay < leastAmount) {
leastPaid = employees.get(i);
leastAmount = leastPaid.grossPay;
}
}
return leastPaid;
} // method findBestPaid
/**
* Returns the next full-time employee from the file
scanned by a specified Scanner
* object.
*
* @param sc – the Scanner object over the file.
*
* @return the next full-time employee scanned in from
sc.
*
*/
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
The Employee.java and FullTimeEmployee.java are unchanged.
The screenshot of an sample run is given below: