Question

In: Computer Science

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 dispense the candies in opposite orders like a pez dispense

Solutions

Expert Solution

This program actually models a stack. Modeling stacks in Perl is pretty easy. The push and pop methods will work fine with the ordinary array decalred with '@'.

The program is given below.

#!/usr/bin/perl

my $choice = 1;
my @pez;
my @count = (1..10);
my $cand;
while($choice>0){
    print "1.Take a Candy\n";
    print "2.View Candies\n";
    print "Enter 0 to quit program \n";
    $choice = <>;
    if($choice == 1){
        $num = @pez;
        if($num == 0){
            print "Empty pez, Add 10 candies\n";
            for(@count){
                print "Enter candy:";
                $cand = <>;
                chomp( $cand );
                push(@pez,$cand);
            }
            print "Pez Dispenser full\n";
            print "Pez Dispenser Contents:\n";
            print "@pez\n";
          
        }
        else{
        $candy = pop(@pez);
        print "Popped candy: $candy\n";
        print "Remaining candies in pez dispenser are:@pez\n";
        }
    }

    elsif($choice == 2){
        print "The candies in pez dispenser are:\n";
        print "@pez\n";
      
    }
}

Screenshots of the code

Screenshot of Output

First when you try to take a candy (option 1), it will promt youn to add 10 candies because the dispenser empty first. After that you can take one candy at a time and view the dispenser as needed. Enter 0 to quit the program.


Related Solutions

MUST USE LOOPS. Write a perl program that replicates the actions of a pez dispenser. The...
MUST USE LOOPS. 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...
Sweet Tooth Inc has a thriving PEZ dispenser business. Most of their revenue comes from the...
Sweet Tooth Inc has a thriving PEZ dispenser business. Most of their revenue comes from the variety of refill flavours they offer. Sweet tooth orders Pez refills by the case. Each case contains 50 packages of Pez Refills and each package contains 10 pez candies. They sell each package for $2 each. They have found that they sell on average 50,000 candies per month. All their orders are phoned in, in the interest of speed and efficiency. They have found...
Write a program that uses nested for loops to print a multiplication table. Your program should...
Write a program that uses nested for loops to print a multiplication table. Your program should print out the multiplication table starting a 1 and going to 12. Output should be like: 1 x 1 = .1 x 2 = 2
Write a program, circleref.cpp, that inputs a double radius of circle. It should print, given this...
Write a program, circleref.cpp, that inputs a double radius of circle. It should print, given this radius, the circumference of the circle, the area of the circle, the surface area of a sphere with that radius and the area of a sphere with that radius. Each of its computation functions returns its answers in call by reference parameters. Here are the formulas for computation: Circumference = 2 • π • r Circle Area = π • r2 Sphere Surface Area...
c++ Write a program that print stars, Max and Min values. It should use the following...
c++ Write a program that print stars, Max and Min values. It should use the following functions: (2 pts) int getNum ( ) should ask the user for a number and return the number. This function should be called by main once for each number to be entered. Input Validation: Do not accept numbers less than -100. (2 pts) void printStars ( int n ) should print n number of stars. If n is less than 0, display "Invalid" message...
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. Asks the user to enter...
Using Perl language: Write a program that reads a string from the standard input, and uses...
Using Perl language: Write a program that reads a string from the standard input, and uses a Perl regular expression to test whether the string looks like a valid credit card number. The card numbers of four major companies follow the following formats: Visa 13 or 16 digits, starting with 4. MasterCard 16 digits, starting with 51 through 55. Discover 16 digits, starting with 6011 or 65. American Express 15 digits, starting with 34 or 37. You should test your...
Write a program in PERL language that does the following: a. Generates 100 random numbers between...
Write a program in PERL language that does the following: a. Generates 100 random numbers between -17 and +31. b. Calculates the average of the square of the generated numbers using a function that you implement. c. Calculates the number of numbers greater than the average using a function that you implement. d. Prints the results single line separated by spaces using a print function that makes call to the previously defined functions in parts b) and c).
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...
write a program to calculate and print payslips write program that calculates and prints payslips. User...
write a program to calculate and print payslips write program that calculates and prints payslips. User inputs are the name of employee, numbers of hours worked and hourly rate c++ language
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT