Question

In: Computer Science

Creating a Perl Product Directory - A product "id" will be used to identify a product...

Creating a Perl Product Directory - A product "id" will be used to identify a product (the key) and the description will be stored as the value

  • The program should print a menu asking you to "add" an entry, "remove" an entry, "lookup" an entry, "list" all entries, and "quit" to end
  • The user will enter a number to select the menu item from above, 1, 2, 3, 4, or 5 respectively.
  1. Asks the user to enter a product ID, then asks them to enter a description... the product ID is stored as the key of a hash and the description is the value
  2. Asks the user to an ID, if that ID exists in the hash, that entry is deleted from the hash
  3. Asks the user to an ID, if that ID exists in the hash, that entry's value is printed (the description)
  4. Prints all the key/value pairs of the hash, each entry on a new line with with a colon or dash separating the ID from the description
  5. Exits the program
  • The program will continuously loop, that is, it will print the menu after each successful action... unless the user chooses 5 to quit, which will end the program. This can be achieved by either using a "while" loop or by using a subroutine that prints the menu after each successful action

Some code is provided to you as a template.

Solutions

Expert Solution

The following code is written in JAVA.

---------------------------------------------------------------------------------------------------------------------------

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;


public class PerlDirectory {

  
public static void main(String[] args) {
/*
Create a hash map with an integer key and string description
*/
  
HashMap<Integer,String> map = new HashMap<Integer,String>();
boolean program = true;
while(program){
System.out.println("******************************");
System.out.println("---------MENU-----------");
System.out.println("1. ADD");
System.out.println("2. REMOVE");
System.out.println("3. LOOKUP");
System.out.println("4. LIST");
System.out.println("5. QUIT");
System.out.println("******************************");
  
Scanner sc = new Scanner(System.in);
System.out.print("Enter your choice : ");
int choice = sc.nextInt();
  
if(choice == 5){
//QUIT AND CLOSE THE MENU , END OF LOOP
program = false;
System.out.println("Menu closed");
}else if(choice == 4){
//LIST ALL ENTRIES
for (Map.Entry<Integer,String> entry : map.entrySet())
System.out.println(entry.getKey() + " - "+ entry.getValue());
  
}else if(choice == 3){
// LOOKUP FOR A PRODUCT IN HASH MAP
System.out.print("Enter product ID : ");
int product_id = sc.nextInt();
  
if(map.containsKey(product_id)){
System.out.println(map.get(product_id));
}else{
System.out.println("Key not found");
}
  
}else if(choice == 2){
// DELETE ITEM FROM HASH MAP
  
System.out.print("Enter product ID : ");
int product_id = sc.nextInt();
  
/*
Here containsKey() is a method which returns true or false depending on
whether the entered key is present in the map or not
*/
if(map.containsKey(product_id)){
map.remove(product_id);
System.out.println("Item successfully removed");
}else{
System.out.println("Key not found");
}
  
}else if(choice == 1){
System.out.print("Enter product ID : ");
int product_id = sc.nextInt();
sc.nextLine();
System.out.print("Enter description : ");
String description = sc.nextLine();
  
//ADD ITEM TO HASH MAP
map.put(product_id,description);
System.out.println("Item Added Successfully");
}else{
System.out.print("Incorrect Menu Item Selected");
  
}
  
}
}
  
}


Related Solutions

Perl is a programming language that can be used on Linux. Write a Perl shell script...
Perl is a programming language that can be used on Linux. Write a Perl shell script named phone.pl that prompts the user to enter first or last or any portion of person’s name, so that can be found the appropriate entry in the phone directory file called “phones”. If the user tries to enter name as the argument on the command line, he/she will get a warning message “You need to provide name when prompted by this script!” If the...
For the following classess IP addresses, identify what the ned id and the host id are...
For the following classess IP addresses, identify what the ned id and the host id are 119.120.231.15/12 11.30.125.141/17 244.192.168.10/18 199.10.155.121/28
Product(p-id, p-name, weight) Retailer(r-id, r-name, city) Sells(r-id, p-id, price)
  Product(p-id, p-name, weight)Retailer(r-id, r-name, city)Sells(r-id, p-id, price) r-id is a foreign key referencing Retailerp-id is a foreign key referencing Product Give a Relational Algebra expression : (IT IS NOT SQL) (a) Find the names of the retailers who are selling the products that have a weight greater than 10 kg.(b) Find the names of the retailers who have never sold a product that has a weight greater than 10 kg.(c) Get the price of the products that have a...
Identify the major problems with creating marketing plans.
Identify the major problems with creating marketing plans.
Identify the distribution channels used for a product or service or service of your choice. Be...
Identify the distribution channels used for a product or service or service of your choice. Be as specific as possible explaining the process for your product or service. Please respond in 200-250 words
1. What command is used to change the current working directory at the command line?
1. What command is used to change the current working directory at the command line?
Discuss how Active Directory provides centralized management and administration, and introduce the tools used for this...
Discuss how Active Directory provides centralized management and administration, and introduce the tools used for this purpose. Discuss the categorizing of Active Directory resources as well as the hierarchy in which they operate and interact with each other. Explain naming contexts within forests and the different items that can be contained within organizational units. # No plagiarism please
Oracle - Create a procedure that accepts product ID as a parameter and returns the name...
Oracle - Create a procedure that accepts product ID as a parameter and returns the name of the product from ProductTable table. Add exception handling to catch if product ID is not in the table. Table to use: CREATE TABLE ProductTable(     ProductID INTEGER NOT NULL primary key,     ProductName VARCHAR(50) NOT NULL,     ListPrice NUMBER(10,2),     Category INTEGER NOT NULL ); / INSERT INTO ProductTable VALUES(299,'Chest',99.99,10); INSERT INTO ProductTable VALUES(300,'Wave Cruiser',49.99,11); INSERT INTO ProductTable VALUES(301,'Megaland Play Tent',59.99,11); INSERT INTO...
A9-75 in this activity, you will be creating budgets for a single product for each of...
A9-75 in this activity, you will be creating budgets for a single product for each of the months in an upcoming quarter. Select a product that you could purchase in large quantities (at a Sam's Club or other warehouse retail chain) and repackage into smaller quantities to offer for sale at a sidealk cafe, a sporting event, a flea market, or other similar venue. Investigate the price a quantity at which this product is available at the warehouse. Choose a...
choose a product for which you are interested for which you are interested in creating a...
choose a product for which you are interested for which you are interested in creating a marketing plan. It can be a product that already exists, or one you would like to launch. (hair oil)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT