-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.py
More file actions
216 lines (208 loc) · 8.75 KB
/
Copy pathmanager.py
File metadata and controls
216 lines (208 loc) · 8.75 KB
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
import json
from dataclasses import dataclass,asdict
from my_package import json_utilities
file_name = "library.json"
library = json_utilities.load_file(file_name)
@dataclass
class BookBlueprint:
title:str
author_name:str
isbn:str
year:int
availability:str
def add_book():
print("You choose option 1, Please enter the details:\n")
title = input("Please enter book title: ")
author_name = input("Please enter author name: ")
while True:
check = True
isbn = input("Please enter isbn number: ")
isbn = isbn.replace("-","")
if len(isbn) == 13:
if library:
for lib in library:
if lib:
if lib["isbn"] == isbn:
check =False
print("A book with this ISBN already exists\n")
break
if check:
break
else:
print("Not the correct format Please enter a 13 digit ISBN\n")
while True:
try:
year = int(input("Please enter book published year: "))
if len(str(year)) == 4:
break
else:
print("Please enter year in YYYY format\n")
except ValueError:
print("Please enter a integer value only\n")
availability = "Available"
if library:
library.append(asdict(BookBlueprint(title,author_name,isbn,year,availability)))
json_utilities.save_file(file_name,library)
print(f"The Book {title} by {author_name} has been added to the Library\n")
def print_books(lib):
for key,val in lib.items():
print(f"{key} : {val}")
print()
def view_books():
print("You choose option 2, Here are all the books:\n")
check = False
if library:
for lib in library:
if lib :
check = True
print_books(lib)
if not check:
print("No books exist Start adding now\n")
def search_book_by(what,value):
if library:
check = False
for lib in library:
if lib :
if lib[what] == value:
check=True
print_books(lib)
if not check:
print(f"The book with this {what} : {value} doesn't exist\n")
else:
print("The library is empty\n")
def search_book(by_what,value):
print("You choose option 3 Lets search:\n")
if by_what.lower() == "title":
search_book_by("title",value)
elif by_what.lower() == "isbn":
search_book_by("isbn",value)
elif by_what.lower() == "author":
search_book_by("author_name",value)
elif by_what.lower() == "year":
value = int(value)
search_book_by("year",value)
elif by_what.lower() == "availability":
search_book_by("availability",value)
else:
print(f"You can't search using {by_what}\n")
def update_book(isbn):
print("You choose option 4, Let us check the ISBN:\n")
selected_lib = {}
if library:
for lib in library:
if lib :
if lib["isbn"] == isbn:
print("ISBN verified\n")
selected_lib = lib
try:
ask_user = int(input("What would you like to update\n" \
"1.Title\n" \
"2.Author\n" \
"3.ISBN\n" \
"4.Year\n" \
"5.Availibility\n" \
"6.Cancel"))
if ask_user==1:
ask_title = input("Please enter updated title\n")
print(f"The book {selected_lib["title"]} was updated to {ask_title}\n")
selected_lib["title"] = ask_title
json_utilities.save_file(file_name,library)
break
elif ask_user == 2:
ask_author = input("Please enter updated Author\n")
print(f"The book {selected_lib["title"]} author was updated to {ask_author} from {selected_lib["author_name"] }\n")
selected_lib["author_name"] = ask_author
json_utilities.save_file(file_name,library)
break
elif ask_user == 3:
isbn = input("Please enter updated ISBN\n")
check = True
for lib_nest in library:
if lib_nest:
if lib_nest != selected_lib and lib_nest["isbn"] == isbn:
check =False
print("A book with this ISBN already exists\n")
break
if check:
print(f"The book {selected_lib["title"]} ISBN was updated to {isbn} from {selected_lib["isbn"]}\n")
selected_lib["isbn"] = isbn
json_utilities.save_file(file_name,library)
elif ask_user == 4:
while True:
try:
year = int(input("Please enter updated year: "))
if len(str(year)) == 4:
break
else:
print("Please enter year in YYYY format\n")
except ValueError:
print("Please enter a integer value only\n")
print(f"The book {selected_lib["title"]} published year was updated to {year} from {selected_lib["year"]}\n")
selected_lib["year"] = year
json_utilities.save_file(file_name,library)
elif ask_user == 5:
avail = input("Please enter updated availability: ").lower()
if avail == "available" or avail == "borrow":
print(f"The book {selected_lib["title"]} availability was updated to {avail.title()} from {selected_lib["availability"]}\n")
selected_lib["availability"] = avail.title()
json_utilities.save_file(file_name,library)
elif ask_user == 6:
return
else:
print("Please enter a value between 1-6 only\n")
except ValueError:
print("Please enter a integer Value only\n")
else:
print("The Library is empty")
def remove_book():
print("You choose option 5:\n")
check = True
user = input("Please enter the book ISBN(without -) you want to borrow: ")
if library:
for lib in library:
if lib :
if lib["isbn"] == user:
check = False
print(f"The Book {lib["title"]} is removed\n")
library.remove(lib)
json_utilities.save_file(file_name,library)
if check:
print("This book doesn't exist\n")
else:
print("The Library is empty")
def borrow_book():
print("You choose option 6:\n")
check = True
user = input("Please enter the book ISBN you want to borrow: ")
if library:
for lib in library:
if lib :
if lib["isbn"] == user and lib["availability"] != "Borrow":
check = False
print(f"The book {lib["title"]} is available you can borrow it\n")
if not check:
lib["availability"] = "Borrow"
json_utilities.save_file(file_name,library)
break
if check:
print("This book is already borrowed or doesn't exist")
else:
print("The Library is empty")
def return_book():
print("You choose option 7:\n")
check = True
user = input("Please enter the book ISBN you want to return: ")
if library:
for lib in library:
if lib :
if lib["isbn"] == user and lib["availability"] != "Available":
check = False
print(f"Thanks for using our library and returning The book {lib["title"]} \n")
if not check:
lib["availability"] = "Available"
json_utilities.save_file(file_name,library)
break
if check:
print("This book is already returned or doesn't exist")
else:
print("The Library is empty")