-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathMain.java
More file actions
293 lines (244 loc) · 10.1 KB
/
Copy pathMain.java
File metadata and controls
293 lines (244 loc) · 10.1 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
package com.example;
import java.sql.*;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static void main(String[] args) {
if (isDevMode(args)) {
DevDatabaseInitializer.start();
}
new Main().run();
}
public void run() {
// Resolve DB settings with precedence: System properties -> Environment variables
String jdbcUrl = resolveConfig("APP_JDBC_URL", "APP_JDBC_URL");
String dbUser = resolveConfig("APP_DB_USER", "APP_DB_USER");
String dbPass = resolveConfig("APP_DB_PASS", "APP_DB_PASS");
if (jdbcUrl == null || dbUser == null || dbPass == null) {
throw new IllegalStateException(
"Missing DB configuration. Provide APP_JDBC_URL, APP_DB_USER, APP_DB_PASS " +
"as system properties (-Dkey=value) or environment variables.");
}
try (Connection connection =
DriverManager.getConnection(jdbcUrl, dbUser, dbPass)) {
//Todo: Starting point for your code
runApplication(connection);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
Scanner scanner = new Scanner(System.in);
private boolean exitAfterLogin = false;
private void runApplication(Connection connection) throws SQLException {
if (loginFlow(connection)) {
menuLoop(connection);
}
}
//User login
private boolean loginFlow(Connection connection) throws SQLException {
while (true) {
System.out.print("Input username: ");
String username = scanner.nextLine().strip();
System.out.print("Input password: ");
String password = scanner.nextLine().strip();
if (checkCredentials(connection, username, password)) {
System.out.println("Login Successful!");
System.out.println();
return true;
}
//If the login is invalid, print a message containing the word invalid and allow exiting via option 0
System.out.print("Credentials invalid. Try again (Enter) or Exit? (0): ");
if ("0".equals(scanner.nextLine().strip())) {
exitAfterLogin = true;
return false;
}
}
}
//Used for a lab exercise
//Check of user credentials
private boolean checkCredentials(Connection connection, String user, String pass)
throws SQLException {
String sql = "SELECT 1 FROM account WHERE name=? AND password=?";
try (PreparedStatement ps = connection.prepareStatement(sql)) {
ps.setString(1, user);
ps.setString(2, pass);
return ps.executeQuery().next();
}
}
private void menuLoop(Connection connection) throws SQLException {
boolean running = !exitAfterLogin;
while (running) {
printMenu();
System.out.print("Choose option: ");
String command = scanner.nextLine().strip();
switch (command) {
case "1", "list" -> listMissions(connection);
case "2", "get" -> getMissionById(connection);
case "3", "count" -> countMissionsByYear(connection);
case "4", "create" -> createAccount(connection);
case "5", "update" -> updateAccountPassword(connection);
case "6", "delete" -> deleteAccount(connection);
case "0", "exit" -> running = false;
default -> System.out.println("Invalid choice.");
}
System.out.println();
}
}
//Menu with options
private void printMenu() {
System.out.println("""
==========================
1) List moon missions
2) Get mission by id
3) Count missions by year
4) Create account
5) Update account password
6) Delete account
0) Exit
==========================
""");
}
private void listMissions(Connection connection) throws SQLException {
String sql = "SELECT spacecraft FROM moon_mission";
try (PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
System.out.println(rs.getString("spacecraft"));
}
}
}
private void getMissionById(Connection connection) throws SQLException {
System.out.print("Mission id: ");
String id = scanner.nextLine();
String sql = "SELECT * FROM moon_mission WHERE mission_id=?";
try (PreparedStatement ps = connection.prepareStatement(sql)) {
ps.setString(1, id);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
System.out.print(rs.getString(i) + " ");
}
System.out.println();
}
}
}
private void countMissionsByYear(Connection connection) throws SQLException {
System.out.print("Year: ");
String year = scanner.nextLine();
String sql = "SELECT COUNT(*) FROM moon_mission WHERE YEAR(launch_date)=?";
try (PreparedStatement ps = connection.prepareStatement(sql)) {
ps.setString(1, year);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
System.out.println("Missions launched in " + year + ": " + rs.getInt(1));
}
}
}
private void createAccount(Connection connection) throws SQLException {
String first = readRequired("First name");
String last = readRequired("Last name");
String ssn = readRequired("SSN");
String password = readRequired("Password");
String username =
first.substring(0, Math.min(2, first.length())) +
last.substring(0, Math.min(2, last.length()));
String checkSql = "SELECT COUNT(*) FROM account WHERE name=? OR ssn=?";
try (PreparedStatement ps = connection.prepareStatement(checkSql)) {
ps.setString(1, username);
ps.setString(2, ssn);
ResultSet rs = ps.executeQuery();
if (rs.next() && rs.getInt(1) > 0) {
System.out.println("Account already exists.");
return;
}
}
String insertSql = """
INSERT INTO account (first_name, last_name, ssn, password, name)
VALUES (?, ?, ?, ?, ?)
""";
try (PreparedStatement ps = connection.prepareStatement(insertSql)) {
ps.setString(1, first);
ps.setString(2, last);
ps.setString(3, ssn);
ps.setString(4, password);
ps.setString(5, username);
ps.executeUpdate();
}
System.out.println("Account successfully created.");
}
private void updateAccountPassword(Connection connection) throws SQLException {
System.out.print("User id: ");
String id = scanner.nextLine();
if (!accountExists(connection, id)) {
System.out.println("User not found.");
return;
}
//Ask for a valid password ie not blank
String newPassword = readRequired("New password");
String sql = "UPDATE account SET password=? WHERE user_id=?";
try (PreparedStatement ps = connection.prepareStatement(sql)) {
ps.setString(1, newPassword);
ps.setString(2, id);
ps.executeUpdate();
}
System.out.println("Password updated.");
System.out.println("Press Enter to continue");
scanner.nextLine();
}
private void deleteAccount(Connection connection) throws SQLException {
System.out.print("User id to delete: ");
String id = scanner.nextLine();
if (!accountExists(connection, id)) {
System.out.println("Account does not exist.");
return;
}
String sql = "DELETE FROM account WHERE user_id=?";
try (PreparedStatement ps = connection.prepareStatement(sql)) {
ps.setString(1, id);
int rows = ps.executeUpdate();
System.out.println("Deleted rows: " + rows);
}
}
private boolean accountExists(Connection connection, String id) throws SQLException {
String sql = "SELECT 1 FROM account WHERE user_id=?";
try (PreparedStatement ps = connection.prepareStatement(sql)) {
ps.setString(1, id);
return ps.executeQuery().next();
}
}
private String readRequired(String label) {
while (true) {
System.out.print(label + ": ");
String value = scanner.nextLine().strip();
if (!value.isEmpty()) {
return value;
}
System.out.println(label + " cannot be empty.");
}
}
/**
* Determines if the application is running in development mode based on system properties,
* environment variables, or command-line arguments.
*
* @param args an array of command-line arguments
* @return {@code true} if the application is in development mode; {@code false} otherwise
*/
private static boolean isDevMode(String[] args) {
if (Boolean.getBoolean("devMode")) //Add VM option -DdevMode=true
return true;
if ("true".equalsIgnoreCase(System.getenv("DEV_MODE"))) //Environment variable DEV_MODE=true
return true;
return Arrays.asList(args).contains("--dev"); //Argument --dev
}
/**
* Reads configuration with precedence: Java system property first, then environment variable.
* Returns trimmed value or null if neither source provides a non-empty value.
*/
private static String resolveConfig(String propertyKey, String envKey) {
String v = System.getProperty(propertyKey);
if (v == null || v.trim().isEmpty()) {
v = System.getenv(envKey);
}
return (v == null || v.trim().isEmpty()) ? null : v.trim();
}
}