Question

In: Computer Science

WRITE IN PERL Creating a Perl Product Directory - A product "id" will be used to...

WRITE IN PERL

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

Here is a template:

#!/usr/bin/perl
use warnings;

%products = (); # empty hash to store products in

sub getChoice(){
print("Welcome to the Product Directory\n");
print("1. Add a Product to Directory\n");
print("2. Remove a Product from Director\n");
print("3. Lookup a Product in Directory\n");
print("4. List all products\n");
print("5. Quit\n");
print("Enter your option: ")
chomp($choice = <STDIN>);
return $choice
}

$c = getChoice();
while($c ne "5"){
if($c eq "1"){
# Ask the user to enter input for a product's ID
# Ask the user to enter input for a product's description
# Add the item to the hash with the supplied ID as the key and description as the value
}
  
# Options 2 - 4
}

Solutions

Expert Solution

Please find the below program. Also attaching the screenshot of sample output.

Please provide your feedback

Thanks and Happy Learning!

Sample output:

#!/usr/bin/perl
use warnings;

%products = (); # empty hash to store products in

sub getChoice()
{
    print("Welcome to the Product Directory\n");
    print("1. Add a Product to Directory\n");
    print("2. Remove a Product from Directory\n");
    print("3. Lookup a Product in Directory\n");
    print("4. List all products\n");
    print("5. Quit\n");
    print("Enter your option: ");
    chomp($choice = <STDIN>);
    return $choice
}

$c = getChoice();
while($c ne "5")
{
    if($c eq "1")
    {
        # Ask the user to enter input for a product's ID
        print("Enter the product ID: ");
        chomp($productId = <STDIN>);
        # Ask the user to enter input for a product's description
        print("Enter the product description: ");
        chomp($productDesc = <STDIN>);
        # Add the item to the hash with the supplied ID as the key and description as the value
        $products{$productId} = $productDesc;
    }
    elsif($c eq "2")
    {
        # Ask the user to enter input for a product's ID
        print("Enter the product ID: ");
        chomp($productId = <STDIN>);
        
        # If the ID exists in the hash, that entry is deleted from the hash
        if (exists($products{$productId}))
        {
            delete($products{$productId});
        }
    }
    elsif($c eq "3")
    {
        # Ask the user to enter input for a product's ID
        print("Enter the product ID: ");
        chomp($productId = <STDIN>);
        
        # If the ID exists in the hash, that entry's value is printed (the description)
        if (exists($products{$productId}))
        {
            print("$products{$productId}\n");
        }
    }
    elsif($c eq "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
        while (($key, $value) = each (%products))
        {
          print "$key:$value\n";
        }
    }

    $c = getChoice();
}

Related Solutions

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. Asks the user to enter a product ID,...
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...
Perl Programming Please write a simple to understand Perl script that computes compound interest, based on...
Perl Programming Please write a simple to understand Perl script that computes compound interest, based on input from user for P, n, r and t.
Perl Programming Write a Perl script that computes compound interest balance, A, based on input from...
Perl Programming Write a Perl script that computes compound interest balance, A, based on input from user for P, n, r and t.
Write a program in c++ that maintains a telephone directory. The Telephone directory keeps records of...
Write a program in c++ that maintains a telephone directory. The Telephone directory keeps records of people’s names and the corresponding phone numbers. The program should read a transaction file and report the result into an output file. The transaction file can include commands such as “Add”, “Delete”, “Display”, and “Update”, and “Search”. Each command is written in one line with a few other information. The “Display” Command should simply display everyone in the directory on the screen The “Add”...
write a sql statement that retrieves product ID (ProductID) and name (Name) from Production.product table for...
write a sql statement that retrieves product ID (ProductID) and name (Name) from Production.product table for all product whose name includes both the words "silver" and "frame"
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...
Write a script that prompts the user for a pathname of a file or directory and...
Write a script that prompts the user for a pathname of a file or directory and then responds with a description of the item as a file along with what the user's permissions are with respect to it (read, write, execute).
Assisted Instruction: Has to be in PERL SCRIPTING LANGUAGE with NO SUBROUTINES Programming Exercise: Write a...
Assisted Instruction: Has to be in PERL SCRIPTING LANGUAGE with NO SUBROUTINES Programming Exercise: Write a program in PERL with NO SUBROUTINES to allow the user to pick a type of arithmetic problem to study. An option of 1 means addition problems only, 2 means subtraction problems only, 3 means multiplication problems only, 4 means division problems only and 5 means random mixture of all these types. (Computer- Assisted Instruction) The use of computers in education is referred to as...
Write a perl program that replicates the actions of a pez dispenser. The program should print...
Write a perl program that replicates the actions of a pez dispenser. The program should print out the contents of the dispenser showing that it is empty, prompt a user for 10 pez candy flavors/colors, print out the contents of the dispenser showing that it is full, dispense the candies one at a time, and then print out the contents of the dispenser showing that it is empty. The dispenser should only take in 10 candies and should load and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT