In java language Instructions: (This is the same asmt we did in class, the only
ID: 3849498 • Letter: I
Question
In java language Instructions: (This is the same asmt we did in class, the only difference is that the data file does not tell us how many employees we have. You need to figure out how to handle this situation.) ----------------- Write a program to model three different kinds of employees in a company using inheritance and File I/O. Your program will consist of three classes that extend the class Employee and a testing class Payroll.java that reads in payroll information from a file then prints it to the console window. The Classes Manager, HourlyWorker, and CommissionEmployee: ----------------------------------------------------------------------------------------- To model different types of employees, extend the provided abstract class Employee by writing three new classes: Manager, HourlyWorker, and CommissionEmployee. Implement the abstract methods as required. Managers get paid a fixed weekly salary, commission employees get paid based on base salary and commission (commission rate x sales), hourly workers get paid an hourly wage with time-and-a-half: 1.5 times the hourly wage for hours worked over 40 hours. Do not create additional fields, and only write methods that override methods from either Employee or Object. The Class Payroll: Create a class called Payroll with a main method to test your classes. This method should create an array of type Employee (think about what size you use?) Then, populate the array using data from the text file payroll.txt using appropriate Java I/O techniques. Each line of the file will look similar to one of these: #Steve, Davis,2000 *John, Kanet, 800, 7000, 0.10 @Thomas, Hill, 20, 50 *Lisa, Green,800,6000,0.10 In order to populate the array, you will need to split() each String on the (,) character so you can access each individual value of data. Based on the first value (e.g., #) your method will know whether to create a new Manager, HourlyWorker, or CommissionWorker object. Once created, populate the object with the remaining values then store it in the array. Finally, iterate through the array of employees using the enhanced for loop syntax, and print out each employee in the following format: Manager: Steve Davis $2000 Commission Employee: John Kanet $1500 (Base salary: $800, sales: $7000, Commission rate: 10%) Hourly Worker: Thomas Hill $1100 (Hourly wage: $20, hours worked: 50)
Explanation / Answer
The program you require is stated below.Hope you will understand it. Please go through the comments in the program for better understanding of the program.
EmpPayroll.java
import java.util.ArrayList;
import java.util.Scanner;
import java.util.InputMismatchException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class EmpPayroll
{
//Used for looking at the input
private Scanner scan;
//Object used for enumerated commands that are inputted by the user
private PayrollMenu PayMenu;
//will be used to store the EmpName of the input file specified in the main method of Company
private String ipFile;
private PrintWriter pw;
private Employee[] listing;
//stores the length of listing
private int length;
public static final int FROMSTART = 100;
public static final String OP = "output.txt";
public EmpPayroll(String args)
{
ipFile = args;
PayMenu = new PayrollMenu();
listing = new Employee[FROMSTART];
length = 0;
try
{
scan = new Scanner(new FileReader(ipFile));
}
catch (FileNotFoundException e)
{
System.pw.println("File was not found.......");
System.exit(0);
}
try
{
pw = new PrintWriter(OP);
}
catch (FileNotFoundException e)
{
System.pw.println("File was not found.......");
System.exit(0);
}
}
//starts executing commands on basis of the input file
public void start()
{
String cmd = scan.nextLine();
if (cmd.compareToIgnoreCase("AddingEmployee") == 0)
addEmp();
else if (cmd.compareToIgnoreCase("RemovingEmployee") == 0)
rmEmp();
else if (cmd.compareToIgnoreCase("ShowingEmployees") == 0)
shEmp();
else if (cmd.compareToIgnoreCase("weeklyPayroll") == 0)
PayWeek();
else
{
System.pw.println("Exiting...");
System.exit(0);
}
}
//Continue the program after the function is executed
public void startAgain()
{
if (scan.hasNext())
{
String cmd = scan.next();
if (cmd.compareToIgnoreCase("AddingEmployee") == 0)
addEmp();
else if (cmd.compareToIgnoreCase("RemovingEmployee") == 0)
rmEmp();
else if (cmd.compareToIgnoreCase("ShowingEmployees") == 0)
shEmp();
else if (cmd.compareToIgnoreCase("weeklyPayroll") == 0)
PayWeek();
}
else
{
System.pw.println();
System.pw.println("Finished Executing.. Examine " + OP + " to watch the result of your inquiries. Now exiting...");
System.pw.println();
pw.close();
System.exit(0);
}
}
//Returns true if EmpName is present in the list already
public boolean doesExist(String EmpName)
{
int uu = 0;
while ((uu <= (length - 1)) && listing[uu] != null)
{
if (EmpName.compareToIgnoreCase(listing[uu].getName()) == 0)
{
return true;
}
uu++;
}return false;
}
//Returns True on listing is full
public boolean listFull()
{
if (length() == FROMSTART)
{
pw.println("Database full. Please delete record of an employee to add new record.");
pw.close();
return true;
}
else return false;
}
public int length() {return length;}
//Adds an Employee
public void addEmp()
{
Employee emp;
int uu = 0;
if (listFull())
{
pw.println("Database full. No Entries can be added. Please delete to free space in database.");
scan.nextLine();
scan.nextLine();
startAgain();
}
String EmpName = scan.next();
EmpName += (" " + scan.next());
if (doesExist(EmpName))
{
pw.println("Employee " + EmpName + " record is present in the database.");
scan.nextLine();
scan.nextLine();
startAgain();
}
String variety = scan.next();
double fkWage = scan.nextDouble();
int wages = (int) (fkWage * 100);
if (variety.compareToIgnoreCase("hourly") == 0)
{
emp = new Hourly(EmpName, wages);
pw.println("Employee " + EmpName + ", an " + variety + " employee earning " + CurrencyFormat.format(wages) + "/hr has been successfully added.");
pw.println();
}
else if (variety.compareToIgnoreCase("salaried") == 0)
{
emp = new Salaried(EmpName, wages);
pw.println("Employee " + EmpName + ", a " + variety + " employee with an annual salary of " + CurrencyFormat.format(wages) + " has been successfully added.");
pw.println();
}
else
{
emp = new Contracted(EmpName, wages);
pw.println("Employee " + EmpName + ", a " + variety + " employee earning " + CurrencyFormat.format(wages) + "/hr has been successfully added.");
pw.println();
}
while (listing[uu] != null)
{
uu++;
}
listing[uu] = emp;
length++;
startAgain();
}
//For removing Records Of Employee From Database
public void rmEmp()
{
int uu = 0;
scan.nextLine();
String EmpName = scan.nextLine();
while (uu <= 99)
{
if (listing[0] == null)
{
pw.println("No employees in the database to delete.");
startAgain();
}
else if (doesExist(EmpName) == true)
{
listing[uu] = listing[length - 1];
listing[length - 1] = null;
length--;
pw.println(EmpName + " successfully removed.");
pw.println();
startAgain();
}
else uu++;
}
pw.println("Employee named " + EmpName + " not found. Deletion of " + EmpName + " failed.");
pw.println();
startAgain();
}
//Prints the list of all employees in database
public void shEmp()
{
int uu = 0;
if (length == 0)
{
pw.println("No records in database to show.Please add some...");
startAgain();
}
while (uu <= 99 && listing[uu] != null)
{
pw.println(listing[uu].toString());
uu++;
}
startAgain();
}
//Calculates and prints the weekly payment of the employees
public void PayWeek()
{
int uu, pp, mm = 0;
String[] indName = new String[length];
int[] indHours = new int[length];
if (length == 0)
{
pw.println("Databse consists of no records to make payment for.");
startAgain();
}
for (uu = 0; uu <= (length - 1); uu++)
{
scan.nextLine();
String EmpName = scan.nextLine();
int hrs = scan.nextInt();
indName[uu] = EmpName;
indHours[uu] = hrs;
}
for (pp = 0; pp <= (length - 1); pp++)
{
for (mm = 0; mm <= (length - 1); mm++)
{
if (indName[pp].compareToIgnoreCase(listing[mm].getName()) == 0)
{
pw.println(listing[mm].createWeeklyPaycheck(indHours[pp]).toString());
}
}
}
startAgain();
}
}
Please rate the answer if it helped.....Thankyou
Hope it helps....
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.