// imports allow you to use code already written by others.  It is good to explore and learn libraries.  The names around the dots often give you a hint to the originator of the code.
import java.util.Scanner; //library for user input
import java.lang.Math; //library for random numbers
import java.io.*; // library for input output classes
import java.io.File;


public class Menu {
    // Instance Variables
    public final String DEFAULT = "\u001B[0m";  // Default Terminal Color
    public final String[][] COLORS = { // 2D Array of ANSI Terminal Colors
        {"Default",DEFAULT},
        {"Red", "\u001B[31m"}, 
        {"Green", "\u001B[32m"}, 
        {"Yellow", "\u001B[33m"}, 
        {"Blue", "\u001B[34m"}, 
        {"Purple", "\u001B[35m"}, 
        {"Cyan", "\u001B[36m"}, 
        {"White", "\u001B[37m"}, 
    };
    // 2D column location for data
    public final int NAME = 0;
    public final int ANSI = 1;  // ANSI is the "standard" for terminal codes


    public static final String ANSI_RESET = "\u001B[0m";
    public static final String ANSI_BLACK = "\u001B[30m";
    public static final String ANSI_RED = "\u001B[31m";
    public static final String ANSI_GREEN = "\u001B[32m";
    public static final String ANSI_YELLOW = "\u001B[33m";
    public static final String ANSI_BLUE = "\u001B[34m";
    public static final String ANSI_PURPLE = "\u001B[35m";
    public static final String ANSI_CYAN = "\u001B[36m";
    public static final String ANSI_WHITE = "\u001B[37m";

    // Constructor on this Object takes control of menu events and actions
    public Menu() {
        Scanner sc = new Scanner(System.in);  // using Java Scanner Object
        
        this.print();  // print Menu
        boolean quit = false;
        while (!quit) {
            try {  // scan for Input
                int choice = sc.nextInt();  // using method from Java Scanner Object
                System.out.print("" + choice + ": ");
                quit = this.action(choice);  // take action
            } catch (Exception e) {
                sc.nextLine(); // error: clear buffer
                System.out.println(e + ": Not a number, try again.");
            }
        }
        sc.close();
    }

    // Print the menu options to Terminal
    private void print() {
        //System.out.println commands below is used to present a Menu to the user. 
        System.out.println("-------------------------\n");
        System.out.println("Choose from these choices");
        System.out.println("-------------------------\n");
        System.out.println("1 - Math");
        System.out.println("2 - Science");
        System.out.println("3 - English");
        System.out.println("4 - Armaan > Mr. Mortenson in color");
        System.out.println("0 - Quit");
        System.out.println("-------------------------\n");
    }

    // Private method to perform action and return true if action is to quit/exit
    private boolean action(int selection) throws Exception {
        
        boolean quit = false;

        switch (selection) {  // Switch or Switch/Case is Control Flow statement and is used to evaluate the user selection
            case 0:  
                System.out.print("Cya!");
                quit = true;
                break;
            case 1:
                System.out.println("Math Calculator: ");
                // stores two numbers
                double num1, num2;

                // Take input from the user
                Scanner scanner = new Scanner(System.in);

                System.out.print("Enter the first number: ");

                // take the inputs
                num1 = scanner.nextDouble();

                System.out.println(num1);

                System.out.print("Enter the operator (+,-,*,/): ");

                char operator = scanner.next().charAt(0);

                System.out.println(operator);

                System.out.print("Enter second number: ");

                num2 = scanner.nextDouble();

                System.out.println(num2);

                double output = 0;

                switch (operator) {

                // case to add two numbers
                case '+':

                    output = num1 + num2;

                    break;

                // case to subtract two numbers
                case '-':

                    output = num1 - num2;

                    break;

                // case to multiply two numbers
                case '*':

                    output = num1 * num2;

                    break;

                // case to divide two numbers
                case '/':

                    output = num1 / num2;

                    break;

                default:

                    System.out.println("Invalid input");

                    break;
                }

                System.out.println("The final result: ");

                // print the final result
                System.out.println(num1 + " " + operator + " " + num2 + " = " + output);

                System.out.println();
                System.out.println(ANSI_RED + "Enter Another option from the menu: " + ANSI_RESET);
                break;

                

            case 2:
                System.out.println("Science (F = m*a) Calculator");
                
                // force calcultor

                // initialize scanner input
                Scanner input;

                // initialize integers to be used for the input and to be displayed at the very end
                double force = 0.0;

                double mass = 0.0;

                double acceleration = 0.0;

                // initialize strings to be used for the input and to be displayed at the very end

                String finalString;
                
                
                // primitive int
                input = new Scanner(System.in);
                System.out.print("Enter force(kg): ");
                try {
                    mass = input.nextDouble();
                    System.out.println(mass);
                } catch (Exception e) {  // if not a number
                    System.out.println("Not an double (form like 9.99), " + e);
                }
                input.close();
                
            
                // primitive int
                input = new Scanner(System.in);
                System.out.print("Enter acceleration(m/s²): ");
                try {
                    acceleration = input.nextDouble();
                    System.out.println(acceleration);
                } catch (Exception e) {  // if not a number	
                    System.out.println("Not an double (form like 9.99), " + e);
                }
                input.close();


                
                // math operation for finding force = mass x acceleration
                force = mass * acceleration; 

                // final formatted string with both variables
                finalString = "force(N) = " + force;

                System.out.println(finalString); 

                System.out.println();
                System.out.println(ANSI_YELLOW + "Enter Another option from the menu: " + ANSI_RESET);
                break;
            case 3:
            System.out.print("English Spell Checker...");
            
            Scanner write = new Scanner(System.in);

            System.out.println("Enter a sentence and the program will check if you made any spelling mistakes or if the word is not in the dictionary: ");
            String sentence = write.nextLine();
            System.out.println(sentence);
            String[] splitSentence = sentence.split(" ");

            for(int i = 0; i < splitSentence.length; i++)
            {
                Scanner read = new Scanner(new File("dictionary.txt"));
                boolean found = false;
                while(read.hasNextLine())
                {
                    String compare = read.nextLine();

                    if(compare.equalsIgnoreCase(splitSentence[i]))
                    {
                        System.out.println(splitSentence[i] + " : correct");
                        found=true;
                        break;
                    }
                }
                if(!found)
                    System.out.println(splitSentence[i] + " : incorrect");
            }
        

            System.out.println();
            System.out.println(ANSI_BLUE + "Enter Another option from the menu: " + ANSI_RESET);
            break;
            case 4:
                for (int i = 0; i < 20; i++) {  // fixed length loading bar
                    int random = (int) (Math.random() * COLORS.length);  // random logic
                    try {
                        Thread.sleep(100);  // delay for loading
                    } catch (Exception e) {
                        System.out.println(e);
                    }
                    System.out.print(COLORS[random][ANSI] + " Armaan > Mr. Mortensen ");
                }
            System.out.println();
            System.out.println();
            System.out.println(ANSI_WHITE + "Enter Another option from the menu: " + ANSI_RESET);
                break;
                    
            default:
                //Prints error message from console
                System.out.print("Unexpected choice, try again.");
        }
        System.out.println(DEFAULT);  // make sure to reset color and provide new line
        return quit;
    }

    // Static driver/tester method
    static public void main(String[] args)  {  
        new Menu(); // starting Menu object
    }

    

}
Menu.main(null);
-------------------------

Choose from these choices
-------------------------

1 - Math
2 - Science
3 - English
4 - Armaan > Mr. Mortenson in color
0 - Quit
-------------------------

java.util.InputMismatchException: Not a number, try again.
1: Math Calculator: 
Enter the first number: 2.0
Enter the operator (+,-,*,/): +
Enter second number: 4.0
The final result: 
2.0 + 4.0 = 6.0

Enter Another option from the menu: 

java.util.InputMismatchException: Not a number, try again.
java.util.InputMismatchException: Not a number, try again.
java.util.InputMismatchException: Not a number, try again.
1: Math Calculator: 
Enter the first number: java.util.InputMismatchException: Not a number, try again.
19: Unexpected choice, try again.
java.util.InputMismatchException: Not a number, try again.