In: Computer Science
Write an interactive Java class Project8Q3, that will display a menu with the available commands 'G', 'D', and 'X'. If 'G' is selected, prompt the user for the ID of a president to display to the screen and then display the president's information. If 'D' is selected, display all the values in the LinkedHashMap to the user with their associated LinkedHashMap key. If 'X' is selected, exit the program. The class should have the method addPresident(id, lastName, firstName, middleInitial) which stores each president's information from the table above into a custom President object class and then into a LinkedHashMap using the ID of the president for the key. Create a second method showPresidents() that displays all the president's information in each element of the LinkedHashMap. Finally create a third method showPresident() that displays the president's information for the ID entered by the user.
ID Last Name First Name Middle Initial
16 Lincoln Abraham null
18 Grant Ulysses S
26 Roosevelt Theodore null
27 Taft William H
29 Harding Warren G
INPUT
########
It is assumed that input file is in same directory as that of program and input file have an header line as in the screenshot below
//############################ PGM START #########################
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Scanner;
public class Project8Q3 {
public static HashMap<String,String> h1=new
HashMap<String,String>();
//method to add president record to hashmap
public static void addPresident(String id,String
lastName,String firstName,String middleInitial) {
//if middle inital is null then add
empty string
if(middleInitial=="null") {
middleInitial="";
}
//add id as key and combination of
all the names as value
h1.put(id,(lastName+" "+firstName+"
"+middleInitial));
}
//method to show all the president info
public static void showPresidents() {
//for each key in hashmap , print
key and its correponsing value
for (String name:
h1.keySet()){
String key = name.toString();
String value = h1.get(name).toString();
System.out.println(key +" " + value);
}
}
//method to print a record based on the id given
public static void showPresident(String id) {
//if id exist in hashmap print its
value
if (h1.containsKey(id)) {
String a = h1.get(id);
System.out.println("President Name:"+a);
}else {
System.out.println("President with the id "+id+" does not
exist");
}
}
//main method
public static void main(String[] args) {
char choice;
Scanner s=new
Scanner(System.in);
try {
//read the file
president.txt
Scanner
file1=new Scanner(new File("president.txt"));
//skipping the
header
if(file1.hasNext())
file1.nextLine();
//reading line
one by one in the file
while(file1.hasNext()) {
//split the line based on space and separate
each value
String temp[]=file1.nextLine().split(" ");
//add the values as one record in hash map
addPresident(temp[0],temp[1],temp[2],temp[3]);
}
file1.close();
//repeat untill
user exit
while(true)
{
System.out.print("__MAIN MENU__\nG. Display
president details using ID\n");
System.out.println("D. Display all president
details\nX. Exit\nEnter your choice: ");
choice=s.nextLine().toUpperCase().charAt(0);
switch(choice) {
case 'G':
System.out.print("Enter ID: ");
String id=s.nextLine();
showPresident(id);
break;
case
'D':System.out.println("Complete President Info......");
showPresidents();
break;
case 'X':
System.out.println("Exiting.........");
System.exit(0);
break;
default:
System.out.println("Wrong choice!!!!!, Please re-enter");
break;
}
System.out.println();
}
} catch (FileNotFoundException e)
{
System.out.println("File doesnot exist in the current directory of
program");
}
s.close();
}
}
//##################################### PGM END
###############################
OUTPUT
########