-
Notifications
You must be signed in to change notification settings - Fork 59
Erika solution #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Erika solution #22
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,8 @@ | ||
| package com.example; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.DriverManager; | ||
| import java.sql.SQLException; | ||
| import java.sql.*; | ||
| import java.util.Arrays; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Main { | ||
|
|
||
|
|
@@ -31,6 +30,246 @@ public void run() { | |
| throw new RuntimeException(e); | ||
| } | ||
| //Todo: Starting point for your code | ||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| System.out.println("Username:"); | ||
| String username = scanner.nextLine(); | ||
|
|
||
| System.out.println("Password:"); | ||
| String password = scanner.nextLine(); | ||
|
|
||
| boolean isValid = validateLogin(username, password, jdbcUrl, dbUser, dbPass); | ||
|
|
||
| if (!isValid) { | ||
| System.out.println("Invalid username or password"); | ||
| // Testarna förväntar sig att man kan välja 0 för att avsluta | ||
| System.out.print("Press 0 to exit or any key to continue..."); | ||
| String choice = scanner.nextLine(); | ||
| if ("0".equals(choice)) { | ||
| return; | ||
| } | ||
| } else { | ||
| System.out.println("Login successful!"); | ||
| } | ||
|
|
||
| // Huvudmeny | ||
| boolean running = true; | ||
| while (running) { | ||
| System.out.println("1) List moon missions"); | ||
| System.out.println("2) Get a moon mission by mission_id"); | ||
| System.out.println("3) Count missions for a given year"); | ||
| System.out.println("4) Create an account"); | ||
| System.out.println("5) Update an account password"); | ||
| System.out.println("6) Delete an account"); | ||
| System.out.println("0) Exit"); | ||
|
|
||
| System.out.print("Enter choice: "); | ||
| String choice = scanner.nextLine(); | ||
|
|
||
| switch (choice) { | ||
| case "1": | ||
| listMoonMissions(jdbcUrl, dbUser, dbPass); | ||
| break; | ||
| case "2": | ||
| System.out.print("Enter mission ID: "); | ||
| String missionId = scanner.nextLine(); | ||
| getMissionById(jdbcUrl, dbUser, dbPass, missionId); | ||
| break; | ||
| case "3": | ||
| System.out.print("Enter year: "); | ||
| String year = scanner.nextLine(); | ||
| countMissionsByYear(jdbcUrl, dbUser, dbPass, year); | ||
| break; | ||
| case "4": | ||
| createAccount(jdbcUrl, dbUser, dbPass, scanner); | ||
| break; | ||
| case "5": | ||
| updateAccountPassword(jdbcUrl, dbUser, dbPass, scanner); | ||
| break; | ||
| case "6": | ||
| deleteAccount(jdbcUrl, dbUser, dbPass, scanner); | ||
| break; | ||
| case "0": | ||
| System.out.println("Exiting..."); | ||
| running = false; | ||
| break; | ||
| default: | ||
| System.out.println("Invalid choice"); | ||
| } | ||
|
|
||
| if (running && !choice.equals("0")) { | ||
| System.out.println("\nPress Enter to continue..."); | ||
| scanner.nextLine(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private boolean validateLogin(String username, String password, | ||
| String jdbcUrl, String dbUser, String dbPass) { | ||
| String sql = "SELECT 1 FROM account WHERE name = ? AND password = ?"; | ||
| try (Connection conn = DriverManager.getConnection(jdbcUrl, dbUser, dbPass); | ||
| PreparedStatement stmt = conn.prepareStatement(sql)) { | ||
|
|
||
| stmt.setString(1, username); | ||
| stmt.setString(2, password); | ||
|
|
||
| try (ResultSet rs = stmt.executeQuery()) { | ||
| return rs.next(); | ||
| } | ||
|
|
||
| } catch (SQLException e) { | ||
| System.err.println("Login error: " + e.getMessage()); | ||
| return false; | ||
| } | ||
| } | ||
|
Comment on lines
+107
to
+124
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Close - ResultSet rs = stmt.executeQuery();
- return rs.next();
+ try (ResultSet rs = stmt.executeQuery()) {
+ return rs.next();
+ }
} catch (SQLException e) {
- System.err.println("Login error: " + e.getMessage());
+ System.err.println("Login error");
return false;
}🤖 Prompt for AI Agents |
||
|
|
||
| private void listMoonMissions(String jdbcUrl, String dbUser, String dbPass) { | ||
| String sql = "SELECT spacecraft FROM moon_mission ORDER BY mission_id"; | ||
| try (Connection conn = DriverManager.getConnection(jdbcUrl, dbUser, dbPass); | ||
| Statement stmt = conn.createStatement(); | ||
| ResultSet rs = stmt.executeQuery(sql)) { | ||
|
|
||
| System.out.println("\n=== Moon Missions ==="); | ||
| while (rs.next()) { | ||
| System.out.println(rs.getString("spacecraft")); | ||
| } | ||
|
|
||
| } catch (SQLException e) { | ||
| System.err.println("Error listing missions: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private void getMissionById(String jdbcUrl, String dbUser, String dbPass, String missionId) { | ||
| String sql = "SELECT * FROM moon_mission WHERE mission_id = ?"; | ||
| try (Connection conn = DriverManager.getConnection(jdbcUrl, dbUser, dbPass); | ||
| PreparedStatement stmt = conn.prepareStatement(sql)) { | ||
|
|
||
| stmt.setInt(1, Integer.parseInt(missionId)); | ||
| ResultSet rs = stmt.executeQuery(); | ||
|
|
||
| if (rs.next()) { | ||
| System.out.println("\n=== Mission Details ==="); | ||
| System.out.println("Mission ID: " + rs.getInt("mission_id")); | ||
| System.out.println("Spacecraft: " + rs.getString("spacecraft")); | ||
| System.out.println("Launch Date: " + rs.getDate("launch_date")); | ||
| System.out.println("Carrier Rocket: " + rs.getString("carrier_rocket")); | ||
| System.out.println("Operator: " + rs.getString("operator")); | ||
| System.out.println("Mission Type: " + rs.getString("mission_type")); | ||
| System.out.println("Outcome: " + rs.getString("outcome")); | ||
| } else { | ||
| System.out.println("Mission not found"); | ||
| } | ||
|
|
||
| } catch (SQLException | NumberFormatException e) { | ||
| System.err.println("Error: " + e.getMessage()); | ||
| } | ||
| } | ||
|
Comment on lines
+142
to
+166
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Close - stmt.setInt(1, Integer.parseInt(missionId));
- ResultSet rs = stmt.executeQuery();
-
- if (rs.next()) {
+ stmt.setInt(1, Integer.parseInt(missionId));
+ try (ResultSet rs = stmt.executeQuery()) {
+ if (rs.next()) {
System.out.println("\n=== Mission Details ===");
System.out.println("Mission ID: " + rs.getInt("mission_id"));
System.out.println("Spacecraft: " + rs.getString("spacecraft"));
System.out.println("Launch Date: " + rs.getDate("launch_date"));
System.out.println("Carrier Rocket: " + rs.getString("carrier_rocket"));
System.out.println("Operator: " + rs.getString("operator"));
System.out.println("Mission Type: " + rs.getString("mission_type"));
System.out.println("Outcome: " + rs.getString("outcome"));
- } else {
- System.out.println("Mission not found");
+ } else {
+ System.out.println("Mission not found");
+ }
}🤖 Prompt for AI Agents |
||
|
|
||
| private void countMissionsByYear(String jdbcUrl, String dbUser, String dbPass, String year) { | ||
| String sql = "SELECT COUNT(*) as count FROM moon_mission WHERE YEAR(launch_date) = ?"; | ||
| try (Connection conn = DriverManager.getConnection(jdbcUrl, dbUser, dbPass); | ||
| PreparedStatement stmt = conn.prepareStatement(sql)) { | ||
|
|
||
| stmt.setInt(1, Integer.parseInt(year)); | ||
| ResultSet rs = stmt.executeQuery(); | ||
|
|
||
| if (rs.next()) { | ||
| int count = rs.getInt("count"); | ||
| System.out.println("Number of missions in " + year + ": " + count); | ||
| } | ||
|
|
||
| } catch (SQLException | NumberFormatException e) { | ||
| System.err.println("Error: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private void createAccount(String jdbcUrl, String dbUser, String dbPass, Scanner scanner) { | ||
| System.out.print("First name: "); | ||
| String firstName = scanner.nextLine(); | ||
|
|
||
| System.out.print("Last name: "); | ||
| String lastName = scanner.nextLine(); | ||
|
|
||
| System.out.print("SSN: "); | ||
| String ssn = scanner.nextLine(); | ||
|
|
||
| System.out.print("Password: "); | ||
| String password = scanner.nextLine(); | ||
|
|
||
| // Skapa username från förnamn + första 3 bokstäver av efternamn | ||
| String username = cap3(firstName) + cap3(lastName); | ||
|
|
||
| String sql = "INSERT INTO account (first_name, last_name, ssn, name, password) " + | ||
| "VALUES (?, ?, ?, ?, ?)"; | ||
|
|
||
| try (Connection conn = DriverManager.getConnection(jdbcUrl, dbUser, dbPass); | ||
| PreparedStatement stmt = conn.prepareStatement(sql)) { | ||
|
|
||
| stmt.setString(1, firstName); | ||
| stmt.setString(2, lastName); | ||
| stmt.setString(3, ssn); | ||
| stmt.setString(4, username); | ||
| stmt.setString(5, password); | ||
|
|
||
| int rows = stmt.executeUpdate(); | ||
| if (rows > 0) { | ||
| System.out.println("Account created successfully!"); | ||
| System.out.println("Username: " + username); | ||
| } | ||
|
|
||
| } catch (SQLException e) { | ||
| System.err.println("Error creating account: " + e.getMessage()); | ||
| } | ||
| } | ||
|
Comment on lines
+186
to
+223
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # First, explore the project structure to understand build system and dependencies
find . -maxdepth 2 -type f \( -name "pom.xml" -o -name "build.gradle" -o -name "build.gradle.kts" \)Repository: ithsjava25/ithsjava25-java-databas-jdbc-javafx-classrom Length of output: 108 🏁 Script executed: # Locate and read the dependency file to see what libraries are available
git ls-files | grep -E "(pom\.xml|build\.gradle|build\.gradle\.kts)" | head -5Repository: ithsjava25/ithsjava25-java-databas-jdbc-javafx-classrom Length of output: 106 🏁 Script executed: # Read the pom.xml to check project dependencies
cat pom.xmlRepository: ithsjava25/ithsjava25-java-databas-jdbc-javafx-classrom Length of output: 2791 🏁 Script executed: # Search for password-related code and authentication logic
rg -i "password" src/main/java --type java -B 2 -A 2Repository: ithsjava25/ithsjava25-java-databas-jdbc-javafx-classrom Length of output: 6569 🏁 Script executed: # Search for any hashing or encoding related code
rg -i "(hash|bcrypt|encrypt|encode)" src/main/java --type javaRepository: ithsjava25/ithsjava25-java-databas-jdbc-javafx-classrom Length of output: 81 🏁 Script executed: # Look for authentication or login methods
rg -i "(login|authenticate|verify|validate.*password)" src/main/java --type java -B 2 -A 5Repository: ithsjava25/ithsjava25-java-databas-jdbc-javafx-classrom Length of output: 2420 🏁 Script executed: # Verify the exact lines of validateLogin method to understand password comparison
sed -n '155,180p' src/main/java/com/example/Main.javaRepository: ithsjava25/ithsjava25-java-databas-jdbc-javafx-classrom Length of output: 1277 🏁 Script executed: # Find the validateLogin method specifically
rg -n "private boolean validateLogin" src/main/java/com/example/Main.java -A 15Repository: ithsjava25/ithsjava25-java-databas-jdbc-javafx-classrom Length of output: 858 Plaintext password storage and validation is a critical security vulnerability — passwords are stored and compared in plaintext across Similarly, SSN should not be stored in plaintext. |
||
|
|
||
| private void updateAccountPassword(String jdbcUrl, String dbUser, String dbPass, Scanner scanner) { | ||
| System.out.print("Enter user ID: "); | ||
| String userId = scanner.nextLine(); | ||
|
|
||
| System.out.print("Enter new password: "); | ||
| String newPassword = scanner.nextLine(); | ||
|
|
||
| String sql = "UPDATE account SET password = ? WHERE user_id = ?"; | ||
|
|
||
| try (Connection conn = DriverManager.getConnection(jdbcUrl, dbUser, dbPass); | ||
| PreparedStatement stmt = conn.prepareStatement(sql)) { | ||
|
|
||
| stmt.setString(1, newPassword); | ||
| stmt.setInt(2, Integer.parseInt(userId)); | ||
|
|
||
| int rows = stmt.executeUpdate(); | ||
| if (rows > 0) { | ||
| System.out.println("Password updated successfully"); | ||
| } else { | ||
| System.out.println("No account found with ID: " + userId); | ||
| } | ||
|
|
||
| } catch (SQLException | NumberFormatException e) { | ||
| System.err.println("Error: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private void deleteAccount(String jdbcUrl, String dbUser, String dbPass, Scanner scanner) { | ||
| System.out.print("Enter user ID to delete: "); | ||
| String userId = scanner.nextLine(); | ||
|
|
||
| String sql = "DELETE FROM account WHERE user_id = ?"; | ||
|
|
||
| try (Connection conn = DriverManager.getConnection(jdbcUrl, dbUser, dbPass); | ||
| PreparedStatement stmt = conn.prepareStatement(sql)) { | ||
|
|
||
| stmt.setInt(1, Integer.parseInt(userId)); | ||
|
|
||
| int rows = stmt.executeUpdate(); | ||
| if (rows > 0) { | ||
| System.out.println("Account deleted successfully"); | ||
| } else { | ||
| System.out.println("No account found with ID: " + userId); | ||
| } | ||
|
|
||
| } catch (SQLException | NumberFormatException e) { | ||
| System.err.println("Error: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -59,4 +298,14 @@ private static String resolveConfig(String propertyKey, String envKey) { | |
| } | ||
| return (v == null || v.trim().isEmpty()) ? null : v.trim(); | ||
| } | ||
| } | ||
|
|
||
| private static String cap3(String s) { | ||
| if (s == null) return ""; | ||
| String t = s.trim(); | ||
| if (t.isEmpty()) return ""; | ||
|
|
||
| String part = t.substring(0, Math.min(3, t.length())); | ||
| return part.substring(0, 1).toUpperCase() + | ||
| part.substring(1).toLowerCase(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don’t allow main menu access after failed login — currently, a user can fail auth and still proceed into CRUD/query operations by choosing “any key to continue”.
📝 Committable suggestion
🤖 Prompt for AI Agents