Repository navigation
Expand file tree
/
Copy pathDatabase.java
More file actions
406 lines (348 loc) · 11 KB
/
Copy pathDatabase.java
File metadata and controls
406 lines (348 loc) · 11 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package application;
import java.sql.*;
import java.util.ArrayList;
/**
* DataBase - a class that handles pulling and pushing data from and to a
* database through SQLLite
*
* @author Edward P, Mohammad N, Aravind U
*
*/
public class Database {
static Connection connection = null;
String url;
Statement statement;
/**
* Constructor - sets up the data base and will put in a table if a table does
* not already exist within the database
*
* @param fileName - the name of the data base
*/
public Database(String fileName) {
url = "jdbc:sqlite:" + fileName;
try {
connection = DriverManager.getConnection(url);
if (connection == null) {
System.out.println("A new database has been created.");
} else {
System.out.println("A database already exists");
}
//
statement = connection.createStatement();
String sql = "CREATE TABLE IF NOT EXISTS HABITS " + " (habit VARCHAR(255), " + " goal INTEGER, "
+ " days VARCHAR(7), " + " status VARCHAR(7), " + " weekly VARCHAR(5)," + " overall VARCHAR(15))";
statement.executeUpdate(sql);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
public static void closeDB() {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//-------------------------------------DATABASE MANIPULATION METHODS--------------------------------------------------------
/**
* addHabit - adds a habit to the data base
*
* @param h - the habit that you want to add to the database
*/
public void addHabit(Habit h) {
String[] info = h.getHabitInfo();
String habit = info[0];
int goal = Integer.parseInt(info[1]);
String days = info[2];
String status = info[3];
System.out.println("GoalDB: " + goal);
System.out.println("DaysDB: " + days);
try {
// gets a connection
statement = connection.createStatement();
String sql = "INSERT INTO HABITS " + "VALUES ('" + habit + "', '" + goal + "', '" + days + "', '" + status
+ "', '000', '000')";
statement.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* addHabit - deletes a habit to the data base
*
* @param h - the habit that you want to add to the database
*/
public void deleteHabit(Habit h) {
String[] info = h.getHabitInfo();
String habit = info[0];
try {
statement = connection.createStatement();
String sql = "DELETE FROM HABITS " + "WHERE habit LIKE '%" + habit + "%'";
statement.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* updateHabit - gets the habit you want from the database and changes the
* string of the habit
*
* @param habit - the String that you would like to change the current string in
* habit to
*/
public void updateHabit(Habit h, String habit) {
try {
statement = connection.createStatement();
String sql = "UPDATE HABITS " + "SET habit = '" + habit + "' WHERE habit LIKE '%" + h.getHabit() + "%'";
statement.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* updateStatus -updates the status of the habit within the database
* @param h - the habit that you like to update
* @param status - the new status that you would like to pass into the database
*/
public void updateStatus(Habit h, String status) {
try {
statement = connection.createStatement();
String sql = "UPDATE HABITS " + "SET status = '" + status + "' WHERE habit LIKE '%" + h.getHabit() + "%'";
statement.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* updateDays - updates the days of the habit within the database
* @param h - the habit that you would like to change
* @param days - the new string of days that you would like to update days to
*/
public void updateDays(Habit h, String days) {
try {
statement = connection.createStatement();
String sql = "UPDATE HABITS " + "SET days = '" + days + "' WHERE habit = '" + h.getHabit() + "'";
statement.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* updateWeeklyStat - changes the value within the DB table for the column
* 'weekly' which holds on to tthe stats of the week
*
* @param stat - should have three characters in the string where charAt(0) is
* the days completed, chatAt(1) is the days missed, and charAt(2)
* is the days still to come
*/
public void updateWeeklyStat(Habit h, String stat) {
int[] test = stringToIntArr(stat);
if (test.length != 3) {
throw new IllegalArgumentException("The value provided is not valid");
}
try {
statement = connection.createStatement();
String sql = "UPDATE HABITS " + "SET weekly = '" + stat + "' WHERE habit LIKE '%" + h.getHabit() + "%'";
statement.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* updateWeeklyStat - changes the value within the DB table for the column
* 'overall' which holds the stats for all time
*
* @param stat - should have two Integer characters in the string where
* charAt(0) is the days completed and chatAt(1) is the days missed
*
*
*/
public void updateOverallStat(Habit h, String stat) {
// to make sure that an invalid argument cannot reach the code
int[] test = stringToIntArr(stat);
if (test.length != 2) {
throw new IllegalArgumentException("The value provided is not valid");
}
try {
statement = connection.createStatement();
String sql = "UPDATE HABITS " + "SET overall = '" + stat + "' WHERE habit LIKE '%" + h.getHabit() + "%'";
statement.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateGoal(Habit h, int i) {
// to make sure that an invalid argument cannot reach the code
try {
statement = connection.createStatement();
String sql = "UPDATE HABITS " + "SET goal = '" + i + "' WHERE habit LIKE '%" + h.getHabit() + "%'";
statement.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* resetWeeklyStat - resets the weekly stat in the data base to 000
*
* @param h - the habit where you want to change the weekly stat
*/
public void resetWeeklyStat(Habit h) {
updateWeeklyStat(h, "000");
}
//----------------------------------------DATA RETRIVAL METHODS-------------------------------------------------------------
/**
* getHabits - gets all the habits from the database
*
* @return - an arraylist<Habit> with all the habits that are stored in the
* database
*/
public ArrayList<Habit> getHabits() {
ArrayList<Habit> habits = new ArrayList<Habit>();
String sql = "SELECT habit, goal, days, status FROM HABITS";
try {
ResultSet rs = statement.executeQuery(sql);
while (rs.next()) {
String habit = rs.getString("habit");
int goal = rs.getInt("goal");
boolean[] days = stringToBoolArr(rs.getString("days"));
int[] status = stringToIntArrStatus(rs.getString("status"));
Habit h = new Habit(habit, goal, days, status);
habits.add(h);
}
return habits;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* getOverallStat - gets the overall column for a certain habit
*
* @param h - the habit that you are looking for in the database
* @return a int[] where int[0] is the number of times completed and int[1] is
* the number of days missed
*/
public int[] getWeeklyStat(Habit h) {
int[] stats = new int[3];
try {
statement = connection.createStatement();
String sql = "SELECT weekly " + "FROM HABITS" + " WHERE habit LIKE '%" + h.getHabit() + "%'";
ResultSet rs = statement.executeQuery(sql);
String statString = rs.getString("weekly");
// puts the values into the array
stats = stringToIntArr(statString);
return stats;
} catch (SQLException e) {
e.printStackTrace();
}
return stats;
}
/**
* getOverallStat - gets the overall column for a certain habit
*
* @param h - the habit that you are looking for in the database
* @return a int[] where int[0] is the number of times completed and int[1] is
* the number of days missed
*/
public int[] getOverallStat(Habit h) {
int stats[] = new int[3];
try {
// gets the data from the database
statement = connection.createStatement();
String sql = "SELECT overall " + "FROM HABITS" + " WHERE habit LIKE '%" + h.getHabit() + "%'";
ResultSet rs = statement.executeQuery(sql);
String statString = rs.getString("overall");
// puts the values into the array
stats = stringToIntArr(statString);
return stats;
} catch (SQLException e) {
e.printStackTrace();
}
return stats;
}
/**
* getStatus - Gets the status of the habit
*
* @param h - the habit that you are looking for in the database
* @return an int[] where the index represents the days of week and the
* corresponding number tells the status of the habit of that day
*/
public int[] getStatus(Habit h) {
int stats[] = new int[3];
try {
// gets the data from the database
statement = connection.createStatement();
String sql = "SELECT stat " + "FROM HABITS" + " WHERE habit LIKE '%" + h.getHabit() + "%'";
ResultSet rs = statement.executeQuery(sql);
String statString = rs.getString("overall");
// puts the values into the array
stats = stringToIntArrStatus(statString);
return stats;
} catch (SQLException e) {
e.printStackTrace();
}
return stats;
}
/**
* printValues - prints the values into the the console
*/
public void printValues() {
ArrayList<Habit> h = getHabits();
for (Habit habit : h) {
System.out.println(habit);
}
}
//------------------------------------HELPER METHODS------------------------------------------------------------------------
/**
* charToInt - converts a char to an int
*
* @param a - the char value that you would like to convert
* @return an integer value of the char given
*/
private int charToInt(char a) {
return Integer.parseInt(String.valueOf(a));
}
/**
* stringToIntArr - takes a string of Intgers seperated by a whitespace and puts
* it into an int array
*
* @param s - the string that you would like to convert into a int array
* @return the int array containing the values from the string;
*/
private int[] stringToIntArrStatus(String s) {
try {
int[] intArr = new int[7];
for (int i = 0; i < 7; i++) {
intArr[i] = charToInt(s.charAt(i));
}
return intArr;
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
return null;
}
private int[] stringToIntArr(String s) {
try {
String[] sArr = s.split(" ");
int[] intArr = new int[sArr.length];
for (int i = 0; i < intArr.length; i++) {
intArr[i] = Integer.parseInt(sArr[i]);
}
return intArr;
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
return null;
}
public boolean[] stringToBoolArr(String goal) {
boolean[] daysBool = new boolean[7];
for (int i = 0; i < goal.length(); i++) {
if (goal.charAt(i) == '1') {
daysBool[i] = true;
} else {
daysBool[i] = false;
}
}
return daysBool;
}
}