Question

In: Computer Science

JAVA LANGUAGE coding required. You are to take the payroll file called DeweyCheatemAndHow.txt to use as...

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

Solutions

Expert Solution

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!


Related Solutions

In java beginner coding language ,Write a class called Sphere that contains instance data that represents...
In java beginner coding language ,Write a class called Sphere that contains instance data that represents the sphere’s diameter. Define the Sphere constructor to accept and initialize the diameter, and include getter and setter methods for the diameter. Include methods that calculate and return the volume and surface area of the sphere. Include a toString method that returns a one-line description of the sphere. Create a driver class called MultiSphere, whose main method instantiates and updates several Sphere objects.
In this problem, we provide you with a Java file called Question2.java. Suppose that there is...
In this problem, we provide you with a Java file called Question2.java. Suppose that there is an array and let us call this array, items. The array items contains n integers. These n integers are in a completely random order in the array. What you are asked to do is to reorder the items in the array such that all the negative integers come before all the zeros and all the positive integers appear at the end. Note, this question...
execute coding examples that demonstrate the 4 scope rules file,function,class,block coding language c++
execute coding examples that demonstrate the 4 scope rules file,function,class,block coding language c++
Please use Java language! With as many as comment! ThanksWrite a static method called "evaluate"...
In Java language Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the expression evaluates to. The method MUST use a stack...
language is java Use method overloading to code an operation class called CircularComputing in which there...
language is java Use method overloading to code an operation class called CircularComputing in which there are 3 overloaded methods as follows: computeObject(double radius)-compute area of a circle computeObject(double radius, double height)-compute area of a cylinder computeObject(double radiusOutside, double radiusInside, double height)-compute volume of a cylindrical object These overloaded methods must have a return of computing result in each Then override toString() method so it will return the object name, the field data, and computing result Code a driver class...
**Please write in Java, in a very simple/beginner coding style/language - thank you!** Directions: Given a...
**Please write in Java, in a very simple/beginner coding style/language - thank you!** Directions: Given a factorial n!. Find the sum of its digits, and the number of trailing zeros (ie: the number of zeros at the end of the factorial). Input: integer nn, a non-negative integer where n≤20n≤20 Output: integer xyxy, the concatenation of x and y, where x is the sum of the digits in n! and y is the number of the zeros in n!) Note, 0≤x,y0≤x,y....
Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
Please use Java language in an easy way with comments! Thanks! Write a static method called...
Please use Java language in an easy way with comments! Thanks! Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the...
Please use Java language in an easy way with comments! Thanks! Write a static method called...
Please use Java language in an easy way with comments! Thanks! Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the...
For c language. I want to read a text file called input.txt for example, the file...
For c language. I want to read a text file called input.txt for example, the file has the form. 4 hello goodbye hihi goodnight where the first number indicates the n number of words while other words are separated by newlines. I want to store these words into a 2D array so I can further work on these. and there are fewer words in the word file than specified by the number in the first line of the file, then...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT