added basic services

This commit is contained in:
Mehul Ahal 2023-01-10 20:37:33 +01:00 committed by LahaLuhem
parent 131446900f
commit af8c80f216
5 changed files with 114 additions and 1 deletions

View File

@ -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();
}
}

View File

@ -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");
}

View File

@ -0,0 +1,7 @@
package constants;
public class ConstValues {
//I
final public static String inputCountryName = "Enter country: ";
final public static String inputDate = "Enter date: ";
}

View File

@ -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();
}
}

View File

@ -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;
}
}