diff --git a/src/Main.java b/src/Main.java index 3e59c38..8c7863b 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,23 @@ +import services.AppConfigService; +import services.DatabaseService; + public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + final App app = new App(); + app.run(); + } +} + +class App { + public App() { + databaseService = new DatabaseService(); + appConfigService = new AppConfigService(); + } + + final DatabaseService databaseService; + final AppConfigService appConfigService; + + public void run() { + appConfigService.promptUserArguments(); } } \ No newline at end of file diff --git a/src/constants/ConstFormatters.java b/src/constants/ConstFormatters.java new file mode 100644 index 0000000..9b2dddd --- /dev/null +++ b/src/constants/ConstFormatters.java @@ -0,0 +1,7 @@ +package constants; + +import java.text.SimpleDateFormat; + +public class ConstFormatters{ + final public static SimpleDateFormat APPLICATION_DATE_FORMATTER = new SimpleDateFormat("dd-MM-yyyy"); +} diff --git a/src/constants/ConstValues.java b/src/constants/ConstValues.java new file mode 100644 index 0000000..24d4cbd --- /dev/null +++ b/src/constants/ConstValues.java @@ -0,0 +1,7 @@ +package constants; + +public class ConstValues { + //I + final public static String inputCountryName = "Enter country: "; + final public static String inputDate = "Enter date: "; +} diff --git a/src/services/AppConfigService.java b/src/services/AppConfigService.java new file mode 100644 index 0000000..45c0549 --- /dev/null +++ b/src/services/AppConfigService.java @@ -0,0 +1,44 @@ +package services; + +import constants.ConstFormatters; +import constants.ConstValues; + +import java.text.ParseException; + +import java.util.Date; +import java.util.Scanner; + +/** + * Prompts and stores the configuration that the app needs to be run with. + */ +public class AppConfigService { + String countryName; + Date date; + + public void promptUserArguments() { + final Scanner scanner = new Scanner(System.in); + System.out.println(ConstValues.inputCountryName); + countryName = scanner.nextLine(); + System.out.println(); + + System.out.println(ConstValues.inputDate); + while (true) { + try { + date = ConstFormatters.APPLICATION_DATE_FORMATTER.parse(scanner.nextLine()); + // Always using exact current date. Otherwise, different day possible at the start of the run. + if (date.after(new Date())) { + System.out.println("There is no data available for the future yet... Try again:"); + continue; + } + + // Not doing anymore month/day validations. + + break; + } catch (ParseException e) { + System.out.printf("Unable to parse date. Should be in format '%s'.\nTry again:%n", ConstFormatters.APPLICATION_DATE_FORMATTER.toPattern()); + } + } + + scanner.close(); + } +} diff --git a/src/services/DatabaseService.java b/src/services/DatabaseService.java new file mode 100644 index 0000000..82d5e0f --- /dev/null +++ b/src/services/DatabaseService.java @@ -0,0 +1,37 @@ +package services; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +/** + * Stores the active connection between the app and the local SQL Server. + */ +public class DatabaseService { + private final String url = "jdbc:postgresql://localhost:5432/lunatech_covid"; + private final String user = "postgres"; + private final String password = "postgres"; + + private final Connection connection; + + public DatabaseService() { + connection = connect(); + } + + /** + * Connect to the PostgreSQL database + * + * @return a Connection object + */ + private Connection connect() { + Connection connection = null; + try { + connection = DriverManager.getConnection(url, user, password); + System.out.println("Connected to the PostgreSQL server successfully."); + } catch (SQLException e) { + System.out.println(e.getMessage()); + } + + return connection; + } +}