-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.py
96 lines (81 loc) · 3.73 KB
/
manager.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
from classes import Book
from classes import Bookcase
from datetime import date
import sqlite3
def new_book(cursor):
title = input("What is its title? ").strip().lower()
author = input("Who is the author? ").strip().lower()
genre = input("Which genre it is?").strip()
pages = int(input("How many pages does it have? ").strip())
today_date = date.today()
cursor.execute("INSERT OR IGNORE INTO Author (name) VALUES (?)", (author,))
cursor.execute("SELECT id FROM Author WHERE name=?", (author,))
author_id = cursor.fetchone()[0]
cursor.execute("INSERT OR IGNORE INTO Genre (name) VALUES (?)", (genre,))
cursor.execute("SELECT id FROM Genre WHERE name=?", (genre,))
genre_id = cursor.fetchone()[0]
cursor.execute("INSERT INTO Bookcase VALUES (?, ?, ?, ?, ?, ?, ?)", (title, author_id, genre_id, today_date.year, today_date.month, today_date.day, pages))
return Book(title, author, genre, today_date, pages)
def print_by_parameter(bookcase):
while True:
parameter = input("By which parameter would you like to order it? (title, author, pages or reading_date) ").strip()
if parameter != "title" and parameter != "author" and parameter != "pages" and parameter != "reading_date":
print("Please enter a valid parameter.")
continue
return bookcase.order_by_parameter(parameter)
def search(bookcase):
title = input("What title you looking for? ")
author = input("What author you looking for? ")
genre = input("What genre you looking for? ")
result = Bookcase(bookcase.search_by_parameter(title.strip(), author.strip(), genre.strip()))
if result.is_empty():
print("Didn't find anything.")
else:
result.print()
def sum_pages(bookcase):
print("Please enter the dates in this form YYYY-MM-DD\nLeave in blank to count all bookcase's lifetime")
date_from = input("Date from: ")
date_to = input("Date to: ")
return bookcase.sum_pages(date_from, date_to)
def main():
print("Welcome to your SmartBookcase! What would you want today?\n")
conn = sqlite3.connect('bookcase.sqlite')
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS Bookcase
(title TEXT, author_id INTEGER, genre_id INTEGER, reading_year INTEGER, reading_month INTEGER, reading_day INTEGER, pages INTEGER)''')
cursor.execute('''CREATE TABLE IF NOT EXISTS Author
(id INTEGER NOT NULL, name TEXT, PRIMARY KEY("id"))''')
cursor.execute('''CREATE TABLE IF NOT EXISTS Genre
(id INTEGER NOT NULL, name TEXT, PRIMARY KEY("id"))''')
bookcase_data = cursor.execute("SELECT * FROM Bookcase").fetchall()
books = list()
for book in bookcase_data:
cursor.execute("SELECT name FROM Author WHERE id=?", (book[1],))
author = cursor.fetchone()[0]
cursor.execute("SELECT name FROM Author WHERE id=?", (book[2],))
genre = cursor.fetchone()[0]
books.append(Book(book[0], author, genre, date(book[3], book[4], book[5]), book[6]))
bookcase = Bookcase(books)
while True:
command = input("Type 1 if you want to add a new book to your bookcase.\nType 2 if you want to see all of your bookcase.\nType 3 if you want to search by something in your bookcase.\nType 4 if you want to know how much pages you have read.\nType 5 if you want to QUIT.\n")
try:
command = int(command)
except:
print("Please, type a valid command.")
continue
if command == 1:
bookcase.add_new_book(new_book(cursor))
conn.commit()
elif command == 2:
print_by_parameter(bookcase).print()
elif command == 3:
search(bookcase)
elif command == 4:
print(sum_pages(bookcase))
elif command == 5:
break
elif command != 0:
print("Please, type a valid command.")
print("--------------------------------------")
if __name__ == "__main__":
main()