how to simulate a vending machine using perl?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nb999
    New Member
    • Jan 2008
    • 4

    how to simulate a vending machine using perl?

    Hello Friends,

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

    Heres the code I wrote:

    [CODE=perl]#!/usr/bin/perl

    $wt = $ARGV[0];

    %vm = (
    M125 => "candy",
    M200 => "cookie",
    M300 => "chips"
    );

    @item_requested =""; # initializing null array
    # using weight sensors sense wait and is provided as input to the script

    if ($wt==100)
    {
    @item_requested =$vm{"M125"};
    }
    elsif ($wt==200)
    {
    @item_requested =$vm{"M200"};
    }
    elsif ($wt==300)
    {
    @item_requested =$vm{"M300"};
    }

    $requested_comm odity=shift(@it em_requested); # outputs first element
    print"Ok, I had requested for ${requested_com modity} \n";[/CODE]

    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!
    Last edited by eWish; Feb 29 '08, 12:44 AM. Reason: Please use [CODE][/CODE] tags
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    Originally posted by nb999
    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

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      This could go anywhere, but here is another take on your vending machine:

      [CODE=perl]#!/usr/bin/perl
      use strict;
      use warnings;

      my $wt = $ARGV[0];

      my %vm = (
      M125 => "candy",
      M200 => "cookie",
      M300 => "chips"
      );

      my @purchased;
      push @purchased, exists $vm{$wt} ? "You purchased $vm{$wt}" : "$vm{$wt} is sold out";
      foreach my $item (@purchased) {
      print "$item\n";
      }[/CODE]

      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.

      Comment

      Working...