-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsvModule.py
157 lines (145 loc) · 6.99 KB
/
csvModule.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
import csv
import os
import time
def fetchName(customerID):
try:
with open("HISTORY.csv", "r", newline="") as customerFile:
customerReader = csv.reader(customerFile)
header = next(customerReader)
for row in customerReader:
if row[0] == str(customerID):
return row[1]
print("\n\tCUSTOMER ID NOT FOUND. PLEASE TRY AGAIN OR CREATE A NEW CUSTOMER RECORD")
return None
except FileNotFoundError:
print("\nNO BOOKING HAS BEEN DONE YET")
return None
except Exception as e:
print(f"\nError reading booking history: {e}")
return None
def customerExists(customerID):
# function to check if customer exists
try:
with open("HISTORY.csv", "r", newline="") as customerFile:
customerReader = csv.reader(customerFile)
next(customerReader)
return any(row[0] == str(customerID) for row in customerReader)
except FileNotFoundError:
return False
def bookingHistory(customerID, customerName, bookingDate, totalSpend):
customerID = int(customerID)
totalSpend = int(totalSpend)
if os.path.exists("HISTORY.csv"):
readFile = open("HISTORY.csv", "r", newline="")
historyReader = csv.reader(readFile)
writeFile = open("TEMPHISTORY.csv", "w", newline="")
historyWriter = csv.writer(writeFile)
header = next(historyReader)
historyWriter.writerow(header)
customerFound = False
for details in historyReader:
if int(details[0]) == customerID:
updated_total_spend = int(details[3]) + totalSpend
historyWriter.writerow([customerID, customerName, bookingDate, updated_total_spend])
customerFound = True
else:
historyWriter.writerow(details)
if customerFound == False:
historyWriter.writerow([customerID, customerName, bookingDate, totalSpend])
readFile.close()
writeFile.close()
os.remove("HISTORY.csv")
os.rename("TEMPHISTORY.csv", "HISTORY.csv")
else:
with open("HISTORY.csv", "w", newline="") as historyFile:
historyWriter = csv.writer(historyFile)
header = ["CUSTOMER ID", "CUSTOMER NAME", "LAST BOOKING", "TOTAL SPEND"]
historyWriter.writerow(header)
historyWriter.writerow([customerID, customerName, bookingDate, totalSpend])
def searchHistoryName():
if os.path.exists("HISTORY.csv")==True:
with open("HISTORY.csv","r") as historyFile:
searchFound=False
historyReader=csv.reader(historyFile)
customerName=input("ENTER THE CUSTOMER NAME TO BE SEARCHED: ").upper()
for row in historyReader:
if row[1]==customerName:
print("\n\t--CUSTOMER DETAILS FOUND--\n")
print("+-------------------------------------------------------------+")
print('%4s'%'ID','%15s'%'CUSTOMER NAME','%15s'%'BOOKING DATE','%10s'%'AMOUNT SPENT')
print("+-------------------------------------------------------------+")
print('%4s'%row[0],'%15s'%row[1],'%15s'%row[2],'%10s'%row[3])
searchFound=True
if searchFound==False:
print("\n\t\t--> CUSTOMER NOT FOUND <--")
else:
print("\t--NO BOOKINGS DONE YET--\n")
def searchHistoryID():
if os.path.exists("HISTORY.csv") == False:
print("\n\t--NO BOOKINGS DONE YET--")
return
with open("HISTORY.csv", "r", newline="") as historyFile:
historyReader = csv.reader(historyFile)
header = next(historyReader)
customerID = input("ENTER THE CUSTOMER ID TO BE SEARCHED: ")
searchFound = False
print("+-------------------------------------------------------------+")
print('%4s'%'ID','%15s'%'CUSTOMER NAME','%15s'%'BOOKING DATE','%10s'%'AMOUNT SPENT')
print("+-------------------------------------------------------------+")
for row in historyReader:
if row[0] == customerID:
print('%4s'%row[0],'%15s'%row[1],'%15s'%row[2],'%10s'%row[3])
searchFound = True
break
if searchFound == False:
print("\n\t\t--> CUSTOMER NOT FOUND <--")
def searchHistoryDate():
if os.path.exists("HISTORY.csv") == False:
print("\n\t--NO BOOKINGS DONE YET--")
return
with open("HISTORY.csv", "r", newline="") as historyFile:
historyReader = csv.reader(historyFile)
header = next(historyReader)
bookingDate = input("ENTER THE BOOKING DATE TO VIEW CUSTOMER DETAILS (YYYY-MM-DD): ").strip()
searchFound = False
print("+-------------------------------------------------------------+")
print('%4s'%'ID','%15s'%'CUSTOMER NAME','%15s'%'BOOKING DATE','%10s'%'AMOUNT SPENT')
print("+-------------------------------------------------------------+")
for row in historyReader:
if row[2] == bookingDate:
print('%4s'%row[0],'%15s'%row[1],'%15s'%row[2],'%10s'%row[3])
time.sleep(0.5)
searchFound = True
if searchFound == False:
print("\n\t--NO RECORDS FOUND FOR THIS DATE--")
def viewBookingHistory():
if os.path.exists("HISTORY.csv")==True:
with open("HISTORY.csv","r") as historyFile:
historyReader=csv.reader(historyFile)
print("+-------------------------------------------------------------+")
print('%4s'%'ID','%15s'%'CUSTOMER NAME','%15s'%'BOOKING DATE','%15s'%'AMOUNT SPENT')
print("+-------------------------------------------------------------+")
header=next(historyReader)
for row in historyReader:
print('%4s'%row[0],'%15s'%row[1],'%15s'%row[2],'%15s'%row[3])
time.sleep(0.5)
else:
print("\t--NO BOOKINGS DONE YET--\n")
def cancelBookingCSV(customerID):
if os.path.exists("HISTORY.csv"):
# Create temporary file
with open("HISTORY.csv", "r", newline="") as readFile, \
open("TEMPHISTORY.csv", "w", newline="") as writeFile:
historyReader = csv.reader(readFile)
historyWriter = csv.writer(writeFile)
# Copy header
header = next(historyReader)
historyWriter.writerow(header)
# Copy all rows except the cancelled booking
for row in historyReader:
if row[0] != str(customerID):
historyWriter.writerow(row)
# Replace original file with updated one
os.remove("HISTORY.csv")
os.rename("TEMPHISTORY.csv", "HISTORY.csv")
print("\n\t--BOOKING CANCELLED SUCCESSFULLY--")