In: Computer Science
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 should load and dispense the candies in opposite orders like a pez dispenser.
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";
}
}
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.