Vending Machine

-

The following code implements a simulated vending machine. You enter coins by clicking buttons (representing coin detector circuits). The merchandise available at or below the sum entered is displayed. You can click one of the items to have it delivered, and your cash-left is updated. At any time you can have your cash returned. There are obvious further checks and 'bells and whistles' that could be added.

NB You need Liberty BASIC or Just BASIC installed, and the three bitmaps, available as vend.zip


    '   Stimulated by the discussions on how much to help students!

    '   To-dos:
    '       Add the routine to give back change and check reserve of coins
    '       Have a finite number of each can in the machine.
    '       Sound effects?

    nomainwin

    Pepsi           = 072
    Coke            = 075
    Sprite          = 060

    pepsiNum        =0

    availableCash   =0

    numHD           =0
    numQu           =0
    numDi           =0
    numNi           =0
    numPe           =0

    button     #w.HD, "+50 cents", [HD],   LR, 340, 250
    button     #w.Qu, "+25 cents", [Qu],   LR, 340, 200
    button     #w.Di, "+10 cents", [Di],   LR, 340, 150
    button     #w.Ni, "+ 5 cents", [Ni],   LR, 340, 100
    button     #w.Pe, "+ 1 cents", [Pe],   LR, 340,  50
    button     #w.Ch, "Done",      [Done], LR, 125, 110

    groupbox   #w.gb1, "Coin entry",        50,  20, 140, 330
    groupbox   #w.gb2, "Affordable items", 200,  20, 220, 150

    textbox    #w.tb1,  60,  40, 100, 40

    texteditor #w.te1, 200, 270, 200, 80

    statictext #w.stHD, str$( numHD), 160, 102,  20, 30
    statictext #w.stQu, str$( numQu), 160, 152,  20, 30
    statictext #w.stDi, str$( numDi), 160, 202,  20, 30
    statictext #w.stNi, str$( numNi), 160, 252,  20, 30
    statictext #w.stPe, str$( numPe), 160, 302,  20, 30
    statictext #w.stPP, "Possible purchases with these coins inserted will appear below..", 210,  38, 200,  30
    statictext #w.stTx, "Keep ading coins until at least one object can be afforded."; chr$( 13);_
        "Then click its image to purchase."; chr$( 13);_
        "Click 'Done' to return your change.",      210, 180, 200, 60

    bmpbutton  #w.pepsi,  "pepsi.bmp",  [pepsiBought],   LR, 150, 200
    bmpbutton  #w.coke,   "coke.bmp",   [cokeBought],    LR, 100, 200
    bmpbutton  #w.sprite, "sprite.bmp", [spriteBought],  LR,  50, 200

    WindowWidth  =440
    WindowHeight =420

    open "Vending Machine" for window as #w

    #w        "trapclose [quit]"
    #w        "font arial 8"
    #w.tb1    "!font arial 24 bold"
    #w.te1    "Returned coin list appears here."
    #w.te1    "!font arial 8"
    #w.tb1    "Nil"
    #w.pepsi  "hide"
    #w.coke   "hide"
    #w.sprite "hide"

    wait

  [Done]
    #w.te1 "!cls"
    #w.te1 "Return "; availableCash; " cents."
    #w.tb1 "Nil"
    availableCash =0
    #w.pepsi  "hide"
    #w.coke   "hide"
    #w.sprite "hide"
    wait

  [whatsOn]
    #w.tb1 availableCash; "c"
    if availableCash >=Pepsi  then #w.pepsi  "show" else #w.pepsi  "hide"
    if availableCash >=Coke   then #w.coke   "show" else #w.coke   "hide"
    if availableCash >=Sprite then #w.sprite "show" else #w.sprite "hide"
    wait

  [pepsiBought]
    availableCash =availableCash -Pepsi
    goto [whatsOn]

  [cokeBought]
    availableCash =availableCash -Coke
    goto [whatsOn]

  [spriteBought]
    availableCash =availableCash -Sprite
    goto [whatsOn]

  [HD]
    availableCash =availableCash +50
    #w.tb1 availableCash
    numHD =numHD +1
    #w.stHD, str$( numHD)
    goto [whatsOn]

  [Qu]
    availableCash =availableCash +25
    #w.tb1 availableCash
    numQu =numQu +1
    #w.stQu, str$( numQu)
    goto [whatsOn]

  [Di]
    availableCash =availableCash +10
    #w.tb1 availableCash
    numDi =numDiD +1
    #w.stDi, str$( numDi)
    goto [whatsOn]

  [Ni]
    availableCash =availableCash + 5
    #w.tb1 availableCash
    numNi =numNi +1
    #w.stNi, str$( numNi)
    goto [whatsOn]

  [Pe]
    availableCash =availableCash + 1
    #w.tb1 availableCash
    numPe =numPe +1
    #w.stPe, str$( numPe)
    goto [whatsOn]


  [quit]
    close #w
    end