@matfah/

Ticket FR 2005

Java

No description

fork
loading
Files
  • Main.java
  • Advance.java
  • StudentAdvance.java
  • Ticket.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
257
import java.lang.reflect.*;
import java.util.ArrayList;

public class Main {
  
  public static void main(String[] args) {
    testAdvance();
    testStudentAdvance();
    System.out.println("All tests passed!");
  }
  
  public static void testAdvance() {
    
    checkAllInstanceVariablesArePrivate(Advance.class);
    
    Constructor<Advance> advConst = null;
    try {
      advConst = Advance.class.getDeclaredConstructor(int.class);
    }
    catch(Exception e) {
      System.out.println("Your Advance class does not have a 1 parameter constructor!");
      System.exit(0);
    }
    
    Advance aAdv1 = null;
    int firstSerial = -1;
    Advance aAdv2 = null;
    int secondSerial = -1;
    try {
      aAdv1 = advConst.newInstance(9);
      firstSerial = nextSerial;
      aAdv2 = advConst.newInstance(10);
      secondSerial = nextSerial;
    }
    catch(Exception e) {
      System.out.println("Error trying to construct an Advance object!");
      System.exit(0);
    }
    
    if(!Ticket.class.isAssignableFrom(Advance.class)) {
      System.out.println("Your Advance class is not a subclass of Ticket!");
      System.exit(0);
    }
    
    try {
      double price = aAdv1.getPrice();
      if(price != 40.0) {
        System.out.println("Result of calling getPrice() from an Advance object with less than 10 days advanced is wrong!");
        System.exit(0);
      }
    }
    catch(Exception e) {
      System.out.println("Error calling getPrice() from an Advance object!");
      System.exit(0);
    }
    
    try {
      double price = aAdv2.getPrice();
      if(price != 30.0) {
        System.out.println("Result of calling getPrice() from an Advance object with 10 or more days advanced is wrong!");
        System.exit(0);
      }
    }
    catch(Exception e) {
      System.out.println("Error calling getPrice() from an Advance object!");
      System.exit(0);
    }
    
    try {
      String result = aAdv1.toString();
      String goal = "Number: " + firstSerial + "\nPrice: " + 40.0;
      
      if(!result.equals(goal)) {
        System.out.println("Result of calling toString() from an Advance object with less than 10 days advanced is wrong!");
      System.exit(0);
      }
    }
    catch(Exception e) {
      System.out.println("Error calling toString() from an Advance object!");
      System.exit(0);
    }
    
    try {
      String result = aAdv2.toString();
      String goal = "Number: " + secondSerial + "\nPrice: " + 30.0;
      
      if(!result.equals(goal)) {
        System.out.println("Result of calling toString() from an Advance object with 10 or more days advanced is wrong!");
      System.exit(0);
      }
    }
    catch(Exception e) {
      System.out.println("Error calling toString() from an Advance object!");
      System.exit(0);
    }
  }
  
  public static void testStudentAdvance() {
    
    checkAllInstanceVariablesArePrivate(StudentAdvance.class);
    
    Constructor<StudentAdvance> studAdvConst = null;
    Constructor<Advance> advConst = null;
    try {
      studAdvConst = StudentAdvance.class.getDeclaredConstructor(int.class);
      advConst = Advance.class.getDeclaredConstructor(int.class);
    }
    catch(Exception e) {
      System.out.println("Your StudentAdvance class does not have a 1 parameter constructor!");
      System.exit(0);
    }
    
    Field[] fields = Advance.class.getDeclaredFields();
    for(Field f: fields)
      f.setAccessible(true);
    
    StudentAdvance saAdv1 = null;
    Advance aAdv1 = null;
    int firstSerial = -1;
    int firstSerialB = -1;
    
    StudentAdvance saAdv2 = null;
    Advance aAdv2 = null;
    int secondSerial = -1;
    int secondSerialB = -1;
    
    try {
      saAdv1 = studAdvConst.newInstance(9);
      firstSerial = nextSerial;
      aAdv1 = advConst.newInstance(9);
      firstSerialB = nextSerial;
      
      saAdv2 = studAdvConst.newInstance(10);
      secondSerial = nextSerial;
      aAdv2 = advConst.newInstance(10);
      secondSerialB = nextSerial;
    }
    catch(Exception e) {
      System.out.println("Error trying to construct a StudentAdvance object!");
      System.exit(0);
    }
    
    try {
      boolean allSameFieldValues = true;
      
      for(Field f : fields) {
        if(f.get(saAdv1) == null && f.get(aAdv1) == null)
          continue;
        if(f.get(saAdv1) == null || f.get(aAdv1) == null) {
          allSameFieldValues = false;
          break;
        }
        if(!f.get(saAdv1).equals(f.get(aAdv1))) {
          allSameFieldValues = false;
          break;
        }
      }
      
      for(Field f : fields) {
        if(f.get(saAdv2) == null && f.get(aAdv2) == null)
          continue;
        if(f.get(saAdv2) == null || f.get(aAdv2) == null) {
          allSameFieldValues = false;
          break;
        }
        if(!f.get(saAdv2).equals(f.get(aAdv2))) {
          allSameFieldValues = false;
          break;
        }
      }
      
      if(!allSameFieldValues) {
        System.out.println("Your StudentAdvance constructor does not call the superclass constructor correctly");
        System.exit(0);
      }
    }
    catch(Exception e) {
      System.out.println("Error with instance variables of StudentAdvance class");
      System.exit(0);
    }
    
    
    if(!Advance.class.isAssignableFrom(StudentAdvance.class)) {
      System.out.println("Your StudentAdvance class is not a subclass of Advance!");
      System.exit(0);
    }
    
    try {
      double price = saAdv1.getPrice();
      if(price != 20.0) {
        System.out.println("Result of calling getPrice() from a StudentAdvance object with less than 10 days advanced is wrong!");
        System.exit(0);
      }
    }
    catch(Exception e) {
      System.out.println("Error calling getPrice() from a StudentAdvance object!");
      System.exit(0);
    }
    
    try {
      double price = saAdv2.getPrice();
      if(price != 15.0) {
        System.out.println("Result of calling getPrice() from a StudentAdvance object with 10 or more days advanced is wrong!");
        System.exit(0);
      }
    }
    catch(Exception e) {
      System.out.println("Error calling getPrice() from an StudentAdvance object!");
      System.exit(0);
    }
    
    try {
      String result = saAdv1.toString();
      String goal = "Number: " + firstSerial + "\nPrice: " + 20.0 + "\n(student ID required)";
      
      if(!result.equals(goal)) {
        System.out.println("Result of calling toString() from a StudentAdvance object with less than 10 days advanced is wrong!");
        System.exit(0);
      }
    }
    catch(Exception e) {
      System.out.println("Error calling toString() from an StudentAdvance object!");
      System.exit(0);
    }
    
    try {
      String result = saAdv2.toString();
      String goal = "Number: " + secondSerial + "\nPrice: " + 15.0 + "\n(student ID required)";
      
      if(!result.equals(goal)) {
        System.out.println("Result of calling toString() from an StudentAdvance object with 10 or more days advanced is wrong!");
        System.exit(0);
      }
    }
    catch(Exception e) {
      System.out.println("Error calling toString() from a StudentAdvance object!");
      System.exit(0);
    }
  }
  
  static int nextSerial;
  public static int nextSerialNumber() {
    nextSerial = (int)(Math.random()*100);
    return nextSerial;
  }
  
  public static void checkAllInstanceVariablesArePrivate(Class c) {
    Field[] fields = c.getDeclaredFields();
    for(Field f: fields) {
      if((f.getModifiers() & Modifier.PRIVATE) != Modifier.PRIVATE)
      {
        System.out.println("Not all fields of " + c.getName() + " are private!");
        System.exit(0);
      }
    }
  }
}