@matfah/

Daily Schedule FR 2006

Java

No description

fork
loading
Files
  • Main.java
  • Appointment.java
  • DailySchedule.java
  • TimeInterval.java
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import java.util.*;
import java.lang.reflect.*;

public class Main {
  public static void main(String[] args) {
    for(int i = 0; i < 24; i++) {
      testConflictsWith();
    }
    for(int i = 0; i < 5; i++) {
      testClearConflicts();
    }  
    for(int i = 0; i < 5; i++) {
      testAddApt();
    }
    System.out.println("All tests passed!");
  }
  
  public static void testConflictsWith() {
    
    int randHourOne = (int)(Math.random()*2) + 1;
    int randHourTwo = (int)(Math.random()*2) + 1;
    
    CustomAppointment first = new CustomAppointment(randHourOne);
    CustomAppointment second = new CustomAppointment(randHourTwo);
    
    boolean equalHours = randHourOne == randHourTwo;
    boolean equalAppt = false;
    
    try {
      equalAppt = first.conflictsWith(second);
    }
    catch(Exception e) {
      System.out.println("Error was generated when calling conflictsWith()");
      System.exit(0);
    }
    
    if(equalHours != equalAppt) {
      System.out.println("conflictsWith() is not returning the correct value");
      System.exit(0);
    }
    
    if(first.getTimeCount == 0) {
      System.out.println("conflictsWith() is not calling getTime()");
      System.exit(0);
    }
    
    if(((CustomTimeInterval)first.interval).overlapsWithCount == 0) {
      System.out.println("conflictsWith() is not calling overlapsWith()");
      System.exit(0);
    }
  }
  
  public static void testClearConflicts() {
    DailySchedule sch = new DailySchedule();
    
    ArrayList<Appointment> appts = null;
    
    try {
      Field f = DailySchedule.class.getDeclaredField("apptList");
      f.setAccessible(true);
      appts = (ArrayList<Appointment>)f.get(sch);
    }
    catch(Exception e) {
      System.out.println("Error with apptList instance variable!");
      System.exit(0);
    }
    
    for(int appointments = 0; appointments < 5; appointments++) {
      
      appts.clear();
      
      int conflictHour = (int)(Math.random()*2) + 1;
      CustomAppointment possibleConflict = new CustomAppointment(conflictHour);
      
      TreeSet<String> memoryAddresses = new TreeSet<String>();
      
      for(int i = 0; i < appointments; i++) {
        
        int randHour = (int)(Math.random()*2) + 1;
        appts.add(new CustomAppointment(randHour));
        if(randHour != conflictHour)
          memoryAddresses.add(appts.get(appts.size()-1).toString());
      }
      
      try {
        sch.clearConflicts(possibleConflict);
      }
      catch(Exception e) {
        System.out.println("Error trying to call clearConflicts()!");
        System.exit(0);
      }
      
      for(Appointment a : appts) {
        if(memoryAddresses.remove(a.toString()) == false) {
          System.out.println("After call to clearConflicts(), at least one Appointment that should have been removed still remains!!");
          System.exit(0);
        }
      }
      
      if(memoryAddresses.size() != 0) {
        System.out.println("After call to clearConflicts(), at least one Appointment that should still be there has been removed!!");
      }
      
    }
  }
  
  public static void testAddApt() {
    CustomDailySchedule sch = new CustomDailySchedule();
    
    ArrayList<Appointment> appts = null;
    
    try {
      Field f = DailySchedule.class.getDeclaredField("apptList");
      f.setAccessible(true);
      appts = (ArrayList<Appointment>)f.get(sch);
    }
    catch(Exception e) {
      System.out.println("Error with apptList instance variable!");
      System.exit(0);
    }
    
    int j;
    boolean emergency;
    for(emergency = true, j = 0; j <= 1; j++, emergency = !emergency) {
      for(int appointments = 0; appointments < 5; appointments++) {
        
        appts.clear();
        sch.clearConflictsCount = 0;
        sch.clearConflictParam = null;

        int conflictHour = (int)(Math.random()*2) + 1;
        CustomAppointment possibleConflict = new CustomAppointment(conflictHour);
        
        TreeSet<String> memoryAddresses = new TreeSet<String>();
        boolean resultShouldBe = true;
        boolean conflictsExits = false;
        
        for(int i = 0; i < appointments; i++) {
          
          int randHour = (int)(Math.random()*2) + 1;
          appts.add(new CustomAppointment(randHour));
          if(!emergency || randHour != conflictHour)
            memoryAddresses.add(appts.get(appts.size()-1).toString());
          
          if(randHour == conflictHour && !emergency)
            resultShouldBe = false;
            
          if(randHour == conflictHour)
            conflictsExits = true;
        }
        
        if(resultShouldBe) {
          memoryAddresses.add(possibleConflict.toString());
        }

        boolean success = false;
        try {
          success = sch.addAppt(possibleConflict, emergency);
        }
        catch(Exception e) {
          System.out.println("Error trying to call addAppt()!");
          System.exit(0);
        }
        
        if(success != resultShouldBe) {
          System.out.println("Boolean result of calling addAppt() was not right!");
          System.exit(0);
        }
        
        if(emergency && sch.clearConflictsCount != 1) {
          System.out.println("addAppt() is not calling clearConflicts() when it's an emergency!");
          System.exit(0);
        }
        
        if(!emergency && sch.clearConflictsCount > 0) {
          System.out.println("addAppt() is calling clearConflicts() when it's not an emergency!");
          System.exit(0);
        }
        
        if(sch.clearConflictsCount != 0 && sch.clearConflictParam != possibleConflict) {
          System.out.println("addAppt() is calling clearConflicts() with the wrong parameter!");
          System.exit(0);
        }
        
        for(Appointment a : appts) {
          if(memoryAddresses.remove(a.toString()) == false) {
            System.out.println("After call to addApt(), at least one Appointment that should have been removed still remains!!");
            System.exit(0);
          }
        }
        
        if(memoryAddresses.size() != 0) {
          System.out.println("After call to addApt(), at least one Appointment that should still be there has been removed!!");
        }
        
      }
    }
  }
}





//simple implementations for testing purposes
class CustomTimeInterval extends TimeInterval{

  int hour;
  int overlapsWithCount = 0;
  
  public CustomTimeInterval(int h){
    hour = h;
  }

  public boolean overlapsWith(TimeInterval interval) { 
    overlapsWithCount++;
    CustomTimeInterval casted = (CustomTimeInterval)interval;
    return casted.hour == hour;
  }
}


class CustomAppointment extends Appointment {

  TimeInterval interval;
  int conflictsWithCount = 0;
  int getTimeCount = 0;
  
  public CustomAppointment(int hour) {
    interval = new CustomTimeInterval(hour);
  }

  public TimeInterval getTime() { 
    getTimeCount++;
    return interval;
  }


  public boolean conflictsWith(Appointment other) { 
    conflictsWithCount++;
    return super.conflictsWith(other);
  }
}

class CustomDailySchedule extends DailySchedule {

  int clearConflictsCount = 0;
  Appointment clearConflictParam;
  
  public void clearConflicts(Appointment appt) { 
    clearConflictsCount++;
    clearConflictParam = appt;
    super.clearConflicts(appt);
  }
  
}