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, 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...
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.
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 complete Java program to print out the name bob
Write a complete Java program to print out the name bob
I need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT