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

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, 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...
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 that uses a for loop to print One of the months of the...
Write a program that uses a for loop to print One of the months of the year is January One of the months of the year is February ...
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.
Write a complete Java program to print out the name bob
Write a complete Java program to print out the name bob
Write an assembly language program that will print out the message of your choosing #NOTE #write...
Write an assembly language program that will print out the message of your choosing #NOTE #write in a simple way, so that i can execute it from command window using masm
Using the IntelliJ IDEA tool, write a program that will print a Christmas tree with the...
Using the IntelliJ IDEA tool, write a program that will print a Christmas tree with the "*". The tree should have at least 10 lines. Look back at the birthday cake lab for examples of how to do this lab. Submit a screen shot of your output in the console (i.e. non REPL tool).
Write a java program that will take a line of input and go through and print...
Write a java program that will take a line of input and go through and print out that line again with all the word numbers swapped with their corresponding numeric representations (only deal with numbers from one to nine). Sample runs might look like this: Please enter a line of input to process: My four Grandparents had five grandchildren My 4 grandparents had 5 grandchildren without array and methods.
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT