Instructions
Requirements and Specifications
Screenshot
Source Code
TF CONSOLE
import java.util.Scanner;
public class TF_Console {
public static double getDouble(Scanner sc, String prompt) {
double d = 0.0;
boolean isValid = false;
while (!isValid) {
System.out.print(prompt);
if (sc.hasNextDouble()) {
d = sc.nextDouble();
isValid = true;
} else {
System.out.println("Error! Invalid decimal value. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return d;
}
public static double getDouble(Scanner sc, String prompt,
double min, double max) {
double d = 0.0;
boolean isValid = false;
while (!isValid) {
d = getDouble(sc, prompt);
if (d <= min) {
System.out.println(
"Error! Number must be greater than " + min + ".");
} else if (d >= max) {
System.out.println(
"Error! Number must be less than " + max + ".");
} else {
isValid = true;
}
}
return d;
}
public static int getInt(Scanner sc, String prompt) {
int i = 0;
boolean isValid = false;
while (!isValid) {
System.out.print(prompt);
if (sc.hasNextInt()) {
i = sc.nextInt();
isValid = true;
} else {
System.out.println("Error! Invalid integer value. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return i;
}
public static int getInt(Scanner sc, String prompt,
int min, int max) {
int i = 0;
boolean isValid = false;
while (!isValid) {
i = getInt(sc, prompt);
if (i <= min) {
System.out.println(
"Error! Number must be greater than " + min + ".");
} else if (i >= max) {
System.out.println(
"Error! Number must be less than " + max + ".");
} else {
isValid = true;
}
}
return i;
}
}
TF COUNTRIES APP
import java.util.ArrayList;
import java.util.Locale;
import java.util.Scanner;
public class TF_CountriesApp {
public static void main(String[] args) {
try(Scanner scanner = new Scanner(System.in)) {
showMenu();
boolean isOver = false;
TF_CountryIO io = new TF_CountryIO();
while(!isOver) {
int choice = TF_Console.getInt(scanner, "Enter menu number: ", 0, 5);
switch (choice) {
case 1:
System.out.println();
for (String country : io.getCountries()) {
System.out.println(country);
}
break;
case 2:
System.out.println();
System.out.print("Enter country: ");
String country = scanner.nextLine().trim();
ArrayList<String> countries = io.getCountries();
countries.add(country);
if (!io.saveCountries(countries)) {
System.out.println("Error on saving countries to file");
}
else {
System.out.println("This country has been saved");
}
break;
case 3:
try {
System.out.println();
System.out.print("Enter country: ");
country = scanner.nextLine().trim();
countries = io.getCountries();
if (!countries.contains(country)) {
throw new TF_NoSuchCountryException("This country is not in the list");
}
countries.remove(country);
if (!io.saveCountries(countries)) {
System.out.println("Error on saving countries to file");
}
else {
System.out.println("This country has been removed");
}
}
catch (TF_NoSuchCountryException e) {
System.out.println(e.getMessage());
}
break;
case 4:
isOver = true;
break;
default:
throw new IllegalStateException();
}
System.out.println();
}
System.out.println("Goodbye.");
}
}
private static void showMenu() {
System.out.println("Country List Manager");
System.out.println();
System.out.println("COMMAND MENU");
System.out.println("1 - List countries");
System.out.println("2 - Add a country");
System.out.println("3 - Delete a country");
System.out.println("4 - Exit");
System.out.println();
}
}
TF COUNTRY IO
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;
public class TF_CountryIO {
private static final String TF_INPUT_FILENAME = "TF_countries.txt";
public ArrayList<String> getCountries() {
checkFile();
try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream(TF_INPUT_FILENAME))) {
StringBuilder builder = new StringBuilder();
int r;
while((r = bis.read()) != -1) {
for(char c : Character.toChars(r)) {
builder.append(c);
}
}
return Arrays.stream(builder.toString().split("\\r?\\n")).collect(Collectors.toCollection(ArrayList::new));
}
catch (IOException e) {
throw new IllegalStateException(e);
}
}
public boolean saveCountries(ArrayList<String> TF_Countries) {
try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(TF_INPUT_FILENAME))) {
for(String country : TF_Countries) {
bos.write((country + System.lineSeparator()).getBytes(StandardCharsets.UTF_8));
}
return true;
}
catch (IOException e) {
return false;
}
}
private void checkFile() {
if (Files.exists(Paths.get(TF_INPUT_FILENAME))) {
return;
}
try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(TF_INPUT_FILENAME))) {
bos.flush();
}
catch (IOException e) {
throw new RuntimeException("Can not create file " + TF_INPUT_FILENAME, e);
}
}
}
TF COUNTRIES APP
import java.util.ArrayList;
import java.util.Locale;
import java.util.Scanner;
public class TF_CountriesApp {
public static void main(String[] args) {
try(Scanner scanner = new Scanner(System.in)) {
showMenu();
boolean isOver = false;
TF_CountryIO io = new TF_CountryIO();
while(!isOver) {
int choice = TF_Console.getInt(scanner, "Enter menu number: ", 0, 5);
switch (choice) {
case 1:
System.out.println();
for (String country : io.getCountries()) {
System.out.println(country);
}
break;
case 2:
System.out.println();
System.out.print("Enter country: ");
String country = scanner.nextLine().trim();
ArrayList<String> countries = io.getCountries();
countries.add(country);
if (!io.saveCountries(countries)) {
System.out.println("Error on saving countries to file");
}
else {
System.out.println("This country has been saved");
}
break;
case 3:
try {
System.out.println();
System.out.print("Enter country: ");
country = scanner.nextLine().trim();
countries = io.getCountries();
if (!countries.contains(country)) {
throw new TF_NoSuchCountryException("This country is not in the list");
}
countries.remove(country);
if (!io.saveCountries(countries)) {
System.out.println("Error on saving countries to file");
}
else {
System.out.println("This country has been removed");
}
}
catch (TF_NoSuchCountryException e) {
System.out.println(e.getMessage());
}
break;
case 4:
isOver = true;
break;
default:
throw new IllegalStateException();
}
System.out.println();
}
System.out.println("Goodbye.");
}
}
private static void showMenu() {
System.out.println("Country List Manager");
System.out.println();
System.out.println("COMMAND MENU");
System.out.println("1 - List countries");
System.out.println("2 - Add a country");
System.out.println("3 - Delete a country");
System.out.println("4 - Exit");
System.out.println();
}
}