Console Based Menu

Objects Used:

  • Used scanner class to get user input and make objects
  • Used system class to print out static methods-
  • Used the java Math Class to calculate for GCM and LCM

PBL Takeaways:

  • By using the Menu () code, the user is able to select which option they want to start. This connects to Frontend because the code allows the program to be more user friendly. -The user is entering what they want the code to process which is different than GUI console which allows the user to select and view different options
import java.util.Scanner; //library for user input
import java.lang.Math; //library for random numbers


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

    // 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 - Say Hello");
        System.out.println("2 - Output colors");
        System.out.println("3 - Loading in color");
        System.out.println("4 - Greatest Common Factor");
        System.out.println("5 - Least Common Multiple");
        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) {
        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("Goodbye, World!");
                quit = true;
                break;
            case 1:
                System.out.print("Hello, World!");
                break;
            case 2:
                for(int i = 0; i < COLORS.length; i++)  // loop through COLORS array
                    System.out.print(COLORS[i][ANSI] + COLORS[i][NAME]);
                break;
            case 3:
                System.out.print("Loading...");
                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] + "#");
                }
                break;

            case 4:
                // two numbers from user input
                Scanner input = new Scanner(System.in);
                System.out.println("Enter a number: ");
                int a = input.nextInt();
                System.out.println("Enter another number: ");
                Scanner input2 = new Scanner(System.in);
                int b = input2.nextInt();
        
                int c = Math.min(a, b);
                input.close();
                input2.close();
                int gcd = 1;
                for(int i = c; i > 0; i--){ // descending
                    if(a % i == 0 && b % i == 0){
                        gcd = i;
                        break; // exit loop if it is a factor
                    }
                }
                System.out.println("The greatest common factor of " + a + " and " + b + " is " + gcd);
                break; 
                
            case 5:
                Scanner num1 = new Scanner(System.in);
                System.out.println("Enter a number: ");
                int x = num1.nextInt();
                Scanner num2 = new Scanner(System.in);
                System.out.println("Enter another number: ");
                int y = num2.nextInt();
        
                num1.close();
                num2.close();
        
                int m = Math.max(x, y); // least common multiple must be at least as large as the larger of the two numbers
        
                while (true) { // repeat until break
                    if(m % x == 0 && m % y == 0){
                        System.out.println("The least common multiple of " + x + " and " + y + " is " + m);
                        break;
                    }
                    m += 1;
                }
                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 - Say Hello
2 - Output colors
3 - Loading in color
4 - Greatest Common Factor
5 - Least Common Multiple
0 - Quit
-------------------------

1: Hello, World!
2: DefaultRedGreenYellowBluePurpleCyanWhite
3: Loading...####################
4: Enter a number: 
Enter another number: 
The greatest common factor of 8 and 12 is 4

5: Enter a number: 
Enter another number: 
The least common multiple of 2 and 4 is 4

0: Goodbye, World!

Desktop GUI Menu

See Brian and I's GUI Console that we put together: https://bgt072105.github.io/CSA-tri1-teamrepo/2022/09/05/GUImenu.html

Objects Used:

  • Used the if else, if else structure that allows text to show up based on what the user selects
  • Scanner class
  • Static variable since GUI doesn't allow inputs

PBL Learnings:

  • Graphical User Interface allows for the user to click and navigate through the console based on what they want to see
  • More faster and easier to understand
  • More visual for the user compared to Console

Hacks

Explain where a Class is defined

  • You define a class whenever you need to add variables, objects or methods to your code
// class definition
public class classname

Explain where an instances of a Class is defined: A class is defined when you have an object. Here, I created a new menu for the user to select different options to input values for.

// new instance of class (object)
new Menu();

Explain where an object is Calling a Method: An object is calling a method when an action needs to be performed for the code to run. Here, I created an object where I can input an integer

// object calling method

int choice = sc.nextInt();  // sc object calls nextInt() method

Explain where an object is Mutating data

// mutating data refers to a change in the original data. Here, the primitive data type is number that is being mutated to create a least common multiple
int x = num1.nextInt();
Scanner num2 = new Scanner(System.in);
System.out.println("Enter another number: ");
int y = num2.nextInt();

num1.close();
num2.close();

int m = Math.max(x, y);

Describe Console, GUI differences, or Code.org differences.

  • Console:

    • User enters inputs and get respective outputs
    • Not as visual
    • Requires very good understanding of script and syntax
    • Example: Bash shell --> inputs by user, outputs through the code
  • GUI:

    • More clickable platform
    • Comfortable to use because it's user-friendly
    • Visual feedback and display
    • Can't really perform multiple tasks at once
    • You can access another computer/platform remotely
    • example: Apple's Macintosh operating system
  • Code.org:

    • Uses the painter as a visual object to help the user understand the code's implementations
    • File and public class " name " should be same
    • Public class was My Neighborhood same as what the file was named
    • Calling a method is done through "variableName.methodName();
    • More basic platform

Extra Observations

  • Need to be very careful with syntax and brackets otherwise it throws off your whole code
  • For GUI Console, inputs would've been useful
  • A problem we faced was that the regular list console wasn't working on Brian's computer so we used a GUI instead
  • We could try having a more visually appealing GUI for our user
  • Need to be careful when naming doubles and integers being attentive to their definitions
  • Not as interesting that the GUI can't take a user input in case we want to make a calculator or something like that