-
Notifications
You must be signed in to change notification settings - Fork 59
Other branch #4
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?
Other branch #4
Changes from all commits
9f1fcda
4c4df08
222d677
405d6ca
6aaa02d
c7c2554
8759fa6
228ffcb
badbe6a
ba97b26
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,36 +1,90 @@ | ||||||
| package com.example; | ||||||
|
|
||||||
| import java.sql.Connection; | ||||||
| import java.sql.DriverManager; | ||||||
| import java.sql.SQLException; | ||||||
| import com.example.jdbc.JdbcAccountRepository; | ||||||
| import com.example.jdbc.JdbcMoonMissionRepository; | ||||||
|
|
||||||
| import java.util.Arrays; | ||||||
| import java.util.Scanner; | ||||||
|
|
||||||
| public class Main { | ||||||
|
|
||||||
| static void main(String[] args) { | ||||||
| private final JdbcAccountRepository accountRepo; | ||||||
| private final JdbcMoonMissionRepository missionRepo; | ||||||
| private final Scanner sc; | ||||||
|
|
||||||
| public Main(SimpleDriverManagerDataSource dataSource) { | ||||||
| this.accountRepo = new JdbcAccountRepository(dataSource); | ||||||
| this.missionRepo = new JdbcMoonMissionRepository(dataSource); | ||||||
| this.sc = new Scanner(System.in); | ||||||
| } | ||||||
|
|
||||||
| public static void main(String[] args) { | ||||||
| if (isDevMode(args)) { | ||||||
| DevDatabaseInitializer.start(); | ||||||
| } | ||||||
| new Main().run(); | ||||||
| } | ||||||
|
|
||||||
| 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"); | ||||||
| String dbPass = resolveConfig("APP_DB_PASS", "APP_DB_PASS"); | ||||||
|
|
||||||
| 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."); | ||||||
| SimpleDriverManagerDataSource ds = new SimpleDriverManagerDataSource(jdbcUrl, dbUser, dbPass); | ||||||
|
|
||||||
| new Main(ds).run(); | ||||||
| } | ||||||
|
|
||||||
| public void run() { | ||||||
| boolean authenticated = false; | ||||||
| String username = ""; | ||||||
|
|
||||||
| // Loop until valid credentials | ||||||
| while (!authenticated) { | ||||||
| System.out.print("Enter your Username (or 0 to exit): "); | ||||||
| username = sc.nextLine(); | ||||||
| if ("0".equals(username)) { | ||||||
| System.out.println("Exiting program."); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| System.out.print("Enter your password: "); | ||||||
| String password = sc.nextLine(); | ||||||
|
|
||||||
| if (accountRepo.validateCredentials(username, password)) { | ||||||
| System.out.println("Welcome / Välkommen: " + username); | ||||||
| authenticated = true; | ||||||
| } else { | ||||||
| System.out.println("Invalid Username or Password. Try again or type 0 to exit."); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| try (Connection connection = DriverManager.getConnection(jdbcUrl, dbUser, dbPass)) { | ||||||
| } catch (SQLException e) { | ||||||
| throw new RuntimeException(e); | ||||||
| // Once authenticated, show menu until user exits | ||||||
| int choice; | ||||||
| do { | ||||||
| getOptions(); | ||||||
|
|
||||||
| // safer input handling | ||||||
| String choiceStr = sc.nextLine(); | ||||||
| try { | ||||||
| choice = Integer.parseInt(choiceStr); | ||||||
| } catch (NumberFormatException e) { | ||||||
| choice = -1; // invalid | ||||||
| } | ||||||
|
|
||||||
| options(choice); | ||||||
| } while (choice != 0); | ||||||
|
|
||||||
| System.out.println("Goodbye, " + username + "!"); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Reads configuration with precedence: Java system property first, then environment variable. | ||||||
| * Returns trimmed value or null if neither source provides a non-empty value. | ||||||
| */ | ||||||
| private static String resolveConfig(String propertyKey, String envKey) { | ||||||
| String v = System.getProperty(propertyKey); | ||||||
| if (v == null || v.trim().isEmpty()) { | ||||||
| v = System.getenv(envKey); | ||||||
| } | ||||||
| //Todo: Starting point for your code | ||||||
| return (v == null || v.trim().isEmpty()) ? null : v.trim(); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
|
|
@@ -48,15 +102,111 @@ private static boolean isDevMode(String[] args) { | |||||
| return Arrays.asList(args).contains("--dev"); //Argument --dev | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Reads configuration with precedence: Java system property first, then environment variable. | ||||||
| * Returns trimmed value or null if neither source provides a non-empty value. | ||||||
| */ | ||||||
| private static String resolveConfig(String propertyKey, String envKey) { | ||||||
| String v = System.getProperty(propertyKey); | ||||||
| if (v == null || v.trim().isEmpty()) { | ||||||
| v = System.getenv(envKey); | ||||||
| public void getOptions() { | ||||||
| System.out.println( | ||||||
| """ | ||||||
| Select an option: | ||||||
| 1 - List moon missions (prints spacecraft names from `moon_mission`). | ||||||
| 2 - Get a moon mission by mission_id (prints details for that mission). | ||||||
| 3 - Count missions for a given year (prompts: year; prints the number of missions launched that year). | ||||||
| 4 - Create an account (prompts: first name, last name, ssn, password; prints confirmation). | ||||||
| 5 - Update an account password (prompts: user_id, new password; prints confirmation). | ||||||
| 6 - Delete an account (prompts: user_id; prints confirmation). | ||||||
| 0 - Exit. | ||||||
| By pressing either of the numbers listed. | ||||||
| """ | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| public void options(int choice) { | ||||||
|
|
||||||
| switch (choice) { | ||||||
|
|
||||||
| case 1: missionRepo. | ||||||
| listMissions(). | ||||||
| forEach(System.out::println); | ||||||
| break; | ||||||
|
|
||||||
| case 2: | ||||||
| System.out.print("Enter a mission ID you want to search for: "); | ||||||
| String missionIdStr = sc.nextLine(); | ||||||
| int missionId; | ||||||
| try{ | ||||||
| missionId = Integer.parseInt(missionIdStr); | ||||||
| } catch (NumberFormatException e) { | ||||||
| System.out.println("Invalid mission ID"); | ||||||
| break; | ||||||
| } | ||||||
|
|
||||||
| missionRepo.getMissionById(missionId).ifPresentOrElse( | ||||||
| mission -> System.out.printf( | ||||||
| "Mission ID: %d%nSpacecraft: %s%nLaunch Date: %s%nCarrier Rocket: %s%nOutcome: %s%nMission Type: %s%nOperator: %s%n", | ||||||
| mission.getMissionId(), | ||||||
| mission.getSpacecraft(), | ||||||
| mission.getLaunchDate(), | ||||||
| mission.getCarrierRocket(), | ||||||
| mission.getOutcome(), | ||||||
| mission.getMissionType(), | ||||||
| mission.getOperator() | ||||||
| ), | ||||||
| () -> System.out.println("No mission found.") | ||||||
| ); | ||||||
| break; | ||||||
|
|
||||||
| 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; | ||||||
|
Comment on lines
+156
to
+162
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. Same The year input also uses 🤖 Prompt for AI Agents |
||||||
|
|
||||||
| case 4: | ||||||
| System.out.println("Enter your first name: "); | ||||||
| String firstName = sc.nextLine(); | ||||||
| System.out.println("Enter your last name: "); | ||||||
| String lastName = sc.nextLine(); | ||||||
| System.out.println("Enter your ssn (social security number): "); | ||||||
| String ssn = sc.nextLine(); | ||||||
| System.out.println("Enter your password: "); | ||||||
| String password = sc.nextLine(); | ||||||
|
|
||||||
| if (accountRepo.createAccount(firstName, lastName, ssn, password)) { | ||||||
| System.out.println("Your account was successfuly created"); | ||||||
|
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. Typo: "successfuly" should be "successfully". - System.out.println("Your account was successfuly created");
+ System.out.println("Your account was successfully created");📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| } else { | ||||||
| System.out.println("Something went wrong during the creation of your account"); | ||||||
| } | ||||||
| break; | ||||||
|
|
||||||
| 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; | ||||||
|
Comment on lines
+181
to
+193
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. Same The userId input also uses 🤖 Prompt for AI Agents |
||||||
|
|
||||||
| 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; | ||||||
|
Comment on lines
+195
to
+202
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. Same The userIdDelete input also uses 🤖 Prompt for AI Agents |
||||||
|
|
||||||
| case 0: | ||||||
| System.out.println("Exiting the program."); | ||||||
| break; | ||||||
|
|
||||||
| default: | ||||||
| System.out.println("Invalid choice"); | ||||||
| } | ||||||
| return (v == null || v.trim().isEmpty()) ? null : v.trim(); | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package com.example; | ||
|
|
||
| import java.time.LocalDate; | ||
|
|
||
| public class MoonMission { | ||
| private int missionId; | ||
| private String spacecraft; | ||
| private LocalDate launchDate; | ||
| private String carrierRocket; | ||
| private String operator; | ||
| private String missionType; | ||
| private String outcome; | ||
|
|
||
| // GETTERS | ||
| public int getMissionId() { | ||
| return missionId; | ||
| } | ||
| public String getSpacecraft() { | ||
| return spacecraft; | ||
| } | ||
| public LocalDate getLaunchDate() { | ||
| return launchDate; | ||
| } | ||
| public String getCarrierRocket() { | ||
| return carrierRocket; | ||
| } | ||
| public String getOperator() { | ||
| return operator; | ||
| } | ||
| public String getMissionType() { | ||
| return missionType; | ||
| } | ||
| public String getOutcome() { | ||
| return outcome; | ||
| } | ||
|
|
||
| // SETTERS | ||
|
|
||
|
|
||
| public void setMissionId(int missionId) { | ||
| this.missionId = missionId; | ||
| } | ||
|
|
||
| public void setSpacecraft(String spacecraft) { | ||
| this.spacecraft = spacecraft; | ||
| } | ||
|
|
||
| public void setLaunchDate(LocalDate launchDate) { | ||
| this.launchDate = launchDate; | ||
| } | ||
|
|
||
| public void setCarrierRocket(String carrierRocket) { | ||
| this.carrierRocket = carrierRocket; | ||
| } | ||
|
|
||
| public void setOutcome(String outcome) { | ||
| this.outcome = outcome; | ||
| } | ||
|
|
||
| public void setMissionType(String missionType) { | ||
| this.missionType = missionType; | ||
| } | ||
|
|
||
| public void setOperator(String operator) { | ||
| this.operator = operator; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package com.example; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.DriverManager; | ||
| import java.sql.SQLException; | ||
|
|
||
| public class SimpleDriverManagerDataSource { | ||
| private final String jdbcUrl; | ||
| private final String dbUser; | ||
| private final String dbPass; | ||
|
|
||
| // Resolve DB settings with precedence: System properties -> Environment variable | ||
| public SimpleDriverManagerDataSource(String jdbcUrl, String dbUser, String dbPass){ | ||
| this.jdbcUrl = jdbcUrl; | ||
| this.dbUser = dbUser; | ||
| this.dbPass = dbPass; | ||
| 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."); | ||
| } | ||
| } | ||
|
|
||
| public Connection getConnection() throws SQLException{ | ||
| return DriverManager.getConnection(jdbcUrl, dbUser, dbPass); | ||
| } | ||
|
|
||
| } |
Uh oh!
There was an error while loading. Please reload this page.