예제 #1
0
 def __init__(self, bookList, clientList):
     '''
     Constructor
     '''
     self.__bookList = bookList
     self.__clientList = clientList
     self.__uiUtilities = Utilities()
예제 #2
0
class Console:
    def __init__(self, libraryController):
        """
        Constructor for the UI class
        Adds the corresponding functions to the menu options
        libraryController is an instance of the controller used in the application
        O(1)
        """
        self.__menu_options = {
            '1': self.__menu1,
            '2': self.__menu2,
            '3': self.__menu3,
            '4': self.__menu4,
            '5': self.__menu5,
            '6': self.__menu6,
            '7': self.__menu7
        }
        self.__submenu1_options = {
            '1': self.__addNewBook,
            '2': self.__removeBook,
            '3': self.__updateBook,
            '4': self.__listBooks
        }
        self.__submenu2_options = {
            '1': self.__addNewClient,
            '2': self.__removeClient,
            '3': self.__updateClient,
            '4': self.__listClients
        }
        self.__submenu7_options = {
            '1': self.__orderBooksByMostRented,
            '2': self.__orderBooksByLeastRented,
            '3': self.__orderClientsByMostActive,
            '4': self.__orderClientsByLeastActive
        }

        self.__libraryController = libraryController

        self.showMenu()

    __menu = "(1) Manage books\n" \
            "(2) Manage clients\n" \
            "(3) Search for a book\n" \
            "(4) Search for a client\n" \
            "(5) Lend a book\n" \
            "(6) Return a book\n" \
            "(7) Reports"
    __submenu1 = "(1) Add a new book\n" \
                "(2) Remove a book\n" \
                "(3) Update a book\n" \
                "(4) List books"
    __submenu2 = "(1) Add a new client\n" \
                "(2) Remove a client\n" \
                "(3) Update a client\n" \
                "(4) List clients"
    __submenu7 = "(1) Order books by most rented\n" \
                "(2) Order books by least rented\n" \
                "(3) Order clients by most active\n" \
                "(4) Order clients by least active"
    __back = "(x) Back"
    __exit = "(x) Exit app"
    __divider = "---------------------"
    __exitValue = "x"
    __backValue = "x"

    __uiUtilities = Utilities()

    def __menu1(self):
        """
        Menu entry 1
        Prints out the submenu for menu 1
        O(1)
        """
        user_input = ""
        while user_input != self.__backValue:
            print self.__submenu1
            print self.__divider
            print self.__back
            user_input = raw_input("Please enter your choice:")
            try:
                self.__submenu1_options[user_input]()
            except KeyError:
                if user_input != 'x':
                    raw_input("*INVALID CHOICE!*")

    def __menu2(self):
        """
        Menu entry 2
        Prints out the submenu for menu 2
        O(1)
        """
        user_input = ""
        while user_input != self.__backValue:
            print self.__submenu2
            print self.__divider
            print self.__back
            user_input = raw_input("Please enter your choice:")
            try:
                self.__submenu2_options[user_input]()
            except KeyError:
                if user_input != 'x':
                    raw_input("*INVALID CHOICE!*")

    def __menu3(self):
        """
        Menu entry 3
        O(n)
        """
        searchQuery = raw_input(
            "Please enter the title or author or description of the book:")
        foundBooks = self.__libraryController.searchBooks(searchQuery)
        if foundBooks == []:
            self.__uiUtilities.printMessage("not found", "search", "book")
        else:
            self.__uiUtilities.printList(foundBooks)

    def __menu4(self):
        """
        Menu entry 4
        O(n)
        """
        searchQuery = raw_input(
            "Please enter the name or the cnp of the client:")
        foundClients = self.__libraryController.searchClients(searchQuery)
        if foundClients == []:
            self.__uiUtilities.printMessage("not found", "search", "client")
        else:
            self.__uiUtilities.printList(foundClients)

    def __menu5(self):
        """
        Menu entry 5
        O(n)
        """
        bookName = raw_input("Please enter the title:")
        bookAuthor = raw_input("Please enter the author:")
        bookDescription = raw_input("Please enter the description:")
        clientName = raw_input("Please enter the client's name:")
        clientCnp = raw_input("Please enter the client's CNP:")
        status = self.__libraryController.lendBook(bookName, bookAuthor, bookDescription, \
                                              clientName, clientCnp)
        if status == "book not found":
            self.__uiUtilities.printMessage("not found", "lend", "book")
        elif status == "client not found":
            self.__uiUtilities.printMessage("not found", "lend", "client")
        elif status == "all books borrowed":
            self.__uiUtilities.printMessage("borrowed", "lend", "book")
        elif status == True:
            self.__uiUtilities.printMessage("success", "lend", "book")

    def __menu6(self):
        """
        Menu entry 6
        O(n)
        """
        bookTitle = raw_input("Please enter the title:")
        bookAuthor = raw_input("Please enter the author:")
        bookDescription = raw_input("Please enter the description:")
        clientName = raw_input("Please enter the client's name:")
        clientCnp = raw_input("Please enter the client's cnp:")
        status = self.__libraryController.returnBook(bookTitle, bookAuthor, bookDescription, \
                                            clientName, clientCnp)
        if status == "book not found":
            self.__uiUtilities.printMessage("not found", "return", "book")
        elif status == "client not found":
            self.__uiUtilities.printMessage("not found", "return", "client")
        elif status == False:
            self.__uiUtilities.printMessage("not borrowed", "return", "book")
        elif status == True:
            self.__uiUtilities.printMessage("success", "return", "book")

    def __menu7(self):
        """
        Menu entry 7
        Prints out the submenu for menu entry 7
        O(1)
        """
        user_input = ""
        while user_input != self.__backValue:
            print self.__submenu7
            print self.__divider
            print self.__back
            user_input = raw_input("Please enter your choice:")
            try:
                self.__submenu7_options[user_input]()
            except KeyError:
                if user_input != 'x':
                    raw_input("*!INVALID CHOICE!*")

    def __addNewBook(self):
        """
        Private UI method for adding a new book
        O(1)
        """
        title = raw_input("Book title:")
        author = raw_input("Book author:")
        description = raw_input("Book description")
        status = self.__libraryController.addNewBook(title, author,
                                                     description)
        if status == None:
            self.__uiUtilities.printMessage("success", "add", "book")
        else:
            self.__uiUtilities.printList(status)

    def __removeBook(self):
        """
        Private UI method for removing a book
        Best case O(1)
        Worst case O(n)
        Average case O(n)
        """
        title = raw_input("Book title:")
        author = raw_input("Book author:")
        description = raw_input("Book description")
        if not self.__libraryController.removeBook(title, author, description):
            self.__uiUtilities.printMessage("not found", "remove", "book")
        else:
            self.__uiUtilities.printMessage("success", "remove", "book")

    def __updateBook(self):
        """
        Private UI method to update a book
        O(n)
        """
        title = raw_input("Book title:")
        author = raw_input("Book author:")
        description = raw_input("Book description")
        newTitle = raw_input("New book title (leave blank if not changed):")
        newAuthor = raw_input("New book author (leave blank if not changed):")
        newDescription = raw_input(
            "New book description (leave blank if not changed):")
        if not self.__libraryController.updateBook(title, author, description, \
                                                    newTitle, newAuthor, newDescription):
            self.__uiUtilities.printMessage("not found", "update", "book")
        else:
            self.__uiUtilities.printMessage("success", "update", "book")

    def __listBooks(self):
        """
        Private UI method to print out the book list
        O(1)
        """
        self.__libraryController.listBooks()

    def __addNewClient(self):
        """
        Private UI method to add a new client
        O(n)
        """
        name = raw_input("Client name:")
        cnp = raw_input("Client cnp:")
        status = self.__libraryController.addNewClient(name, cnp)
        if status == None:
            self.__uiUtilities.printMessage("success", "add", "client")
        else:
            self.__uiUtilities.printList(status)

    def __removeClient(self):
        """
        Private UI method to remove a client
        O(n)
        """
        name = raw_input("Client name:")
        cnp = raw_input("Client cnp:")
        if not self.__libraryController.removeClient(name, cnp):
            self.__uiUtilities.printMessage("not found", "remove", "client")
        else:
            self.__uiUtilities.printMessage("success", "remove", "client")

    def __updateClient(self):
        """
        Private UI method to update a client
        O(n)
        """
        name = raw_input("Client name:")
        cnp = raw_input("Client cnp:")
        newName = raw_input("New client name (leave blank if unchanged):")
        newCnp = raw_input("New client cnp (leave blank if unchanged):")
        if not self.__libraryController.updateClient(name, cnp, newName,
                                                     newCnp):
            self.__uiUtilities.printMessage("not found", "update", "client")
        else:
            self.__uiUtilities.printMessage("success", "update", "client")

    def __listClients(self):
        """
        Private UI method to print out the list of clients
        O(1)
        """
        self.__libraryController.listClients()

    def __orderBooksByMostRented(self):
        """
        Private UI method to order books by most rented
        O(n)
        """
        self.__libraryController.orderBooks("desc")

    def __orderBooksByLeastRented(self):
        """
        Private UI method to order books by least rented
        O(n)
        """
        self.__libraryController.orderBooks("asc")

    def __orderClientsByMostActive(self):
        """
        Private UI method to order clients by most active
        O(n)
        """
        self.__libraryController.orderClients("desc")

    def __orderClientsByLeastActive(self):
        """
        Private UI method to order clients by least active
        O(n)
        """
        self.__libraryController.orderClients("asc")

    def showMenu(self):
        """
        UI method to print out and handle the main menu
        O(1)
        """
        user_input = ""
        while user_input != self.__exitValue:
            print self.__menu
            print self.__divider
            print self.__exit
            user_input = raw_input("Please enter your choice:")
            try:
                self.__menu_options[user_input]()
                user_input = ""
            except KeyError:
                if user_input != 'x':
                    raw_input("*INVALID CHOICE!*")
예제 #3
0
class LibraryController:
    '''
    This is the controller class.
    All messages from the ui to the domain and vice-versa must pass through this controller.
    '''

    def __init__(self, bookList, clientList):
        '''
        Constructor
        '''
        self.__bookList = bookList
        self.__clientList = clientList
        self.__uiUtilities = Utilities()
    
    #------Books
    
    def searchBooks(self, searchQuery):
        """
        Searches for books with a given detail (title, author, description)
        searchQuery is a string
        Returns a list of books based on the search query
        O(n)
        """
        return self.__bookList.findBook(searchQuery)
    
    def searchClients(self, searchQuery):
        """
        Searches for clients with a given detail (name, cnp)
        searchClients is a string
        Returns a list of books based on the search query
        O(n)
        """
        return self.__clientList.findClient(searchQuery)
    
    def lendBook(self, bookTitle, bookAuthor, bookDescription, clientName, clientCnp):
        """
        Controller method to lend a book
        Returns "book not found" or "client not found" if the book/client was not found in the lists
        Returns "all books borrowed" if all the books with those details are borrowed
        Returns True if everything is ok
        O(n+m)
        """
        booksInList = self.__bookList.bookInList(bookTitle, bookAuthor, bookDescription)
        if booksInList == []:
            return "book not found"
        clientInList = self.__clientList.clientInList(clientName, clientCnp)
        if clientInList == False:
            return "client not found"
        book = self.__bookList.lendBook(booksInList, clientInList)
        if not book:
            return "all books borrowed"
        self.__clientList.borrowBook(book, clientInList)
        return True
            
    
    def returnBook(self, bookTitle, bookAuthor, bookDescription, \
                   clientName, clientCnp):
        """
        Controller method to return a book
        Returns "book not found" if the book with those details was not found in the list
        Returns "client not found" if the client with those details does not exist
        Returns "book is not borrowed" if the book with those details is not borrowed
        Returns True if everything is ok
        O(n+m)
        """
        booksInList = self.__bookList.bookInList(bookTitle, bookAuthor, bookDescription)
        if booksInList == []:
            return "book not found"
        clientInList = self.__clientList.clientInList(clientName, clientCnp)
        if clientInList == False:
            return "client not found"
        book = self.__bookList.returnBook(booksInList, clientInList)
        if not book:
            return False
        self.__clientList.returnBook(book, clientInList)
        return True
    
    def addNewBook(self, title, author, description):
        """
        Controller method to add a new book
        the parameters are all strings
        O(1)
        """
        return self.__bookList.addNewBook(title, author, description)

    def removeBook(self, title, author, description):
        """
        Controller Method to remove a book
        the parameters are all strings
        Returns False if the book was not found, True otherwise
        Best case O(1)
        Worst case O(n)
        Average case O(n)
        """
        book = Book(title, author, description)
        if not self.__bookList.removeBook(book, 0):
            return False
        return True
     
    def updateBook(self, oldTitle, oldAuthor, oldDescription, \
                   newTitle, newAuthor, newDescription):
        """
        Updates the details of a book from the list
        the parameters are all strings
        Returns False if the book is not found
        Returns True if everything is ok
        O(n)
        """
        if newTitle == "":
            newTitle = oldTitle
        if newAuthor == "":
            newAuthor = oldAuthor
        if newDescription == "":
            newDescription = oldDescription
        if not self.__bookList.updateBook(oldTitle, oldAuthor, oldDescription, \
                   newTitle, newAuthor, newDescription):
            return False
        return True
        
    def listBooks(self):
        """
        Prints out the entire list of books
        O(n)
        """
        self.__uiUtilities.printObjectList(self.__bookList)
        
    def orderBooks(self, direction):
        """
        Orders the book list by popularity ascending or descending.
        direction is a string "asc" or "desc"
        O(n)
        """
        self.__uiUtilities.printList(self.__bookList.orderBooks(direction))

    #------Clients

    def addNewClient(self, name, cnp):
        """
        Controller method to add a new client
        name and cnp are strings
        O(n)
        """
        return self.__clientList.addNewClient(name, cnp)

    def removeClient(self, name, cnp):
        """
        Private method to remove a client
        name and cnp are strings
        Returns False if the client was not found, True otherwise
        O(n)
        """
        if not self.__clientList.removeClient(name, cnp):
            return False
        return True

    def updateClient(self, oldName, oldCnp, newName, newCnp):
        """
        Updates the details of a client from the list
        All parameters are strings
        Returns False if the client was not found, True otherwise
        O(n)
        """
        if newName == "":
            newName = oldName
        if newCnp == "":
            newCnp = oldCnp
        if not self.__clientList.updateClient(oldName, oldCnp, newName, newCnp):
            return False
        return True     
            
    def listClients(self):
        """
        Prints out the entire list of clients
        O(n)
        """
        self.__uiUtilities.printObjectList(self.__clientList)
        
    def orderClients(self, direction):
        """
        Orders the client list by activity ascending or descending.
        direction is a string "asc" or "desc"
        O(n)
        """
        self.__uiUtilities.printList(self.__clientList.orderClients(direction))