Write a JAVA Program named Project2 that maintains wage information for the empl
ID: 3686861 • Letter: W
Question
Write a JAVA Program named Project2 that maintains wage information for the employees of a company.
The company maintains a text file of all of the current employees called EmployeesIn.dat.
Each line of the file contains the employees name (last name, first name), a character code indicating their wage status (h for hourly or s for salary), and their wage (pay/hour for hourly employees and the annual salary for salary employees). The name, wage status and wage are separated by varying amounts of white space (at minimum, there are two spaces).
For example,
Harris, Joan h 5.00
Baird, Joey h 7.50
Kinsey, Paul h 8.00
Olson, Margaret s 15000
Campbell, Peter s 20000
Draper, Donald s 40000
Sterling, Roger s 45000
Cooper, Bertram s 50000
Once a week (generally on Friday), updates to the file are made and the weekly paycheck amount is calculated. The update information is read from a second text file called Updates.dat. Updates can be any of the following:
adding a new employee (n),
raising the rate of pay for all employees by a given percentage (r), or
dismissal of an existing employee (d).
For example, the file could contain the following lines:
n Pryce, Lane s 40000
r 5
d Kinsey
After performing the updates, the revised information is written to a text file named EmployeesOut.dat a summary report is displayed on the console in the following format,
New Employee added: Pryce, Lane
New Wages:
Harris, Joan $5.25/hour
Baird, Joey $7.88/hour
Olson, Margaret $15750.00/year
Campbell, Peter $21000.00/year
Draper, Donald $42000.00/year
Sterling, Roger $47250.00/year
Cooper, Bertram $52500.00/year
Pryce, Lane $42000.00/year
Deleted Employee: Kinsey, Paul
Paycheck amounts are calculated based on each employees number of hours worked. This information is included in the file HoursWorked.dat. This file simply lists each employees last name and the number of hours worked that week, separated by white space. For example,
Harris 65
Baird 40
Olson 70
Campbell 40
Draper 60
Sterling 40
Cooper 35
Pryce 45
After calculating each employees pay for the week, a report is displayed on the console in the following format,
Paycheck amount:
Harris, Joan $406.88
Baird, Joey $315.00
Olson, Margaret $302.88
Campbell, Peter $403.85
Draper, Donald $807.69
Sterling, Roger $908.65
Cooper, Bertram $1009.62
Pryce, Lane $807.69
--------- Total $4962.26
(Please note that all the examples shown are for formatting purposes only; do not expect that the numeric values reflect correct calculations in any way.)
Write two classes that store information about an employee:
HourlyEmployee, which extends the abstract class Employee. The constructor will take a name and hourly wage as its parameters. Methods will include computePay and toString. To determine the employees pay computePay multiples the first 40 hours (or fewer) by the employees hourly wage. Hours worked beyond 40 are paid at time-and a-half (1.5 times the hourly wage). toString() returns a string containing the employees name and hourly wage, formatted as shown in the example output of the r command. Note that spaces are added between the names and wage so that the entire string is 40 characters long.
SalariedEmployee, which extends the abstract class Employee. The constructor will take a name and annual salary as its parameters. Methods will include a getter and a setter for the annual salary, along with computePay and toString(). (Note that the annual salary must be converted to an hourly wage, because thats what the Employee class requires. To do this conversion, assume that a salaried employee worked 40 hours a week for 52 weeks) computePay always returns 1/52 of the annual salary, regardless of the number of hours worked. toString() returns a string containing the employees name and annual salary, formatted as shown in the example output for the r command. (Do not add a separate instance variable for the annual salary. Follow the program specifications in order to receive full credit for this project.)
Use an array to store employee records of data type Employee. The array should be managed in a class called PersonnelManager. Each element of the array will store a reference to either an HourlyEmployee object or a SalariedEmployee object. The array used to store employee objects must contain only one element initially. When the array becomes full, it must be doubled in size by calling a method written for this purpose in the PersonnelManager class.
Your main method is located in the Project2 class. It instantiates a PersonnelManager object and calls methods to handle reading and writing the data files. It instantiates a PersonnelManager object in order to process the employee updates and generate the weekly payroll
Here are a few other requirements for the program:
After updating the employee records and calculating the weekly payroll, the revised employee information is to be stored in the same format as the original EmployeesIn.dat file. However, name this output file EmployeesOut.dat.
Dollar amounts must be displayed correctly, with two digits after the decimal point. For example, make sure that your program displays $100.00 not $100.0.
All input may be preceded or followed by spaces. Character command codes may be uppercase or lowercase letters. If the user enters a character code other than n, d, h, s, or r, the program must display the following message along with the line containing the invalid character:
Command was not recognized; <line containing the error>
Write this program in JAVA and compile it in JDK 1.8 or better. Follow all commenting conventions discussed in class, including a comment block at the top of the each file with your name, date, the course number and section, program purpose, input, and output. It is expected that your program will be well documented and you are required to include a private helper method called printHeading that outputs the following information to the console in an easy-to-read format: your name, the project number, the course identifier, and the current semester. You will call this method as the first statement in your main method.
Explanation / Answer
Employee.dat
Michael Jordan $600000.00/year
Lebron James $600.00/hour
Employee.java
import java.util.*;
public class Employee
{
//Instance variables
private String name;
private double hrWage;
//Constructor
public Employee(String name, double hrWage)
{
this.name = name;
this.hrWage = hrWage;
}
public void setName(String name)
{ this.name = name; }
public void setWage(double hrWage)
{
this.hrWage = hrWage;
}
public void raiseWage(double percentage)
{
this.hrWage = hrWage*(1+(percentage/100)); //20$/hr * (1 + 30%) increase
}
public double computePay(int hrsworked)
{ return this.hrWage*hrsworked; }
public String getName()
{ return this.name; }
public double getWage()
{ return this.hrWage; }
}
HourlyEmployee.java
import java.util.*;
import java.text.*;
public class HourlyEmployee extends Employee
{
public HourlyEmployee(String name, double hrWage)
{
super(name,hrWage);
}
public double computePay(int hrsworked)
{
if(hrsworked>40)
return 40*(super.getWage()) + (hrsworked-40)*(1.5*super.getWage());
else
return hrsworked*(super.getWage());
}
public String getName()
{ return super.getName(); }
public String toString()
{
DecimalFormat fix = new DecimalFormat("0.00");
return super.getName()+" $"+fix.format(super.getWage())+"/hour ";
}
}
Personnel.java
import java.text.*;
import java.io.*;
import java.util.*;
public class Personnel
{
public static Scanner stdin = new Scanner(System.in);
public static ArrayList<Object> database = new ArrayList<Object>(1);
public static void main (String[]args) throws IOException
{
menu();
}
public static void menu() throws IOException
{
clearScreen();
for(int i = 0; i < 40; i++)
System.out.print("-");
System.out.println();
System.out.print("|Commands: n - New employee | ");
System.out.print("| c - Compute paychecks | ");
System.out.print("| r - Raise Wages | ");
System.out.print("| p - Print records | ");
System.out.print("| d - Download data | ");
System.out.print("| u - Upload data | ");
System.out.print("| q - Quit | ");
for(int i = 0; i < 40; i++)
System.out.print("-");
System.out.println();
System.out.print("Enter Command:");
char command = stdin.nextLine().toLowerCase().charAt(0);
switch (command)
{
case 'n': new_emp();
menu();
break;
case 'c': compute_pay();
menu();
break;
case 'r': raise_wages();
menu();
break;
case 'p': print_records();
menu();
break;
case 'd': download();
menu();
break;
case 'u': upload();
menu();
break;
case 'q': break;
default : System.out.println("Command was not recognized; press enter to try again...");
stdin.nextLine();
menu();
}
}
private static void clearScreen()
{
System.out.println("u001b[Hu001b[2J");
}
public static void new_emp()
{
System.out.print("Enter the name of new employee: ");
String emp_name = stdin.nextLine();
System.out.print(" Hourly (h) or salaried (s): ");
char emp_type = stdin.nextLine().toLowerCase().charAt(0);
switch (emp_type)
{
case 'h':
System.out.print(" Enter hourly wage: ");
double hrwage = stdin.nextDouble();
database.add(new HourlyEmployee(emp_name,hrwage));
break;
case's':
System.out.print(" Enter Annual Salary: ");
double salary = stdin.nextDouble();
database.add(new SalariedEmployee(emp_name,salary));
break;
//If anything else show error message
default: System.out.println("Input was not h or s; please try again:");
new_emp();
break;
}
System.out.println("Press Enter to continue...");
stdin.nextLine();
stdin.nextLine();
}
public static void compute_pay() //Computes pay for any employee
{
DecimalFormat fix = new DecimalFormat("0.00"); //To format currency properly
database.trimToSize(); //Always trim arraylist for efficiency
for(int i = 0; i < database.size(); i++)
{
System.out.print(" Enter number of hours worked by "+((Employee) database.get(i)).getName()+":");
int hrs = stdin.nextInt();
System.out.println("Pay: $"+fix.format( ((Employee) database.get(i)).computePay(hrs)) );
}
System.out.println();
System.out.println("Press Enter to continue...");
stdin.nextLine();
stdin.nextLine();
}
public static void raise_wages() //Raises all wages by a certain percentage
{
database.trimToSize();
System.out.print("Enter percentage increase: ");
double percentincrease = stdin.nextDouble();
for(int i = 0; i < database.size(); i++)
((Employee) database.get(i)).raiseWage(percentincrease);
System.out.println(" New Wages _________");
print_records();
stdin.nextLine();
}
public static void print_records()
{
if(database.isEmpty())
System.out.println("There are currently no records");
else
{
for(int i = 0; i < database.size(); i++)
System.out.print(((Employee) database.get(i)).toString());
}
System.out.println("Press Enter to continue...");
stdin.nextLine();
}
public static void download() throws IOException //Saves all current records in file Employee.dat
{
database.trimToSize();
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("Employee.dat")));
for(int i = 0; i < database.size(); i++)
out.print(((Employee) database.get(i)).toString());
out.close();
System.out.println("Total Employees Processed:"+database.size()+" Press Enter to continue...");
stdin.nextLine();
}
public static void upload() throws IOException //Loads all current records from file Employee.dat into arraylist
{
database.trimToSize();
int count = 0;
Scanner in = new Scanner(new File("Employee.dat"));
while (in.hasNext())
{
//Sample Strings
//"Mike Dumas $44.00/hour"
//"Susan Stroupe $44000.00/year"
String line = in.nextLine();
String emp_name = line.substring(0,(line.indexOf("$"))).trim();
Double payrate = Double.parseDouble( line.substring((line.indexOf("$")+1),line.indexOf("/")) );
if(line.endsWith("hour")) //if true its a hourly employee
{database.add(new HourlyEmployee(emp_name,payrate));
count++;}
if(line.endsWith("year")) //if true its a salaried employee
{database.add(new SalariedEmployee(emp_name,payrate));
count++;}
}
System.out.println("Successfully Uploaded "+count+" Employees Press Enter to Continue...");
stdin.nextLine();
}
}
SalariedEmployee.java
import java.text.*;
public class SalariedEmployee extends Employee
{
public SalariedEmployee(String name, double AnnualSal)
{super( name,( AnnualSal/(52*40) ) ); }
public double getAnnualSal()
{return super.getWage()*40*52; }
public void setAnnualSal(double AnnualSal)
{super.setWage( AnnualSal/(52*40) ); }
public double computePay(int hrs) //return 1/52 of annual salary regardless of hrs worked
{return getAnnualSal()/52; }
public String getName()
{ return super.getName(); }
public String toString()
{ DecimalFormat fix = new DecimalFormat("0.00");
return super.getName()+" $"+fix.format(getAnnualSal())+"/year ";}
}
sample output
----------------------------------------
|Commands: n - New employee |
| c - Compute paychecks |
| r - Raise Wages |
| p - Print records |
| d - Download data |
| u - Upload data |
| q - Quit |
----------------------------------------
Enter Command:n
Enter the name of new employee: jaya
Hourly (h) or salaried (s): h
Enter hourly wage: 300
Press Enter to continue...
[H [2J
----------------------------------------
|Commands: n - New employee |
| c - Compute paychecks |
| r - Raise Wages |
| p - Print records |
| d - Download data |
| u - Upload data |
| q - Quit |
----------------------------------------
Enter Command:n
Enter the name of new employee: kumari
Hourly (h) or salaried (s): s
Enter Annual Salary: 200000
Press Enter to continue...
[H [2J
----------------------------------------
|Commands: n - New employee |
| c - Compute paychecks |
| r - Raise Wages |
| p - Print records |
| d - Download data |
| u - Upload data |
| q - Quit |
----------------------------------------
Enter Command:c
Enter number of hours worked by jaya:68
Pay: $24600.00
Enter number of hours worked by kumari:8
Pay: $3846.15
Press Enter to continue...
[H [2J
----------------------------------------
|Commands: n - New employee |
| c - Compute paychecks |
| r - Raise Wages |
| p - Print records |
| d - Download data |
| u - Upload data |
| q - Quit |
----------------------------------------
Enter Command:r
Enter percentage increase: 5
New Wages
_________
jaya $315.00/hour
kumari $210000.00/year
Press Enter to continue...
[H [2J
----------------------------------------
|Commands: n - New employee |
| c - Compute paychecks |
| r - Raise Wages |
| p - Print records |
| d - Download data |
| u - Upload data |
| q - Quit |
----------------------------------------
Enter Command:p
jaya $315.00/hour
kumari $210000.00/year
Press Enter to continue...
[H [2J
----------------------------------------
|Commands: n - New employee |
| c - Compute paychecks |
| r - Raise Wages |
| p - Print records |
| d - Download data |
| u - Upload data |
| q - Quit |
----------------------------------------
Enter Command:d
Total Employees Processed:2
Press Enter to continue...
[H [2J
----------------------------------------
|Commands: n - New employee |
| c - Compute paychecks |
| r - Raise Wages |
| p - Print records |
| d - Download data |
| u - Upload data |
| q - Quit |
----------------------------------------
Enter Command:u
Successfully Uploaded 2 Employees
Press Enter to Continue...
[H [2J
----------------------------------------
|Commands: n - New employee |
| c - Compute paychecks |
| r - Raise Wages |
| p - Print records |
| d - Download data |
| u - Upload data |
| q - Quit |
----------------------------------------
Enter Command:q
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.