Conversation
Signed-off-by: Jesper Larsson <jesper.larsson@iths.se>
Signed-off-by: Jesper Larsson <jesper.larsson@iths.se>
Signed-off-by: Jesper Larsson <jesper.larsson@iths.se>
Signed-off-by: Jesper Larsson <jesper.larsson@iths.se>
WalkthroughThe PR adds a header badge to README.md and substantially refactors Main.java to introduce an interactive application with login authentication, menu-driven operations for moon mission management, and account administration features backed by database queries using PreparedStatement. Changes
Sequence DiagramsequenceDiagram
actor User
participant App as Main<br/>(Application)
participant DB as Database
User->>App: Launch Application
activate App
App->>User: Prompt for Login
User->>App: Enter Credentials
App->>DB: Verify Username & Password<br/>(PreparedStatement)
DB-->>App: Auth Result
alt Authentication Failed
App->>User: Invalid Credentials
App->>User: Retry or Exit?
User->>App: Retry
App->>User: Prompt for Login
else Authentication Successful
App->>User: Login Successful
loop Interactive Menu
App->>User: Display Menu Options<br/>(List, Get, Count, Create, Update, Delete)
User->>App: Select Operation
alt Query Operations
App->>DB: Execute Query<br/>(List Missions / Get Mission / Count by Year)
DB-->>App: ResultSet
App->>User: Display Results
else Write Operations
App->>User: Prompt for Input
User->>App: Provide Details<br/>(validated)
App->>DB: INSERT/UPDATE/DELETE<br/>(PreparedStatement)
DB-->>App: Confirmation
App->>User: Operation Confirmation
end
App->>User: Continue or Exit?
end
App->>User: Goodbye
end
deactivate App
Estimated code review effort🎯 4 (Complex) | ⏱️ ~50 minutes
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/main/java/com/example/Main.java (1)
13-18: Critical: Missingpublicmodifier on main method.The
mainmethod must be declaredpublicfor the JVM to invoke it as the application entry point. Without it, runningjava com.example.Mainwill fail with "Error: Main method not found in class".- static void main(String[] args) { + public static void main(String[] args) {
🧹 Nitpick comments (7)
src/main/java/com/example/Main.java (7)
5-5: Unused import.
Comparatoris imported but never used in this file.-import java.util.Comparator;
11-11: Consider making the Scanner field private.The field has package-private access by default. For better encapsulation, mark it
private.- Scanner scanner = new Scanner(System.in); + private final Scanner scanner = new Scanner(System.in);
34-53: Remove commented-out code and consider retry logic.Line 35 has leftover commented code that should be removed. Additionally, after a failed login, the user is stuck in an infinite loop with only the exit option (0). Consider whether retry attempts should be allowed.
while(true) { - //Scanner scanner = new Scanner(System.in); boolean authorized = login(connection);
87-99: Improve ResultSet handling and column reference.
- The
ResultSetshould be explicitly closed (use try-with-resources or alias in query)- Using
count(*)as the column name is fragile; prefer a column alias or indexCOUNT(*)always returns exactly one row, soif (rs.next())is clearer thanwhile- String accQuery = "select count(*) from account where binary name = ? and binary password = ?"; + String accQuery = "SELECT COUNT(*) AS cnt FROM account WHERE BINARY name = ? AND BINARY password = ?"; try(PreparedStatement statement = connection.prepareStatement(accQuery)){ statement.setString(1, unm); statement.setString(2, pw); - ResultSet rs = statement.executeQuery(); - while(rs.next()) { - if(rs.getInt("count(*)") == 1){return true;} + try (ResultSet rs = statement.executeQuery()) { + return rs.next() && rs.getInt("cnt") == 1; } - return false; } catch (SQLException e) { throw new RuntimeException(e); }
118-126: Close ResultSet explicitly.The
ResultSetshould be closed via try-with-resources to ensure proper resource cleanup.try(PreparedStatement statement = connection.prepareStatement(spaceshipQuery)){ - ResultSet rs = statement.executeQuery(); - while(rs.next()) { - System.out.println(rs.getString("spacecraft")); + try (ResultSet rs = statement.executeQuery()) { + while(rs.next()) { + System.out.println(rs.getString("spacecraft")); + } } } catch (SQLException e) {The same pattern should be applied to
getMission,missionsCountYear, andcreateAccountmethods.
219-228: Prefer explicit column list in INSERT statement.Using
INSERT INTO account VALUES (0, ?, ...)relies on column order and assumes the first column is auto-increment. Explicitly listing columns improves clarity and resilience to schema changes.- String newAccQuery = "insert into account values (0, ?, ?, ?, ?, ?)"; + String newAccQuery = "INSERT INTO account (name, password, first_name, last_name, ssn) VALUES (?, ?, ?, ?, ?)";
250-252: Consider checking affected row count.
executeUpdate()returns the number of affected rows. Ifuser_iddoesn't exist, this prints "updated" even though nothing changed. Consider providing feedback when no rows are affected.- statement.executeUpdate(); - - System.out.println("updated"); + int rowsAffected = statement.executeUpdate(); + if (rowsAffected > 0) { + System.out.println("updated"); + } else { + System.out.println("No account found with that ID"); + }The same applies to
deleteAccount().
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
README.md(1 hunks)src/main/java/com/example/Main.java(2 hunks)
🔇 Additional comments (3)
README.md (1)
1-1: LGTM!Standard GitHub Classroom badge addition for tracking assignment due dates.
src/main/java/com/example/Main.java (2)
280-345: LGTM!The input validation helpers are well-structured with clear prompts and appropriate validation logic. Good use of
Pattern.matches()for SSN format validation and proper handling of edge cases.
348-373: Existing utility methods remain correct.The
isDevModeandresolveConfigmethods are unchanged and continue to work correctly with the new interactive flow.
All test pass, but missing comments and can be improved
Summary by CodeRabbit
New Features
Documentation
✏️ Tip: You can customize this high-level summary in your review settings.