-
Notifications
You must be signed in to change notification settings - Fork 0
/
slytherin-whatabook-consoleapp.py
201 lines (169 loc) · 6.94 KB
/
slytherin-whatabook-consoleapp.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
# // Title: slytherin-whatabook-consoleapp.py
# // Author: John Vanhessche
# // Date: 8 December 2022
# // Description: slytherin-whatabook-consoleapp.py
# // Modified By: Walter McCue on 12 December 2022
#importing MongoClient
from pymongo import MongoClient;
import datetime;
#Connecting to MongoDB
url = "mongodb+srv://web335_user:s3cret@bellevueuniversity.ouotidt.mongodb.net/web335DBretryWrites=true&w=majority"
client = MongoClient(url)
#access variable for web335DB
db = client['web335DB']
########################################Application Start#############################################################################
def main():
printWelcome()
# Main welcome script to capture user initial choice
def printWelcome():
print("Welcome to WhatABook. Please enter a choice below:")
print("1 - List ALL books")
print("2 - List books by criteria")
print("3 - View Wish List")
print("4 - Exit")
# choice is evaluated and sent to the appropriate function
# no need to hve a separate function for listing all books, so that is executed immediately
# the other choices have further selections that need to be made
choice = int(input("\nEnter a selection: ")) #capture user input
if choice == 1:
for book in db.books.find():
print(
"Title: ", book["title"] + "\n"
"Genre: ", book["genre"] + "\n"
"Author: " , book["author"] + "\n"
"Book ID: ", book["bookId"] + "\n\n"
)
main()
elif choice == 2:
listByChoice()
elif choice == 3:
wishList()
else:
exit() #any invalid selection terminates the application.
# The is the parent for listBy{Author, Genre, BookId} functions. This allows for a cleaner Welcome screen and a return point.
def listByChoice():
print("\nPlease select your criteria")
print("1 - List books by Author")
print("2 - List books by Genre")
print("3 - List books by bookId")
print("\n0 - Home")
criteria = int(input("\nEnter a selection: ")) #capture user input
if criteria == 1:
listByAuthor()
elif criteria == 2:
listByGenre()
elif criteria == 3:
listByBookId()
elif criteria == 0: #They can escape back to the Welcome screen by typing 0
printWelcome()
else:
print("Not a valid selection")
listByChoice() #typing anything other than the valid selections, returns to the top of the function
# The function will ask the user type in an author, then it should return books for that author.
def listByAuthor():
author = str(input("\nFind an author. Example: John Grisham: "))
authorObject = db.books.find_one({"author": author})
if(authorObject):
print("Title: ", authorObject["title"] + "\n"
"Genre: ", authorObject["genre"] + "\n"
"Author: " , authorObject["author"] + "\n"
"Book ID: ", authorObject["bookId"] + "\n\n")
main()
else:
print("Not a Valid Author")
listByAuthor()
#child function to listByChoice. Provides user with a list of genre's to select from
def listByGenre():
print("\nSelect from one of the following genre's:")
print("1 - Thriller")
print("2 - Horror")
print("3 - Non-Fiction")
print("4 - Fantasy")
print("5 - How-To")
print("\n0 - Go Back")
#Evaluate the selection and execute the appropriate mongo command.
#TODO need to format the output better, not sure how to do this yet.... pretty()??, set project and sort fields??
genre = int(input("\nEnter a selection: ")) #capture user input
#evaluate user input and select the appropriate mongo command.
if genre == 1:
for book in db.books.find({"genre": "Thriller"}):
print("Title: ", book["title"] + "\n"
"Genre: ", book["genre"] + "\n"
"Author: " , book["author"] + "\n"
"Book ID: ", book["bookId"] + "\n\n")
main()
elif genre == 2:
for book in db.books.find({"genre": "Horror"}):
print("Title: ", book["title"] + "\n"
"Genre: ", book["genre"] + "\n"
"Author: " , book["author"] + "\n"
"Book ID: ", book["bookId"] + "\n\n")
main()
elif genre == 3:
for book in db.books.find({"genre": "Non-Fiction"}):
print("Title: ", book["title"] + "\n"
"Genre: ", book["genre"] + "\n"
"Author: " , book["author"] + "\n"
"Book ID: ", book["bookId"] + "\n\n")
main()
elif genre == 4:
for book in db.books.find({"genre": "Fantasy"}):
print("Title: ", book["title"] + "\n"
"Genre: ", book["genre"] + "\n"
"Author: " , book["author"] + "\n"
"Book ID: ", book["bookId"] + "\n\n")
main()
elif genre == 5:
for book in db.books.find({"genre": "How-To"}):
print("Title: ", book["title"] + "\n"
"Genre: ", book["genre"] + "\n"
"Author: " , book["author"] + "\n"
"Book ID: ", book["bookId"] + "\n\n")
main()
elif genre == 0:
listByChoice(); #escapes back to parent
else:
listByGenre(); #not a valid selection, so return to the top of the function
#This function will return a book by it's bookId. This is NOT the mongo _id.
def listByBookId():
bookId = str(input("\nEnter a five-digit book Id (example: b****): ")) #capture user input
bookObject = db.books.find_one({"bookId": bookId})
if(bookObject):
print("Title: ", bookObject["title"] + "\n"
"Genre: ", bookObject["genre"] + "\n"
"Author: " , bookObject["author"] + "\n"
"Book ID: ", bookObject["bookId"] + "\n\n")
main()
else:
print("Not a valid bookId")
listByBookId()
#This function will ask a user to enter their customer Id. it will then return the books in that customer's wishlist to that Id.
def wishList():
print("To view a wishlist, please type the customerId:")
customerId = str(input("\nEnter your five-digit customer Id (example: c****): ")) #capture user input
customerObject = db.customers.find_one({"customerId": customerId})
if(customerObject):
pipeline = [
{
"$match": {
"customerId": customerId
}
},
{
"$project": {
"_id": 0,
"wishlist": 1
}
}
]
print("Wishlist for " + customerObject["firstName"] + " " + customerObject["lastName"] + ": ")
for book in customerObject["wishlist"]:
print("Title: ", book["title"] + "\n"
"Author: ", book["author"] + "\n"
"Genre: ", book["genre"] + "\n"
"Book ID: ", book["bookId"] + "\n\n")
main()
else:
print("Customer not found")
wishList()
main()