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 - Factorial Calculator");
System.out.println("5 - Degrees to Radians");
System.out.println("6 - Grade Calculator");
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 < 10; 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] + "Hey! ");
}
break;
case 4:
System.out.print("Enter an Integer:");
Scanner myObj = new Scanner(System.in);
int number = myObj.nextInt();
int i,fact=1;
for(i=1;i<=number;i++){
fact=fact*i;
}
System.out.println("\nFactorial of "+number+" is: "+fact);
break;
case 5:
System.out.print("Enter a Degree Value:");
Scanner myObj2 = new Scanner(System.in);
double number2 = myObj2.nextDouble();
double radians = Math.toRadians(number2);
System.out.println("\nValue in Radians = " + String.format("%.2f", radians));
break;
case 6:
/* This program assumes that the student has 6 subjects,
* thats why I have created the array of size 6. You can
* change this as per the requirement.
*/
int marks[] = new int[5];
float total=0, avg;
Scanner scanner = new Scanner(System.in);
for(i=0; i<5; i++) {
System.out.print("Enter Grade for Period "+(i+1)+": ");
int test = scanner.nextInt();
marks[i] = test;
System.out.println(test);
System.out.println(marks[i]);
total = total + marks[i];
}
scanner.close();
//Calculating average here
avg = total/5;
System.out.println("The student Grade is: ");
if(avg>=80) {
System.out.println("A " + avg);
}
else if(avg>=60 && avg<80){
System.out.println("B " + avg);
}
else if(avg>=40 && avg<60){
System.out.println("C " + avg);
}
else{
System.out.println("D " + avg);
}
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);