// 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
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
boolean quit = false;
while (!quit) {
try { // scan for Input
this.print(); // print Menu
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 - Unit Converter");
System.out.println("2 - Inverse Calculator");
System.out.println("3 - Right Triangle Hypotenuse 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("bye");
quit = true;
break;
case 1:
unitConverter();
break;
case 2:
inverse();
break;
case 3:
hypotenuseCalc();
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
}
public void unitConverter() {
int choice = 0;
double num = 0.0;
Scanner in = new Scanner(System.in);
System.out.print("In what unit is your current number in? (1=meters;2=feet;3=miles)\n");
try {
choice = in.nextInt();
System.out.println(choice);
} catch (Exception e) {
System.out.println("not an int");
}
in.close();
in = new Scanner(System.in);
System.out.print("whats the number you want to convert\n");
try {
num = in.nextDouble();
System.out.println(num);
} catch (Exception e) {
System.out.println("not a double");
}
in.close();
if (choice==1) {
double feet = num*3.28084;
double miles = num*0.000621371;
System.out.println(num + " in feet is " + feet + " ft\n" + num + " in miles is " + miles + " mi");
}
else if (choice==2) {
double meter = num*0.3048;
double miles = num/5280;
System.out.println(num + " in meters is " + meter + " m\n" + num + " in miles is " + miles + " mi");
}
else if (choice==3) {
double meter = num*1609.34;
double feet = num*5280;
System.out.println(num + " in meters is " + meter + " m\n" + num + " in feet is " + feet + " ft");
}
}
public void inverse() {
double num = 0.0;
Scanner in = new Scanner(System.in);
in = new Scanner(System.in);
System.out.print("what do you want to find the inverse of\n");
try {
num = in.nextDouble();
System.out.println(num);
} catch (Exception e) {
System.out.println("not a double");
}
in.close();
System.out.println("the inverse of " + num + " is " + 1/num);
}
public void hypotenuseCalc() {
double num1 = 0.0;
double num2 = 0.0;
Scanner in = new Scanner(System.in);
in = new Scanner(System.in);
System.out.print("what is the length of one side\n");
try {
num1 = in.nextDouble();
System.out.println(num1);
} catch (Exception e) {
System.out.println("not a double");
}
in.close();in = new Scanner(System.in);
System.out.print("what is the length of the other side\n");
try {
num2 = in.nextDouble();
System.out.println(num2);
} catch (Exception e) {
System.out.println("not a double");
}
in.close();
System.out.println("the length of the hypotenuse is " + Math.sqrt(Math.pow(num1, 2) + Math.pow(num2, 2)));
}
}
Menu.main(null);