Skip to content

Instantly share code, notes, and snippets.

@Shigetorum635
Created May 26, 2022 07:17
Show Gist options
  • Save Shigetorum635/46b79fd208ce71be9c5ae26589fa340a to your computer and use it in GitHub Desktop.
Save Shigetorum635/46b79fd208ce71be9c5ae26589fa340a to your computer and use it in GitHub Desktop.
A small vending machine made in Nim.
type Drink = tuple[drinkName: string, liters: int, price: int]
type
VendingMachine = object
name: string
drinks: seq[Drink]
money: int
var pareton : ref VendingMachine = new(VendingMachine)
pareton.name = "Pareton"
pareton.drinks = @[("Aquarius", 2, 1)]
pareton.money = 0
proc buyDrink*(machine: var ref VendingMachine, drink: string) =
var found: bool = false
var tries: int = 0
while(found == false and tries <= 3):
if(tries == 3):
echo drink," not found in the machine. Sorry!"
break
for drinks in machine.drinks:
tries += 1
if drinks.drinkName == drink:
found = true
machine.money = machine.money + drinks.price
echo "Bought ", drink, " machine now has $", machine.money
break
buyDrink(pareton, "Aquarius")
buyDrink(pareton, "CocaCola")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment