-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrental_oke.py
464 lines (421 loc) · 15.4 KB
/
rental_oke.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# Install librari yg diperlukan apabila belum terinstall
try:
import matplotlib.pyplot as plt
except ImportError:
import os
os.system('pip install matplotlib')
import matplotlib.pyplot as plt
try:
from prettytable import PrettyTable
except ImportError:
import os
os.system('pip install prettytable')
from prettytable import PrettyTable
from datetime import datetime
import random
import string
now = datetime.now()
# Kembali ke Menu Utama
def back():
input('\nTekan ENTER untuk kembali ke menu utama...')
menu()
# Mengubah file vehicle.txt
def modify(tf, mod):
if tf:
with open(r'Vehicle.txt', 'w+') as rew:
for w in mod:
rew.write(w)
# Menampilkan mobil yang tersedia untuk disewakan
def displayCars(u):
table = PrettyTable(['Nomor Kendaraan', 'Model Kendaraan',
'Type (Ordinary/Premium)', 'Free mileage (km)', 'Daily rent (€)'])
with open(r'Vehicle.txt', 'r') as f1:
cars1 = f1.readlines()
for i in cars1:
i = i.split(',')
i = [x.replace('\n', "") for x in i]
if u == 'A':
if i[5] == 'A':
table.add_row([i[0], i[1], i[2], i[3], i[4]])
elif u == 'R':
if i[5] == 'R':
table.add_row([i[0], i[1], i[2], i[3], i[4]])
else:
print('\nInvalid choice.')
return
print(table)
# Menambah dan menghapus kendaraan
def Add_Vehicle():
lcpl = input('\nEnter Vehicle ID: ')
ehto = True
with open(r'Vehicle.txt', 'r') as fv:
vec = fv.readlines()
for vc in vec:
vc = vc.split(',')
if lcpl == vc[0]:
print('\nVehicle already exists in database.')
ehto = False
if ehto:
name = input('Enter Vehicle Name: ')
while True:
typ = input('Enter vehicle type (O/P): ')
if typ == 'P':
try:
mileage = float(
input("Enter Type-P Vehicle's mileage allowance: "))
except Exception:
print('\nInvalid choice.\n')
continue
else:
mileage = str(mileage)
break
elif typ == 'O':
mileage = '0'
break
else:
print('\nInvalid choice.\n')
continue
while True:
try:
dailyrent = float(input("Enter daily rent rate: "))
except Exception:
print('\nInvalid choice.\n')
continue
else:
dailyrent = str(dailyrent)
break
stat = input(
"Enter status (A/R, if choose R please add rent details to rentVehicle.txt): ")
with open(r'Vehicle.txt', 'a') as fa:
fa.write('\n'+lcpl + ','+name+','+typ + ',' +
mileage + ','+dailyrent+','+stat)
while True:
try:
con = input('\nDo you want to add another vehicle? (Y/N): ')
except Exception:
print("\nInvalid choice.")
continue
else:
if con == 'Y':
Add_Vehicle()
elif con == 'N':
menu()
return
else:
print("\nInvalid choice.")
continue
def Delete_Vehicle():
lcpl2 = input('\nEnter Vehicle ID: ')
with open(r'Vehicle.txt', 'r') as fd:
ved = fd.readlines()
for vd in ved:
vd = vd.split(',')
if lcpl2 == vd[0]:
vd = ','.join(vd)
with open(r'deletedVehicles.txt', 'a') as fr:
fr.write(vd+'\n')
pos = ved.index(vd)
del ved[pos]
modify(True, ved)
break
else:
print("\nVehicle does not exist.")
while True:
try:
con = input('\nDo you want to delete another vehicle? (Y/N): ')
except Exception:
print("\nInvalid choice.")
continue
else:
if con == 'Y':
Delete_Vehicle()
elif con == 'N':
menu()
return
else:
print("\nInvalid choice.")
continue
# Menambahkan detail rental ke dalam file
def rentDetails(path, app):
plate = app.split(',')
if path == 1:
with open(r'rentVehicle.txt', 'a') as q:
q.write(str(app))
print('\nCar '+plate[1]+' is rented successfully.')
elif path == 2:
with open(r'Transactions.txt', 'a') as p:
p.write(str(app))
print('\nCar '+plate[1]+' is returned successfully.')
# Membuat receipt ID dan melakukan cek apabila sudah dilakukan
def receiptID():
while True:
code = ''.join([random.choice(string.ascii_letters + string.digits)
for n in range(10)])
with open('rentVehicle.txt', 'r') as cd:
cdc = cd.readlines()
for cdi in cdc:
cdi = cdi.split(',')
if cdi[0] == code:
continue
else:
return code
# Melakukan cek Odometer
def odometer(way, qq):
if way == 1:
while True:
try:
vv = float(
input('Enter odometer reading at time of renting: '))
except Exception:
print('\nOdometer reading must be a float or an integer.\n')
continue
else:
return vv
elif way == 2:
while True:
try:
ww = float(
input('Enter odometer reading at time of returning: '))
except Exception:
print('\nOdometer reading must be a float or an integer.\n')
continue
else:
if (ww) < (qq):
print(
'\nEnd odometer value must be larger than initial odometer value.\n')
continue
else:
return ww
# Menambahkan accessories
def accessories():
while True:
paina = input('\nDo you want to add them? (Y/N): ')
if paina == 'Y':
return 20
elif paina == 'N':
return 0
else:
print("\nInvalid choice.")
continue
# Sewakan kendaraan
def rentVehicle(id1):
with open(r'Vehicle.txt', 'r') as f2:
cars2 = f2.readlines()
for j in cars2:
j = [i.rstrip() for i in j.split(',')]
if id1 == j[0]:
if j[5] == 'A':
rentID = input('Enter Renter ID: ')
odo = str(odometer(1, 0))
acc = 0
recid = receiptID()
if j[2] == 'P':
print(
'\nThe following accessories could be added to your vehicle for only €20:')
print('1. HD Dash Cam')
print('2. GPS Navigation')
print('3. Engine Heater')
acc = accessories()
print('\nCar '+j[0]+' is rented to '+rentID)
table1 = PrettyTable(['Receipt ID', recid])
table1.add_row(['Renter ID', rentID])
table1.add_row(['Vehicle ID', id1])
table1.add_row(['Description', j[1]])
table1.add_row(['Status', 'A -> R'])
table1.add_row(['Accessories', '€'+str(acc)])
table1.add_row(['Daily rate', '€'+j[4]])
table1.add_row(['Date/time of rent',
now.strftime("%d/%m/%Y %H:%M")])
table1.add_row(['Rent starting odometer', odo])
print(table1)
appnew = (recid+','+id1+','+rentID+',' +
now.strftime("%d/%m/%Y %H:%M") +
','+odo+','+str(acc)+'\n')
rentDetails(1, appnew)
j[5] = 'A\n'
dx = cars2.index(','.join(j))
j[5] = 'R\n'
j = ','.join(j)
cars2[dx] = j
return True, cars2
elif j[5] == 'R\n':
print('\nVehicle is on rent.')
return False, []
else:
print('\nVehicle does not exist.')
return False, []
# Mengembalikan kendaraan
def rentComplete(id2):
with open(r'rentVehicle.txt', 'r') as f3:
cars3 = f3.readlines()
for r in cars3:
r = r.split(',')
if id2 == r[0]:
with open(r'Vehicle.txt', 'r') as f4:
rented = f4.readlines()
for v in rented:
v = v.split(',')
if v[5] == 'R\n':
odo2 = str(odometer(2, float(r[4])))
startdate = r[3]
dayrent = datetime.strptime(
startdate, '%d/%m/%Y %H:%M')
daydelta = (now - dayrent).days
kms = float(odo2)-float(r[4])
typefee = 0
if v[2] == 'O':
typefee = 0.020
elif v[2] == 'P':
typefee = 0.025
charge = str(
((daydelta+1)*float(v[4]))+((daydelta+1)*float(r[5]))+(kms*typefee))
print('\nCar '+r[1]+' is returned from '+r[2])
table2 = PrettyTable(['Receipt ID', r[0]])
table2.add_row(['Renter ID', r[2]])
table2.add_row(['Vehicle ID', r[1]])
table2.add_row(['Description', v[1]])
table2.add_row(['Status', 'R -> A'])
table2.add_row(
['Accessories', '€'+r[5].strip('\n')])
table2.add_row(['Daily rate', '€'+v[4]])
table2.add_row(['Date/time of rent', r[3]])
table2.add_row(['Date/time of return',
now.strftime("%d/%m/%Y %H:%M")])
table2.add_row(['Number of days', daydelta+1])
table2.add_row(['Rent starting odometer', r[4]])
table2.add_row(['Rent end odometer', odo2])
table2.add_row(['Distance traveled', kms])
table2.add_row(['Rental charges', '€'+charge])
print(table2)
appdone = (id2+','+r[1]+',' + r[2]+',' + now.strftime(
"%d/%m/%Y %H:%M") + ','+r[4]+','+odo2+','+charge+'\n')
rentDetails(2, appdone)
dy = rented.index(','.join(v))
v[5] = 'A\n'
v = ','.join(v)
rented[dy] = v
return True, rented
else:
print(
"Receipt ID found but vehicle is not on rent.")
return False, []
else:
print('\nReceipt ID does not exist.')
return False, []
# Data visualisasi
def Graph1():
ordinary = 0
premium = 0
with open(r'Vehicle.txt', 'r') as g:
gc = g.readlines()
for gline in gc:
gline = gline.split(',')
gline = [gl.replace('\n', '') for gl in gline]
if gline[2] == 'O':
ordinary += 1
elif gline[2] == 'P':
premium += 1
print('\nNumber of Ordinary Vehicles: ', ordinary)
print('Number of Premium Vehicles: ', premium)
def Graph2():
dct = {}
total = 0
with open(r'Transactions.txt', 'r') as g2:
gd = g2.readlines()
for item in gd:
item = item.split(',')
item = [gh.replace('\n', '') for gh in item]
item[6] = float(item[6])
total += item[6]
paivamaara = datetime.strptime(
item[3], '%d/%m/%Y %H:%M')
paiva = paivamaara.strftime('%m/%Y')
try:
dct[paiva] = item[6]+float(dct[paiva])
except KeyError:
dct[paiva] = 0
dct[paiva] = item[6]+float(dct[paiva])
print('\nIncome earned for last', len(dct.keys()), 'month(s):')
for key, value in dct.items():
print(f'{key}: €{value:.2f}')
# Menghilangkan garis tambahan
def extraLinesTerminator():
with open('Vehicle.txt', 'r') as z:
content = z.readlines()
for i in content:
if i == '\n' or not i:
content.remove(i)
modify(True, content)
# Menu utama
def menu():
extraLinesTerminator()
print('\n Vehicle Menu')
print('Display Cars 1')
print('Add/delete Vehicle 2')
print('Rent Vehicle 3')
print('Complete Rent 4')
print('Reporting Vehicle Information 5')
print('Exit 6')
try:
choice = int(input('\nChoose your options: '))
except Exception:
print("\nInvalid choice.")
back()
else:
if choice == 1:
slt = input(
'\nA for available vehicles or R for vehicles on rent: ')
displayCars(slt)
back()
elif choice == 2:
try:
slt2 = int(
input('\n1 to add vehicles and 2 to delete vehicles: '))
except Exception:
print("\nInvalid choice.")
back()
else:
if slt2 == 1:
Add_Vehicle()
elif slt2 == 2:
Delete_Vehicle()
else:
print("\nInvalid choice.")
back()
elif choice == 3:
idc2 = input('\nEnter Vehicle ID: ')
o2 = rentVehicle(idc2)
modify(o2[0], o2[1])
back()
elif choice == 4:
idc3 = input('\nEnter Receipt ID: ')
o3 = rentComplete(idc3)
modify(o3[0], o3[1])
back()
elif choice == 5:
print(
'\nDisplay the number of vehicles of each type: Ordinary and Premium 1')
print(
'Display the income earned for last months (maximum 12) 2')
try:
opp = int(input('\nCHoose your options: '))
except Exception:
print("\nInvalid choice.")
back()
else:
if opp == 1:
Graph1()
back()
elif opp == 2:
Graph2()
back()
else:
print("\nInvalid choice.")
back()
elif choice == 6:
print('\nThanks for using Car Rental System. Bye!')
return
else:
print('\nInvalid choice.')
back()
menu()