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

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.
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 simple airline ticket reservation program. The program should display a menu with the following...
Write a simple airline ticket reservation program. The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit on the number of flights. Create a linked list...
Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names     &
Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close Do you...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                       ...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close...
Create a menu in Java. A menu is a presentation of options for you to select....
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. Buy Stock. B. Sell Stock X. Exit Enter your Selection: 3. Prompt for the value menuChoice....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT