|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 19, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "class Book:\n", |
| 10 | + " \n", |
| 11 | + " BOOK_TYPES = (\"HARDCOVER\", \"PAPERBACK\", \"EBOOK\")\n", |
| 12 | + " __booklist = None\n", |
| 13 | + " \n", |
| 14 | + " @classmethod\n", |
| 15 | + " def getbooktypes(cls):\n", |
| 16 | + " return cls.BOOK_TYPES\n", |
| 17 | + " \n", |
| 18 | + " \n", |
| 19 | + " def __init__(self, title, author, pages, price, booktype):\n", |
| 20 | + " self.title = title\n", |
| 21 | + " self.author = author\n", |
| 22 | + " self.pages = pages\n", |
| 23 | + " self.price = price\n", |
| 24 | + " if (not booktype in Book.BOOK_TYPES):\n", |
| 25 | + " raise ValueError(f\"{booktype} is not a valid booktype.\")\n", |
| 26 | + " self.booktype = booktype\n", |
| 27 | + " \n", |
| 28 | + " @staticmethod\n", |
| 29 | + " def getbooklist():\n", |
| 30 | + " if Book.__booklist == None:\n", |
| 31 | + " Book.__booklist = []\n", |
| 32 | + " return Book.__booklist\n", |
| 33 | + " \n", |
| 34 | + " def getprice(self):\n", |
| 35 | + " if hasattr(self, \"_discount\"):\n", |
| 36 | + " return self.price - (self.price*self._discount)\n", |
| 37 | + " else:\n", |
| 38 | + " return self.price\n", |
| 39 | + " \n", |
| 40 | + " def setdiscount(self,amount):\n", |
| 41 | + " self._discount = amount\n", |
| 42 | + " \n" |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "code", |
| 47 | + "execution_count": 9, |
| 48 | + "metadata": {}, |
| 49 | + "outputs": [], |
| 50 | + "source": [ |
| 51 | + "b1 = Book(\"War and Peace\", \"Leo Tolstoy\", 1225, 39.95)\n", |
| 52 | + "b2 = Book(\"The catcher in the Rye\", \"JD Salinger\", 234, 29.95)\n", |
| 53 | + "\n" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "code", |
| 58 | + "execution_count": 10, |
| 59 | + "metadata": {}, |
| 60 | + "outputs": [ |
| 61 | + { |
| 62 | + "name": "stdout", |
| 63 | + "output_type": "stream", |
| 64 | + "text": [ |
| 65 | + "39.95\n" |
| 66 | + ] |
| 67 | + } |
| 68 | + ], |
| 69 | + "source": [ |
| 70 | + "print(b1.getprice())" |
| 71 | + ] |
| 72 | + }, |
| 73 | + { |
| 74 | + "cell_type": "code", |
| 75 | + "execution_count": 11, |
| 76 | + "metadata": {}, |
| 77 | + "outputs": [ |
| 78 | + { |
| 79 | + "name": "stdout", |
| 80 | + "output_type": "stream", |
| 81 | + "text": [ |
| 82 | + "29.95\n", |
| 83 | + "22.4625\n" |
| 84 | + ] |
| 85 | + } |
| 86 | + ], |
| 87 | + "source": [ |
| 88 | + "print(b2.getprice())\n", |
| 89 | + "b2.setdiscount(0.25)\n", |
| 90 | + "print(b2.getprice())" |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "cell_type": "code", |
| 95 | + "execution_count": 20, |
| 96 | + "metadata": {}, |
| 97 | + "outputs": [ |
| 98 | + { |
| 99 | + "name": "stdout", |
| 100 | + "output_type": "stream", |
| 101 | + "text": [ |
| 102 | + "Book types: ('HARDCOVER', 'PAPERBACK', 'EBOOK')\n" |
| 103 | + ] |
| 104 | + } |
| 105 | + ], |
| 106 | + "source": [ |
| 107 | + "\n", |
| 108 | + "print(\"Book types:\", Book.getbooktypes())\n", |
| 109 | + "b1 = Book(\"War and Peace\", \"Leo Tolstoy\", 1225, 39.95, \"HARDCOVER\")\n", |
| 110 | + "b2 = Book(\"War and Peace\", \"Leo Tolstoy\", 1225, 39.95, \"PAPERBACK\")\n" |
| 111 | + ] |
| 112 | + }, |
| 113 | + { |
| 114 | + "cell_type": "code", |
| 115 | + "execution_count": 22, |
| 116 | + "metadata": {}, |
| 117 | + "outputs": [], |
| 118 | + "source": [ |
| 119 | + "booklist = Book.getbooklist()\n", |
| 120 | + "booklist.append(b1)\n", |
| 121 | + "booklist.append(b2)" |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "code", |
| 126 | + "execution_count": 23, |
| 127 | + "metadata": {}, |
| 128 | + "outputs": [ |
| 129 | + { |
| 130 | + "data": { |
| 131 | + "text/plain": [ |
| 132 | + "[<__main__.Book at 0x1d32693c6a0>, <__main__.Book at 0x1d32693c970>]" |
| 133 | + ] |
| 134 | + }, |
| 135 | + "execution_count": 23, |
| 136 | + "metadata": {}, |
| 137 | + "output_type": "execute_result" |
| 138 | + } |
| 139 | + ], |
| 140 | + "source": [ |
| 141 | + "booklist" |
| 142 | + ] |
| 143 | + }, |
| 144 | + { |
| 145 | + "cell_type": "code", |
| 146 | + "execution_count": 24, |
| 147 | + "metadata": {}, |
| 148 | + "outputs": [ |
| 149 | + { |
| 150 | + "data": { |
| 151 | + "text/plain": [ |
| 152 | + "'War and Peace'" |
| 153 | + ] |
| 154 | + }, |
| 155 | + "execution_count": 24, |
| 156 | + "metadata": {}, |
| 157 | + "output_type": "execute_result" |
| 158 | + } |
| 159 | + ], |
| 160 | + "source": [ |
| 161 | + "booklist[0].title" |
| 162 | + ] |
| 163 | + }, |
| 164 | + { |
| 165 | + "cell_type": "code", |
| 166 | + "execution_count": null, |
| 167 | + "metadata": {}, |
| 168 | + "outputs": [], |
| 169 | + "source": [] |
| 170 | + } |
| 171 | + ], |
| 172 | + "metadata": { |
| 173 | + "kernelspec": { |
| 174 | + "display_name": "Python 3", |
| 175 | + "language": "python", |
| 176 | + "name": "python3" |
| 177 | + }, |
| 178 | + "language_info": { |
| 179 | + "codemirror_mode": { |
| 180 | + "name": "ipython", |
| 181 | + "version": 3 |
| 182 | + }, |
| 183 | + "file_extension": ".py", |
| 184 | + "mimetype": "text/x-python", |
| 185 | + "name": "python", |
| 186 | + "nbconvert_exporter": "python", |
| 187 | + "pygments_lexer": "ipython3", |
| 188 | + "version": "3.8.5" |
| 189 | + } |
| 190 | + }, |
| 191 | + "nbformat": 4, |
| 192 | + "nbformat_minor": 4 |
| 193 | +} |
0 commit comments