+1 (315) 557-6473 

Create A Program to Create a Country List Manager Assignment Solution.


Instructions

Objective
To complete a Java assignment, you are required to write a program to create a country list manager using the Java programming language. This program will enable you to manage a list of countries efficiently. You will need to implement various functionalities such as adding countries to the list, removing them, displaying the list, and possibly sorting it. This assignment will test not only your understanding of Java programming concepts but also your ability to design and implement a functional program. Make sure to apply appropriate data structures and coding practices to ensure the program's efficiency and correctness.

Requirements and Specifications

Program to create a country list manager in java language
Program to create a country list manager in java language 1

Screenshot

Program to create a country list manager in java language 2
Program to create a country list manager in java language 3
Program to create a country list manager in java language 4

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

    }

}