-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path3-tier.lua
95 lines (76 loc) · 2.16 KB
/
3-tier.lua
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
require('_class')
-- Data access layer
local Data = class()
function Data:ctor()
self.products = {
milk = {price = 1.50, quantity = 10},
eggs = {price = 0.20, quantity = 100},
cheese = {price = 2.00, quantity = 10}
}
end
function Data:get(key, default)
print()
print("(Fetching from Data Store)")
return self.products[key] or default
end
-- Business Logic Layer
local BusinessLogic = class()
function BusinessLogic:ctor()
self.data = Data.new()
end
function BusinessLogic:product_list()
if not self.product_list_ then
self.product_list_ = {}
for key, value in pairs(self.data['products']) do
table.insert(self.product_list_, key)
end
end
return self.product_list_
end
function BusinessLogic:product_information(product)
return self.data:get(product, nil)
end
-- User Interface layer
local UI = class()
function UI:ctor()
self.business_logic = BusinessLogic.new()
end
function UI:get_product_list()
print('PRODUCT LIST:')
for _, product in ipairs(self.business_logic:product_list()) do
print(product)
end
end
function UI:get_product_information(product)
local product_info = self.business_logic:product_information(product)
if product_info then
print('PRODUCT INFORMATION:')
print(string.format('Name: %s, Price: %.2f, Quantity: %d', product, product_info.price, product_info.quantity))
else
print(string.format('That product "%s" does not exist in the records', product))
end
end
local ui = UI.new()
ui:get_product_list()
ui:get_product_information('cheese')
ui:get_product_information('eggs')
ui:get_product_information('milk')
ui:get_product_information('arepas')
---------------------------------------------------------------------------
-- OUTPUT
---------------------------------------------------------------------------
-- PRODUCT LIST:
-- eggs
-- cheese
-- milk
-- (Fetching from Data Store)
-- PRODUCT INFORMATION:
-- Name: cheese, Price: 2.00, Quantity: 10
-- (Fetching from Data Store)
-- PRODUCT INFORMATION:
-- Name: eggs, Price: 0.20, Quantity: 100
-- (Fetching from Data Store)
-- PRODUCT INFORMATION:
-- Name: milk, Price: 1.50, Quantity: 10
-- (Fetching from Data Store)
-- That product "arepas" does not exist in the records