-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMy_market.lua
More file actions
65 lines (54 loc) · 1.62 KB
/
Copy pathMy_market.lua
File metadata and controls
65 lines (54 loc) · 1.62 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
65
function createCart()
-- Liste interne des articles du panier
local items = {}
-- Objet panier exposé à l'extérieur
local cart = {}
cart.addItem = function(name, price)
-- On accepte seulement un nom texte et un prix numérique
if name ~= nil and price ~= nil and type(name) == "string" and type(price) == "number" then
table.insert(items, {Name = name, Price = price})
else
print("Article inccorect")
end
end
cart.getTotal = function()
-- Additionne le prix de tous les articles du panier
local total = 0
if (items ~= nil) then
for index, item in ipairs(items) do
total = item.Price + total
end
end
return total
end
cart.getList = function()
-- Affiche le contenu du panier, ou un message si la liste est vide
if (#items == 0) then
print("La list est vide")
else
for index, item in ipairs(items) do
print(index .." - ".. item.Name .." coûte ".. item.Price .. " $")
end
end
return #items
end
cart.clearList = function()
-- Vide complètement le panier
items = {}
end
return cart
end
cart = createCart()
cart.addItem("Pain", 1)
cart.addItem("Bonbon", 115)
cart.addItem("iphone", "test")
cart.addItem("biscuit", 8.50)
cart.addItem("salade", 5)
cart.addItem("jsp", 67)
cart.getList()
total = cart.getTotal()
print("La note " ..total.." $")
cart.clearList()
cart.getList()
total = cart.getTotal()
print("La note " ..total.." $")