-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
vending_machine_demo.go
57 lines (43 loc) · 1.25 KB
/
vending_machine_demo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package vending_machine
import "fmt"
// Run demonstrates the Vending Machine functionality.
func Run() {
// Initialize the vending machine
vm := NewVendingMachine()
// Create some products
coke := NewProduct("Coke", 1.5)
pepsi := NewProduct("Pepsi", 1.5)
water := NewProduct("Water", 1.0)
// Add products to the inventory
vm.inventory.AddProduct(coke, 5)
vm.inventory.AddProduct(pepsi, 3)
vm.inventory.AddProduct(water, 2)
// Demonstrate a transaction
fmt.Println("Starting Vending Machine Demo")
fmt.Println("\nSelecting Coke")
vm.SelectProduct(coke)
fmt.Println("Inserting coins")
vm.InsertCoin(QUARTER)
vm.InsertCoin(QUARTER)
vm.InsertCoin(QUARTER)
vm.InsertCoin(QUARTER)
fmt.Println("Dispensing product")
vm.DispenseProduct()
fmt.Println("Returning change")
vm.ReturnChange()
// Another example with insufficient funds
fmt.Println("\nSelecting Pepsi with insufficient funds")
vm.SelectProduct(pepsi)
vm.InsertCoin(QUARTER)
fmt.Println("Trying to dispense Pepsi")
vm.DispenseProduct()
fmt.Println("Adding more coins for Pepsi")
vm.InsertCoin(QUARTER)
vm.InsertCoin(QUARTER)
vm.InsertCoin(QUARTER)
vm.InsertCoin(QUARTER)
fmt.Println("Dispensing product")
vm.DispenseProduct()
fmt.Println("Returning change")
vm.ReturnChange()
}