Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 1 addition & 0 deletions mvnw
100644 → 100755

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.5.4</version>
<configuration>
<systemPropertyVariables>
<api.version>1.44</api.version>
</systemPropertyVariables>
</configuration>
<executions>
<execution>
<id>integration-test</id>
Expand Down
200 changes: 197 additions & 3 deletions src/main/java/com/example/Main.java
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 {

Expand All @@ -20,17 +19,25 @@ public void run() {
String dbUser = resolveConfig("APP_DB_USER", "APP_DB_USER");
String dbPass = resolveConfig("APP_DB_PASS", "APP_DB_PASS");

Scanner sc = new Scanner(System.in);
Comment thread
TatjanaTrajkovic marked this conversation as resolved.
Outdated

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)) {
if(!loginHandler(connection, sc)){
return;
}
menuHandler(connection, sc);

} catch (SQLException e) {
throw new RuntimeException(e);
}
//Todo: Starting point for your code

}

/**
Expand Down Expand Up @@ -59,4 +66,191 @@ private static String resolveConfig(String propertyKey, String envKey) {
}
return (v == null || v.trim().isEmpty()) ? null : v.trim();
}

private boolean loginHandler(Connection conn, Scanner sc) throws SQLException {
System.out.println("Username: ");
String user = sc.nextLine().trim();
System.out.println("Password: ");
String password = sc.nextLine().trim();

String sql = """
SELECT user_id
FROM account
WHERE name = ? AND password = ?
""";
try(PreparedStatement ps = conn.prepareStatement(sql)){
ps.setString(1, user);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
if (rs.next()){
return true;
}
System.out.println("Invalid username or password");
return false;
}
}
Comment thread
TatjanaTrajkovic marked this conversation as resolved.

private void menuHandler(Connection connection, Scanner sc) throws SQLException{

while(true){

System.out.println("-----Menu options-----");
System.out.println("1. List moon missions (prints spacecraft names from `moon_mission`)");
System.out.println("2. Get a moon mission by mission_id (prints details for that mission)");
System.out.println("3. Count missions for a given year (prompts: year; prints the number of missions launched that year)");
System.out.println("4. Create an account (prompts: first name, last name, ssn, password; prints confirmation)");
System.out.println("5. Update an account password (prompts: user_id, new password; prints confirmation)");
System.out.println("6. Delete an account (prompts: user_id; prints confirmation)");
System.out.println("0. Exit");

System.out.println("Enter an option: ");
int choice = Integer.parseInt(sc.nextLine().trim());
Comment thread
TatjanaTrajkovic marked this conversation as resolved.

switch (choice){
case 1 -> listMoonMissions(connection);
case 2 -> getMissionById(connection, sc);
case 3 -> missionsCountByYear(connection, sc);
case 4 -> createAccount(connection, sc);
case 5 -> updateAccount(connection, sc);
case 6 -> deleteAccount(connection, sc);
case 0 -> {return;}
default ->
System.out.println("Invalid option. Please try again.");
}
}
}

public void listMoonMissions(Connection connection) throws SQLException{
String sql = """
SELECT spacecraft
FROM moon_mission
""";

try(PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery()){

while (rs.next()){
String spacecraft = rs.getString("spacecraft");
System.out.println(spacecraft);
}
}
}

public void getMissionById(Connection connection, Scanner sc) throws SQLException{
System.out.println("MissionId: ");
int missionId = Integer.parseInt(sc.nextLine().trim());
Comment thread
TatjanaTrajkovic marked this conversation as resolved.
Outdated

String sql = """
SELECT *
FROM moon_mission
WHERE mission_id = ?
""";
try(PreparedStatement ps = connection.prepareStatement(sql)){
ps.setInt(1, missionId);
ResultSet rs = ps.executeQuery();

if (rs.next()) {
System.out.println("=== Moon Mission Details ===");
System.out.println("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(" No mission found with ID: " + missionId);
}
}

}

public void missionsCountByYear(Connection connection, Scanner sc) throws SQLException{
System.out.println("Launch date:");
Comment thread
TatjanaTrajkovic marked this conversation as resolved.
Outdated
int launchYear = Integer.parseInt(sc.nextLine().trim());
Comment thread
TatjanaTrajkovic marked this conversation as resolved.

String sql = """
SELECT COUNT(*) AS missionCount
FROM moon_mission
WHERE YEAR(launch_date) = ?
""";
try(PreparedStatement ps = connection.prepareStatement(sql)){
ps.setInt(1, launchYear);
ResultSet rs = ps.executeQuery();
if(rs.next()){
int count = rs.getInt("missionCount");
System.out.println("Number of mission in " + launchYear + ": " + count);
Comment thread
TatjanaTrajkovic marked this conversation as resolved.
Outdated
} else{
System.out.println("No result found.");
}
}
}

public void createAccount(Connection connection, Scanner sc) throws SQLException{
System.out.println("First name: ");
String firstName = sc.nextLine().trim();
System.out.println("Last name: ");
String lastName = sc.nextLine().trim();
System.out.println("SSN: ");
String ssn = sc.nextLine().trim();
System.out.println("Password: ");
String password = sc.nextLine().trim();

String sql = """
INSERT INTO account (first_name, last_name, ssn, password)
VALUES (?, ?, ?, ?);
""";

try(PreparedStatement ps = connection.prepareStatement(sql)){
ps.setString(1, firstName);
ps.setString(2, lastName);
ps.setString(3, ssn);
ps.setString(4, password);

ps.executeUpdate();
}
System.out.println("account created");
}
Comment thread
TatjanaTrajkovic marked this conversation as resolved.

public void updateAccount(Connection connection, Scanner sc) throws SQLException{
System.out.println("User id:");
String userId = sc.nextLine().trim();
System.out.println("New password: ");
String newPassword = sc.nextLine().trim();

String sql = """
UPDATE account
SET password = ?
WHERE user_id = ?
""";

try(PreparedStatement ps = connection.prepareStatement(sql)){
ps.setString(1, newPassword);
ps.setString(2, userId);

ps.executeUpdate();
}
System.out.println("Password updated");
}
Comment thread
TatjanaTrajkovic marked this conversation as resolved.

private void deleteAccount(Connection connection, Scanner sc) throws SQLException{
System.out.println("User id: ");
int userId = Integer.parseInt(sc.nextLine().trim());

String sql = """
DELETE FROM account
WHERE user_id = ?
""";
try(PreparedStatement ps = connection.prepareStatement(sql)){
ps.setInt(1, userId);

ps.executeUpdate();
}
System.out.println("Account deleted");
}
Comment thread
TatjanaTrajkovic marked this conversation as resolved.


}



Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ void getMoonMissionById_printsDetails() throws Exception {
) + System.lineSeparator();

String out = runMainWithInput(input);

System.out.println(out);
assertThat(out)
.as("CLI should print details that include the spacecraft name for the selected mission")
.contains("Luna 3")
Expand Down