Source of TimeBook.java


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