-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ_015_50xp_discounted_inventory.nim
More file actions
64 lines (45 loc) · 1.34 KB
/
Q_015_50xp_discounted_inventory.nim
File metadata and controls
64 lines (45 loc) · 1.34 KB
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
58
59
60
61
62
63
64
import strformat
import strutils
import helpers
type
Product = object
name: string
price: int
proc initProduct(name: string, price: int): Product =
Product(name: name, price: price)
const
discountUsername = "kevin"
discount = 0.5
inventory: array[1..7, Product] = [
initProduct("Rope", 10),
initProduct("Torches", 15),
initProduct("Climbing Equipment", 25),
initProduct("Clean water", 1),
initProduct("Machete", 20),
initProduct("Canoe", 200),
initProduct("Food Supplies", 1),
]
proc getPriceOf(product: Product, forUser: string): float =
let username = forUser
let price_modifier = if username.toLower == discountUsername : discount else: 1
result = product.price.toFloat * price_modifier
proc showInventory() =
for index, product in inventory:
echo index, " -- ", product.name
proc showCost(product:Product, cost: float) =
echo fmt"The cost of {product.name} is {cost} gold"
proc askForProduct(): Product =
let menu_choice = askForIntInRange(
"What number do you want to see the price of",
low(inventory),
high(inventory)
)
result = inventory[menu_choice]
proc main() =
let username = askForInput("Enter your name")
showInventory()
let product = askForProduct()
let price = getPriceOf(product, username)
showCost(product, price)
when isMainModule:
main()