From 24da335f42739494d76df98b62dbaed063b7e9c6 Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Mon, 1 Dec 2025 08:12:17 +0000 Subject: [PATCH 01/10] add deadline --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d20aaf9..7071577 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/339Lr3BJ) ### How the tests work (and Docker requirement) This project ships with an end‑to‑end CLI integration test suite that uses Testcontainers to spin up a temporary MySQL database. From 35c0aa179ccc6b98bae7e076aa05d0e083a646ba Mon Sep 17 00:00:00 2001 From: Linda Eskilsson Date: Wed, 10 Dec 2025 10:02:26 +0100 Subject: [PATCH 02/10] Added log in prompt --- mvnw | 0 pom.xml | 23 +++++++++++++++++++++++ src/main/java/com/example/Main.java | 25 +++++++++++++++++++++++-- 3 files changed, 46 insertions(+), 2 deletions(-) mode change 100644 => 100755 mvnw diff --git a/mvnw b/mvnw old mode 100644 new mode 100755 diff --git a/pom.xml b/pom.xml index 002ff66..421f7e6 100644 --- a/pom.xml +++ b/pom.xml @@ -46,6 +46,12 @@ 1.21.3 test + + org.slf4j + slf4j-simple + 2.0.13 + + org.testcontainers mysql @@ -53,7 +59,24 @@ + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.2.2 + + + + true + com.example.Main + + + + + org.apache.maven.plugins maven-failsafe-plugin diff --git a/src/main/java/com/example/Main.java b/src/main/java/com/example/Main.java index 6dc6fbd..2e3dbd5 100644 --- a/src/main/java/com/example/Main.java +++ b/src/main/java/com/example/Main.java @@ -1,13 +1,15 @@ package com.example; +import java.io.Console; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Arrays; -public class Main { +public class +Main { - static void main(String[] args) { + public static void main(String[] args) { if (isDevMode(args)) { DevDatabaseInitializer.start(); } @@ -31,6 +33,25 @@ public void run() { throw new RuntimeException(e); } //Todo: Starting point for your code + Console console = System.console(); + String uName; + String pWord; + + if (console != null) { + uName = console.readLine("Username: "); + char[] pwChars = console.readPassword("Password: "); + pWord = new String(pwChars); + } else { + // IntelliJ fallback (du är här nu!) + System.out.print("Username: "); + uName = new java.util.Scanner(System.in).nextLine(); + + System.out.print("Password: "); + pWord = new java.util.Scanner(System.in).nextLine(); + } + + System.out.println("You entered username: " + uName); + } /** From 6282ae270d60ca575adff5fd6529e964f78f0ed9 Mon Sep 17 00:00:00 2001 From: Linda Eskilsson Date: Wed, 10 Dec 2025 10:29:45 +0100 Subject: [PATCH 03/10] Step 2. Added conncetion to database --- src/main/java/com/example/Main.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/example/Main.java b/src/main/java/com/example/Main.java index 2e3dbd5..10ac7dc 100644 --- a/src/main/java/com/example/Main.java +++ b/src/main/java/com/example/Main.java @@ -9,6 +9,9 @@ public class Main { + private Connection connection; // Koppla upp mot databas + + public static void main(String[] args) { if (isDevMode(args)) { DevDatabaseInitializer.start(); @@ -17,6 +20,7 @@ public static void main(String[] args) { } 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"); @@ -28,10 +32,15 @@ public void run() { "as system properties (-Dkey=value) or environment variables."); } - try (Connection connection = DriverManager.getConnection(jdbcUrl, dbUser, dbPass)) { + // Connection, sparas i instansvariabel + try { + this.connection = DriverManager.getConnection(jdbcUrl, dbUser, dbPass); + System.out.println("Connected to DB!"); } catch (SQLException e) { - throw new RuntimeException(e); + throw new RuntimeException("Could not connect to DB", e); } + + //Todo: Starting point for your code Console console = System.console(); String uName; From 9e8cb48561f83632d142b191dc08399da868ee7b Mon Sep 17 00:00:00 2001 From: Linda Eskilsson Date: Wed, 10 Dec 2025 11:26:08 +0100 Subject: [PATCH 04/10] Step 3. log in function successfully implemented --- src/main/java/com/example/Main.java | 67 ++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/example/Main.java b/src/main/java/com/example/Main.java index 10ac7dc..2833052 100644 --- a/src/main/java/com/example/Main.java +++ b/src/main/java/com/example/Main.java @@ -1,10 +1,8 @@ package com.example; -import java.io.Console; -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 { @@ -40,29 +38,56 @@ public void run() { throw new RuntimeException("Could not connect to DB", e); } - //Todo: Starting point for your code - Console console = System.console(); - String uName; - String pWord; - - if (console != null) { - uName = console.readLine("Username: "); - char[] pwChars = console.readPassword("Password: "); - pWord = new String(pwChars); - } else { - // IntelliJ fallback (du är här nu!) - System.out.print("Username: "); - uName = new java.util.Scanner(System.in).nextLine(); - - System.out.print("Password: "); - pWord = new java.util.Scanner(System.in).nextLine(); + Scanner scanner = new Scanner(System.in); + + while (!login(this.connection, scanner)) { + // fortsätt försöka tills login lyckas + } + + System.out.println("Login succsessfull! let's move on to the menu..."); + } + + public static boolean login(Connection conn, Scanner scanner) { + + System.out.print("Username (or 0 to exit): "); + String username = scanner.nextLine(); + + if (username.equals("0")) { + System.out.println("Exiting..."); + System.exit(0); } - System.out.println("You entered username: " + uName); + System.out.print("Password (or 0 to exit): "); + String password = scanner.nextLine(); + + if (password.equals("0")) { + System.out.println("Exiting..."); + System.exit(0); + } + String sql = "SELECT * FROM account WHERE first_name = ? AND password = ?"; + + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, username); + stmt.setString(2, password); + + ResultSet rs = stmt.executeQuery(); + + if (!rs.next()) { + System.out.println("Invalid credentials"); + return false; + } + + System.out.println("Login successful!"); + return true; + + } catch (SQLException e) { + throw new RuntimeException("Login failed", e); + } } + /** * Determines if the application is running in development mode based on system properties, * environment variables, or command-line arguments. From d041fade9655833a171083a1393bc5180d570ef2 Mon Sep 17 00:00:00 2001 From: Linda Eskilsson Date: Wed, 10 Dec 2025 14:38:41 +0100 Subject: [PATCH 05/10] Added menu and methods - list, gett mission by id --- src/main/java/com/example/Main.java | 117 +++++++++++++++++++++------- 1 file changed, 90 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/example/Main.java b/src/main/java/com/example/Main.java index 2833052..301dbf6 100644 --- a/src/main/java/com/example/Main.java +++ b/src/main/java/com/example/Main.java @@ -33,59 +33,122 @@ public void run() { // Connection, sparas i instansvariabel try { this.connection = DriverManager.getConnection(jdbcUrl, dbUser, dbPass); - System.out.println("Connected to DB!"); + System.out.println("Connected to database successfully."); } catch (SQLException e) { - throw new RuntimeException("Could not connect to DB", e); + throw new RuntimeException("Could not connect to database", e); } //Todo: Starting point for your code + Scanner scanner = new Scanner(System.in); - while (!login(this.connection, scanner)) { - // fortsätt försöka tills login lyckas + // LOGIN LOOP (mandatory before menu) + while (true) { + System.out.print("Username (or 0 to exit): "); + String username = scanner.nextLine(); + if (username.equals("0")) return; + + System.out.print("Password (or 0 to exit): "); + String password = scanner.nextLine(); + if (password.equals("0")) return; + + if (login(username, password)) { + break; + } else { + System.out.println("invalid"); + } } - System.out.println("Login succsessfull! let's move on to the menu..."); + // MENU LOOP AFTER LOGIN + boolean running = true; + while (running) { + printMenu(); + String choice = scanner.nextLine(); + + switch (choice) { + case "1" -> listMoonMissions(); + case "2" -> getMoonMissionById(scanner); + case "3" -> countMissionsByYear(scanner); + case "4" -> createAccount(scanner); + case "5" -> updatePassword(scanner); + case "6" -> deleteAccount(scanner); + case "0" -> running = false; + default -> System.out.println("invalid"); + } + } } - public static boolean login(Connection conn, Scanner scanner) { - System.out.print("Username (or 0 to exit): "); - String username = scanner.nextLine(); + // todo: LOGIN uses column `name` created from CONCAT in init.sql + private boolean login(String username, String password) { + String sql = "SELECT * FROM account WHERE name = ? AND password = ?"; + + try (PreparedStatement stmt = connection.prepareStatement(sql)) { + stmt.setString(1, username); + stmt.setString(2, password); + + ResultSet rs = stmt.executeQuery(); + return rs.next(); - if (username.equals("0")) { - System.out.println("Exiting..."); - System.exit(0); + } catch (SQLException e) { + throw new RuntimeException("Login failed", e); } + } - System.out.print("Password (or 0 to exit): "); - String password = scanner.nextLine(); - if (password.equals("0")) { - System.out.println("Exiting..."); - System.exit(0); + // todo: print CLI menu + private void printMenu() { + System.out.println("1) List moon missions"); + System.out.println("2) Get moon mission"); + System.out.println("3) Count missions by year"); + System.out.println("4) Create account"); + System.out.println("5) Update account password"); + System.out.println("6) Delete account"); + System.out.println("0) Exit"); + } + + // todo: choice 1 - List moon missions + private void listMoonMissions() { + String sql = "SELECT spacecraft FROM moon_mission"; + + try (Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery(sql)) { + + while (rs.next()) { + System.out.println(rs.getString("spacecraft")); + } + + } catch (SQLException e) { + throw new RuntimeException("Could not list missions", e); } + } - String sql = "SELECT * FROM account WHERE first_name = ? AND password = ?"; + // todo: choice 2 - Get mission by ID + private void getMoonMissionById(Scanner scanner) { + System.out.print("mission_id: "); + int id = Integer.parseInt(scanner.nextLine()); - try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setString(1, username); - stmt.setString(2, password); + String sql = "SELECT * FROM moon_mission WHERE mission_id = ?"; + try (PreparedStatement stmt = connection.prepareStatement(sql)) { + stmt.setInt(1, id); ResultSet rs = stmt.executeQuery(); - if (!rs.next()) { - System.out.println("Invalid credentials"); - return false; + if (rs.next()) { + System.out.println( + rs.getInt("mission_id") + " " + + rs.getString("spacecraft") + " " + + rs.getString("launch_date") + ); + } else { + System.out.println("invalid"); } - System.out.println("Login successful!"); - return true; - } catch (SQLException e) { - throw new RuntimeException("Login failed", e); + throw new RuntimeException(e); } } + /** From 19c5e230163d943705f6a34f93b941ddcd34e347 Mon Sep 17 00:00:00 2001 From: Linda Eskilsson Date: Wed, 10 Dec 2025 15:02:19 +0100 Subject: [PATCH 06/10] Added methods - count missions by year --- src/main/java/com/example/Main.java | 36 +++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/example/Main.java b/src/main/java/com/example/Main.java index 301dbf6..c68e657 100644 --- a/src/main/java/com/example/Main.java +++ b/src/main/java/com/example/Main.java @@ -42,7 +42,7 @@ public void run() { Scanner scanner = new Scanner(System.in); - // LOGIN LOOP (mandatory before menu) + // todo: Log in loop while (true) { System.out.print("Username (or 0 to exit): "); String username = scanner.nextLine(); @@ -55,12 +55,13 @@ public void run() { if (login(username, password)) { break; } else { - System.out.println("invalid"); + System.out.println("Invalid username or password"); } } - // MENU LOOP AFTER LOGIN + // todo: menu after successfully logged in boolean running = true; + while (running) { printMenu(); String choice = scanner.nextLine(); @@ -140,15 +141,40 @@ private void getMoonMissionById(Scanner scanner) { rs.getString("spacecraft") + " " + rs.getString("launch_date") ); + System.out.println("------------"); } else { - System.out.println("invalid"); + System.out.println("Invalid username or password"); } } catch (SQLException e) { throw new RuntimeException(e); } } - + + + // todo: choice 3 - Count missions by year + private void countMissionsByYear(Scanner scanner) { + System.out.print("Total missions for 2019: "); + int year = Integer.parseInt(scanner.nextLine()); + + String sql = "SELECT COUNT(*) FROM moon_mission WHERE YEAR(launch_date) = ?"; + + try (PreparedStatement stmt = connection.prepareStatement(sql)) { + stmt.setInt(1, year); + ResultSet rs = stmt.executeQuery(); + rs.next(); + System.out.println(rs.getInt(1)); + + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + //todo: Choice 4 - Add costumer method. + + + + /** From 5d3835749830f081744ced333749a8d87a6785c1 Mon Sep 17 00:00:00 2001 From: Linda Eskilsson Date: Wed, 10 Dec 2025 15:19:51 +0100 Subject: [PATCH 07/10] Added methods - add costumer --- src/main/java/com/example/Main.java | 40 ++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/example/Main.java b/src/main/java/com/example/Main.java index c68e657..9df30df 100644 --- a/src/main/java/com/example/Main.java +++ b/src/main/java/com/example/Main.java @@ -171,7 +171,45 @@ private void countMissionsByYear(Scanner scanner) { } //todo: Choice 4 - Add costumer method. - + private void createAccount(Scanner scanner) { + System.out.print("first name: "); + String first = scanner.nextLine(); + + System.out.print("last name: "); + String last = scanner.nextLine(); + + System.out.print("ssn: "); + String ssn = scanner.nextLine(); + + System.out.print("password: "); + String pw = scanner.nextLine(); + + String sql = """ + INSERT INTO account (first_name, last_name, ssn, password) + VALUES (?, ?, ?, ?) + """; + + try (PreparedStatement stmt = connection.prepareStatement(sql)) { + stmt.setString(1, first); + stmt.setString(2, last); + stmt.setString(3, ssn); + stmt.setString(4, pw); + stmt.executeUpdate(); + + System.out.println("created"); + + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + + // todo: Choice - 5 Update password method + private void updatePassword(Scanner scanner) { + } + + // todo: Choice 6 - Delete account method + From 114c9cfb15483cc95fa6adbcd90020025a35be51 Mon Sep 17 00:00:00 2001 From: Linda Eskilsson Date: Wed, 10 Dec 2025 15:21:17 +0100 Subject: [PATCH 08/10] Added methods - update password --- src/main/java/com/example/Main.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/example/Main.java b/src/main/java/com/example/Main.java index 9df30df..407e2d6 100644 --- a/src/main/java/com/example/Main.java +++ b/src/main/java/com/example/Main.java @@ -206,6 +206,24 @@ INSERT INTO account (first_name, last_name, ssn, password) // todo: Choice - 5 Update password method private void updatePassword(Scanner scanner) { + System.out.print("user_id: "); + int id = Integer.parseInt(scanner.nextLine()); + + System.out.print("new password: "); + String newPw = scanner.nextLine(); + + String sql = "UPDATE account SET password = ? WHERE user_id = ?"; + + try (PreparedStatement stmt = connection.prepareStatement(sql)) { + stmt.setString(1, newPw); + stmt.setInt(2, id); + stmt.executeUpdate(); + + System.out.println("updated"); + + } catch (SQLException e) { + throw new RuntimeException(e); + } } // todo: Choice 6 - Delete account method @@ -214,7 +232,6 @@ private void updatePassword(Scanner scanner) { - /** * Determines if the application is running in development mode based on system properties, * environment variables, or command-line arguments. From 5631195c4cd3aebf798b9085f45665f1ac20e527 Mon Sep 17 00:00:00 2001 From: Linda Eskilsson Date: Wed, 10 Dec 2025 15:21:42 +0100 Subject: [PATCH 09/10] Added methods - delete account --- src/main/java/com/example/Main.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/main/java/com/example/Main.java b/src/main/java/com/example/Main.java index 407e2d6..b5ebe74 100644 --- a/src/main/java/com/example/Main.java +++ b/src/main/java/com/example/Main.java @@ -227,6 +227,23 @@ private void updatePassword(Scanner scanner) { } // todo: Choice 6 - Delete account method + private void deleteAccount(Scanner scanner) { + System.out.print("user_id: "); + int id = Integer.parseInt(scanner.nextLine()); + + String sql = "DELETE FROM account WHERE user_id = ?"; + + try (PreparedStatement stmt = connection.prepareStatement(sql)) { + stmt.setInt(1, id); + stmt.executeUpdate(); + + System.out.println("deleted"); + + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + From 3f7f1447a926e0361678ca927d0d0c3f73e5651b Mon Sep 17 00:00:00 2001 From: Linda Eskilsson Date: Wed, 10 Dec 2025 15:29:01 +0100 Subject: [PATCH 10/10] All tests are green --- src/main/java/com/example/Main.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/example/Main.java b/src/main/java/com/example/Main.java index b5ebe74..7b91f99 100644 --- a/src/main/java/com/example/Main.java +++ b/src/main/java/com/example/Main.java @@ -99,6 +99,7 @@ private boolean login(String username, String password) { // todo: print CLI menu private void printMenu() { + System.out.println(" ---> MENU <--- "); System.out.println("1) List moon missions"); System.out.println("2) Get moon mission"); System.out.println("3) Count missions by year");