Question: In Java, how do get the program to accept the following user input and implement the command? Example Code so far: package com.egyptianExample; import java.util.

In Java, how do get the program to accept the following user input and implement the command?
Example Code so far:
package com.egyptianExample;
import java.util.*;
import org.json.simple.*;
public class EgyptianPyramidsAppExample {
protected Pharaoh[] pharaohArray;
protected Pyramid[] pyramidArray;
public static void main(String[] args){
// create and start the app
EgyptianPyramidsAppExample app = new EgyptianPyramidsAppExample();
app.start();
}
// main loop for app
public void start(){
Scanner scan = new Scanner(System.in);
Character command ='_';
// loop until user quits
while (command !='q'){
printMenu();
System.out.print("Enter a command: ");
command = menuGetCommand(scan);
executeCommand(scan, command);
}
}
// constructor to initialize the app and read commands
public EgyptianPyramidsAppExample(){
// read egyptian pharaohs
String pharaohFile =
"/Users/jerom/Documents/GitHub/class-java/egyptian-pyramids/demo/src/main/java/com/egyptianExample/pharaoh.json";
JSONArray pharaohJSONArray = JSONFile.readArray(pharaohFile);
// create and intialize the pharaoh array
initializePharaoh(pharaohJSONArray);
// read pyramids
String pyramidFile =
"/Users/jerom/Documents/GitHub/class-java/egyptian-pyramids/demo/src/main/java/com/egyptian/pyramid.json";
JSONArray pyramidJSONArray = JSONFile.readArray(pyramidFile);
// create and initialize the pyramid array
initializePyramid(pyramidJSONArray);
}
// initialize the pharaoh array
private void initializePharaoh(JSONArray pharaohJSONArray){
// create array and hash map
pharaohArray = new Pharaoh[pharaohJSONArray.size()];
// initalize the array
for (int i =0; i pharaohJSONArray.size(); i++){
// get the object
JSONObject o =(JSONObject) pharaohJSONArray.get(i);
// parse the json object
Integer id = toInteger(o,"id");
String name = o.get("name").toString();
Integer begin = toInteger(o, "begin");
Integer end = toInteger(o, "end");
Integer contribution = toInteger(o, "contribution");
String hieroglyphic = o.get("hieroglyphic").toString();
// add a new pharoah to array
Pharaoh p = new Pharaoh(id, name, begin, end, contribution, hieroglyphic);
pharaohArray[i]= p;
}
}
// initialize the pyramid array
private void initializePyramid(JSONArray pyramidJSONArray){
// create array and hash map
pyramidArray = new Pyramid[pyramidJSONArray.size()];
// initalize the array
for (int i =0; i pyramidJSONArray.size(); i++){
// get the object
JSONObject o =(JSONObject) pyramidJSONArray.get(i);
// parse the json object
Integer id = toInteger(o,"id");
String name = o.get("name").toString();
JSONArray contributorsJSONArray =(JSONArray) o.get("contributors");
String[] contributors = new String[contributorsJSONArray.size()];
for (int j =0; j contributorsJSONArray.size(); j++){
String c = contributorsJSONArray.get(j).toString();
contributors[j]= c;
}
// add a new pyramid to array
Pyramid p = new Pyramid(id, name, contributors);
pyramidArray[i]= p;
}
}
// get a integer from a json object, and parse it
private Integer toInteger(JSONObject o, String key){
String s = o.get(key).toString();
Integer result = Integer.parseInt(s);
return result;
}
// get first character from input
private static Character menuGetCommand(Scanner scan){
Character command ='_';
String rawInput = scan.nextLine();
if (rawInput.length()>0){
rawInput = rawInput.toLowerCase();
command = rawInput.charAt(0);
}
return command;
}
// print all pharaohs
private void printAllPharaoh(){
for (int i =0; i pharaohArray.length; i++){
printMenuLine();
pharaohArray[i].print();
printMenuLine();
}
}
private Boolean executeCommand(Scanner scan, Character command){
Boolean success = true;
switch (command){
case '1':
printAllPharaoh();
break;
case 'q':
System.out.println("Thank you for using Nassef's Egyptian Pyramid App!");
break;
default:
System.out.println("ERROR: Unknown commmand");
success = false;
}
return success;
}
private static void printMenuCommand(Character command, String desc){
System.out.printf("%s\t\t%s
", command, desc);
}
private static void printMenuLine(){
System.out.println(
"------------------------
In Java, how do get the program to accept the

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!