-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.txt
More file actions
362 lines (291 loc) · 10.7 KB
/
Database.txt
File metadata and controls
362 lines (291 loc) · 10.7 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
1. What design principles does this code violate?
Error Handling
Isolated Change DesignPrinciples
High-Quality Abstraction (abstract all the way)
2. Without actually doing so, explain how you would refactor this code to improve its design.
Don't swallow exceptions
Pull out DAO into a separate class to isolate and abstract all of those functions away, with an abstract interface to work between these classes and the new DAO class
Don't use primitives for url, name, daysTimes, id, and credits (depending on how these will be used in other areas of the project - developer's discretion)
Abstract lists
Course.java
import java.sql.*;
public class Course {
private String name;
private int credits;
static String url = "jdbc:odbc:Reggie";
static { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }
catch (Exception ignored) {} }
public static Course create(String name, int credits)
throws Exception
{
Connection conn = null;
try {
conn = DriverManager.getConnection(url, "", "");
Statement statement = conn.createStatement();
statement.executeUpdate(
"DELETE FROM course WHERE name = '" + name + "';");
statement.executeUpdate(
"INSERT INTO course VALUES ('" + name
+ "', '" + credits + "');");
return new Course(name, credits);
} finally {
try { conn.close(); } catch (Exception ignored) {}
}
}
public static Course find(String name) {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, "", "");
Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery(
"SELECT * FROM course WHERE Name = '" + name + "';");
if (!result.next()) return null;
int credits = result.getInt("Credits");
return new Course(name, credits);
} catch (Exception ex) {
return null;
} finally {
try { conn.close(); } catch (Exception ignored) {}
}
}
public void update() throws Exception {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, "", "");
Statement statement = conn.createStatement();
statement.executeUpdate(
"DELETE FROM COURSE WHERE name = '" + name + "';");
statement.executeUpdate(
"INSERT INTO course VALUES('" +
name + "','" + credits + "');");
} finally {
try { conn.close(); } catch (Exception ignored) {}
}
}
Course(String name, int credits) {
this.name = name;
this.credits = credits;
}
public int getCredits() {
return credits;
}
public String getName() {
return name;
}
}
Offering.java
import java.sql.*;
public class Offering {
private int id;
private Course course;
private String daysTimes;
static String url = "jdbc:odbc:Reggie";
static { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }
catch (Exception ignored) {} }
public static Offering create(Course course, String daysTimesCsv)
throws Exception
{
Connection conn = null;
try {
conn = DriverManager.getConnection(url, "", "");
Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery(
"SELECT MAX(ID) FROM offering;");
result.next();
int newId = 1 + result.getInt(1);
statement.executeUpdate("INSERT INTO offering VALUES ('"
+ newId + "','" + course.getName()
+ "','" + daysTimesCsv + "');");
return new Offering(newId, course, daysTimesCsv);
} finally {
try { conn.close(); } catch (Exception ignored) {}
}
}
public static Offering find(int id) {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, "", "");
Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery(
"SELECT * FROM offering WHERE ID =" + id + ";");
if (result.next() == false)
return null;
String courseName = result.getString("Course");
Course course = Course.find(courseName);
String dateTime = result.getString("DateTime");
conn.close();
return new Offering(id, course, dateTime);
} catch (Exception ex) {
try { conn.close(); } catch (Exception ignored) {}
return null;
}
}
public void update() throws Exception {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, "", "");
Statement statement = conn.createStatement();
statement.executeUpdate(
"DELETE FROM Offering WHERE ID=" + id + ";");
statement.executeUpdate(
"INSERT INTO Offering VALUES('" + id + "','" +
course.getName() + "','" + daysTimes + "');");
} finally {
try { conn.close(); } catch (Exception ignored) {}
}
}
public Offering(int id, Course course, String daysTimesCsv) {
this.id = id;
this.course = course;
this.daysTimes = daysTimesCsv;
}
public int getId() {
return id;
}
public Course getCourse() {
return course;
}
public String getDaysTimes() {
return daysTimes;
}
public String toString() {
return "Offering " + getId() + ": "
+ getCourse() + " meeting " + getDaysTimes();
}
}
Schedule.java
import java.util.*;
import java.sql.*;
public class Schedule {
String name;
int credits = 0;
static final int minCredits = 12;
static final int maxCredits = 18;
boolean overloadAuthorized = false;
ArrayList schedule = new ArrayList();
static String url = "jdbc:odbc:Reggie";
static { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); }
catch (Exception ignored) {} }
public static void deleteAll() throws Exception {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, "", "");
Statement statement = conn.createStatement();
statement.executeUpdate("DELETE * FROM schedule;");
} finally {
try { conn.close(); } catch (Exception ignored) {}
}
}
public static Schedule create(String name) throws Exception {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, "", "");
Statement statement = conn.createStatement();
statement.executeUpdate(
"DELETE FROM schedule WHERE name = '" + name + "';");
return new Schedule(name);
} finally {
try { conn.close(); } catch (Exception ignored) {}
}
}
public static Schedule find(String name) {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, "", "");
Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery(
"SELECT * FROM schedule WHERE Name= '" + name + "';");
Schedule schedule = new Schedule(name);
while (result.next()) {
int offeringId = result.getInt("OfferingId");
Offering offering = Offering.find(offeringId);
schedule.add(offering);
}
return schedule;
} catch (Exception ex) {
return null;
} finally {
try { conn.close(); } catch (Exception ignored) {}
}
}
public static Collection all() throws Exception {
ArrayList result = new ArrayList();
Connection conn = null;
try {
conn = DriverManager.getConnection(url, "", "");
Statement statement = conn.createStatement();
ResultSet results = statement.executeQuery(
"SELECT DISTINCT Name FROM schedule;");
while (results.next())
result.add(Schedule.find(results.getString("Name")));
} finally {
try { conn.close(); } catch (Exception ignored) {}
}
return result;
}
public void update() throws Exception {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, "", "");
Statement statement = conn.createStatement();
statement.executeUpdate(
"DELETE FROM schedule WHERE name = '" + name + "';");
for (int i = 0; i < schedule.size(); i++) {
Offering offering = (Offering) schedule.get(i);
statement.executeUpdate(
"INSERT INTO schedule VALUES('" + name + "','"
+ offering.getId() + "');");
}
} finally {
try { conn.close(); } catch (Exception ignored) {}
}
}
public Schedule(String name) {
this.name = name;
}
public void add(Offering offering) {
credits += offering.getCourse().getCredits();
schedule.add(offering);
}
public void authorizeOverload(boolean authorized) {
overloadAuthorized = authorized;
}
public List analysis() {
ArrayList result = new ArrayList();
if (credits < minCredits)
result.add("Too few credits");
if (credits > maxCredits && !overloadAuthorized)
result.add("Too many credits");
checkDuplicateCourses(result);
checkOverlap(result);
return result;
}
public void checkDuplicateCourses(ArrayList analysis) {
HashSet courses = new HashSet();
for (int i = 0; i < schedule.size(); i++) {
Course course = ((Offering) schedule.get(i)).getCourse();
if (courses.contains(course))
analysis.add("Same course twice - " + course.getName());
courses.add(course);
}
}
public void checkOverlap(ArrayList analysis) {
HashSet times = new HashSet();
for (Iterator iterator = schedule.iterator();
iterator.hasNext();)
{
Offering offering = (Offering) iterator.next();
String daysTimes = offering.getDaysTimes();
StringTokenizer tokens = new StringTokenizer(daysTimes, ",");
while (tokens.hasMoreTokens()) {
String dayTime = tokens.nextToken();
if (times.contains(dayTime))
analysis.add("Course overlap - " + dayTime);
times.add(dayTime);
}
}
}
public String toString() {
return "Schedule " + name + ": " + schedule;
}
}