Part 1: Finish a partially completed program that: reads pay roll data from a fi
ID: 3876977 • Letter: P
Question
Part 1:
Finish a partially completed program that:
reads pay roll data from a file using a BufferedReader
splits it into variables
calculates regular pay, overtime and total pay
writes the results to a file using a BufferedWriter.
Use the attached file PayrollProcessor.java and the input file EmployeePayData.txt. EmployeePayroll.txt shows the correct results based on this input.
Use the split method of the String class, to split data from the input file as shown in BufferedReaderWriterDemo.java attached.
Part 2
In a new java file called addressBook.java
Create two parallel arrays:
1 array of names (i.e. strings) called Names
1 array of phone numbers (i.e. strings) called PhoneNumbers
They will contain 10 elements each.
You can hardcode the values.
Then, create a function which will look for a specific person in the array Names. If the name exists, the function will display the name followed by the phone number. If the name does not exist, the function will display a message stating that the person does not exist.
It is perfectly ok to start with an existing java example that was given to you. Make sure to use your best programming practices.
1st Program
/*
** Program : PayrollProcessor.java
**
** Purpose : To calculate payroll and write it to output file using input data from a file.
**
** Developer:
**
** TODO: Complete this program.
*/
package payrollProcessor;
import java.io.IOException;
import java.util.regex.PatternSyntaxException;
import java.io.File;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader ;
import java.io.FileWriter;
public class StringParser
{
final static String IN_FILE_NAME = "c:/data/EmployeePayData.txt"; // file path may need to be different on your system
final static String OUT_FILE_NAME = "c:/data/EmployeePayroll.txt"; // file path may need to be different on your system
final static double MAX_REGULAR_HOURS = 40.0;
final static double OVERTIME_FACTOR = 1.5;
public static void main(String[] args)
{
// Declare File, FileReader and BufferedReader objects
// Declare File, FileWriter and BufferedWriter objects
try
{
// Instantiate File, FileReader and BufferedReader objects
}
catch (Exception ex)
{
System.out.println(" An Exception occurred while creating a BufferedReader for " + IN_FILE_NAME + " : " + ex.getMessage() + ". ");
System.exit(-1);
}
try
{
// Instantiate File, FileWriter and BufferedWriter objects
}
catch (Exception ex)
{
System.out.println(" An Exception occurred while creating a BufferedWriter for " + OUT_FILE_NAME + " : " + ex.getMessage() + ". ");
System.exit(-2);
}
int recordsProcessed = 0;
try
{
recordsProcessed = processPayrollFile(bufferedReaderObject, bufferedWriterObject);
}
catch (PatternSyntaxException ex)
{
System.out.println(" A PatternSyntaxException occurred while reading " + IN_FILE_NAME + " : " + ex.getMessage() + ". ");
}
catch (IOException ex)
{
System.out.println(" An IOException occurred while reading " + IN_FILE_NAME + " : " + ex.getMessage() + ". ");
}
catch (Exception ex)
{
System.out.println(" An Exception occurred while reading " + IN_FILE_NAME + " : " + ex.getMessage() + ". ");
}
finally // actions to perform regardless of whether exceptions previously occurred.
{
System.out.println(" recordsProcessed: " + recordsProcessed);
try
{
bufferedReaderObject.close();
}
catch( Exception ex)
{
System.out.println(" An Exception occurred while closing " + IN_FILE_NAME + " : " + ex.getMessage() + ". ");
}
try
{
bufferedWriterObject.close();
}
catch( Exception ex)
{
System.out.println(" An Exception occurred while closing " + OUT_FILE_NAME + " : " + ex.getMessage() + ". ");
}
}
}
// Why is this method static?
private static int processPayrollFile(BufferedReader br, BufferedWriter bw) throws PatternSyntaxException, IOException, Exception
{
String employeeID = "";
String lastName = "";
String firstName = "";
String dept = "";
double payrate = 0.0;
double hoursWorked = 0.0;
double[] weeklyPayArray = new double[3]; // Position 0 = regular pay, Position 1 = Overtime pay, Position 3 = Total
int recordsProcessed = 0;
String inputLine = "";
while ( ...) // complete this with code that both reads the BufferedReader and controls the loop.
{
// add code to split the input line.
// add code to assign data to variables
weeklyPayArray = calcPay( hoursWorked, payrate);
// add code to write data to the BufferedReader. Remember to convert doubles to Strings. Writing Strings is a bit awkward. The relevant method needs the starting position and the length.
// Write a new line character after each employee. Use the Java system line separator so you don't ave to hard code " " or " ".
recordsProcessed++;
}
return recordsProcessed;
}
// Why is this method static?
private static double[] calcPay(double hours, double rate) // Why is this method static?
{
// Create a 3 element array of doubles for pay data -- Position 0 = regular pay, Position 1 = Overtime pay, Position 3 = Total
// Calculate regular pay for hours less than MAX_REGULAR_HOURS
// Calculate overtime pay for hours over MAX_REGULAR_HOURS. Avoid double counting hours as both regular and OT.
// Calculate total pay
// return the array
}
}
Second Program
/*
** Program : BufferedReaderDemo1.java
**
** Purpose : To demonstrate using a BufferedReader to read a file.
**
** Developer: F DAngelo
**
*/
package brbwDemo;
import java.io.IOException;
import java.util.regex.PatternSyntaxException;
import java.io.File;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader ;
import java.io.FileWriter;
public class BufferedReaderWriterDemo
{
final static String IN_FILE_NAME = "c:/data/StudentRawData.txt";
final static String OUT_FILE_NAME = "c:/data/FormattedStudentOutput.txt";
final static double MAX_REGULAR_HOURS = 40.0;
final static double OVERTIME_FACTOR = 1.5;
public static void main(String[] args)
{
// The program uses these constants to keep track of data item it is reading from the file.
File inFileObject = null;
FileReader readerObject = null;
BufferedReader bufferedReaderObject = null;
File outFileObject = null;
FileWriter writerObject = null;
BufferedWriter bufferedWriterObject = null;
try
{
inFileObject = new File( IN_FILE_NAME );
readerObject = new FileReader( inFileObject );
bufferedReaderObject = new BufferedReader( readerObject );
}
catch (Exception ex)
{
System.out.println(" An Exception occurred while creating a BufferedReader for " + IN_FILE_NAME + " : " + ex.getMessage() + ". ");
System.exit(-1);
}
try
{
outFileObject = new File( OUT_FILE_NAME );
writerObject = new FileWriter( outFileObject );
bufferedWriterObject = new BufferedWriter( writerObject );
}
catch (Exception ex)
{
System.out.println(" An Exception occurred while creating a BufferedWriter for " + OUT_FILE_NAME + " : " + ex.getMessage() + ". ");
System.exit(-2);
}
int recordsProcessed = 0;
try
{
recordsProcessed = processInputFile(bufferedReaderObject, bufferedWriterObject);
}
catch (PatternSyntaxException ex)
{
System.out.println(" A PatternSyntaxException occurred while reading " + IN_FILE_NAME + " : " + ex.getMessage() + ". ");
}
catch (IOException ex)
{
System.out.println(" An IOException occurred while reading " + IN_FILE_NAME + " : " + ex.getMessage() + ". ");
}
catch (Exception ex)
{
System.out.println(" An Exception occurred while reading " + IN_FILE_NAME + " : " + ex.getMessage() + ". ");
}
finally // actions to perform regardless of whether exceptions previously occurred.
{
System.out.println(" recordsProcessed: " + recordsProcessed );
try
{
bufferedReaderObject.close();
}
catch( Exception ex)
{
System.out.println(" An Exception occurred while closing " + IN_FILE_NAME + " : " + ex.getMessage() + ". ");
}
try
{
bufferedWriterObject.close();
}
catch( Exception ex)
{
System.out.println(" An Exception occurred while closing " + OUT_FILE_NAME + " : " + ex.getMessage() + ". ");
}
}
}
private static int processInputFile(BufferedReader br, BufferedWriter bw) throws PatternSyntaxException, IOException, Exception
{
final int ID_INDEX = 0;
final int LNAME_INDEX = 1;
final int FNAME_INDEX = 2;
final int MAJOR_INDEX = 3;
final int GPA_INDEX = 4;
int recordsProcessed = 0;
String inputLine = "";
while ( (inputLine = br.readLine()) != null)
{
String[] inputArray = inputLine.split(":");
// not using in calculations so String is OK.
// Produce formatted, easy-to-read output:
// The minus sign in the format specifier left-justifies the value.
String formattedOutput =
String.format("Student ID: %-6s Last name: %-16s First name: %-16s Major %-4s GPA: %4s %n",
inputArray[ID_INDEX], inputArray[LNAME_INDEX], inputArray[FNAME_INDEX],
inputArray[MAJOR_INDEX], inputArray[GPA_INDEX]);
bw.write(formattedOutput);
bw.write(System.lineSeparator());
recordsProcessed++;
}
return recordsProcessed;
}
private static double[] calcPay(double hours, double rate)
{
double[] payArray = new double[3]; // Position 0 = regular pay, Position 1 = Overtime pay, Position 3 = Total
if (hours <= MAX_REGULAR_HOURS )
{
payArray[0] = rate * hours;
payArray[1] = 0.0;
}
else
{
double regHours = MAX_REGULAR_HOURS;
double otHours = hours - MAX_REGULAR_HOURS;
payArray[0] = rate * regHours;
payArray[1] = rate * otHours * OVERTIME_FACTOR;
}
payArray[2] = payArray[0] + payArray[1];
return payArray;
}
}
Results
EmployeePayData
1000:voltaire:francois:writing:27.00:40
1100:austen:jane:writing:25.00:50
1200:montaigne:michel:phil:24.50:40
1300:du chatelet:emilie:math:30.00:60
1400:goethe:johann:writing:24.50:40
EmployeePayroll
1000:voltaire:francois:writing:1080.0:0.0:1080.0:
1100:austen:jane:writing:1000.0:375.0:1375.0:
1200:montaigne:michel:phil:980.0:0.0:980.0:
1300:du chatelet:emilie:math:1200.0:900.0:2100.0:
1400:goethe:johann:writing:980.0:0.0:980.0:
Explanation / Answer
package payrollProcessor;
import java.io.IOException;
import java.util.regex.PatternSyntaxException;
import java.io.File;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader ;
import java.io.FileWriter;
public class StringParser
{
final static String IN_FILE_NAME = "c:/data/EmployeePayData.txt"; // file path may need to be different on your system
final static String OUT_FILE_NAME = "c:/data/EmployeePayroll.txt"; // file path may need to be different on your system
final static double MAX_REGULAR_HOURS = 40.0;
final static double OVERTIME_FACTOR = 1.5;
public static void main(String[] args)
{
File inFileObject = null;
FileReader readerObject = null;
BufferedReader bufferedReaderObject = null;
File outFileObject = null;
FileWriter writerObject = null;
BufferedWriter bufferedWriterObject = null;
try
{
inFileObject = new File( IN_FILE_NAME );
readerObject = new FileReader( inFileObject );
bufferedReaderObject = new BufferedReader( readerObject );
}
catch (Exception ex)
{
System.out.println(" An Exception occurred while creating a BufferedReader for " + IN_FILE_NAME + " : " + ex.getMessage() + ". ");
System.exit(-1);
}
try
{
outFileObject = new File( OUT_FILE_NAME );
writerObject = new FileWriter( outFileObject );
bufferedWriterObject = new BufferedWriter( writerObject );
}
catch (Exception ex)
{
System.out.println(" An Exception occurred while creating a BufferedWriter for " + OUT_FILE_NAME + " : " + ex.getMessage() + ". ");
System.exit(-2);
}
int recordsProcessed = 0;
try
{
recordsProcessed = processPayrollFile(bufferedReaderObject, bufferedWriterObject);
}
catch (PatternSyntaxException ex)
{
System.out.println(" A PatternSyntaxException occurred while reading " + IN_FILE_NAME + " : " + ex.getMessage() + ". ");
}
catch (IOException ex)
{
System.out.println(" An IOException occurred while reading " + IN_FILE_NAME + " : " + ex.getMessage() + ". ");
}
catch (Exception ex)
{
System.out.println(" An Exception occurred while reading " + IN_FILE_NAME + " : " + ex.getMessage() + ". ");
}
finally // actions to perform regardless of whether exceptions previously occurred.
{
System.out.println(" recordsProcessed: " + recordsProcessed);
try
{
bufferedReaderObject.close();
}
catch( Exception ex)
{
System.out.println(" An Exception occurred while closing " + IN_FILE_NAME + " : " + ex.getMessage() + ". ");
}
try
{
bufferedWriterObject.close();
}
catch( Exception ex)
{
System.out.println(" An Exception occurred while closing " + OUT_FILE_NAME + " : " + ex.getMessage() + ". ");
}
}
}
// This method is to process input file data SO do not need to create object a file is read once
private static int processPayrollFile(BufferedReader br, BufferedWriter bw) throws PatternSyntaxException, IOException, Exception
{
String employeeID = "";
String lastName = "";
String firstName = "";
String dept = "";
double payrate = 0.0;
double hoursWorked = 0.0;
final int ID_INDEX = 0;
final int LNAME_INDEX = 1;
final int FNAME_INDEX = 2;
final int DEPT_INDEX = 3;
final int PAYRATE_INDEX = 4;
final int HOURWORK_INDEX = 4;
double[] weeklyPayArray = new double[3]; // Position 0 = regular pay, Position 1 = Overtime pay, Position 3 = Total
int recordsProcessed = 0;
String inputLine = "";
while ((inputLine = br.readLine()) != null) // complete this with code that both reads the BufferedReader and controls the loop.
{
String[] inputArray = inputLine.split(":");
employeeID = inputArray[ID_INDEX];
lastName = inputArray[LNAME_INDEX];
firstName = inputArray[FNAME_INDEX];
dept = inputArray[DEPT_INDEX];
payrate = Double.parseDouble(inputArray[PAYRATE_INDEX]);
hoursWorked = Double.parseDouble(inputArray[HOURWORK_INDEX]);
weeklyPayArray = calcPay( hoursWorked, payrate);
String formattedOutput =
String.format("Employee ID: %-6s Last name: %-16s First name: %-16s dept %-10s payrate: %14s hoursWorked: %14s total weeklypay: %14s %n",
employeeID, lastName, firstName,
dept, payrate,hoursWorked,weeklyPayArray);
bw.write(formattedOutput);
bw.write(System.lineSeparator());
recordsProcessed++;
}
return recordsProcessed;
}
// Why is this method static?
private static double[] calcPay(double hours, double rate) // Why is this method static?
{
double[] payArray = new double[3]; // Position 0 = regular pay, Position 1 = Overtime pay, Position 3 = Total
if (hours <= MAX_REGULAR_HOURS )
{
payArray[0] = rate * hours;
payArray[1] = 0.0;
}
else
{
double regHours = MAX_REGULAR_HOURS;
double otHours = hours - MAX_REGULAR_HOURS;
payArray[0] = rate * regHours;
payArray[1] = rate * otHours * OVERTIME_FACTOR;
}
payArray[2] = payArray[0] + payArray[1];
return payArray;
}
}
--------------------------------------------------------------------------------------------
public class AddressBook {
private String[] names = {"Shubham","Ram","Shyam","Virat","Sachin","Yuvraj","Rohit","Sunil","Modi","Yogi"};
private String[] phoneNumbers= {"9528340384","1234567890","31245435","124235346","325465362","2533425345","436523463","324545675","98785443665","87097856765"};
//Search person and return person name followed by phone number
public String searchPerson(String person){
//check person using for loop
for(int i=0;i< names.length;i++){
if (names[i].equalsIgnoreCase(person) ){
return names[i]+ " "+phoneNumbers[i];
}
}
return "Person does not exist.";
}
public static void main(String[] args) {
AddressBook addressBook = new AddressBook();
System.out.println(addressBook.searchPerson("Shubham"));
System.out.println(addressBook.searchPerson("Bravo"));
}
}
Sample OutPut:
Shubham 9528340384
Person does not exist.