Purpose : This assignment is designed work with text files, string manipulation
ID: 3533906 • Letter: P
Question
Purpose: This assignment is designed work with text files, string manipulation and
inheritance.
Assignment Description:
A Java program should be created to read an employee text file (Information.txt) and
decide from the employee number and shift which department and shift the employee is
assigned. A list of the employees and their information should be listed to the monitor as
well as written to a text file named Department.txt.
Instructions:
Create a class named Employee including a constructor and the necessary accessor and
mutator methods. The class should keep the following information in fields:
o Employee Name
o Employee Number in the format X-NNN, where each N is a digit and X is a
letter which can be one of the following:
? H - represents Human Resources
? A - represents Accounting
? P - represents Production
? S - represents Shipping
o Hire Date
Next, write a class named ProductionWorker that inherits from the Employee class. Include
a constructor and the necessary accessor and mutator methods. The ProductionWorker class
should keep the following information in fields:
o Shift Number which is an integer and can be one of the following:
? 1 - represents Morning Shift
? 2 - represents Swing Shift
? 3 - represents Night Shift
o Hourly Pay Rate
Input: The input file will be named Information.txt. One record (line) will contain the
employee name, employee number, hire date, shift number and pay rate each separated by
spaces. A sample text file is below.
Jane Rivers, A-902, 05/16/2001, 1, 16.25
Bob Cox, S-823, 06/21/1990, 2, 17.50
Ann Ramsey, A-715, 02/12/1998, 1, 16.25
Joseph Chandler, P-723, 12/22/2000, 3, 14.35
Arnold Kennedy, S-133, 08/10/1999, 2, 18.20
Larry Huber, P-198, 02/12/2000, 3, 17.25
Annette Wilson, H-501, 04/04/1995, 1, 20.25
Robert Ferguson, H-674, 04/10/2002, 2, 16.50
Ava Gaines, H-434, 01/05/2000, 3, 15.65
Output: Output should consist of a listing of the employee name, employee number,
employee's department, hire date, shift name and pay rate This information should be
displayed on the monitor as well as written to a text file named Department.txt. The format
for writing to the monitor could be as follows:
Jane Rivers A-902 Accounting 05/16/2001 Morning Shift 16.25
Bob Cox S-823 Shipping 06/21/1990 Swing Shift 17.50
Ann Ramsey A-715 Accounting 02/12/1998 Morning Shift 16.25
Joseph Chandler P-723 Production 12/22/2000 Night Shift 14.35
Arnold Kennedy S-133 Shipping 08/10/1999 Swing Shift 18.20
Larry Huber P-198 Production 02/12/2000 Night Shift 17.25
Annette Wilson H-501 Human Resources 04/04/1995 Morning Shift 20.25
Robert Ferguson H-674 Human Resources 04/10/2002 Swing Shift 16.50
Ava Gaines P-434 Production 01/05/2000 Night Shift 15.65
Requirements:
? Inheritance must be used as described in the instructions
Explanation / Answer
######################## Employee.java ######################## /* Information for Employee class * * Employee Name Employee Number in the format X-NNN, where each N is a digit and X is a letter which can be one of the following: H - represents Human Resources A - represents Accounting P - represents Production S - represents Shipping Hire Date * */ public class Employee { private String name; private String num; private String date; public Employee(String name, String num, String date) { this.name = name; this.num = num; this.date = date; //conversion of dept number logic. if (num.startsWith("A")) { this.num = num + " Accounting"; }else if(num.startsWith("H")) { this.num = num + " Human Resources"; }else if(num.startsWith("P")) { this.num = num + " Production"; }else if(num.startsWith("S")) { this.num = num + " Shipping"; } } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } } ################################## PersonalWroker.java ################################## /* Information for PersonalWorker class * Shift Number which is an integer and can be one of the following: * 1 - represents Morning Shift * 2 - represents c * 3 - represents Night Shift * Hourly Pay Rate */ public class PersonalWorker extends Employee { private String payrate; private String[] shiftArr = {"","Morning Shift", "Morning Shift", "Night Shift"}; private String shiftName; public PersonalWorker(String name, String num, String date, int shift, String payrate) { super(name, num, date); //Conversion of shiftName logic this.shiftName = shiftArr[shift]; this.payrate = payrate; } public String getPayrate() { return payrate; } public void setPayrate(String payrate) { this.payrate = payrate; } public String getShiftName() { return shiftName; } public void setShiftName(String shiftName) { this.shiftName = shiftName; } @Override public String toString() { return getName() + " " + getNum() + " " +getDate() + " " + shiftName + " " + payrate; } } ######################################## FileProcessor.java ######################################## import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; public class FileProcessor { private static String empFile = "Information.txt"; private static String deptFile = "Department.txt"; public static void main(String[] args){ ArrayList empData = readFile(empFile); ArrayList opData = new ArrayList(); String name; String num; String date; int shift; String payrate; for (String str: empData) { StringTokenizer st = new StringTokenizer(str, ","); name = st.nextToken().trim(); num = st.nextToken().trim(); date = st.nextToken().trim(); shift = Integer.parseInt(st.nextToken().trim()); payrate = st.nextToken().trim(); PersonalWorker pw = new PersonalWorker(name, num, date, shift, payrate); opData.add(pw.toString()); System.out.println(pw.toString()); } writeToFile(opData, deptFile); } private static ArrayList readFile(String fileName) { ArrayList data = new ArrayList(); try { BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName))); String line; while ((line = br.readLine()) != null) { data.add(line); } br.close(); } catch (IOException e) { System.out.println("ERROR: unable to read file " + fileName); e.printStackTrace(); } return data; } private static void writeToFile(ArrayList data, String fileName) { try { BufferedWriter out = new BufferedWriter(new FileWriter(fileName)); for (String str: data) { out.write(str + " "); } out.close(); } catch (IOException e) {} } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.