In: Computer Science
JAVA LANGUAGE coding required.
You are to take the payroll file called DeweyCheatemAndHow.txt to use as input to your program and produce a file that lists all the employee and their gross pay. At the end of the file you are to show the average gross pay for each type of employee.
In order to do this, you will need to create the following classes:
Employee:
Attribues:
First name
Last name
Address
Phone
SSN
Methods:
Appropriate constructors
Set methods for first name, last name, ssn
Get methods for last name, first name, ssn, address, phone
toString
Equals
Salaried: (Child of Employee)
Attributes:
Salary
Methods
Appropriate constructors
Set methods for Salary
Get methods for Salary
toString
Pay
Hourly: (Child of Employee)
Attributes:
Pay rate
Hours worked
Methods
Appropriate constructors
Set methods for pay rate, hours worked
Get methods for pay rate, hours worked
toString
Pay
Piece: (Child of Employee)
Attributes:
Quantity
Piece rate
Methods
Appropriate constructors
Set methods for Quantity, Piece rate
Get methods for Quantity, Piece rate
toString
Pay
Each line in the file start with a single letter that represents whether the employee is salaried, hourly or piece rate. The letters are s for salaried, h for hourly and p for piece rate. Below is an example of what each line will look like:
Code ssn first name last name address phone salary
s 123-45-6789 William Tell 1313 Mockingbird lane 513-556-7058 500.75
Code ssn first name last name address phone pay rate hours worked
h 123-45-6789 Scott Tell 1313 Mockingbird lane 513-556-7058 10 40.0
Code ssn first name last name address phone pay per piece number of pieces
p 123-45-6789 Chris Stevens 1313 Mockingbird lane 513-556-7058 7.50 1000
You are to process all the entries provided in the file and produce output that looks like this:
Dewey, Cheatem and How Law Firm
Payroll Report
First Name Last Name SSN Gross Pay $XXX.
First Name Last Name SSN Gross Pay $XXX.
First Name Last Name SSN Gross Pay $XXX.
First Name Last Name SSN Gross Pay $XXX.
First Name Last Name SSN Gross Pay $XXX.
First Name Last Name SSN Gross Pay $XXX.
First Name Last Name SSN Gross Pay $XXX.
.
.
.
Average salaried employee gross pay: $XXX
Average hourly employee gross pay: $XXX
Average pieces employee gross pay: $XXX
Please note that for this assignment you are to use the data in the file below as the source to input into your program but that the inputting is to be handled by your program prompting the user to input the information. The next assignment will address inputting directly from a file.
s 028-13-3948 Andrew Smith 1313 Mockingbird Lane 513-556-7000
500.75
s 028-24-9971 Andrew Whaley 1776 Liberty Ave 513-556-7001 675
h 112-45-7867 Saif Altarouti 3427 Smith Rd 513-556-7002 20 40
s 123-45-6789 Nicholas Alonso 920 Ohio Ave 513-556-7003 900
s 123-94-3938 Abigail Smith 1600 Penn St 513-556-7004 1200
h 123-97-4556 Matthew Stewart 2925 Campus Green 513-556-7005 16.5
40
s 142-78-2367 Syeda Mullen 345 Ludlow Ave 513-556-7006 763
p 143-49-0923 Arianna Rhine 768 Stratford Dr 513-556-7007 7.5
1000
p 193-93-1283 Emmalese Nuerge 132 Greyfox Rd 513-556-7008 8.5
1010
p 211-54-823 Joshua Ayers 671 Buckwheat Rd 513-556-7009 5.5
500
h 258-29-9102 Nicholas Roth 734 Student Dr 513-556-7010 25 35
Here is your solution if you have any doubt then you can write in the comment section.
I have taken input from the file as given in question if you want input from another method or any other modification then please write in the comment section.
Please give feedback.
Solution:
import java.io.File; // Import the File class
import java.util.Scanner; // Import the Scanner class to read text files
import java.io.FileWriter;
//class Employee
class Employee
{
//variables
String firstname;
String lastname;
String address;
String phone;
String SSN;
//constructor
Employee(String SSN,String firstname,String lastname,String address,String phone)
{
this.firstname=firstname;
this.lastname=lastname;
this.address=address;
this.phone=phone;
this.SSN=SSN;
}
//setter methods
public void setFirstname(String name)
{
firstname=name;
}
public void setLastname(String name)
{
lastname=name;
}
public void setSSN(String ssn)
{
SSN=ssn;
}
//getter methods
public String getFristname()
{
return firstname;
}
public String getlastname()
{
return lastname;
}
public String getSSN()
{
return SSN;
}
public String getPhone()
{
return phone;
}
public String getAddress()
{
return address;
}
//Override toString() method
public String toString()
{
return String.format(firstname + " " + lastname);
}
//Override equals() method
public boolean equals(Object o)
{
Employee emp=(Employee) o;
if(SSN.equals(emp.getSSN()))
return true;
else
return false;
}
}
//class Salaried
class Salaried extends Employee
{
double Salary;
//constructor
Salaried(String SSN,String firstname,String lastname,String address,String phone, double salary)
{
//call super class constructor
super(SSN,firstname,lastname,address,phone);
Salary=salary;
}
//getter methods
public double getSalary()
{
return Salary;
}
//setter methods
public void setSalary(double salary)
{
Salary=salary;
}
//calculate gross pay
public double pay()
{
return Salary;
}
//Override toString() method
public String toString()
{
return String.format(firstname + " " + lastname);
}
}
//class Hourly
class Hourly extends Employee
{
double Pay_rate;
double Hours_worked;
//constructor
Hourly(String SSN,String firstname,String lastname,String address,String phone, double rate,double hour)
{
//call super class constructor
super(SSN,firstname,lastname,address,phone);
Pay_rate=rate;
Hours_worked=hour;
}
//getter methods
public double getPayRate()
{
return Pay_rate;
}
public double getHoursWorked()
{
return Hours_worked;
}
//setter methods
public void setPayRate(double rate)
{
Pay_rate=rate;
}
public void setHoursWorked(double hour)
{
Hours_worked=hour;
}
//calculate gross pay
public double pay()
{
return Pay_rate*Hours_worked;
}
//Override toString()
public String toString()
{
return String.format(firstname + " " + lastname);
}
}
//class Piece
class Piece extends Employee
{
int Quantity;
double Piece_Rate;
//constructor
Piece(String SSN,String firstname,String lastname,String address,String phone,double rate,int qty)
{
//call super class constructor
super(SSN,firstname,lastname,address,phone);
Quantity=qty;
Piece_Rate=rate;
}
//getter methods
public int getQuantity()
{
return Quantity;
}
public double getPieceRate()
{
return Piece_Rate;
}
//setter methods
public void setQuantity(int qty)
{
Quantity=qty;
}
public void setPieceRate(double rate)
{
Piece_Rate=rate;
}
//calculate gross pay
public double pay()
{
return Quantity*Piece_Rate;
}
//Override toString() method
public String toString()
{
return String.format(firstname + " " + lastname);
}
}
//class Main
public class Main
{
public static void main(String[] args) {
//try catch block to handle Exception
try {
//variables to store gross pay
double salary_pay=0,piece_pay=0,hourly_pay=0;
//variables to count Number of Employee in each class
int salary_count=0,piece_count=0,hourly_count=0;
//open a file
File file = new File("DeweyCheatemAndHow.txt");
//open file in write mode
FileWriter myWriter = new FileWriter("Output.txt");
//Scanner Object to read a file
Scanner myReader = new Scanner(file);
//while loop to read all lines in a given file
while (myReader.hasNextLine())
{
//reading next line
String data = myReader.nextLine();
//split the string by spaces
String [] str=data.split(" +");
//if 1st String is "s"
if(str[0].equals("s"))
{
//increase salary_count means Employee in salary class increase
salary_count++;
//as address in file is of 3 lines so add these three strings
String address=str[4]+" "+str[5]+" "+str[6];
//convert string to double
double salary=Double.parseDouble(str[8]);
//create object of Salaried class
Salaried emp=new Salaried(str[1],str[2],str[3],address,str[7],salary);
//add pay to Salaried gross pay
salary_pay+=emp.pay();
//writing into file
myWriter.write(emp.getFristname()+" "+emp.getlastname()+" "+emp.getSSN()+" $"+emp.pay()+"\n");
}
//if first String is "h"
else if(str[0].equals("h"))
{
//increase hourly_count
hourly_count++;
//as address in file is of 3 lines so add these three strings
String address=str[4]+" "+str[5]+" "+str[6];
//convert String to double
double rate=Double.parseDouble(str[8]);
double hour_worked=Double.parseDouble(str[9]);
//create object of Hourly class
Hourly emp=new Hourly(str[1],str[2],str[3],address,str[7],rate,hour_worked);
//add pay to hourly gross pay
hourly_pay+=emp.pay();
//writing into file
myWriter.write(emp.getFristname()+" "+emp.getlastname()+" "+emp.getSSN()+" $"+emp.pay()+"\n");
}
else
{
//increase piece_count
piece_count++;
//as address in file is of 3 lines so add these three strings
String address=str[4]+" "+str[5]+" "+str[6];
//string to double
double rate=Double.parseDouble(str[8]);
//string to int
int qty=Integer.parseInt(str[9]);
//create employee of Piece class
Piece emp=new Piece(str[1],str[2],str[3],address,str[7],rate,qty);
//add pay in Piece gross pay
piece_pay+=emp.pay();
//writing data into file
myWriter.write(emp.getFristname()+" "+emp.getlastname()+" "+emp.getSSN()+" $"+emp.pay()+"\n");
}
}
//writing gross salaries into file
myWriter.write("Average salaried employee gross pay: $"+salary_pay/salary_count+"\n");
myWriter.write("Average Hourly employee gross pay: $"+hourly_pay/hourly_count+"\n");
myWriter.write("Average Pieces employee gross pay: $"+piece_pay/piece_count+"\n");
//closing files
myReader.close();
myWriter.close();
}
//catch exception
catch (Exception e)
{
System.out.println("An error occurred.");
}
}
}
Input:
DeweyCheatemAndHow.txt
s 028-13-3948 Andrew Smith 1313 Mockingbird Lane 513-556-7000
500.75
s 028-24-9971 Andrew Whaley 1776 Liberty Ave 513-556-7001 675
h 112-45-7867 Saif Altarouti 3427 Smith Rd 513-556-7002 20 40
s 123-45-6789 Nicholas Alonso 920 Ohio Ave 513-556-7003 900
s 123-94-3938 Abigail Smith 1600 Penn St 513-556-7004 1200
h 123-97-4556 Matthew Stewart 2925 Campus Green 513-556-7005 16.5
40
s 142-78-2367 Syeda Mullen 345 Ludlow Ave 513-556-7006 763
p 143-49-0923 Arianna Rhine 768 Stratford Dr 513-556-7007 7.5
1000
p 193-93-1283 Emmalese Nuerge 132 Greyfox Rd 513-556-7008 8.5
1010
p 211-54-823 Joshua Ayers 671 Buckwheat Rd 513-556-7009 5.5
500
h 258-29-9102 Nicholas Roth 734 Student Dr 513-556-7010 25 35
Output:
Output.txt

Thank You!