Source of TimeBook.java


  1: //OneWayNoRepeatList.java
  2: 
  3: /**
  4:  * Class that records the time worked by each of a
  5:  * company's employees during one five-day week. 
  6:  * A sample application is in the main method.
  7:  */
  8: public class TimeBook
  9: {
 10:     private int numberOfEmployees;
 11:     private int[][] hours;  //hours[i][j] has the hours for employee j on day i.
 12:     private int[] weekHours;//weekHours[i] has the week's hours worked for 
 13:     //employee i + 1.
 14:     private int[] dayHours; //dayHours[i] has the total hours worked by all
 15:     //employees on day i. 
 16:     private static final int NUMBER_OF_WORKDAYS = 5;
 17:     private static final int MON = 0;
 18:     private static final int TUE = 1;
 19:     private static final int WED = 2;
 20:     private static final int THU = 3;
 21:     private static final int FRI = 4;
 22: 
 23:     /**
 24:      * Reads hours worked for each employee on each day of the
 25:      * work week into the two-dimensional array hours. (The method
 26:      * for input is just a stub in this preliminary version.)
 27:      * Computes the total weekly hours for each employee and
 28:      * the total daily hours for all employees combined.
 29:      */
 30:     public static void main(String[] args)
 31:     {
 32:         final int NUMBER_OF_EMPLOYEES = 3;
 33:         TimeBook book = new TimeBook(NUMBER_OF_EMPLOYEES);
 34:         book.setHours();
 35:         book.update();
 36:         book.showTable();
 37:     }
 38: 
 39:     public TimeBook
 40:     (
 41:         int theNumberOfEmployees
 42:     )
 43:     {
 44:         numberOfEmployees = theNumberOfEmployees;
 45:         hours = new int[NUMBER_OF_WORKDAYS][numberOfEmployees];
 46:         weekHours = new int[numberOfEmployees];
 47:         dayHours = new int[NUMBER_OF_WORKDAYS];
 48:     }
 49: 
 50:     public void setHours() //This is a stub.
 51:     {
 52:         hours[0][0] = 8;
 53:         hours[0][1] = 0;
 54:         hours[0][2] = 9;
 55:         hours[1][0] = 8;
 56:         hours[1][1] = 0;
 57:         hours[1][2] = 9;
 58:         hours[2][0] = 8;
 59:         hours[2][1] = 8;
 60:         hours[2][2] = 8;
 61:         hours[3][0] = 8;
 62:         hours[3][1] = 8;
 63:         hours[3][2] = 4;
 64:         hours[4][0] = 8;
 65:         hours[4][1] = 8;
 66:         hours[4][2] = 8;
 67:     }
 68: 
 69:     public void update()
 70:     {
 71:         computeWeekHours();
 72:         computeDayHours();
 73:     }
 74: 
 75:     private void computeWeekHours()
 76:     {
 77:         for (int employeeNumber = 1; employeeNumber <= numberOfEmployees; employeeNumber++)
 78:         {//Process one employee:
 79:             int sum = 0;
 80:             for (int day = MON; day <= FRI; day++)
 81:                 sum = sum + hours[day][employeeNumber - 1];
 82:             //sum contains the sum of all the hours worked in
 83:             //one week by the employee with number employeeNumber.
 84:             weekHours[employeeNumber - 1] = sum;
 85:         }
 86:     }
 87: 
 88:     private void computeDayHours()
 89:     {
 90:         for (int day = MON; day <= FRI; day++)
 91:         {//Process one day (for all employees):
 92:             int sum = 0;
 93:             for (int employeeNumber = 1; employeeNumber <= numberOfEmployees;
 94:                      employeeNumber++)
 95:                 sum = sum + hours[day][employeeNumber - 1];
 96:             //sum contains the sum of all hours worked by all 
 97:             //employees on one day.
 98:             dayHours[day] = sum;
 99:         }
100:     }
101: 
102:     public void showTable()
103:     {
104:         //heading
105:         System.out.print("Employee  ");
106:         for (int employeeNumber = 1; employeeNumber <= numberOfEmployees;
107:                  employeeNumber++)
108:             System.out.print(employeeNumber + "   ");
109:         System.out.println("Totals");
110:         System.out.println();
111: 
112:         //row entries
113:         for (int day = MON; day <= FRI; day++)
114:         {
115:             System.out.print(getDayName(day) + " ");
116:             for (int column = 0; column < hours[day].length; column++)
117:                 System.out.print(hours[day][column] + "   ");
118:             System.out.println(dayHours[day]);
119:         }
120:         System.out.println();
121: 
122:         System.out.print("Total  =  ");
123:         for (int column = 0; column < numberOfEmployees; column++)
124:             System.out.print(weekHours[column] + "  ");
125:         System.out.println();
126:     }
127: 
128:     //Converts 0 to "Monday", 1 to "Tuesday" etc.
129:     //Blanks are inserted to make all strings the same length.
130:     private String getDayName
131:     (
132:         int day
133:     )
134:     {
135:         String dayName = null;
136:         switch (day)
137:         {
138:         case MON:
139:             dayName = "Monday   ";
140:             break;
141:         case TUE:
142:             dayName = "Tuesday  ";
143:             break;
144:         case WED:
145:             dayName = "Wednesday";
146:             break;
147:         case THU:
148:             dayName = "Thursday ";
149:             break;
150:         case FRI:
151:             dayName = "Friday   ";
152:             break;
153:         default:
154:             System.out.println("Fatal Error.");
155:             System.exit(0);
156:             break;
157:         }
158:         return dayName;
159:     }
160: }