Object oriented
{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "BA865 - HW01.ipynb", "provenance": [], "private_outputs": true, "collapsed_sections": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "d7WQ-S46ORa-", "colab_type": "text" }, "source": [ "#HW 01 - Implement Classes to Represent a Book Library" ] }, { "cell_type": "markdown", "metadata": { "id": "NUQtrLBoOiDI", "colab_type": "text" }, "source": [ "
\n", "Before you submit this assignment, please carefully read the submission instructions in blue text below. You must name this .ipynb file:\n", " \n", "yourlastname_yourfirstname_HW1.ipynb\n", " \n", "You must turn in this assignment by uploading the \n", ".ipynb file to the assignment on questrom tools. Do not email me the file.\n", " \n", "Points will be deducted for improper submission!\n", "\n", "\n", "I have created a Library class to represent a book library.\n", "\n", "\n", "Your task is to create two classes:\n", " - A book class called `Book`\n", " - A patron class (a customer of the library) called `Patron`\n", "\n", "These classes will interact with my Library class to facilitate patrons checking out books from the library.\n", " \n", "\n", "## The Book Class - Specifications\n", "The `Book` class needs to have the following **properties**:\n", " - `isbn` - a string holding the isbn number of the book\n", " - `title` - a string holding the title of the book\n", " - `author` - a string holding the author of the book\n", " \n", "\n", "## The Patron Class - Specifications\n", "The `Patron` class needs to have:\n", "- the following **properties**:\n", " - `id` - a string holding the id of the patron\n", " - `name` - a string holding the name of the patron\n", " - `booksOnLoan` - a dictionary with key `isbn` and value `bookObject`\n", "- the following **methods**:\n", " - `checkOutBook(isbn,library)` for checking out a book from a library that takes the arguments:\n", " - `isbn` - a book isbn number (string)\n", " - `library` - a library object \n", " - `checkInBook(isbn, library)` for checking back in a book to the library that takes the arguments:\n", " - `isbn` - a book isbn number (string)\n", " - `library` - a library object\n", "- Both the `checkInBook()` and `checkOutBook()` methods should print out what is happening: \n", " - e.g., \"Patron Dylan Walker (52103) is checking out B006NZWXO2\"\n", " - follow this format\n", "- Both the `checkInBook()` and `checkOutBook()` methods of patron must call the library's `checkInBook()` and `checkOutBook()` methods and pass the appropriate arguments.\n", " - Within the code of the `checkInBook()` and `checkOutBook()` methods of patron, you can pass the patron object using the keyword `self` to the library's `checkInBook()` and `checkOutBook()` methods (which both require a patron object as one of their arguments).\n", "\n", " \n", "\n", "Read over the code that defines my Library class carefully. Make sure you understand it. **You cannot change my Library class at all.**\n", " \n", "\n", "Some things to note:\n", "- notice how I establish empty dictionaries as properties of `Library` to hold the books, number of copies of each book, and the books that patrons have on loan.\n", "- notice how `Library.checkOutBook()` will return `None` if the book isn't in the library or has no copies currently; otherwise it will return a book object.\n", "- notice how `Library.checkInBook()` will return `False` if the book isn't carried by the Library or if isn't in the library's `patronLoans` (i.e., the patron didn't check out the book from this library); otherwise it will return `True`\n", "- You should make use of these returns when you implement the `checkInBook()` and `checkOutBook()` methods for patron.\n", "\n", "Your solution must be able to run properly with the test code that I have provided below. **You cannot change the test code at all.**" ] }, { "cell_type": "code", "metadata": { "id": "U2TXtLBPOQIz", "colab_type": "code", "colab": {} }, "source": [ "# My Library Class\n", "# You cannot change anything in this class\n", "# Run this code cell before you run your own below.\n", "class Library():\n", " def __init__(self):\n", " self.books = {} # key: isbn ; value: bookObject\n", " self.bookCopies = {} # key: isbn ; value: numCopies (int)\n", " self.patronLoans = {} # key: patronId ; value: list of isbn numbers representing the books the patron has checked out\n", " \n", " def addBook(self,book,numCopies):\n", " self.books[book.isbn] = book\n", " self.bookCopies[book.isbn] = numCopies\n", " \n", " def checkOutBook(self,isbn,patron):\n", " if not isbn in self.books.keys():\n", " print(\"Sorry, we don't have that book in the library\")\n", " return None\n", " elif self.bookCopies[isbn] == 0:\n", " print(f\"Sorry, we don't have any copies of '{self.books[isbn].title}' available. Try back later.\")\n", " return None\n", " else:\n", " if not patron.id in self.patronLoans.keys():\n", " self.patronLoans[patron.id] = [] # patron hasn't checked out a book here before; make an empty list\n", " self.patronLoans[patron.id].append(isbn)\n", " self.bookCopies[isbn]-=1\n", " book = self.books[isbn]\n", " return book\n",