-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
281 lines (208 loc) · 7.75 KB
/
Copy pathMain.java
File metadata and controls
281 lines (208 loc) · 7.75 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
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
class Main{
private static Scanner scan = new Scanner(System.in);
private static double totalPace = 0;
private static double totalDist = 0;
private static double totalMood = 0;
private static double totalExhaust = 0;
private static double totalTime = 0;
private static int numRuns;
/**
* This method gets today's current date
* @return String today's date
*/
public static String getDateToday(){
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date dateToday = new Date();
return format.format(dateToday);
}
/**
* This method creates a new run based on user input.
* @return new Run with parameters filled
*/
public static Run newRun(){
//TODO: catching input errors
System.out.print("Minutes run: ");
int mins = scan.nextInt();
System.out.print("Seconds run: ");
int secs = scan.nextInt();
System.out.print("Distance run (mi): ");
double dist = scan.nextDouble();
System.out.print("On a scale of 1-5, how tired are you? : ");
int exhaust = scan.nextInt();
System.out.print("On a scale of 1-5, with 1 being the worst and 5 being the best, "
+ "how do you feel mentally today? : ");
int mood = scan.nextInt();
//Run(double dist, int mins, int secs, int mind, int tiredness)
Run myRun = new Run(dist, mins, secs, mood, exhaust);
return myRun;
}
/**
* Reads data from the file containing the runs and adds up totals for each category.
* @param file String of file name containing the runs
* @throws FileNotFoundException
*/
public static void readData(String file) throws FileNotFoundException{
Scanner scanner = new Scanner(new FileInputStream(file));
for (int i = 0; scanner.hasNextLine(); i++){
scanner.next();
totalDist+=Double.parseDouble(scanner.next());
totalTime+=Double.parseDouble(scanner.next());
totalPace+=Double.parseDouble(scanner.next());
totalExhaust+=Double.parseDouble(scanner.next());
totalMood+=Double.parseDouble(scanner.next());
numRuns++;
scanner.nextLine();
}
scanner.close();
}
/**
* This method is used to average different categories of data (distance, time, etc)
* @param total
* @param numNums
* @return the average based on the number of runs
*/
public static double getAvg(double total, int numNums){
return (total / numNums);
}
/**
* This method averages each category of data and prints out the results for the user.
* @param year
*/
public static void processData(String year){
//TODO: formatting
System.out.println("Averges for the Year " + year);
System.out.println("Pace:" + getAvg(totalPace,numRuns));
System.out.println("Distance:" + getAvg(totalDist,numRuns));
System.out.println("Time Run:" + getAvg(totalTime,numRuns));
System.out.println("Mood:" + getAvg(totalMood, numRuns));
System.out.println("Tiredness:" + getAvg(totalExhaust, numRuns));
}
/**
* This class connects to the local host w/o SSL
* @return Connection the connection to the local host
* @throws Exception
*/
public static Connection getConnection() throws Exception{
Scanner scan = new Scanner(System.in);
//create interface here?
try{
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/myData?autoReconnect=true&useSSL=false";
String username = "root";
System.out.println("Please enter your password to acess the database.");
String password = scan.next();
Class.forName(driver);
Connection con = DriverManager.getConnection(url,username,password);
System.out.println("Connected");
return con;
}
catch(Exception e){
System.out.println(e);
}
return null;
}
/**
* This method creates a new log for the database if one is not already created.
* @param con Database to connect to
* @throws Exception
*/
public static void createLog(Connection con) throws Exception{
try{
String createLog = "CREATE TABLE IF NOT EXISTS log(id int NOT NULL AUTO_INCREMENT, date varchar(10),distance varChar(5), time varChar(5), pace varChar(5), tiredness char(1), mood char(1), PRIMARY KEY(id))";
PreparedStatement create = con.prepareStatement(createLog);
create.executeUpdate();
}
catch(Exception e){
System.out.println(e);
}
finally{
System.out.println("Log created");
}
}
/**
* This method adds an individual run to the log.
* @param con Database to connect to
* @param run Run to log
* @throws Exception
*/
public static void addRun(Connection con, Run run) throws Exception{
final String distance = Double.toString(run.getDistance());
final String time = run.getComboTime();
final String pace = run.getPace();
final String tiredness = Integer.toString(run.getExhaustion());
final String mood = Integer.toString(run.getMood());
try{
String insert = "INSERT INTO log (date, distance, time, pace, tiredness, mood) VALUES(" + "CURDATE()" + "," + distance + "," + time + "," + pace + "," + tiredness + "," + mood + ")";
PreparedStatement posted = con.prepareStatement(insert);
posted.executeUpdate();
}
catch (Exception e){
System.out.println(e);
}
finally{
System.out.println("Run logged");
}
}
/**
* This method prints out the avgs from runs in the past week, month, and year
* @param con database connection
* @throws Exception in case info cannot be quieried
*/
public static void printResults(Connection con) throws Exception{
System.out.println("This week");
System.out.println(" Avg Pace: " + getAvgInfo(con, 7, "pace") + " min/mi");
System.out.println(" Avg Level of Run: " + getAvgInfo(con, 7, "tiredness"));
System.out.println(" Avg Mental Wellness: " + getAvgInfo(con, 7, "mood"));
System.out.println("\nThis month");
System.out.println(" Avg Pace: " + getAvgInfo(con, 30, "pace") + " min/mi");
System.out.println(" Avg Level of Run: " + getAvgInfo(con, 30, "tiredness"));
System.out.println(" Avg Mental Wellness: " + getAvgInfo(con, 30, "mood"));
System.out.println("\nThis year");
System.out.println(" Avg Pace: " + getAvgInfo(con, 365, "pace") + " min/mi");
System.out.println(" Avg Level of Run: " + getAvgInfo(con, 365, "tiredness"));
System.out.println(" Avg Mental Wellness: " + getAvgInfo(con, 365, "mood"));
}
/**
* This method uses the avg aggregate to get the averages for various columns of the log.
* @param con database connection
* @param amtTime number of days to avg over
* @param field the field to be queried (pace,tiredness, etc)
* @return Double the avg cut off at 2 decimal places
* @throws Exception
*/
public static Double getAvgInfo(Connection con, int amtTime, String field) throws Exception{
try{
PreparedStatement statementMonth = con.prepareStatement("SELECT avg(" + field +") FROM log WHERE date >= DATE_SUB(CURDATE(), INTERVAL " + amtTime +" DAY)");
ResultSet result = statementMonth.executeQuery();
result.next();
return ((((int)(Double.parseDouble(result.getString("avg(" + field +")"))*100))/100.0));
}
catch(Exception e){
System.out.println(e);
}
return null;
}
public static void main(String[] args) throws Exception {
//print today's date
System.out.println(getDateToday());
//create new run
Run run = newRun();
Connection con = getConnection();
//add info to the database
createLog(con);
addRun(con, run);
//print the avgs for week, month, year
printResults(con);
}
}