By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,852 Members | 1,426 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,852 IT Pros & Developers. It's quick & easy.

how to simulate a vending machine using perl?

P: 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
Share this Question
Share on Google+
2 Replies


eWish
Expert 100+
P: 971
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
Expert 2.5K+
P: 4,059
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

Post your reply

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