// 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
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 to find top 3 GDP's of each region");
System.out.println("-------------------------\n");
System.out.println("1 - Global GDP");
System.out.println("2 - North America");
System.out.println("3 - South America");
System.out.println("4 - Africa");
System.out.println("5 - Europe-Middle East");
System.out.println("6 - Central Asia/Caucases");
System.out.println("7 - Asia-Pacific");
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("Thank you for using this GUI!");
quit = true;
break;
case 1:
System.out.println("US - 20.49 trillion dollars");
System.out.println("China - 13.4 trillion dollars");
System.out.println("Japan - 4.9 trillion dollars");
break;
case 2:
System.out.println("US - 20,412,870 million dollars");
System.out.println("Canada - 1,798,512 million dollars");
System.out.println("Mexico - 1,212.831 million dollars");
break;
case 3:
System.out.println("Brazil - 1,833,274 million dollars");
System.out.println("Mexico - 1,322,740 million dollars");
System.out.println("Argentina - 505,235 million dollars");
break;
case 4:
System.out.println("Nigeria - 510.588 trillion dollars");
System.out.println("Egypt - 435.621 trillion dollars");
System.out.println("South Africa - 426.166 trillion dollars");
break;
case 5:
System.out.println("Germany - 4,256.540 trillion dollars");
System.out.println("UK - 3,376.003 trillion dollars");
System.out.println("France - 2,936.702 trillion dollars");
break;
case 6:
System.out.println("Azerbaijan - 70.34 billion dollars");
System.out.println("Georgia - 20.89 billion dollars");
System.out.println("Armenia - 14.39 billion dollars");
break;
case 7:
System.out.println("China - 13.4 trillion dollars");
System.out.println("Japan - 4.9 trillion dollars");
System.out.println("India - 8.91 trillion dollars");
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
}
}