Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 22.06 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(0** *** *** *): ')
  309.         if number != int():
  310.             print('Error int number!')
  311.             break
  312.         gender = input('Enter: 1-male / 2-female: ')
  313.         if gender == str(1):
  314.             cursor.execute("select name from gender where id = '{}'".format(1))
  315.             male = cursor.fetchall()
  316.             male = male[0][0]
  317.         elif gender == str(2):
  318.             cursor.execute("select name from gender where id = '{}'".format(2))
  319.             female = cursor.fetchall()
  320.             female = female[0][0]
  321.         else:
  322.             print('Error: gender_id!')
  323.             break
  324.         visit = Visitor(None, first_name, second_name, birth_date, number, gender)
  325.         if lib.add_visitor(visit) == False:
  326.             break
  327.         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))
  328.         cursor.execute("SELECT last_insert_id()")
  329.         vid = cursor.fetchall()
  330.         db.commit()
  331.         # print(vid)
  332.         visit.id = vid[0][0]
  333.         # lib.add_visitor(visit)
  334.     elif number == 4:
  335.         name_book = input('Enter book name: ')
  336.         name_book = name_book.capitalize()
  337.         author_name = input('Enter author name: ')
  338.         author_name = author_name.capitalize()
  339.         author_last_name = input('Enter author last name: ')
  340.         author_last_name = author_last_name.capitalize()
  341.         found = False
  342.         for author in lib.authors:
  343.             if author.name == author_name and author.last_name == author_last_name:
  344.                 found = True
  345.                 aid = author.id
  346.                 db.commit()
  347.         if not found:
  348.             cursor.execute("INSERT INTO author (id, name, last_name) VALUES (NULL, '{0}', '{1}')".format(author_name, author_last_name))
  349.             cursor.execute("Select last_insert_id()")
  350.             aid = cursor.fetchall()
  351.             aid = aid[0][0]
  352.             # print(aid)
  353.             author = Author(aid, author_name, author_last_name)
  354.             lib.authors.append(author)
  355.             db.commit()
  356.         for book in lib.books:
  357.             if book.name == name_book:
  358.                 found = True
  359.                 print('This book exist')
  360.         if not found:
  361.             date = input('Enter date(yyyy-mm-dd): ')
  362.             count = int(input('Enter count:'))
  363.             available_count = count
  364.             # print(available_count)
  365.             # print(count)
  366.             cursor.execute("INSERT INTO book (id, name, date, count, available_count) VALUES (NULL, '{0}', '{1}', {2}, {3})".format(name_book, date, count, available_count))
  367.             cursor.execute("SELECT last_insert_id()")
  368.             bid = cursor.fetchall()
  369.             bid = bid[0][0]
  370.             db.commit()
  371.             cursor.execute("INSERT INTO book_has_author (book_id, author_id) VALUES ({0},{1})".format(bid, aid))
  372.             db.commit()
  373.             bok = Book(bid, name_book, author_name, date, count, available_count)
  374.             lib.add_book(bok)
  375.     elif number == 5:
  376.         if len(lib.authors) == 0:
  377.             print("Author do not exist")
  378.         for i in lib.authors:
  379.             print('ID:', i.id, ',' + 'name author: ' + i.name + ', last name: ' + i.last_name)
  380.     elif number == 6:
  381.         old_name = input('Enter old name of visitor: ')
  382.         old_name = old_name.capitalize()
  383.         old_last_name = input('Enter old last name of visitor: ')
  384.         old_last_name = old_last_name.capitalize()
  385.         old_number = input('Enter old number of visitor: ')
  386.         if old_number != int():
  387.             print('Error int number!')
  388.             break
  389.         if lib.user_exist(old_name, old_last_name, old_number) == False:
  390.             break
  391.         else:
  392.             new_name = input('Enter new name visitor: ')
  393.             new_name = new_name.capitalize()
  394.             new_last_name = input('Enter new last name visitor: ')
  395.             new_last_name = new_last_name.capitalize()
  396.             new_number = input('Enter new number visitor: ')
  397.             if new_number != int():
  398.                 print('Error int number!')
  399.                 break
  400.             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))
  401.             db.commit()
  402.             if new_last_name == '':
  403.                 lib.edit_visitors(old_name, old_last_name, old_number, new_name, old_last_name, new_number)
  404.             elif new_name == '':
  405.                 lib.edit_visitors(old_name, old_last_name, old_number, old_name, new_last_name, new_number)
  406.             elif new_number == '':
  407.                 lib.edit_visitors(old_name, old_last_name, old_number, new_name, new_last_name, old_number)
  408.             else:
  409.                 lib.edit_visitors(old_name, old_last_name, old_number, new_name, new_last_name, new_number)
  410.     elif number == 7:
  411.         visitor_name = input('Enter visitor name: ')
  412.         visitor_name = visitor_name.capitalize()
  413.         visitor_last_name = input('Enter visitor last name: ')
  414.         visitor_last_name = visitor_last_name.capitalize()
  415.         vis = lib.find_visitor_by_name(visitor_name, visitor_last_name)
  416.         data_take = input('Enter data take(yyyy-mm-dd): ')
  417.         if vis == None:
  418.             print('Enter again visitor')
  419.             break
  420.         else:
  421.             book_name = input('Enter book name: ')
  422.             book_name = book_name.capitalize()
  423.             book_author = input('Enter author name: ')
  424.             book_author = book_author.capitalize()
  425.             bo = lib.find_book_by_name(book_name, book_author)
  426.             if bo == None:
  427.                 print('Enter again book')
  428.                 break
  429.             else:
  430.                 cursor.execute("SELECT available_count FROM book WHERE name = '{}'".format(book_name))
  431.                 avcoif = cursor.fetchall()
  432.                 avcoif = avcoif[0][0]
  433.                 if avcoif == 0:
  434.                     print("This book is not available!")
  435.                 else:
  436.                     lib.write_book_visitor(vis, bo, data_take)
  437.                     cursor.execute("SELECT id FROM visitor WHERE name = '{0}' AND last_name = '{1}'".format(visitor_name, visitor_last_name))
  438.                     idvisitor = cursor.fetchall()
  439.                     idvisitor = idvisitor[0][0]
  440.                     # print(idvisitor)
  441.                     cursor.execute("SELECT id FROM book WHERE name = '{}'".format(book_name))
  442.                     idbook = cursor.fetchall()
  443.                     idbook = idbook[0][0]
  444.                     # print(idbook)
  445.                     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))
  446.                     cursor.execute("SELECT available_count FROM book WHERE id = {}".format(idbook))
  447.                     avco = cursor.fetchall()
  448.                     # print(avco)
  449.                     avco = avco[0][0]
  450.                     # print(avco)
  451.                     result = int(avco) - 1
  452.                     cursor.execute("UPDATE book SET available_count = '{0}' WHERE id = {1}".format(result, idbook))
  453.                     db.commit()
  454.     elif number == 8:
  455.         name1 = input('Enter visitor name: ')
  456.         name1 = name1.capitalize()
  457.         last_na = input('Enter last_name: ')
  458.         last_na = last_na.capitalize()
  459.         if lib.find_visitor_by_name(name1, last_na) == None:
  460.             print("This visitor not exist")
  461.             break
  462.         else:
  463.             book = input('Enter book: ')
  464.             book = book.capitalize()
  465.             author = input('Enter author: ')
  466.             author = author.capitalize()
  467.             if lib.find_book_by_name(book, author) == None:
  468.                 print("This book not exist")
  469.                 break
  470.             else:
  471.                 data = input('Enter data remove(yyyy-mm-dd): ')
  472.                 cursor.execute("SELECT name FROM book")
  473.                 bookname = [row[0] for row in cursor.fetchall()]
  474.                 # print(bookname)
  475.                 cursor.execute("SELECT id FROM visitor WHERE name = '{0}' AND last_name = '{1}'".format(name1, last_na))
  476.                 mid = cursor.fetchall()
  477.                 mid = mid[0][0]
  478.                 # print(mid)
  479.                 for i in bookname:
  480.                     if i == book:
  481.                         cursor.execute("UPDATE book_in_visitor SET returning_date = '{0}' WHERE visitor_id = {1}".format(data, mid))
  482.                         cursor.execute("SELECT available_count FROM book WHERE name = '{}'".format(book))
  483.                         avbo = cursor.fetchall()
  484.                         avbo = avbo[0][0]
  485.                         result = int(avbo) + 1
  486.                         cursor.execute("UPDATE book SET available_count = '{0}' WHERE name = '{1}'".format(result, book))
  487.                         db.commit()
  488.                         lib.add_data_of_remove(name1, last_na, book, author, data)
  489.     elif number == 9:
  490.         for i in lib.history:
  491.             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))
  492.     elif number == 10:
  493.         i = input("Enter book name: ")
  494.         i = i.capitalize()
  495.         found = False
  496.         for book in lib.books:
  497.             if book.name == i:
  498.                 print('ID: ' + str(book.id) + ', Book: ' + book.name + ', Author: ' + book.author)
  499.                 found = True
  500.         if not found:
  501.             print("Not found")
  502.     elif number == 11:
  503.         i = input("Enter author name: ")
  504.         i = i.capitalize()
  505.         found = False
  506.         for book in lib.books:
  507.             if book.author == i:
  508.                 print('ID: ' + str(book.id) + ', Book: ' + book.name + ', Author: ' + book.author)
  509.                 found = True
  510.         if not found:
  511.             print("Not found")
  512.     elif number == 12:
  513.         name = input("Enter visitor name: ")
  514.         name = name.capitalize()
  515.         last_name = input("Enter visitor last name: ")
  516.         last_name = last_name.capitalize()
  517.         for i in lib.history:
  518.             if i.visitor_name == name and i.visitor_last_name == last_name and i.remove_of_data == 'None':
  519.                 print('Name: ' + i.visitor_name + ', last name: ' + i.visitor_last_name + ', book: ' + i.book_name + ', author: ' + i.book_author + ', date:' + i.data_of_take)
  520.  
  521. # lib = Library()
  522. #
  523. # book1 = Book()
  524. # book1.name = '300 spartans'
  525. # book1.author = 'Zak Plahin'
  526. #
  527. # book2 = Book()
  528. # book2.name = 'Hello'
  529. # book2.author = 'Jack Frans'
  530. #
  531. # book3 = Book()
  532. # book3.name = 'Madame Bovary'
  533. # book3.author = ''
  534. #
  535. # visitor1 = Visitor()
  536. # visitor1.name = 'Oleg'
  537. # visitor1.last_name = 'See'
  538. #
  539. # visitor2 = Visitor()
  540. # visitor2.name = 'Victor'
  541. # visitor2.last_name = 'Selo'
  542. #
  543. # visitor3 = Visitor()
  544. # visitor3.name = 'Victor'
  545. # visitor3.last_name = 'Selo'
  546. #
  547. # lib.add_visitor(visitor1)
  548. # lib.add_visitor(visitor2)
  549. # lib.add_visitor(visitor3)
  550. # lib.add_book(book2)
  551. #
  552. # lib.write_book_visitor(visitor2, book3)
  553. # lib.write_book_visitor(visitor1, book1)
  554. # lib.print_history()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement