Programming 9. Design a Work Hours Calculator using Two-Dimensional Arrays
In this project, we are to design a Java program with which we can analyze hours worked by employees in a week. The program is a command line application. A user can request an analysis of the hours worked stored in a file based on the command line arguments.
File Structure
An input file to the file records the hours worked in a week. It is assumed that the organization has no more than 100 employees.
The structure of an input file is as follows:
- Each line is a record.
- The file begins with a header record.
- After the header, the file contain a list of employee records, each record consists of the employee ID, and the hours worked each day in the week.
The following is an example of the input file:
ID Sun Mon Tue Wed Thu Fri Sat
N000001 8 4 5 6 4 5 4
N000002 10 4 9 5 5 10 4
N000003 9 10 6 8 6 10 7
N000004 8 4 1 9 1 8 2
N000005 8 6 5 9 6 6 6
Functionalities
The program has three functionalities.
Compute Hours Worked for Each Employee
When a user provides GetWeeklyHours
, the path to an input file, and the path to an output file on the command line, the program
shall
- load the data from the input file to arrays, a single-dimensional array for employee IDs and a two-dimensional array for hours worked daily by each employee in the week;
- compute the total hours worked in the week for each employee,
- and save the results to the output file that should be formatted similar to the input file except with an additional column for the total hours worked.
The following is a running example:
java WorkHoursCalculator GetWeeklyHours hours.txt weekly.txt
Wrote weekly.txt
where the content of the output file weekly.txt
is as follows:
ID Sun Mon Tue Wed Thu Fri Sat Total
N000001 8 4 5 6 4 5 4 36
N000002 10 4 9 5 5 10 4 47
N000003 9 10 6 8 6 10 7 56
N000004 8 4 1 9 1 8 2 33
N000005 8 6 5 9 6 6 6 46
which also exhibits the format of the output file.
Compute Daily Hours Worked
This functionality is to compute total hours worked by all employees daily. To request this functionality, the user shall enter
GetDailyHours
, the path to an input file, and the path to an output file as the command line arguments. The program shall
- load the data from the input file to arrays, a single-dimensional array for employee IDs and a two-dimensional array for hours worked daily by each employee in the week;
- compute the total hours worked in each day by all employees in the week;
- and save the results to the output file that should be formatted similar to the input file except with the last line as the total hours worked daily.
The following is a running example:
$ java WorkHoursCalculator GetDailyHours hours.txt daily.txt
Wrote daily.txt
where the content of the output file daily.txt
is as follows:
ID Sun Mon Tue Wed Thu Fri Sat
N000001 8 4 5 6 4 5 4
N000002 10 4 9 5 5 10 4
N000003 9 10 6 8 6 10 7
N000004 8 4 1 9 1 8 2
N000005 8 6 5 9 6 6 6
Total 43 28 26 37 22 39 23
The example also shows what is required for the format of the output file.
Find Longest Work Day
To request this functionality, the user should give three command line arguments, GetLongestDay
, the path to an input file,
and the path to an output file, and the program should locate the first longest day worked by an employee. The program shall
- load the data from the input file to arrays, a single-dimensional array for employee IDs and a two-dimensional array for hours worked daily by each employee in the week;
- find the first occurrences of the longest work hours in a day by an employee;
- and save the results to the output file whose content is exhibited in the running example below.
$ java WorkHoursCalculator GetLongestDay hours.txt longest.txt
Wrote longest.txt
The output file longest.txt
has the following content:
Longest Day Worked in the Week
Employee ID: N000002
Hours worked: 10
Day: Sunday
In the input file hours.txt
, the longest hours worked in a day is 10 hours. Several employees worked 10 hours in several days.
The program should always save the first occurrence to the file.
Divide-and-Conquer Strategy.
Although no sub-exercises given for this project, you should follow the divide-and-conquer strategy by designing the following methods:
public static int loadHours(String inFilePath, String[] ids, int[][] hours) throws FileNotFoundException
The method should be designed and used as follows. Since it is assumed that the organization has no more than 100 employees, you should create two arrays for employee IDs and hours worked. The arrays should be large enough to hold all data in the input file. The method should skip the header, read the employee IDs, and the hours worked to the two arrays. However, since the arrays may be bigger than actual records in the input file, the method should return an integer indicating the number of records read into the arrays. To process the arrays for the requested functionalities, you should use the return value to determine how many records there are in the arrays, which is the design purpose of the return value.
There are a variety of ways to read the data for an employee. If you wish to read the data one line at a time and wish to split the
line into multiple parts, you should specify the delimiter as \\s+
. Here, \\s+
defines the pattern of the String that separates
the fields in the line, i.e., Strings consists of 1 or more spaces – \\s
means white space character and +
means the white space
character repeats 1 or more times. However, you should use the way with which you are most comfortable.
public static int[] computeWeeklyHours(int size, int[][] hours)
This method is to compute total hours worked in the week by each employee. The hours should be returned as a single-dimensional array.
Parameter size
indicates the
number of records in the arrays since some array elements may be unused due to the design of the loadHours
method.
public static void saveWeeklyHours(int size, String[] ids, int[][] hours, int[] weeklyHours, String outFilePath) throws FileNotFoundException
Method saveWeeklyHours
is to save the total hours worked in the week by each employee to the output file. Parameter size
indicates the
number of records in the arrays since some array elements may be unused due to the design of the loadHours
method.
public static int[] computeDailyHours(int size, int[][] hours)
The computeDailyHours
method computes the total hours worked in a day by all employees in the week. As before,
parameter size
indicates the
number of records in the arrays since some array elements may be unused due to the design of the loadHours
method. The total hours
worked each week day is returned in a single-dimensional array.
public static void saveDailyHours(int size, String[] ids, int[][] hours, int[] dailyHours, String outFilePath) throws FileNotFoundException
This method save the total hours worked in a day by all employees in the week to the output file.
Parameter size
indicates the
number of records in the arrays since some array elements may be unused due to the design of the loadHours
method.
public static int[] getLongestDay(int size, int[][] hours)
Method getLongestDay
is to determine the longest hours an employee worked in a day. The definition of parameter size
is the same
as before. The return value is an array of size 2. The array stores the array index of the two-dimensional array hours.
public static void saveLongestDay(String[] ids, int[][] hours, int[] longHourIndex, String outFilePath) throws FileNotFoundException
Method saveLongestDay
saves the longest day data to the output file.