covid_10012023_covid-reports/src/services/AppConfigService.java

51 lines
1.6 KiB
Java

package services;
import utils.Formatters;
import utils.ConstText;
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 {
private String countryName;
public String getCountryName() {return countryName;}
private Date date;
public Date getDate() {return date;}
/**
* Prompts the user to input the country name and date, and stores the input.
* It will keep prompting the user until the date is in the correct format.
*/
public void promptAndSetUserArguments() {
// Closing even on an uncaught exception
try (Scanner scanner = new Scanner(System.in)) {
System.out.println(ConstText.inputCountryName);
countryName = scanner.nextLine();
System.out.println();
System.out.println(ConstText.inputDate);
while (true) {
try {
date = Formatters.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(ConstText.dateInFuture);
continue;
}
// Not doing anymore month/day validations.
break;
} catch (ParseException e) {
System.out.printf(ConstText.unableParseDate, Formatters.APPLICATION_DATE_FORMATTER.toPattern());
}
}
}
}
}