Question

In: Computer Science

Write an interactive Java class Project8Q3, that will display a menu with the available commands 'G',...

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

Solutions

Expert Solution

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
########


Related Solutions

Write a java program that will first display the following menu: Choose one of the following...
Write a java program that will first display the following menu: Choose one of the following 1- To add 2 double integers 2- To add 2 integer numbers 3- To add 3 double numbers 4- To add 3 integer numbers After reading user’s choice, use a switch case statement to call the corresponding method Void add 1 (double n1, double n2) Void add2() Double add3 (double n1, double n2, double n3) Double add4 ()
Java code for a binary tree that does the following:  Display a menu to the...
Java code for a binary tree that does the following:  Display a menu to the user and request for the desired option.  Based on the user’s input, request for additional information as follows: o If the user wants to add a node, request for the name (or ID) of the new node to be added as well as the name of the desired parent of that node.  If the parent node already has two children, the new...
Java code for a binary tree that does the following:  Display a menu to the...
Java code for a binary tree that does the following:  Display a menu to the user and request for the desired option.  Based on the user’s input, request for additional information as follows: o If the user wants to add a node, request for the name (or ID) of the new node to be added as well as the name of the desired parent of that node.  If the parent node already has two children, the new...
Class, Let's create a menu in Java. A menu is a presentation of options for you...
Class, Let's create a menu in Java. A menu is a presentation of options for you to select. You can do this in the main() method. 1. Create a String variable named menuChoice. 2. Display 3 options for your program. I recommend using 3 System.out.println statements and a 4th System.out.print to show "Enter your Selection:". This needs to be inside the do -- while loop. A. Deposit Cash. B. Withdraw Cash X. Exit Enter your Selection: 3. Prompt for the...
Write an interactive Java class which accepts an input argument when the application is executed from...
Write an interactive Java class which accepts an input argument when the application is executed from the command-line. Accept input from the user and compare the value entered to the command-line argument value. If the strings do not equal, display "INVALID VALUE! TRY AGAIN!", otherwise display "PERMISSION GRANTED!" and exit the program.
Java Create a method to display a menu. When this method is called it should receive...
Java Create a method to display a menu. When this method is called it should receive the user's name, great the user, and ask them to make a selection. 1 - Change your name, 2 - Test your IQ, 3 - Display a table, 4 - Play a game, 5 - Exit.
Create a Java class file for a Car class. In the File menu select New File......
Create a Java class file for a Car class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Car. For Package: select csci2011.lab7. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab7; /** * * @author Your Name */ public class Car { } Implement the Car...
Write a program to prompt the user to display the following menu: Sort             Matrix                   Q
Write a program to prompt the user to display the following menu: Sort             Matrix                   Quit If the user selects ‘S’ or ‘s’, then prompt the user to ask how many numbers you wish to read. Then based on that fill out the elements of one dimensional array with integer numbers. Then sort the numbers and print the original and sorted numbers in ascending order side by side. How many numbers: 6 Original numbers:                     Sorted numbers 34                                                                         2          55                                                      ...
Write a program to prompt the user to display the following menu: Sort             Matrix                   Q
Write a program to prompt the user to display the following menu: Sort             Matrix                   Quit If the user selects ‘S’ or ‘s’, then prompt the user to ask how many numbers you wish to read. Then based on that fill out the elements of one dimensional array with integer numbers. Then sort the numbers and print the original and sorted numbers in ascending order side by side. How many numbers: 6 Original numbers:                     Sorted numbers 34                                                                         2          55                                                      ...
in java Write a Java Program that displays a menu with five different options: 1. Lab...
in java Write a Java Program that displays a menu with five different options: 1. Lab Test Average Calculator 2. Dice Roll 3. Circle Area Calculator 4. Compute Distance 5. Quit The program will display a menu with each of the options above, and then ask the user to enter their choice. There is also a fifth option to quit, in which case, the program will simply display a goodbye message. Based on the user’s choice, one of the options...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT