473,408 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

how to simulate a vending machine using perl?

4
Hello Friends,

I was trying to simulate a vending machine using perl just as a fun project.

Heres the code I wrote:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. $wt = $ARGV[0];
  4.  
  5. %vm = (
  6.         M125 => "candy",
  7.         M200 => "cookie",
  8.         M300 => "chips"
  9.       );
  10.  
  11. @item_requested=""; # initializing null array
  12. # using weight sensors sense wait and is provided as input to the script
  13.  
  14. if ($wt==100)
  15. {
  16.     @item_requested=$vm{"M125"};
  17. }
  18. elsif ($wt==200)
  19. {
  20.     @item_requested=$vm{"M200"};
  21. }
  22. elsif ($wt==300)
  23. {
  24.     @item_requested=$vm{"M300"};
  25. }
  26.  
  27. $requested_commodity=shift(@item_requested); # outputs first element
  28. print"Ok, I had requested for ${requested_commodity} \n";
Now its doing what it is suppose to. But in the script I am giving weight externally and then script finds relevent commodity. By weight I mean the weight of money. So for ex if it weighs some "x" amount then it corresponds to item N in my hash table. Then it pushes the item in array and outputs it. But, I was trying to modify the criteria to make it more solid.

Any suggestion/help is very appreciated.

Thanks!
Feb 29 '08 #1
2 1849
eWish
971 Expert 512MB
I was trying to modify the criteria to make it more solid.
What additional criteria would you like script to use? You could modify your conditionals to need more information. But without knowing that were are unable to help.

--Kevin
Feb 29 '08 #2
KevinADC
4,059 Expert 2GB
This could go anywhere, but here is another take on your vending machine:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. my $wt = $ARGV[0]; 
  6.  
  7. my %vm = (
  8.         M125 => "candy",
  9.         M200 => "cookie",
  10.         M300 => "chips"
  11. );
  12.  
  13. my @purchased;
  14. push @purchased, exists $vm{$wt} ? "You purchased $vm{$wt}" : "$vm{$wt} is sold out";
  15. foreach my $item (@purchased) {
  16.     print "$item\n";
  17. }
Fairly useless as-is, but you could loop through several purchases and keep adding them into the @purchased array, calculate change, decrement items from the machine after each purchase, all sorts of stuff. This could be a good excersize to start learning object oriented programming. The vending machine would be the object and you could make the stuff inside the machine attributes of the object and have methods to purchase stuff and so on.
Feb 29 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: pradeepkumar | last post by:
give me the code for vending machine in c++. it has 10 items in a .first display all the items and its cost and its quantityonhand.then ask the customer if he wishes to buy.if yes tell him to select...
0
by: joestevens232 | last post by:
I am seriously stuck and have been working on this for hours and hours and can't figure out my next step....heres the program assignment. This vending machine dispenses 1. M&Ms ($.65), 2. Chips...
9
by: vpascuzzi | last post by:
Here's the deal: I've been working on this little program forever now, and can't seem to get the final little glitches out of it. I am to build a vending machine, using 2 header .h files (one...
7
by: JohnSmith70 | last post by:
Please help I need help with this homework. Its about vending machines, and it should be a piece of cake to you code experts out there. Its about vending machines. Heres the question: It...
1
by: Kunthea | last post by:
I am not that good at programming and I need a little push on how I can start my programming on the vending machine. The basic of the homework is that it displays four snacks and corresponding...
3
by: ismaeel | last post by:
i very need for intrfase of vending machine for netbense sending to email plese
1
by: abil | last post by:
i've already build a program that contain all the price, the change given back to the customer but i dun have no idea which function i should use to do program print report... #include <cstdlib>...
3
by: exospire | last post by:
Hello everyone, I made a program that simulates a vending machine for class. I finally got it to execute without any errors, yet something is not right. I have functions that are being called upon...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.