|
1 | 1 | /**
|
2 |
| - * Data.java contains the classes for the Java object representation for |
3 |
| - * our data elements. |
4 |
| - * Includes classes for: |
5 |
| - * * Tutors (and their preferences) |
6 |
| - * * Office hour slots (and their preferences) |
| 2 | + * Data.java contains the classes for the Java object representation for our data elements. Includes |
| 3 | + * classes for: * Tutors (and their preferences) * Office hour slots (and their preferences) |
7 | 4 | * Populate these classes using the Gson library
|
8 | 5 | */
|
9 | 6 |
|
|
17 | 14 | import com.google.gson.JsonPrimitive;
|
18 | 15 |
|
19 | 16 | /**
|
20 |
| - * Class directly populated by the JSON reader. Each index corresponds exactly |
21 |
| - * to the index in any of the courses[]. |
| 17 | + * Class directly populated by the JSON reader. Each index corresponds exactly to the index in any |
| 18 | + * of the courses[]. |
22 | 19 | */
|
23 | 20 | public class Data {
|
24 | 21 |
|
25 |
| - /** |
26 |
| - * Array of all course names. Each index corresponds exactly to the index in |
27 |
| - * any of the courses[]. |
28 |
| - */ |
29 |
| - public String[] courseNames; |
30 |
| - /** |
31 |
| - * Array of all tutors. |
32 |
| - */ |
33 |
| - public Tutor[] tutors; |
| 22 | + /** |
| 23 | + * Array of all course names. Each index corresponds exactly to the index in any of the courses[]. |
| 24 | + */ |
| 25 | + public String[] courseNames; |
| 26 | + /** |
| 27 | + * Array of all tutors. |
| 28 | + */ |
| 29 | + public Tutor[] tutors; |
34 | 30 |
|
35 |
| - /** |
36 |
| - * Array of all slots. Each index corresponds exactly to the slot's sid. |
37 |
| - */ |
38 |
| - public Slot[] slots; |
| 31 | + /** |
| 32 | + * Array of all slots. Each index corresponds exactly to the slot's sid. |
| 33 | + */ |
| 34 | + public Slot[] slots; |
39 | 35 |
|
40 |
| - /** |
41 |
| - * Initialize some data fields not done by the JSON parser. |
42 |
| - */ |
43 |
| - public void init() { |
44 |
| - // initialize the adjacent slot references |
45 |
| - for (Slot s : slots) { |
46 |
| - s.adjacentSlots = new Slot[s.adjacentSlotIDs.length]; |
47 |
| - for (int i = 0; i < s.adjacentSlots.length; ++i) { |
48 |
| - for (Slot t : slots) { |
49 |
| - if (t.sid == s.adjacentSlotIDs[i]) { |
50 |
| - s.adjacentSlots[i] = t; |
51 |
| - break; |
52 |
| - } |
53 |
| - } |
54 |
| - } |
| 36 | + /** |
| 37 | + * Initialize some data fields not done by the JSON parser. |
| 38 | + */ |
| 39 | + public void init() { |
| 40 | + // initialize the adjacent slot references |
| 41 | + for (Slot s : slots) { |
| 42 | + s.adjacentSlots = new Slot[s.adjacentSlotIDs.length]; |
| 43 | + for (int i = 0; i < s.adjacentSlots.length; ++i) { |
| 44 | + for (Slot t : slots) { |
| 45 | + if (t.sid == s.adjacentSlotIDs[i]) { |
| 46 | + s.adjacentSlots[i] = t; |
| 47 | + break; |
| 48 | + } |
55 | 49 | }
|
| 50 | + } |
56 | 51 | }
|
| 52 | + } |
57 | 53 |
|
58 |
| - /** |
59 |
| - * Returns a string of all assignments in the set of slots. Output is |
60 |
| - * formatted in rows of "[SLOT_ID] [TUTOR_ID]" if no slot is assigned to the |
61 |
| - * tutor, [TUTOR_ID] is -1. |
62 |
| - */ |
63 |
| - public String assignments() { |
64 |
| - String ret = ""; |
65 |
| - for (Slot s : slots) { |
66 |
| - ret += String.valueOf(s.sid) + " "; |
67 |
| - ret += String.valueOf(s.tutors.get(0).tid); |
68 |
| - for (int i = 1; i < s.tutors.size(); i++) |
69 |
| - ret += " " + String.valueOf(s.tutors.get(i).tid); |
70 |
| - ret += "\n"; |
71 |
| - } |
72 |
| - return ret.trim(); |
| 54 | + /** |
| 55 | + * Returns a string of all assignments in the set of slots. Output is formatted in rows of |
| 56 | + * "[SLOT_ID] [TUTOR_ID]" if no slot is assigned to the tutor, [TUTOR_ID] is -1. |
| 57 | + */ |
| 58 | + public String assignments() { |
| 59 | + String ret = ""; |
| 60 | + for (Slot s : slots) { |
| 61 | + ret += String.valueOf(s.sid) + " "; |
| 62 | + ret += String.valueOf(s.tutors.get(0).tid); |
| 63 | + for (int i = 1; i < s.tutors.size(); i++) |
| 64 | + ret += " " + String.valueOf(s.tutors.get(i).tid); |
| 65 | + ret += "\n"; |
73 | 66 | }
|
| 67 | + return ret.trim(); |
| 68 | + } |
74 | 69 |
|
75 |
| - public String formattedAssignments() { |
76 |
| - JsonObject jason = new JsonObject(); |
77 |
| - for (Slot s : slots) { |
78 |
| - JsonArray j = new JsonArray(); |
79 |
| - for (Tutor t : s.tutors) { |
80 |
| - j.add(new JsonPrimitive(t.tid)); |
81 |
| - } |
82 |
| - jason.add(s.name.substring(s.name.length() - 3), j); |
| 70 | + public String formattedAssignments() { |
| 71 | + JsonObject jason = new JsonObject(); |
| 72 | + for (Slot s : slots) { |
| 73 | + JsonArray j = new JsonArray(); |
| 74 | + for (Tutor t : s.tutors) { |
| 75 | + j.add(new JsonPrimitive(t.tid)); |
83 | 76 | }
|
84 |
| - |
85 |
| - return jason.toString(); |
86 |
| - } |
87 |
| - |
88 |
| - public String readableFormattedAssignments() { |
89 |
| - String ret = ""; |
90 |
| - for (Slot s : slots) { |
91 |
| - ret += s.details(); |
92 |
| - for (int i = 0; i < s.tutors.size(); i++) |
93 |
| - ret += " " + String.valueOf(s.tutors.get(i).name); |
94 |
| - ret += "\n"; |
95 |
| - } |
96 |
| - return ret.trim(); |
97 |
| - } |
98 |
| - |
99 |
| - public void clearAssignments() { |
100 |
| - for (int i = 0; i < tutors.length; i++) |
101 |
| - tutors[i].slots = new ArrayList <Slot> (); |
102 |
| - for (int i = 0; i < slots.length; i++) |
103 |
| - slots[i].tutors = new ArrayList <Tutor> (); |
| 77 | + jason.add(s.name.substring(s.name.length() - 3), j); |
104 | 78 | }
|
105 | 79 |
|
106 |
| - @Override |
107 |
| - public String toString() { |
108 |
| - return "Data {" + "courseNames=" + Arrays.toString(courseNames) |
109 |
| - + ", tutors=" + Arrays.toString(tutors) + ", slots=" |
110 |
| - + Arrays.toString(slots) + "}"; |
111 |
| - } |
112 |
| - |
113 |
| - @Override |
114 |
| - public Data clone() { |
115 |
| - Data ret = new Data(); |
116 |
| -// ret.courseNames = courseNames.clone(); |
117 |
| - ret.tutors = tutors.clone(); |
118 |
| - ret.slots = slots.clone(); |
119 |
| - return ret; |
| 80 | + return jason.toString(); |
| 81 | + } |
| 82 | + |
| 83 | + public String readableFormattedAssignments() { |
| 84 | + String ret = ""; |
| 85 | + for (Slot s : slots) { |
| 86 | + ret += s.details(); |
| 87 | + for (int i = 0; i < s.tutors.size(); i++) |
| 88 | + ret += " " + String.valueOf(s.tutors.get(i).name); |
| 89 | + ret += "\n"; |
120 | 90 | }
|
| 91 | + return ret.trim(); |
| 92 | + } |
| 93 | + |
| 94 | + public void clearAssignments() { |
| 95 | + for (int i = 0; i < tutors.length; i++) |
| 96 | + tutors[i].slots = new ArrayList<Slot>(); |
| 97 | + for (int i = 0; i < slots.length; i++) |
| 98 | + slots[i].tutors = new ArrayList<Tutor>(); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public String toString() { |
| 103 | + return "Data {" + "courseNames=" + Arrays.toString(courseNames) + ", tutors=" |
| 104 | + + Arrays.toString(tutors) + ", slots=" + Arrays.toString(slots) + "}"; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public Data clone() { |
| 109 | + Data ret = new Data(); |
| 110 | + // ret.courseNames = courseNames.clone(); |
| 111 | + ret.tutors = tutors.clone(); |
| 112 | + ret.slots = slots.clone(); |
| 113 | + return ret; |
| 114 | + } |
121 | 115 |
|
122 | 116 | }
|
0 commit comments