-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA7.py
234 lines (227 loc) · 9.33 KB
/
A7.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
numOfbooking=0
numOfComment=0
import traceback
import pyodbc
conn = pyodbc.connect(
'driver={SQL Server};server=cypress.csil.sfu.ca;uid=s_caijiez;pwd=jA6emjNf424j6aYq')
# ^^^ 2 values must be change for your own program.
# Since the CSIL SQL Server has configured a default database for each user, there is no need to specify it (<username>354)
cur = conn.cursor()
# to validate the connection, there is no need to change the following line
cur.execute('SELECT username from dbo.helpdesk')
row = cur.fetchone()
while row:
print('SQL Server standard login name = ' + row[0])
row = cur.fetchone()
def incrementDate(date):
month = 0
day = 0
year = 0
if date[1] != '/':
month = month+int(date[0])
month = month*10
month += int(date[1])
if date[4] != '/':
day += int(date[3])
day *= 10
day += int(date[4])
year += int(date[6])
year *= 10
year += int(date[7])
year *= 10
year += int(date[8])
year *= 10
year += int(date[9])
else:
day += int(date[3])
year += int(date[5])
year *= 10
year += int(date[6])
year *= 10
year += int(date[7])
year *= 10
year += int(date[8])
else:
month = int(date[0])
if date[3] != '/':
day += int(date[2])
day *= 10
day += int(date[3])
year += int(date[5])
year *= 10
year += int(date[6])
year *= 10
year += int(date[7])
year *= 10
year += int(date[8])
else:
day = int(date[2])
year += int(date[4])
year *= 10
year += int(date[5])
year *= 10
year += int(date[6])
year *= 10
year += int(date[7])
if day ==29 and month ==2:
day = 1
month+=1
elif day!=30 and day!=31:
day+=1
elif day==30 and month==1 or month==3 or month==5 or month==7 or month==8 or month==10 or month==12:
day+=1
elif day == 31 and month ==1 or month ==3 or month ==5 or month ==7 or month ==8 or month ==10 or month ==12:
day=1
month+=1
elif day == 30 and month ==4 or month ==6 or month ==9 or month ==11:
day=1
month+=1
elif day==31 and month==12:
year+=1
result=str(month)+'/'+str(day)+'/'+str(year)
return result
# users' options
cursor = conn.cursor()
cursorA=conn.cursor()
cursorB=conn.cursor()
while True:
print("Please select any requests that you want!\n Type 1 for Searing Listings.\n Type 2 for Booking Listing.\n Type 3 for Writing Review.")
queryA = input() # query will be string
# searching list
if queryA == '1':
print("Please enter the minimum price that you want.")
querymin = input()
print("Please enter the maximum price that you want.")
querymax = input()
print("Please enter the number of bedrooms that you need.")
querybed = input()
print("Please enter the start date. e.g.'1/1/2016'Do not add zero.(m/d/y).")
querystart = input()
print("Please enter the end date e.g.'1/1/2016'Do not add zero(m/d/y).")
queryend = input()
print()
print()
try:
SQLCommand = ("SELECT DISTINCT C.listing_id,C.price FROM Listings L,Calendar C WHERE C.listing_id=L.id AND L.number_of_bedrooms=? AND C.price>=? AND C.price<=? AND C.date=? AND C.available=?;")
value = [int(querybed), float(querymin), float(querymax),querystart,1]
cursor.execute(SQLCommand, value)
except:
traceback.print_exc()
conn.rollback()
#for loop for checking the availability of these period
else:
results = cursor.fetchall()
count=0
for row in results:
temp=row[0]
total_price=float(row[1])
# print("total_price1:",total_price)
date=incrementDate(querystart)
flag=1
while date!=queryend and flag==1:
try:
SQLCommand = (
"SELECT C.available,C.price FROM Calendar C WHERE C.listing_id=? AND C.date=?;")
value = [int(row[0]),date]
cursorA.execute(SQLCommand, value)
except Exception as e:
print('Sorry ,an error is occurred!',e)
else:
subresult=cursorA.fetchone()
while subresult:
if subresult[0]==0 or float(row[1])<float(querymin) or float(row[1])>float(querymax):
flag=0
else:
total_price+=float(row[1])
subresult=cursorA.fetchone()
if flag==1:
date=incrementDate(date)
else :
break
if date==queryend:
try:
SQLCommand=(
"SELECT DISTINCT L.id,L.name,L.number_of_bedrooms,LEFT(L.description,25) FROM Listings L,Calendar C WHERE C.listing_id=L.id AND L.id=? AND C.price>=? AND C.price<=? AND C.date=? AND C.available=?;")
value=[temp,float(querymin),float(querymax),querystart,1]
cursorB.execute(SQLCommand,value)
except Exception as e:
print('Sorry ,an error is occurred!',e)
else:
final=cursorB.fetchall()
for row in final:
if count==0:
print("************The following is all the available rooms!************")
count+=1
print("id:",row[0])
print("name:",row[1])
print("number of bedrooms:",row[2])
print("description:",row[3])
print("total_price:",total_price)
print()
if count==0:
print("There is not available room during these period.\nPlease enter 1 to change your request!")
print()
#booking list
elif queryA=='2':
print("Please enter the booking info!")
print("Please enter the listing's id that you want to book!")
listing_id=int(input())
print("Please enter you name!")
guest_name=input()
print("Please enter the start day!")
stay_from=input()
print("Please enter the end day!")
stay_to=input()
print("Please enter the number of guests!")
number_of_guests=int(input())
try:
SQLCommand=("INSERT INTO Bookings(id,listing_id,guest_name,stay_from,stay_to,number_of_guests)VALUES(?,?,?,?,?,?)")
values=[numOfbooking,listing_id,guest_name,stay_from,stay_to,number_of_guests]
cursor.execute(SQLCommand,values)
except Exception as e:
print('Sorry ,an error is occurred!',e)
else:
conn.commit()
numOfbooking+=1
print("You are successful to book your room!")
print()
#write review
elif queryA=='3':
print("Please enter your name!")
guest_name=input()
try:
SQLCommand=("SELECT * FROM Bookings B WHERE B.guest_name=?")
values=[guest_name]
cursor.execute(SQLCommand,values)
except Exception as e:
print('Sorry ,an error is occurred!',e)
else:
results=cursor.fetchone()
print("************The following is your booking info.************")
while results:
print("id:", str(results[0]))
print("listing_id:",str(results[1]))
print("guest_name:",str(results[2]))
print("stay_from:",str(results[3]))
print("stay_to:",str(results[4]))
print("number of guests:",str(results[5]))
print()
results=cursor.fetchone()
print("Please enter the listing_id that you want to write review and shown above!")
listing_id=int(input())
print("Please enter you name!")
name=input()
print("Please enter the review you want to write!")
review=input()
try:
SQLCommand=("INSERT INTO Reviews(id,listing_id,comments,guest_name) VALUES(?,?,?,?)")
values=[numOfComment,listing_id,review,name]
cursor.execute(SQLCommand,values)
except Exception as e:
print('Sorry ,an error is occurred!',e)
else:
conn.commit()
numOfComment+=1
print("Thanks for writing the comment!")
print()
conn.close()