-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_membership.py
207 lines (149 loc) · 6.85 KB
/
module_membership.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from tabulate import tabulate
class Membership:
"""
A class for system of self-service cashier.
Attributes
----------
user_data (dict):
collection of username and its membership
username (str):
input name of user
Methods
-------
show_benefit():
Function to display all membership benefit (table form)
show_requirements():
Function to display all requirements to subscribe membership (table form)
predict_membership(monthly_expense, monthly_income):
Function to predict membership based on euclidean distance
show_membership(username):
Function to show membership based on username
calculate_price(membership, list_harga_barang):
Function to calculate final price and discount price based on membership
"""
user_data = {'Sumbul':'Platinum',
'Ana':'Gold',
'Cahya':'Platinum'
}
def __init__(self,username: str):
self.username = username.title()
def show_benefit(self):
"""
Function to display all membership benefit (table form)
Parameters
----------
None
Returns
-------
None
"""
headers = ['Membership','Disount','Another Benefit']
list_membership = ['Platinum','Gold','Silver']
list_discount = ['15%','10%','8%']
list_benefit = ['Benefit Silver + Gold + Voucher Liburan + Cashback max. 30%',
'Benefit Silver + Voucher Ojek Online',
'Voucher Makanan']
table_content = [[membership,discount,benefit] for membership,discount,benefit in \
zip(list_membership,list_discount,list_benefit)]
print("------------------------------------------------------------------------------------------")
print(tabulate(table_content,headers, tablefmt="github",colalign=('center','center','center'),stralign=('center')))
print("------------------------------------------------------------------------------------------")
def show_requirements(self):
"""
Function to display all requirements to subscribe membership (table form)
Parameters
----------
None
Returns
-------
None
"""
headers = ['Membership','Monthly Expense (juta)','Monthly Income (juta)']
list_membership = ['Platinum','Gold','Silver']
list_monthly_expense = [8,6,5]
list_monthly_income = [15,10,7]
table_content = [[membership,monthly_expense,monthly_income] for membership,monthly_expense,monthly_income in \
zip(list_membership,list_monthly_expense,list_monthly_income)]
print("---------------------------------------------------------------------")
print(tabulate(table_content,headers, tablefmt="github",colalign=('center','center','center'),numalign=('center')))
print("---------------------------------------------------------------------")
def predict_membership(self,monthly_expense: float|int, monthly_income: float|int):
"""
Function to predict membership based on euclidean distance
Parameters
----------
monthly_expense (float/int): total cost for shopping per month
monthly_income (float/int): total earning per month
Returns
-------
None
"""
self.monthly_expense = monthly_expense
self.monthly_income = monthly_income
list_monthly_expense = [8,6,5]
list_monthly_income = [15,10,7]
list_membership = ['Platinum','Gold','Silver']
distance = []
dict_membership = {}
for i in range(0,len(list(zip(list_monthly_expense,list_monthly_income)))):
distance_1 = (self.monthly_expense - list_monthly_expense[i])**2
distance_2 = (self.monthly_income - list_monthly_income[i])**2
distance.append(round((distance_1 + distance_2)**(1/2),2))
for i in range(0,len(list(zip(distance,list_membership)))):
dict_membership.update({list_membership[i]:distance[i]})
print(f"Hasil perhitungan Euclidean Distance dari user {self.username} adalah {dict_membership}")
tier_membership = [key for index,(key,value) in enumerate(dict_membership.items()) if value<=min(list(dict_membership.values()))]
print()
print(f"'{tier_membership[0]}'")
Membership.user_data.update({self.username:f"{tier_membership[0]}"})
def show_membership(self,username: str) -> str:
"""
Function to show membership based on username
Parameters
----------
username (str): input username
Returns
-------
None
"""
self.username = username
if self.username in list(Membership.user_data.keys()):
return f"{str(Membership.user_data[self.username])}"
else:
return f"Username tidak ditemukan"
def calculate_price(self,membership: str, list_harga_barang: list):
"""
Function to calculate final price and discount price based on membership
Parameters
----------
membership (str): input tier membership based on username in user data
list_harga_barang (list): collection of item price, can be integer/float
Returns
-------
None
"""
self.list_harga_barang = list_harga_barang
self.membership = membership.title()
list_discount = [15/100,10/100,8/100]
try:
if self.username in list(Membership.user_data.keys()):
if self.membership in list(Membership.user_data.values()):
total_price = sum(self.list_harga_barang)
if self.membership == 'Platinum':
total_price = total_price - list_discount[0]*total_price
print(f"Total belanja yang dibayarkan adalah: Rp {int(total_price)}")
elif self.membership == 'Gold':
total_price = total_price - list_discount[1]*total_price
print(f"Total belanja yang dibayarkan adalah: Rp {int(total_price)}")
elif self.membership == 'Silver':
total_price = total_price - list_discount[2]*total_price
print(f"Total belanja yang dibayarkan adalah: Rp {int(total_price)}")
elif self.membership not in list(Membership.user_data.values()):
print("Membership tidak ditemukan")
elif self.username not in list(Membership.user_data.keys()):
print("Username tidak ditemukan")
except:
print("Input data anda salah")
finally:
print()
print("Terima kasih!")