Skip to content

Other branch#4

Open
LinusWestling wants to merge 10 commits intomainfrom
otherBranch
Open

Other branch#4
LinusWestling wants to merge 10 commits intomainfrom
otherBranch

Conversation

@LinusWestling
Copy link

@LinusWestling LinusWestling commented Dec 5, 2025

Summary by CodeRabbit

  • New Features

    • Interactive user authentication with credential validation
    • Menu-driven console interface for app navigation
    • Account management: create, update password, and delete accounts
    • Mission queries: list missions, view mission details, count missions by year
  • Documentation

    • Added GitHub Classroom deadline badge to README

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Dec 5, 2025

Walkthrough

Refactors startup to an instance-based Main with DI, adds repository interfaces and JDBC implementations, introduces a MoonMission model and a SimpleDriverManagerDataSource, and converts the CLI to an authenticated, menu-driven flow for mission and account operations. Documentation badge added to README.

Changes

Cohort / File(s) Summary
Documentation
README.md
Added a top-level GitHub Classroom deadline badge before the existing "How the tests work" section.
Application entry / CLI
src/main/java/com/example/Main.java
Converted Main to an instance-based flow with public Main(SimpleDriverManagerDataSource), public void run(), getOptions(), options(int), and resolveConfig(...). Main now performs an interactive authentication loop and a menu-driven CLI for missions and account operations.
Model
src/main/java/com/example/MoonMission.java
New POJO MoonMission with private fields (missionId, spacecraft, launchDate, carrierRocket, operator, missionType, outcome) and public getters/setters.
Connection manager
src/main/java/com/example/SimpleDriverManagerDataSource.java
New class encapsulating JDBC config (jdbcUrl, dbUser, dbPass) with constructor validation and public Connection getConnection() using DriverManager.
Repository interfaces
src/main/java/com/example/repositorys/AccountRepository.java, src/main/java/com/example/repositorys/MoonMissionRepository.java
New interfaces: AccountRepository (createAccount, updatePassword, deleteAccount, validateCredentials) and MoonMissionRepository (listMissions, getMissionById, countMissionsByYear).
JDBC repository implementations
src/main/java/com/example/jdbc/JdbcAccountRepository.java, src/main/java/com/example/jdbc/JdbcMoonMissionRepository.java
New JDBC-backed implementations using SimpleDriverManagerDataSource. JdbcAccountRepository implements account CRUD and credential validation via prepared statements. JdbcMoonMissionRepository implements mission listing, retrieval (maps row to MoonMission), and counting by year; both use try-with-resources and wrap SQLExceptions in RuntimeExceptions.

Sequence Diagram(s)

sequenceDiagram
    actor User
    participant Main
    participant CLI as Scanner (CLI)
    participant AccountRepo as JdbcAccountRepository
    participant MissionRepo as JdbcMoonMissionRepository
    participant DB as Database

    User->>Main: run()
    Main->>CLI: prompt credentials
    CLI-->>Main: username, password
    Main->>AccountRepo: validateCredentials(username, password)
    AccountRepo->>DB: SELECT ... FROM account WHERE name=? AND password=?
    DB-->>AccountRepo: result
    AccountRepo-->>Main: boolean (valid/invalid)
    alt valid
        Main->>Main: enter menu loop
        loop menu interactions
            Main->>CLI: show options / prompt input
            CLI-->>Main: choice
            alt list missions
                Main->>MissionRepo: listMissions()
                MissionRepo->>DB: SELECT spacecraft FROM moon_mission
                DB-->>MissionRepo: rows
                MissionRepo-->>Main: List<String>
                Main->>CLI: display list
            else get mission by id
                Main->>CLI: prompt id
                CLI-->>Main: id
                Main->>MissionRepo: getMissionById(id)
                MissionRepo->>DB: SELECT * FROM moon_mission WHERE mission_id=?
                DB-->>MissionRepo: row
                MissionRepo-->>Main: Optional<MoonMission>
                Main->>CLI: display mission
            else count by year
                Main->>CLI: prompt year
                CLI-->>Main: year
                Main->>MissionRepo: countMissionsByYear(year)
                MissionRepo->>DB: SELECT COUNT(*) ... WHERE YEAR(launch_date)=?
                DB-->>MissionRepo: count
                MissionRepo-->>Main: int
                Main->>CLI: display count
            else account CRUD
                Main->>CLI: prompt details
                CLI-->>Main: account data
                Main->>AccountRepo: create/update/delete(...)
                AccountRepo->>DB: INSERT/UPDATE/DELETE SQL
                DB-->>AccountRepo: result
                AccountRepo-->>Main: boolean
                Main->>CLI: display confirmation
            end
        end
    else invalid
        Main->>CLI: allow retry or exit
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

  • Pay special attention to:
    • Main.java: authentication/menu control flow, input validation, and correct wiring of repositories.
    • JdbcAccountRepository: credential query column names and proper parameterization.
    • JdbcMoonMissionRepository: mapping ResultSet to MoonMission (date handling to LocalDate).
    • SimpleDriverManagerDataSource: constructor validation and connection handling.
    • Exception wrapping and resource management across JDBC classes.

Poem

🐰 I hopped in to wire datasource and thread,
Prompted credentials, and menus I fed,
Missions and accounts all tidy and neat,
JDBC carrots lined up for a treat,
A rabbit-approved refactor — quick and fleet.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning, 1 inconclusive)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 12.82% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
Title check ❓ Inconclusive The title 'Other branch' is vague and does not convey meaningful information about the substantial changes in this PR, which include introducing a complete JDBC-based repository pattern, new entity classes, and refactoring of the main application flow. Consider renaming to a more descriptive title that captures the main objective, such as 'Implement JDBC repository pattern with account and mission management' or 'Refactor to instance-based architecture with repository pattern'.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between badbe6a and ba97b26.

📒 Files selected for processing (2)
  • src/main/java/com/example/Main.java (2 hunks)
  • src/main/java/com/example/jdbc/JdbcMoonMissionRepository.java (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
src/main/java/com/example/Main.java (3)
src/main/java/com/example/jdbc/JdbcAccountRepository.java (1)
  • JdbcAccountRepository (11-81)
src/main/java/com/example/jdbc/JdbcMoonMissionRepository.java (1)
  • JdbcMoonMissionRepository (15-89)
src/main/java/com/example/DevDatabaseInitializer.java (1)
  • DevDatabaseInitializer (6-24)
src/main/java/com/example/jdbc/JdbcMoonMissionRepository.java (2)
src/main/java/com/example/MoonMission.java (1)
  • MoonMission (5-67)
src/main/java/com/example/SimpleDriverManagerDataSource.java (1)
  • SimpleDriverManagerDataSource (7-28)
🔇 Additional comments (2)
src/main/java/com/example/jdbc/JdbcMoonMissionRepository.java (1)

15-39: JDBC repository implementation looks correct and resource-safe

The constructor DI, try-with-resources usage, null-safe launch_date mapping, and Optional<MoonMission> handling all look solid and consistent with the other JDBC repositories. No functional or robustness issues stand out here.

Also applies to: 41-88

src/main/java/com/example/Main.java (1)

11-21: Instance-based wiring and auth/menu flow are clean and cohesive

The move to constructor-based wiring of JdbcAccountRepository, JdbcMoonMissionRepository, and a shared Scanner, plus the run() method with an authentication loop and safer menu choice parsing, makes the entrypoint much clearer and easier to extend. resolveConfig correctly centralizes configuration resolution with system-property/env precedence.

Also applies to: 30-33, 35-76, 78-88, 105-119


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 6

🧹 Nitpick comments (5)
src/main/java/com/example/SimpleDriverManagerDataSource.java (1)

12-12: Misleading comment about configuration precedence.

The comment suggests this class resolves precedence between system properties and environment variables, but the class simply receives pre-resolved values. The precedence logic is in Main.resolveConfig(). Consider updating or removing this comment to avoid confusion.

-    // Resolve DB settings with precedence: System properties -> Environment variable
+    // Simple JDBC connection factory; expects pre-resolved configuration values
src/main/java/com/example/repositorys/MoonMissionRepository.java (1)

1-1: Package name typo: repositorys should be repositories.

The conventional English plural of "repository" is "repositories". Consider renaming the package.

src/main/java/com/example/repositorys/AccountRepository.java (1)

3-8: Consider using primitive boolean instead of boxed Boolean.

The interface methods return Boolean (boxed type), which allows null returns. Unless null has a specific semantic meaning (e.g., "unknown"), prefer primitive boolean to avoid potential NullPointerException when unboxing.

 public interface AccountRepository {
-    Boolean createAccount(String firstName, String lastName, String ssn, String password);
-    Boolean updatePassword(int userId, String newPassword);
-    Boolean deleteAccount(int userId);
-    Boolean validateCredentials(String username, String password);
+    boolean createAccount(String firstName, String lastName, String ssn, String password);
+    boolean updatePassword(int userId, String newPassword);
+    boolean deleteAccount(int userId);
+    boolean validateCredentials(String username, String password);
 }
src/main/java/com/example/MoonMission.java (1)

5-67: Model class is functional but could benefit from toString().

The class works for its purpose. Consider adding a toString() method to simplify debugging and logging. The current JavaBean style (default constructor + setters) is appropriate for JDBC ResultSet mapping.

src/main/java/com/example/Main.java (1)

11-13: Fields use concrete types instead of repository interfaces.

Declaring fields as JdbcAccountRepository and JdbcMoonMissionRepository instead of AccountRepository and MoonMissionRepository reduces testability and flexibility. Coding to interfaces enables easier mocking and swapping implementations.

-    private final JdbcAccountRepository accountRepo;
-    private final JdbcMoonMissionRepository missionRepo;
+    private final AccountRepository accountRepo;
+    private final MoonMissionRepository missionRepo;
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e629e94 and badbe6a.

📒 Files selected for processing (8)
  • README.md (1 hunks)
  • src/main/java/com/example/Main.java (2 hunks)
  • src/main/java/com/example/MoonMission.java (1 hunks)
  • src/main/java/com/example/SimpleDriverManagerDataSource.java (1 hunks)
  • src/main/java/com/example/jdbc/JdbcAccountRepository.java (1 hunks)
  • src/main/java/com/example/jdbc/JdbcMoonMissionRepository.java (1 hunks)
  • src/main/java/com/example/repositorys/AccountRepository.java (1 hunks)
  • src/main/java/com/example/repositorys/MoonMissionRepository.java (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (3)
src/main/java/com/example/jdbc/JdbcMoonMissionRepository.java (2)
src/main/java/com/example/MoonMission.java (1)
  • MoonMission (5-67)
src/main/java/com/example/SimpleDriverManagerDataSource.java (1)
  • SimpleDriverManagerDataSource (7-28)
src/main/java/com/example/jdbc/JdbcAccountRepository.java (1)
src/main/java/com/example/SimpleDriverManagerDataSource.java (1)
  • SimpleDriverManagerDataSource (7-28)
src/main/java/com/example/repositorys/MoonMissionRepository.java (1)
src/main/java/com/example/MoonMission.java (1)
  • MoonMission (5-67)
🔇 Additional comments (13)
README.md (3)

1-114: Inconsistency between summary and provided code.

The enriched summary states that "an AI-generated summary section describing changes" was "placed before the existing 'How the tests work' section," but the provided code shows the badge on line 1 immediately followed by "### How the tests work" on line 2—no intervening summary section is visible. Please clarify whether this section was intentionally omitted from the provided code snippet or whether the summary's description of changes is inaccurate.


1-1: Badge addition improves accessibility.

The GitHub Classroom deadline badge on line 1 is a helpful addition that clearly signals the assignment deadline to users accessing the repository.


2-114: Documentation is comprehensive and well-structured.

The README provides clear, detailed instructions covering test prerequisites (Docker), dev mode setup with three configuration options, IntelliJ configuration steps, and well-articulated assignment requirements (G and VG levels). The progression from high-level overview to detailed setup steps is logical and easy to follow.

src/main/java/com/example/SimpleDriverManagerDataSource.java (1)

7-27: Implementation looks correct.

The class properly validates configuration at construction time and uses try-with-resources-compatible Connection objects. Note that each getConnection() call creates a new connection without pooling, which is fine for this simple use case but may become a bottleneck under load.

src/main/java/com/example/repositorys/MoonMissionRepository.java (1)

8-11: Interface design looks reasonable.

The repository interface is clean. Note that listMissions() returns List<String> (spacecraft names only) rather than List<MoonMission>. This works for the current menu display use case but limits reusability if full mission details are needed elsewhere.

src/main/java/com/example/repositorys/AccountRepository.java (1)

7-7: Clarify username semantics in validateCredentials.

createAccount accepts ssn as identifier, but validateCredentials takes username. Verify that the implementation correctly maps between these identifiers, or consider renaming the parameter for consistency.

src/main/java/com/example/jdbc/JdbcMoonMissionRepository.java (2)

71-71: YEAR() function is MySQL-specific.

The SQL function YEAR(launch_date) works in MySQL but not in PostgreSQL or H2 (which use EXTRACT(YEAR FROM launch_date)). If database portability is a concern, consider using standard SQL or parameterizing the query approach.


23-39: JDBC resource handling looks correct.

Good use of try-with-resources for Connection, PreparedStatement, and ResultSet. This ensures proper cleanup even on exceptions.

src/main/java/com/example/Main.java (1)

35-76: Authentication and menu loop structure looks good.

The authentication loop with exit option and the menu-driven flow are well-structured. Good use of safe input parsing for the menu choice.

src/main/java/com/example/jdbc/JdbcAccountRepository.java (4)

11-17: Constructor-based injection with final data source is solid

The class design with a final SimpleDriverManagerDataSource injected via the constructor is clean and testable; no issues here.


51-62: Delete implementation is straightforward

The delete-by-user_id implementation with prepared statement and executeUpdate() == 1 is fine and consistent with the other CRUD methods.


19-49: Ensure password handling is secure and clarify responsibility

The JDBC usage (try-with-resources, prepared statements, executeUpdate() == 1) looks correct for both createAccount and updatePassword. However, as written these methods will persist whatever string is passed as password/newPassword:

  • If callers are passing raw user passwords, this effectively stores passwords in plaintext, which is a serious security issue.
  • Either:
    • Hash & salt the password inside this repository before persisting, or
    • Enforce that callers pass a pre-hashed value and rename parameters to something like hashedPassword / newHashedPassword to make that explicit.

Also consider whether you want to propagate a custom checked/unchecked exception type instead of a bare RuntimeException to distinguish DB failures from other runtime errors.


64-80: Fix schema mismatch and avoid swallowing SQL errors in validateCredentials

Two concerns here:

  1. Likely schema/column mismatch

    • createAccount writes first_name, last_name, ssn, password, but validateCredentials queries WHERE name = ? AND password = ?.
    • Unless there is a separate name column being populated elsewhere, this will not match the inserted data.
    • Align the query with your actual login identifier (e.g. username, ssn, or user_id) and keep it consistent with createAccount/the DB schema. The "Adjust column names…" comment is easy to forget; it's better to fix the SQL now than rely on that note.
  2. Swallowing SQLExceptions changes behavior semantics

    • Returning false on any SQLException makes connection/DB issues indistinguishable from "invalid credentials", which is hard to debug and can mask outages.
    • For consistency with the other methods, consider either throwing a runtime/custom exception on failure, or at least logging and rethrowing, instead of silently returning false.

Optionally, you could also:

  • Query less data (e.g. SELECT 1 or EXISTS(...)) instead of SELECT * for a simple existence check.

Combined with the earlier comment on password storage, ensure this method verifies the same hashed value you persist in createAccount/updatePassword.

Comment on lines +149 to +155
case 3:
System.out.print("Enter the year you wish to see the number of missions: ");
int year = sc.nextInt();
sc.nextLine();
int numOfMissions = missionRepo.countMissionsByYear(year);
System.out.println("There were: " + numOfMissions + " year " + year);
break;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Same InputMismatchException risk for year input.

The year input also uses sc.nextInt() without exception handling.

🤖 Prompt for AI Agents
In src/main/java/com/example/Main.java around lines 149 to 155, the code reads
the year with sc.nextInt() without handling InputMismatchException; wrap the
read in a try/catch (or read the line with sc.nextLine() and parse
Integer.parseInt in a try/catch) to handle non-integer input, consume the
invalid token on error (sc.nextLine()), log or print a friendly error message
and reprompt or abort the operation gracefully, then only call
missionRepo.countMissionsByYear(year) when a valid integer year has been
obtained.

String password = sc.nextLine();

if (accountRepo.createAccount(firstName, lastName, ssn, password)) {
System.out.println("Your account was successfuly created");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Typo: "successfuly" should be "successfully".

-                    System.out.println("Your account was successfuly created");
+                    System.out.println("Your account was successfully created");
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
System.out.println("Your account was successfuly created");
System.out.println("Your account was successfully created");
🤖 Prompt for AI Agents
In src/main/java/com/example/Main.java around line 168, the user-facing message
contains a spelling typo ("successfuly"); update the string literal to
"successfully" so the println reads "Your account was successfully created",
then recompile and run relevant tests or execute the flow to verify the
corrected message appears.

Comment on lines +174 to +186
case 5:
System.out.println("Enter the userId of the account you would like to update your password for: ");
int userId = sc.nextInt();
sc.nextLine();
System.out.println("Enter your new password");
String newPassword = sc.nextLine();

if (accountRepo.updatePassword(userId, newPassword)) {
System.out.println("Your password has been updated!");
} else {
System.out.println("Something went wrong during password update.");
}
break;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Same InputMismatchException risk for userId input.

The userId input also uses sc.nextInt() without exception handling.

🤖 Prompt for AI Agents
In src/main/java/com/example/Main.java around lines 174 to 186, the call to
sc.nextInt() for userId can throw InputMismatchException if the user enters
non-integer input; update the code to validate and safely read the userId by
either using sc.hasNextInt() in a loop to re-prompt until a valid integer is
entered or wrap sc.nextInt() in a try-catch(InputMismatchException) where on
exception you consume the invalid token with sc.nextLine(), notify the user and
re-prompt; also ensure you clear the input buffer with sc.nextLine() after a
successful nextInt() and validate the integer (e.g., positive) before calling
accountRepo.updatePassword(...).

Comment on lines +188 to +195
case 6:
System.out.println("Enter userID of the account u want deleted: ");
int userIdDelete = sc.nextInt();
sc.nextLine();

boolean isDeleted = accountRepo.deleteAccount(userIdDelete);
System.out.println(isDeleted ? "You account was successfully deleted!" : "Something went wrong deleting your account.");
break;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Same InputMismatchException risk for userIdDelete input.

The userIdDelete input also uses sc.nextInt() without exception handling.

🤖 Prompt for AI Agents
In src/main/java/com/example/Main.java around lines 188 to 195, the code reads
userIdDelete with sc.nextInt() without guarding against InputMismatchException;
update this block to validate and safely parse the user input by either wrapping
the sc.nextInt() call in a try-catch for InputMismatchException (consuming the
invalid token with sc.nextLine() and informing the user) or reading the input as
a line (sc.nextLine()) and parsing with Integer.parseInt inside a try-catch to
handle NumberFormatException; ensure you only call accountRepo.deleteAccount
when a valid integer was obtained and keep the existing success/failure message
behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant