Skip to content

Instantly share code, notes, and snippets.

@mcustiel
Created December 13, 2022 21:17
Show Gist options
  • Save mcustiel/dfa1291386e25c332b4bd4d1e3c90475 to your computer and use it in GitHub Desktop.
Save mcustiel/dfa1291386e25c332b4bd4d1e3c90475 to your computer and use it in GitHub Desktop.
Simple ATM Logic in Golang
package main
import "fmt"
type AtmValue struct {
Val int
Count int
}
type ATM []AtmValue
type Result map[int]int
func main() {
var atm ATM = ATM{
{10, 100},
// {20, 80},
{20, 1},
{50, 50},
{100, 10},
}
var amount int
var banknoteIndex int = 3
var result Result = make(Result)
var atmValue AtmValue
amount = 190
for amount > 0 && banknoteIndex >= 0 {
fmt.Printf("Current amount is %d. Banknote: %d\n", amount, banknoteIndex)
atmValue = atm[banknoteIndex]
for atmValue.Count > 0 && amount >= atmValue.Val {
fmt.Printf("Discounting %d\n", atmValue.Val)
amount -= atmValue.Val
result[atmValue.Val]++
atm[banknoteIndex].Count--
}
banknoteIndex--
}
fmt.Printf("\nMoney: %+v.\nATM: %+v\n", result, atm)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment