Advertisement
Guest User

Untitled

a guest
Sep 19th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 21.75 KB | None | 0 0
  1. import pymysql
  2. import random
  3.  
  4. db = pymysql.connect("localhost", "root", "Hero2of1war", "lib")
  5. cursor = db.cursor()
  6.  
  7.  
  8. class Library(object):
  9.     def __init__(self):
  10.         super(Library, self).__init__()
  11.  
  12.         self.number_of_library = ''  # library_number
  13.         self.street = ''
  14.         self.phone_number = 0
  15.         self.books = []
  16.         self.visitors = []
  17.         self.history = []
  18.         self.authors = []
  19.  
  20.     def add_book(self, book):
  21.         # not_found = True
  22.         # for i in self.books:
  23.         #     if i.name == book.name and i.author == book.author:
  24.         #         not_found = False
  25.         #         break
  26.         # if not_found:
  27.         #     self.books.append(book)
  28.         # else:
  29.         #     print('Book exist!')
  30.  
  31.         for i in self.books:
  32.             if i.name == book.name and i.author == book.author:
  33.                 print('Book exist!')
  34.                 break
  35.         else:
  36.             self.books.append(book)
  37.  
  38.     def delete_book(self, name, author):
  39.         book = False
  40.         for i in lib.books:
  41.             if i.name == name and i.author == author:
  42.                 self.books.remove(i)
  43.                 return True
  44.         if book == False:
  45.                 print('This book not exist')
  46.                 return False
  47.  
  48.     def search_book(self, book):
  49.         for i in self.books:
  50.             if i.name == book:
  51.                 return i
  52.  
  53.     def add_visitor(self, visitor):
  54.         not_found = True
  55.         for i in self.visitors:
  56.             if i.name == visitor.name and i.last_name == visitor.last_name and i.number == visitor.number:
  57.                 not_found = False
  58.         if not_found == True:
  59.                 self.visitors.append(visitor)
  60.         else:
  61.             print('Visitor exist!')
  62.             return False
  63.  
  64.     def remove_visitor(self, name, last_name, number):
  65.         remove = False
  66.         for i in lib.visitors:
  67.             if i.name == name and i.last_name == last_name and i.number == number:
  68.                 lib.visitors.remove(i)
  69.                 return True
  70.         if remove == False:
  71.                 print('Can not delete!')
  72.                 return False
  73.  
  74.     def write_book_visitor(self, write, book, data):
  75.         book_in_visitor = BookInVisitor(visitor_name, visitor_last_name, book_name, book_author, data)
  76.         book_in_visitor.visitor_name = write.name
  77.         book_in_visitor.visitor_last_name = write.last_name
  78.         book_in_visitor.book_name = book.name
  79.         book_in_visitor.book_author = book.author
  80.         book_in_visitor.data_of_take = data
  81.  
  82.         self.history.append(book_in_visitor)
  83.  
  84.     def print_history(self):
  85.         for i in self.history:
  86.             print(i.visitor_name + ' take', i.book_name)
  87.  
  88.     def edit_book(self, old_name, new_name):
  89.         for i in lib.books:
  90.             if i.name == old_name:
  91.                 i.name = new_name
  92.  
  93.     def edit_visitors(self, old_name, old_last_name, old_number, new_name, new_last_name, new_number):
  94.         for i in lib.visitors:
  95.             if i.name == old_name and i.last_name == old_last_name and i.number == old_number:
  96.                 i.name = new_name
  97.                 i.last_name = new_last_name
  98.                 i.number = new_number
  99.  
  100.     def user_exist(self, old_name, old_last_name, old_number):
  101.         not_exist = False
  102.         for i in lib.visitors:
  103.             if i.name == old_name and i.last_name == old_last_name and i.number == old_number:
  104.                 return True
  105.         if not_exist == False:
  106.                 print('Not exist')
  107.                 return False
  108.  
  109.     def find_book_by_name(self, name, author):
  110.         for i in lib.books:
  111.             if i.name == name and i.author == author:
  112.                 return i
  113.         # return None
  114.  
  115.     def find_visitor_by_name(self, name, last_name):
  116.         for i in lib.visitors:
  117.             if i.name == name and i.last_name == last_name:
  118.                 return i
  119.         # return None
  120.  
  121.     # add_return_date
  122.     def add_data_of_remove(self, name, last_name, book, author, remove_date):
  123.         for i in lib.history:
  124.             if i.visitor_name == name and i.visitor_last_name == last_name and i.book_name == book and i.book_author == author:
  125.                 i.remove_of_data = remove_date
  126.  
  127.     def initialize_database(self):
  128.         cursor.execute("INSERT INTO gender (id, name) VALUES (NULL, 'male')")
  129.         cursor.execute("INSERT INTO gender (id, name) VALUES (NULL, 'female')")
  130.         db.commit()
  131.  
  132.  
  133.  
  134. class Book(object):
  135.     def __init__(self,id, name, author, date, count = '', available_count = ''):
  136.         super(Book, self).__init__()
  137.  
  138.         self.id = id
  139.         self.name = name
  140.         self.author = author
  141.         self.date = date
  142.         self.count = count
  143.         self.available_count = available_count
  144.  
  145. class Visitor(object):
  146.     def __init__(self, id, name, last_name, birth_date, number, gender):
  147.         super(Visitor, self).__init__()
  148.  
  149.         self.id = id
  150.         self.name = name
  151.         self.last_name = last_name
  152.         self.birth_date = birth_date
  153.         self.number = number
  154.         self.gender = gender
  155.  
  156. class BookInVisitor(object):
  157.     def __init__(self, visitor_name, visitor_last_name, book_name, book_author, data_of_take, remove_of_data=''):
  158.         super(BookInVisitor, self).__init__()
  159.  
  160.         self.visitor_name = visitor_name
  161.         self.visitor_last_name = visitor_last_name
  162.         self.book_name = book_name
  163.         self.book_author = book_author
  164.         self.data_of_take = data_of_take
  165.         self.remove_of_data = remove_of_data
  166.  
  167. class Author(object):
  168.     def __init__(self, id = '', name = '', last_name = ''):
  169.         super(Author, self).__init__()
  170.  
  171.         self.id = id
  172.         self.name = name
  173.         self.last_name = last_name
  174.  
  175.  
  176. user = 'Mike'
  177. password = '123456'
  178.  
  179. lib = Library()
  180.  
  181. user_input = input("Enter user: ")
  182. password_input = input("Enter password: ")
  183.  
  184. if user == user_input and password == password_input:
  185.  
  186. cursor.execute("SELECT * FROM gender")
  187. bk = cursor.fetchall()
  188. if len(bk) == 0:
  189.     lib.initialize_database()
  190.  
  191. cursor.execute("SELECT * FROM author")
  192. au = cursor.fetchall()
  193. for author in au:
  194.     author = Author(author[0], author[1], author[2])
  195.     lib.authors.append(author)
  196.  
  197. cursor.execute("SELECT * FROM book")
  198. bk = cursor.fetchall()
  199. for book in bk:
  200.     # cursor.execute("SELECT id from book WHERE name = '{0}'".format(book[1]))
  201.     # b = cursor.fetchall()
  202.     b = book[0]
  203.     # print(b)
  204.     cursor.execute("SELECT author_id from book_has_author WHERE book_id = {}".format(b))
  205.     v = cursor.fetchall()
  206.     v = v[0][0]
  207.     # print(v)
  208.     cursor.execute("SELECT name FROM author WHERE id = {}".format(v))
  209.     na = cursor.fetchall()
  210.     na = na[0][0]
  211.     # print(na)
  212.     # print(book)
  213.     book = Book(book[0], book[1], na, book[2], book[3], book[4])
  214.     lib.books.append(book)
  215.  
  216. cursor.execute("SELECT * FROM visitor")
  217. vs = cursor.fetchall()
  218. for visitor in vs:
  219.     visitor = Visitor(visitor[0], visitor[1], visitor[2], visitor[3], visitor[4], visitor[6])
  220.     lib.visitors.append(visitor)
  221.     # print(visitor)
  222.  
  223. cursor.execute("SELECT * FROM book_in_visitor")
  224. bookinvisitor = cursor.fetchall()
  225. for bkinvs in bookinvisitor:
  226.     """айді списка"""
  227.     idfirst = bkinvs[0]
  228.     # cursor.execute("SELECT taking_date FROM book_in_visitor WHERE id = {}".format(idfirst))
  229.     # takingdate = cursor.fetchall()  # takingDate, TakingDate, taking_date
  230.     # takingdate = takingdate[0][0]
  231.     takingdate = str(bkinvs[1])
  232.     # print(takingdate)
  233.     # cursor.execute("SELECT returning_date FROM book_in_visitor WHERE id = {}".format(idfirst))
  234.     # returningdate = cursor.fetchall()
  235.     # returningdate = returningdate[0][0]
  236.     returningdate = str(bkinvs[2])
  237.     # print(returningdate)
  238.     # cursor.execute("SELECT visitor_id FROM book_in_visitor WHERE id = {}".format(idfirst))
  239.     # """берем число в вісітора_айді"""
  240.     # visid = cursor.fetchall()
  241.     # visid = visid[0][0]
  242.     visid = str(bkinvs[3])
  243.     cursor.execute("SELECT name FROM visitor WHERE id = {}".format(visid))
  244.     """дістаєм імя"""
  245.     visname = cursor.fetchall()
  246.     visname = visname[0][0]
  247.     # print(visname)
  248.     cursor.execute("SELECT last_name FROM visitor WHERE id = {}".format(visid))
  249.     """дістаєм прізвище"""
  250.     vislastname = cursor.fetchall()
  251.     vislastname = vislastname[0][0]
  252.     # print(vislastname)
  253.     # cursor.execute("SELECT book_id FROM book_in_visitor WHERE id = {}".format(idfirst))
  254.     # """берем число бук_айді"""
  255.     # bookid = cursor.fetchall()
  256.     # bookid = bookid[0][0]
  257.     bookid = str(bkinvs[4])
  258.     # print(bookid)
  259.     cursor.execute("SELECT name FROM book WHERE id = {}".format(bookid))
  260.     bookname = cursor.fetchall()
  261.     bookname = bookname[0][0]
  262.     # print(bookname)
  263.     cursor.execute("SELECT author_id FROM book_has_author WHERE book_id = {}".format(bookid))
  264.     authorid = cursor.fetchall()
  265.     authorid = authorid[0][0]
  266.     # print(authorid)
  267.     cursor.execute("SELECT name FROM author WHERE id = {}".format(authorid))
  268.     authorname = cursor.fetchall()
  269.     authorname = authorname[0][0]
  270.     # print(authorname)
  271.     # cursor.execute("SELECT last_name FROM author WHERE id = {}".format(authorid))
  272.     # authorlastname = cursor.fetchall()
  273.     # authorlastname = authorlastname[0][0]
  274.     # print(authorlastname)
  275.     bookvisitor = BookInVisitor(visname, vislastname, bookname, authorname, takingdate, returningdate)
  276.     lib.history.append(bookvisitor)
  277.  
  278.  
  279. # book1 = Book(None, '300 spartans', 'Zak Plahin', '2001')
  280. # lib.add_book(book1)
  281.  
  282. # visitor1 = Visitor(None, 'Oleg', 'See', '12-12-2001', '+380635887884', 'male')
  283. # lib.add_visitor(visitor1)
  284. #
  285. # visitor2 = Visitor(None, 'Chak', 'Broflovski', '09-11-2001', '+38073911112', 'male')
  286. # lib.add_visitor(visitor2)
  287.  
  288.  
  289.  
  290. while True:
  291.     number = int(input('1 - Show all books\n2 - Show all visitors\n3 - Add visitor\n4 - Add book\n5 - Show author\n6 - Change visitor\n7 - Write book on visitor\n8 - Add remove date\n9 - Show history\n10 - Serch book by name\n11 - Search book by author\n12 - Show books in visitor\n:'))
  292.     if number == 1:
  293.         if len(lib.books) == 0:
  294.             print('Our library has not book')
  295.         for i in lib.books:
  296.             print(str(i.id) + 'Book: ' + i.name +', author: ' + i.author + ', date: ' + str(i.date))
  297.     elif number == 2:
  298.         if len(lib.visitors) == 0:
  299.             print('Our library has not visitors')
  300.         for i in lib.visitors:
  301.             print(str(i.id) + 'Name: ' + i.name + ', last name: ' + i.last_name + ', birth date: ' + str(i.birth_date) + ', number: ' + i.number + ', gender: ' + str(i.gender))
  302.     elif number == 3:
  303.         first_name = input('Enter name: ')
  304.         first_name = first_name.capitalize()
  305.         second_name = input('Enter last_name: ')
  306.         second_name = second_name.capitalize()
  307.         birth_date = input('Enter birth date(yyyy-mm-dd): ')
  308.         number = input('Enter your number: ')
  309.         gender = input('Enter: 1-male / 2-female: ')
  310.         if gender == str(1):
  311.             cursor.execute("select name from gender where id = '{}'".format(1))
  312.             male = cursor.fetchall()
  313.             male = male[0][0]
  314.         elif gender == str(2):
  315.             cursor.execute("select name from gender where id = '{}'".format(2))
  316.             female = cursor.fetchall()
  317.             female = female[0][0]
  318.         else:
  319.             print('Error: gender_id!')
  320.             break
  321.         visit = Visitor(None, first_name, second_name, birth_date, number, gender)
  322.         if lib.add_visitor(visit) == False:
  323.             break
  324.         cursor.execute("INSERT INTO visitor (id, name, last_name, birth_date, number, gender_id) VALUES (NULL, '{0}','{1}','{2}','{3}','{4}')".format(first_name, second_name, birth_date, number, gender))
  325.         cursor.execute("SELECT last_insert_id()")
  326.         vid = cursor.fetchall()
  327.         db.commit()
  328.         # print(vid)
  329.         visit.id = vid[0][0]
  330.         # lib.add_visitor(visit)
  331.     elif number == 4:
  332.         name_book = input('Enter book name: ')
  333.         name_book = name_book.capitalize()
  334.         author_name = input('Enter author name: ')
  335.         author_name = author_name.capitalize()
  336.         author_last_name = input('Enter author last name: ')
  337.         author_last_name = author_last_name.capitalize()
  338.         found = False
  339.         for author in lib.authors:
  340.             if author.name == author_name and author.last_name == author_last_name:
  341.                 found = True
  342.                 aid = author.id
  343.                 db.commit()
  344.         if not found:
  345.             cursor.execute("INSERT INTO author (id, name, last_name) VALUES (NULL, '{0}', '{1}')".format(author_name, author_last_name))
  346.             cursor.execute("Select last_insert_id()")
  347.             aid = cursor.fetchall()
  348.             aid = aid[0][0]
  349.             # print(aid)
  350.             author = Author(aid, author_name, author_last_name)
  351.             lib.authors.append(author)
  352.             db.commit()
  353.         for book in lib.books:
  354.             if book.name == name_book:
  355.                 found = True
  356.                 print('This book exist')
  357.         if not found:
  358.             date = input('Enter date(yyyy-mm-dd): ')
  359.             count = int(input('Enter count:'))
  360.             available_count = count
  361.             # print(available_count)
  362.             # print(count)
  363.             cursor.execute("INSERT INTO book (id, name, date, count, available_count) VALUES (NULL, '{0}', '{1}', {2}, {3})".format(name_book, date, count, available_count))
  364.             cursor.execute("SELECT last_insert_id()")
  365.             bid = cursor.fetchall()
  366.             bid = bid[0][0]
  367.             db.commit()
  368.             cursor.execute("INSERT INTO book_has_author (book_id, author_id) VALUES ({0},{1})".format(bid, aid))
  369.             db.commit()
  370.             bok = Book(bid, name_book, author_name, date, count, available_count)
  371.             lib.add_book(bok)
  372.     elif number == 5:
  373.         if len(lib.authors) == 0:
  374.             print("Author do not exist")
  375.         for i in lib.authors:
  376.             print('ID:', i.id, ',' + 'name author: ' + i.name + ', last name: ' + i.last_name)
  377.     elif number == 6:
  378.         old_name = input('Enter old name of visitor: ')
  379.         old_name = old_name.capitalize()
  380.         old_last_name = input('Enter old last name of visitor: ')
  381.         old_last_name = old_last_name.capitalize()
  382.         old_number = input('Enter old number of visitor: ')
  383.         if lib.user_exist(old_name, old_last_name, old_number) == False:
  384.             break
  385.         else:
  386.             new_name = input('Enter new name visitor: ')
  387.             new_name = new_name.capitalize()
  388.             new_last_name = input('Enter new last name visitor: ')
  389.             new_last_name = new_last_name.capitalize()
  390.             new_number = input('Enter new number visitor: ')
  391.             cursor.execute("UPDATE visitor SET name = '{0}', last_name = '{1}', number = '{2}' WHERE number = '{3}'". format(new_name, new_last_name, new_number, old_number))
  392.             db.commit()
  393.             if new_last_name == '':
  394.                 lib.edit_visitors(old_name, old_last_name, old_number, new_name, old_last_name, new_number)
  395.             elif new_name == '':
  396.                 lib.edit_visitors(old_name, old_last_name, old_number, old_name, new_last_name, new_number)
  397.             elif new_number == '':
  398.                 lib.edit_visitors(old_name, old_last_name, old_number, new_name, new_last_name, old_number)
  399.             else:
  400.                 lib.edit_visitors(old_name, old_last_name, old_number, new_name, new_last_name, new_number)
  401.     elif number == 7:
  402.         visitor_name = input('Enter visitor name: ')
  403.         visitor_name = visitor_name.capitalize()
  404.         visitor_last_name = input('Enter visitor last name: ')
  405.         visitor_last_name = visitor_last_name.capitalize()
  406.         vis = lib.find_visitor_by_name(visitor_name, visitor_last_name)
  407.         data_take = input('Enter data take(yyyy-mm-dd): ')
  408.         if vis == None:
  409.             print('Enter again visitor')
  410.             break
  411.         else:
  412.             book_name = input('Enter book name: ')
  413.             book_name = book_name.capitalize()
  414.             book_author = input('Enter author name: ')
  415.             book_author = book_author.capitalize()
  416.             bo = lib.find_book_by_name(book_name, book_author)
  417.             if bo == None:
  418.                 print('Enter again book')
  419.                 break
  420.             else:
  421.                 cursor.execute("SELECT available_count FROM book WHERE name = '{}'".format(book_name))
  422.                 avcoif = cursor.fetchall()
  423.                 avcoif = avcoif[0][0]
  424.                 if avcoif == 0:
  425.                     print("This book is not available!")
  426.                 else:
  427.                     lib.write_book_visitor(vis, bo, data_take)
  428.                     cursor.execute("SELECT id FROM visitor WHERE name = '{0}' AND last_name = '{1}'".format(visitor_name, visitor_last_name))
  429.                     idvisitor = cursor.fetchall()
  430.                     idvisitor = idvisitor[0][0]
  431.                     # print(idvisitor)
  432.                     cursor.execute("SELECT id FROM book WHERE name = '{}'".format(book_name))
  433.                     idbook = cursor.fetchall()
  434.                     idbook = idbook[0][0]
  435.                     # print(idbook)
  436.                     cursor.execute("INSERT INTO book_in_visitor (id, taking_date, returning_date, visitor_id, book_id) VALUES (NULL, '{0}', {1}, {2}, {3})".format(data_take, 'NULL', idvisitor, idbook))
  437.                     cursor.execute("SELECT available_count FROM book WHERE id = {}".format(idbook))
  438.                     avco = cursor.fetchall()
  439.                     # print(avco)
  440.                     avco = avco[0][0]
  441.                     # print(avco)
  442.                     result = int(avco) - 1
  443.                     cursor.execute("UPDATE book SET available_count = '{0}' WHERE id = {1}".format(result, idbook))
  444.                     db.commit()
  445.     elif number == 8:
  446.         name1 = input('Enter visitor name: ')
  447.         name1 = name1.capitalize()
  448.         last_na = input('Enter last_name: ')
  449.         last_na = last_na.capitalize()
  450.         if lib.find_visitor_by_name(name1, last_na) == None:
  451.             print("This visitor not exist")
  452.             break
  453.         else:
  454.             book = input('Enter book: ')
  455.             book = book.capitalize()
  456.             author = input('Enter author: ')
  457.             author = author.capitalize()
  458.             if lib.find_book_by_name(book, author) == None:
  459.                 print("This book not exist")
  460.                 break
  461.             else:
  462.                 data = input('Enter data remove(yyyy-mm-dd): ')
  463.                 cursor.execute("SELECT name FROM book")
  464.                 bookname = [row[0] for row in cursor.fetchall()]
  465.                 # print(bookname)
  466.                 cursor.execute("SELECT id FROM visitor WHERE name = '{0}' AND last_name = '{1}'".format(name1, last_na))
  467.                 mid = cursor.fetchall()
  468.                 mid = mid[0][0]
  469.                 # print(mid)
  470.                 for i in bookname:
  471.                     if i == book:
  472.                         cursor.execute("UPDATE book_in_visitor SET returning_date = '{0}' WHERE visitor_id = {1}".format(data, mid))
  473.                         cursor.execute("SELECT available_count FROM book WHERE name = '{}'".format(book))
  474.                         avbo = cursor.fetchall()
  475.                         avbo = avbo[0][0]
  476.                         result = int(avbo) + 1
  477.                         cursor.execute("UPDATE book SET available_count = '{0}' WHERE name = '{1}'".format(result, book))
  478.                         db.commit()
  479.                         lib.add_data_of_remove(name1, last_na, book, author, data)
  480.     elif number == 9:
  481.         for i in lib.history:
  482.             print(i.visitor_name + ' ' + i.visitor_last_name + ' take: ' + i.book_name + ', author: ' + i.book_author + ', date: ' + str(i.data_of_take) + ', return: ' + str(i.remove_of_data))
  483.     elif number == 10:
  484.         i = input("Enter book name: ")
  485.         i = i.capitalize()
  486.         found = False
  487.         for book in lib.books:
  488.             if book.name == i:
  489.                 print('ID: ' + str(book.id) + ', Book: ' + book.name + ', Author: ' + book.author)
  490.                 found = True
  491.         if not found:
  492.             print("Not found")
  493.     elif number == 11:
  494.         i = input("Enter author name: ")
  495.         i = i.capitalize()
  496.         found = False
  497.         for book in lib.books:
  498.             if book.author == i:
  499.                 print('ID: ' + str(book.id) + ', Book: ' + book.name + ', Author: ' + book.author)
  500.                 found = True
  501.         if not found:
  502.             print("Not found")
  503.     elif number == 12:
  504.         name = input("Enter visitor name: ")
  505.         name = name.capitalize()
  506.         last_name = input("Enter visitor last name: ")
  507.         last_name = last_name.capitalize()
  508.         for i in lib.history:
  509.             if i.visitor_name == name and i.visitor_last_name == last_name and i.remove_of_data == 'None':
  510.                 print('Name: ' + i.visitor_name + ', last name: ' + i.visitor_last_name + ', book: ' + i.book_name + ', author: ' + i.book_author + ', date:' + i.data_of_take)
  511.  
  512. # lib = Library()
  513. #
  514. # book1 = Book()
  515. # book1.name = '300 spartans'
  516. # book1.author = 'Zak Plahin'
  517. #
  518. # book2 = Book()
  519. # book2.name = 'Hello'
  520. # book2.author = 'Jack Frans'
  521. #
  522. # book3 = Book()
  523. # book3.name = 'Madame Bovary'
  524. # book3.author = ''
  525. #
  526. # visitor1 = Visitor()
  527. # visitor1.name = 'Oleg'
  528. # visitor1.last_name = 'See'
  529. #
  530. # visitor2 = Visitor()
  531. # visitor2.name = 'Victor'
  532. # visitor2.last_name = 'Selo'
  533. #
  534. # visitor3 = Visitor()
  535. # visitor3.name = 'Victor'
  536. # visitor3.last_name = 'Selo'
  537. #
  538. # lib.add_visitor(visitor1)
  539. # lib.add_visitor(visitor2)
  540. # lib.add_visitor(visitor3)
  541. # lib.add_book(book2)
  542. #
  543. # lib.write_book_visitor(visitor2, book3)
  544. # lib.write_book_visitor(visitor1, book1)
  545. # lib.print_history()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement