-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMy_inventory.lua
More file actions
78 lines (66 loc) · 2.18 KB
/
Copy pathMy_inventory.lua
File metadata and controls
78 lines (66 loc) · 2.18 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
66
67
68
69
70
71
72
73
74
75
76
77
78
function isWeight(max, current, item)
current = current + item
if current > max then
print("The object is too heavy.")
return false
else
return true
end
end
function createInventory(maxWeight)
local Inventory = {}
local MaxWeight = maxWeight
local weight = 0
local items = {}
Inventory.addItem =function(item)
if type(item) =="table" and item.name ~= nil and item.weight ~= nil and type(item.name) =="string" and type(item.weight) == "number" and item.weight > 0 then
if isWeight(MaxWeight, weight, item.weight) == true then
weight = weight + item.weight
table.insert(items, item)
return weight
end
else
print("ERROR : incorect value !!")
return -1
end
end
Inventory.removeItem = function(itemName)
if #items == 0 then
print("The inventory is empty")
return 0
elseif type(itemName) == "string" and itemName ~= nil then
for index = #items, 1, -1 do
if items[index].name == itemName then
local removedWeight = items[index].weight
table.remove(items, index)
weight = weight - removedWeight
end
end
else
print("ERROR : incorect value !!")
return -1
end
return weight
end
Inventory.getTotalWeight = function()
return weight
end
Inventory.listItems = function()
if #items == 0 then
print("The inventory is empty")
else
for index, item in ipairs(items) do
print ("Name object : "..item.name.." The weight of the object : " ..item.weight)
end
end
end
return Inventory
end
local inventory = createInventory(100)
inventory.addItem({name="epee", weight=5})
inventory.addItem({name="eau", weight=10})
inventory.addItem({name="nouriture", weight=30})
inventory.addItem({name="jsp", weight=9})
inventory.listItems()
print("Total : "..inventory.getTotalWeight())
print(inventory.removeItem("epee"))