covid_10012023_covid-reports/src/services/AppConfigService.java

49 lines
1.5 KiB
Java

package services;
import utils.Formatters;
import utils.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 {
public String countryName;
public Date 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() {
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 = 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("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", Formatters.APPLICATION_DATE_FORMATTER.toPattern());
}
}
scanner.close();
}
}