diff --git a/hw_6/hw_6_stock_SOLID.ipynb b/hw_6/hw_6_stock_SOLID.ipynb new file mode 100644 index 0000000..a5fd91b --- /dev/null +++ b/hw_6/hw_6_stock_SOLID.ipynb @@ -0,0 +1,1205 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 95, + "id": "1feb8d79-cb18-4be4-a2e4-4c3c7c4d762d", + "metadata": {}, + "outputs": [], + "source": [ + "# S" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "8570e64b-68d2-4737-9945-7ff0f3a53379", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "****************************************************************\n", + "Total number of products available: 6\n", + "products = {'Kitkat', 'Potato', 'Pepsi', 'Coke', 'Tapioco', 'Hersheys'}\n", + "Can {'Coke': {'price': 5.0, 'quantity': 5, 'volume': 500}, 'Pepsi': {'price': 4.0, 'quantity': 4, 'volume': 250}}\n", + "Chips {'Potato': {'price': 10.0, 'quantity': 2, 'flavour': 'salt', 'packet_size': 100}, 'Tapioco': {'price': 10.0, 'quantity': 3, 'flavour': 'chilly', 'packet_size': 100}}\n", + "Chocolate {'Kitkat': {'price': 1.0, 'quantity': 15}, 'Hersheys': {'price': 2.0, 'quantity': 20}}\n", + "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n", + "You have chosen Chocolate Kitkat\n", + "feed in atleast 1.0 or more: \n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + " 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Here is the balance : 1.0\n", + "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n", + "Thank you for choosing this vending machine\n", + "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n", + "Can {'Coke': {'price': 5.0, 'quantity': 5, 'volume': 500}, 'Pepsi': {'price': 4.0, 'quantity': 4, 'volume': 250}}\n", + "Chips {'Potato': {'price': 10.0, 'quantity': 2, 'flavour': 'salt', 'packet_size': 100}, 'Tapioco': {'price': 10.0, 'quantity': 3, 'flavour': 'chilly', 'packet_size': 100}}\n", + "Chocolate {'Kitkat': {'price': 1.0, 'quantity': 14}, 'Hersheys': {'price': 2.0, 'quantity': 20}}\n" + ] + } + ], + "source": [ + "class Stock:\n", + " counter = 0\n", + " stock_set = set()\n", + "\n", + " def __init__(self, stock_code, desc, price, available):\n", + " self.stock_code = stock_code\n", + " self.desc = desc\n", + " self.price = price\n", + " self.available = available\n", + " Stock.counter += 1\n", + " Stock.stock_set.add(self.desc)\n", + "\n", + " def sell_price(self):\n", + " sp = round(self.price, 2)\n", + " print('cost = $', sp)\n", + "\n", + " def label(self):\n", + " print(f\"{self.desc} costs you {self.price}\")\n", + "\n", + " def sale(self, discount):\n", + " new_sp = round(self.price * (1 - discount), 2)\n", + " print('the discounted price of {} is $ {}'.format((self.stock_code, self.desc), new_sp))\n", + " return new_sp\n", + "\n", + "\n", + "class Chocolate(Stock):\n", + "\n", + " category = 'chocolate'\n", + "\n", + " def __init__(self, stock_code, desc, price, available):\n", + " super().__init__(stock_code, desc, price, available)\n", + "\n", + " def __repr__(self):\n", + " print(\"You have chosen = {self.label}\")\n", + "\n", + "\n", + "class Chips(Stock):\n", + " category = 'chips'\n", + "\n", + " def __init__(self, stock_code, desc, price, packet_size, flavour, available):\n", + " Stock.__init__(self, stock_code, desc, price, available)\n", + " self.packet_size = packet_size\n", + " self.flavour = flavour\n", + "\n", + " def __repr__(self):\n", + " print(\"You have chosen = {self.label}\")\n", + "\n", + " def one_for_one(self):\n", + " # buy one get one offer\n", + " sp = round(self.price, 2)\n", + " print('Buy two packets of {} and get one free. You pay {}'.format(self.packet_size, sp))\n", + "\n", + "\n", + "class Canned(Stock):\n", + " category = 'cans'\n", + "\n", + " def __init__(self, stock_code, desc, price, volume, available):\n", + " Stock.__init__(self, stock_code, desc, price, available)\n", + " self.volume = volume\n", + "\n", + " def label(self):\n", + " print(f'{self.desc}')\n", + " self.sell_price()\n", + "\n", + " def apply_discount(self, discount):\n", + " new_sp = self.sale(discount)\n", + " print('Price reduced ! you save = $ ', self.price - new_sp)\n", + "\n", + "\n", + "def choose_product(product):\n", + " product_name = product.stock_code\n", + " product_count = product.available\n", + " print(\"$\"*100)\n", + " print(f\"You have chosen {product_name} {product.desc}\")\n", + " if product_count != 0:\n", + " print(f\"feed in atleast {product.price} or more: \")\n", + " money = float(input())\n", + " if money > product.price:\n", + " balance = money - product.price\n", + " print(f\"Here is the balance : {balance}\")\n", + " product.available -= 1\n", + " elif money == product.price:\n", + " print(\"Perfect Change - Thank you\")\n", + " product.available -= 1\n", + " else:\n", + " print(\"Feed in more cash/coin\")\n", + " print(\"$\"*100)\n", + " print(\"Thank you for choosing this vending machine\")\n", + " print(\"$\"*100)\n", + "\n", + "\n", + "class VendingMachine:\n", + " def __init__(self):\n", + " self.money = 0\n", + " self.products = {} \n", + " \n", + "\n", + "\n", + "class Stock_Manipulation(VendingMachine):\n", + " \n", + " \n", + " def stock_details_can(self, product):\n", + " d2 = {product.desc: {}}\n", + " d2[product.desc]['price'] = product.price\n", + " d2[product.desc]['quantity'] = product.available\n", + " d2[product.desc]['volume'] = product.volume\n", + " self.products.setdefault(product.stock_code, {}).update(d2)\n", + " \n", + " def stock_details_chips(self, product):\n", + " d2 = {product.desc: {}}\n", + " d2[product.desc]['price'] = product.price\n", + " d2[product.desc]['quantity'] = product.available\n", + " d2[product.desc]['flavour'] = product.flavour\n", + " d2[product.desc]['packet_size'] = product.packet_size\n", + " self.products.setdefault(product.stock_code, {}).update(d2)\n", + "\n", + "\n", + " def stock_details_chocolates(self, product):\n", + " d2 = {product.desc: {}}\n", + " d2[product.desc]['price'] = product.price\n", + " d2[product.desc]['quantity'] = product.available\n", + " self.products.setdefault(product.stock_code, {}).update(d2)\n", + " \n", + " def display_vending_machine(self):\n", + " for i, v in self.products.items():\n", + " print(i, v)\n", + "\n", + "\n", + "\n", + "print('****************************************************************')\n", + "ca_1 = Canned('Can', 'Coke', 5.00, 500, 5)\n", + "ca_2 = Canned('Can', 'Pepsi', 4.00, 250, 4)\n", + "ch_1 = Chips('Chips', 'Potato', 10.00, 100, 'salt', 2)\n", + "ch_2 = Chips('Chips', 'Tapioco', 10.00, 100, 'chilly', 3)\n", + "chl_1 = Chocolate('Chocolate', 'Kitkat', 1.00, 15)\n", + "chl_2 = Chocolate('Chocolate', 'Hersheys', 2.00, 20)\n", + "print(\"Total number of products available: \",Stock.counter)\n", + "print(\"products = \", Stock.stock_set)\n", + "vm = VendingMachine()\n", + "sd = Stock_Manipulation()\n", + "sd.stock_details_can(ca_1)\n", + "sd.stock_details_can(ca_2)\n", + "sd.stock_details_chips(ch_1)\n", + "sd.stock_details_chips(ch_2)\n", + "sd.stock_details_chocolates(chl_1)\n", + "sd.stock_details_chocolates(chl_2)\n", + "sd.display_vending_machine()\n", + "choose_product(chl_1)\n", + "sd.stock_details_chocolates(chl_1)\n", + "sd.display_vending_machine()" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "id": "eb5d1d6c-063d-485c-b5ff-183ce347327f", + "metadata": {}, + "outputs": [], + "source": [ + "# O" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "5bc637a3-bad7-4bc5-b8ce-6ea14b49d58c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "****************************************************************\n", + "Total number of products available: 6\n", + "products = {'Kitkat', 'Potato', 'Pepsi', 'Coke', 'Tapioco', 'Hersheys'}\n", + "Can {'Coke': {'price': 5.0, 'quantity': 5, 'volume': 500}, 'Pepsi': {'price': 4.0, 'quantity': 4, 'volume': 250}}\n", + "Chips {'Potato': {'price': 10.0, 'quantity': 2, 'flavour': 'salt', 'packet_size': 100}, 'Tapioco': {'price': 10.0, 'quantity': 3, 'flavour': 'chilly', 'packet_size': 100}}\n", + "Chocolate {'Kitkat': {'price': 1.0, 'quantity': 15}, 'Hersheys': {'price': 2.0, 'quantity': 20}}\n", + "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n", + "You have chosen Chips Tapioco\n", + "feed in atleast 10.0 or more: \n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + " 56\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Here is the balance : 46.0\n", + "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n", + "Thank you for choosing this vending machine\n", + "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n", + "Can {'Coke': {'price': 5.0, 'quantity': 5, 'volume': 500}, 'Pepsi': {'price': 4.0, 'quantity': 4, 'volume': 250}}\n", + "Chips {'Potato': {'price': 10.0, 'quantity': 2, 'flavour': 'salt', 'packet_size': 100}, 'Tapioco': {'price': 10.0, 'quantity': 2, 'flavour': 'chilly', 'packet_size': 100}}\n", + "Chocolate {'Kitkat': {'price': 1.0, 'quantity': 15}, 'Hersheys': {'price': 2.0, 'quantity': 20}}\n" + ] + } + ], + "source": [ + "from abc import ABC, abstractmethod\n", + "\n", + "class Stock:\n", + " counter = 0\n", + " stock_set = set()\n", + "\n", + " def __init__(self, stock_code, desc, price, available):\n", + " self.stock_code = stock_code\n", + " self.desc = desc\n", + " self.price = price\n", + " self.available = available\n", + " Stock.counter += 1\n", + " Stock.stock_set.add(self.desc)\n", + "\n", + " def sell_price(self):\n", + " sp = round(self.price, 2)\n", + " print('cost = $', sp)\n", + "\n", + " def label(self):\n", + " print(f\"{self.desc} costs you {self.price}\")\n", + "\n", + " def sale(self, discount):\n", + " new_sp = round(self.price * (1 - discount), 2)\n", + " print('the discounted price of {} is $ {}'.format((self.stock_code, self.desc), new_sp))\n", + " return new_sp\n", + "\n", + "\n", + "class Chocolate(Stock):\n", + "\n", + " category = 'chocolate'\n", + "\n", + " def __init__(self, stock_code, desc, price, available):\n", + " super().__init__(stock_code, desc, price, available)\n", + "\n", + " def __repr__(self):\n", + " print(\"You have chosen = {self.label}\")\n", + "\n", + "\n", + "class Chips(Stock):\n", + " category = 'chips'\n", + "\n", + " def __init__(self, stock_code, desc, price, packet_size, flavour, available):\n", + " Stock.__init__(self, stock_code, desc, price, available)\n", + " self.packet_size = packet_size\n", + " self.flavour = flavour\n", + "\n", + " def __repr__(self):\n", + " print(\"You have chosen = {self.label}\")\n", + "\n", + " def one_for_one(self):\n", + " # buy one get one offer\n", + " sp = round(self.price, 2)\n", + " print('Buy two packets of {} and get one free. You pay {}'.format(self.packet_size, sp))\n", + "\n", + "\n", + "class Canned(Stock):\n", + " category = 'cans'\n", + "\n", + " def __init__(self, stock_code, desc, price, volume, available):\n", + " Stock.__init__(self, stock_code, desc, price, available)\n", + " self.volume = volume\n", + "\n", + " def label(self):\n", + " print(f'{self.desc}')\n", + " self.sell_price()\n", + "\n", + " def apply_discount(self, discount):\n", + " new_sp = self.sale(discount)\n", + " print('Price reduced ! you save = $ ', self.price - new_sp)\n", + "\n", + "class VendingMachine:\n", + " \n", + " products = {}\n", + " \n", + " def __init__(self):\n", + " self.money = 0\n", + " \n", + " def display_vending_machine(self):\n", + " for k, v in self.products.items():\n", + " print(k, v)\n", + " \n", + "class StockManipulation(ABC):\n", + " \n", + " @abstractmethod\n", + " def update_stock(self, product):\n", + " pass\n", + "\n", + " \n", + "class StockDetailsCan(StockManipulation, VendingMachine):\n", + " def update_stock(self, product):\n", + " d2 = {product.desc: {}}\n", + " d2[product.desc]['price'] = product.price\n", + " d2[product.desc]['quantity'] = product.available\n", + " d2[product.desc]['volume'] = product.volume\n", + " self.products.setdefault(product.stock_code, {}).update(d2)\n", + "\n", + "class StockDetailsChips(StockManipulation, VendingMachine):\n", + " def update_stock(self, product):\n", + " d2 = {product.desc: {}}\n", + " d2[product.desc]['price'] = product.price\n", + " d2[product.desc]['quantity'] = product.available\n", + " d2[product.desc]['flavour'] = product.flavour\n", + " d2[product.desc]['packet_size'] = product.packet_size\n", + " self.products.setdefault(product.stock_code, {}).update(d2)\n", + "\n", + "class StockDetailsChocolate(StockManipulation, VendingMachine):\n", + " def update_stock(self, product):\n", + " d2 = {product.desc: {}}\n", + " d2[product.desc]['price'] = product.price\n", + " d2[product.desc]['quantity'] = product.available\n", + " self.products.setdefault(product.stock_code, {}).update(d2)\n", + "\n", + "\n", + "def choose_product(product):\n", + " product_name = product.stock_code\n", + " product_count = product.available\n", + " print(\"$\"*100)\n", + " print(f\"You have chosen {product_name} {product.desc}\")\n", + " if product_count != 0:\n", + " print(f\"feed in atleast {product.price} or more: \")\n", + " money = float(input())\n", + " if money > product.price:\n", + " balance = money - product.price\n", + " print(f\"Here is the balance : {balance}\")\n", + " product.available -= 1\n", + " elif money == product.price:\n", + " print(\"Perfect Change - Thank you\")\n", + " product.available -= 1\n", + " else:\n", + " print(\"Feed in more cash/coin\")\n", + " print(\"$\"*100)\n", + " print(\"Thank you for choosing this vending machine\")\n", + " print(\"$\"*100)\n", + "\n", + "print('****************************************************************')\n", + "\n", + "ca_1 = Canned('Can', 'Coke', 5.00, 500, 5)\n", + "ca_2 = Canned('Can', 'Pepsi', 4.00, 250, 4)\n", + "ch_1 = Chips('Chips', 'Potato', 10.00, 100, 'salt', 2)\n", + "ch_2 = Chips('Chips', 'Tapioco', 10.00, 100, 'chilly', 3)\n", + "chl_1 = Chocolate('Chocolate', 'Kitkat', 1.00, 15)\n", + "chl_2 = Chocolate('Chocolate', 'Hersheys', 2.00, 20)\n", + "print(\"Total number of products available: \",Stock.counter)\n", + "print(\"products = \", Stock.stock_set)\n", + "vm = VendingMachine()\n", + "can_obj = StockDetailsCan()\n", + "can_obj.update_stock(ca_1)\n", + "can_obj.update_stock(ca_2)\n", + "chips_obj = StockDetailsChips()\n", + "chips_obj.update_stock(ch_1)\n", + "chips_obj.update_stock(ch_2)\n", + "choco_obj = StockDetailsChocolate()\n", + "choco_obj.update_stock(chl_1)\n", + "choco_obj.update_stock(chl_2)\n", + "vm = VendingMachine()\n", + "vm.display_vending_machine()\n", + "choose_product(ch_2)\n", + "chips_obj.update_stock(ch_2)\n", + "vm.display_vending_machine()" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "id": "f09b0a2b-3d47-480f-a3fd-e7a804dfdf69", + "metadata": {}, + "outputs": [], + "source": [ + "# L" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "30b86467-f52a-4bb2-8d20-e032c3752987", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "****************************************************************\n", + "Total number of products available: 6\n", + "products = {'Kitkat', 'Potato', 'Pepsi', 'Coke', 'Tapioco', 'Hersheys'}\n", + "Can {'Coke': {'price': 5.0, 'quantity': 500, 'volume': 5}, 'Pepsi': {'price': 4.0, 'quantity': 250, 'volume': 5}}\n", + "Chips {'Potato': {'price': 10.0, 'quantity': 100, 'flavour': 'salt', 'packet_size': 2}, 'Tapioco': {'price': 10.0, 'quantity': 100, 'flavour': 'salt', 'packet_size': 2}}\n", + "Chocolate {'Kitkat': {'price': 1.0, 'quantity': 15}, 'Hersheys': {'price': 2.0, 'quantity': 20}}\n", + "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n", + "You have chosen Chips Tapioco\n", + "feed in atleast 10.0 or more: \n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + " 45\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Here is the balance : 35.0\n", + "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n", + "Thank you for choosing this vending machine\n", + "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n", + "Can {'Coke': {'price': 5.0, 'quantity': 500, 'volume': 5}, 'Pepsi': {'price': 4.0, 'quantity': 250, 'volume': 5}}\n", + "Chips {'Potato': {'price': 10.0, 'quantity': 100, 'flavour': 'salt', 'packet_size': 2}, 'Tapioco': {'price': 10.0, 'quantity': 99, 'flavour': 'salt', 'packet_size': 2}}\n", + "Chocolate {'Kitkat': {'price': 1.0, 'quantity': 15}, 'Hersheys': {'price': 2.0, 'quantity': 20}}\n" + ] + } + ], + "source": [ + "from abc import ABC, abstractmethod\n", + "\n", + "class Stock:\n", + " counter = 0\n", + " stock_set = set()\n", + "\n", + " def __init__(self, stock_code, desc, price, available):\n", + " self.stock_code = stock_code\n", + " self.desc = desc\n", + " self.price = price\n", + " self.available = available\n", + " Stock.counter += 1\n", + " Stock.stock_set.add(self.desc)\n", + "\n", + " def sell_price(self):\n", + " sp = round(self.price, 2)\n", + " print('cost = $', sp)\n", + "\n", + " def label(self):\n", + " print(f\"{self.desc} costs you {self.price}\")\n", + "\n", + " def sale(self, discount):\n", + " new_sp = round(self.price * (1 - discount), 2)\n", + " print('the discounted price of {} is $ {}'.format((self.stock_code, self.desc), new_sp))\n", + " return new_sp\n", + "\n", + "\n", + "class Chocolate(Stock):\n", + "\n", + " category = 'chocolate'\n", + "\n", + " def __init__(self, stock_code, desc, price, available):\n", + " super().__init__(stock_code, desc, price, available)\n", + "\n", + " def __repr__(self):\n", + " print(\"You have chosen = {self.label}\")\n", + "\n", + "\n", + "class Chips(Stock):\n", + " category = 'chips'\n", + "\n", + " def __init__(self, stock_code, desc, price, packet_size):\n", + " Stock.__init__(self, stock_code, desc, price, packet_size)\n", + "\n", + " def __repr__(self):\n", + " print(\"You have chosen = {self.label}\")\n", + "\n", + " def one_for_one(self):\n", + " # buy one get one offer\n", + " sp = round(self.price, 2)\n", + " print('Buy two packets of {} and get one free. You pay {}'.format(self.packet_size, sp))\n", + "\n", + "\n", + "class Canned(Stock):\n", + " category = 'cans'\n", + "\n", + " def __init__(self, stock_code, desc, price, available):\n", + " Stock.__init__(self, stock_code, desc, price, available)\n", + "\n", + " def label(self):\n", + " print(f'{self.desc}')\n", + " self.sell_price()\n", + "\n", + " def apply_discount(self, discount):\n", + " new_sp = self.sale(discount)\n", + " print('Price reduced ! you save = $ ', self.price - new_sp)\n", + "\n", + "class VendingMachine:\n", + " \n", + " products = {}\n", + " \n", + " def __init__(self):\n", + " self.money = 0\n", + " \n", + " def display_vending_machine(self):\n", + " for k, v in self.products.items():\n", + " print(k, v)\n", + "\n", + " \n", + "class StockManipulation(ABC):\n", + " \n", + " @abstractmethod\n", + " def update_stock(self, product):\n", + " pass\n", + "\n", + " \n", + "class StockDetailsCan(StockManipulation, VendingMachine):\n", + " \n", + " def __init__(self, volume):\n", + " self.volume = volume\n", + "\n", + " def update_stock(self, product):\n", + " d2 = {product.desc: {}}\n", + " d2[product.desc]['price'] = product.price\n", + " d2[product.desc]['quantity'] = product.available\n", + " d2[product.desc]['volume'] = self.volume \n", + " self.products.setdefault(product.stock_code, {}).update(d2)\n", + "\n", + "class StockDetailsChips(StockManipulation, VendingMachine):\n", + "\n", + " def __init__(self, flavour, packet_size):\n", + " self.flavour = flavour\n", + " self.packet_size = packet_size \n", + " \n", + " def update_stock(self, product):\n", + " d2 = {product.desc: {}}\n", + " d2[product.desc]['price'] = product.price\n", + " d2[product.desc]['quantity'] = product.available\n", + " d2[product.desc]['flavour'] = self.flavour\n", + " d2[product.desc]['packet_size'] = self.packet_size\n", + " self.products.setdefault(product.stock_code, {}).update(d2)\n", + "\n", + "class StockDetailsChocolate(StockManipulation, VendingMachine):\n", + "\n", + " \n", + " def update_stock(self, product):\n", + " d2 = {product.desc: {}}\n", + " d2[product.desc]['price'] = product.price\n", + " d2[product.desc]['quantity'] = product.available\n", + " self.products.setdefault(product.stock_code, {}).update(d2)\n", + "\n", + "\n", + "def choose_product(product):\n", + " product_name = product.stock_code\n", + " product_count = product.available\n", + " print(\"$\"*100)\n", + " print(f\"You have chosen {product_name} {product.desc}\")\n", + " if product_count != 0:\n", + " print(f\"feed in atleast {product.price} or more: \")\n", + " money = float(input())\n", + " if money > product.price:\n", + " balance = money - product.price\n", + " print(f\"Here is the balance : {balance}\")\n", + " product.available -= 1\n", + " elif money == product.price:\n", + " print(\"Perfect Change - Thank you\")\n", + " product.available -= 1\n", + " else:\n", + " print(\"Feed in more cash/coin\")\n", + " print(\"$\"*100)\n", + " print(\"Thank you for choosing this vending machine\")\n", + " print(\"$\"*100)\n", + "\n", + "print('****************************************************************')\n", + "\n", + "ca_1 = Canned('Can', 'Coke', 5.00, 500)\n", + "ca_2 = Canned('Can', 'Pepsi', 4.00, 250)\n", + "ch_1 = Chips('Chips', 'Potato', 10.00, 100)\n", + "ch_2 = Chips('Chips', 'Tapioco', 10.00, 100)\n", + "chl_1 = Chocolate('Chocolate', 'Kitkat', 1.00, 15)\n", + "chl_2 = Chocolate('Chocolate', 'Hersheys', 2.00, 20)\n", + "print(\"Total number of products available: \",Stock.counter)\n", + "print(\"products = \", Stock.stock_set)\n", + "vm = VendingMachine()\n", + "can_obj = StockDetailsCan(5)\n", + "can_obj.update_stock(ca_1)\n", + "can_obj.update_stock(ca_2)\n", + "chips_obj = StockDetailsChips('salt', 2)\n", + "chips_obj.update_stock(ch_1)\n", + "chips_obj.update_stock(ch_2)\n", + "choco_obj = StockDetailsChocolate()\n", + "choco_obj.update_stock(chl_1)\n", + "choco_obj.update_stock(chl_2)\n", + "vm = VendingMachine()\n", + "vm.display_vending_machine()\n", + "choose_product(ch_2)\n", + "chips_obj.update_stock(ch_2)\n", + "vm.display_vending_machine()" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "id": "361e3c97-b82e-4d83-b259-f01049f155b9", + "metadata": {}, + "outputs": [], + "source": [ + "# I" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "320cd653-3ef8-44b3-bb40-9157365e0bd7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "****************************************************************\n", + "Total number of products available: 6\n", + "products = {'Kitkat', 'Potato', 'Pepsi', 'Coke', 'Tapioco', 'Hersheys'}\n", + "product has volume = 5\n", + "product has volume = 5\n", + "product has flavour = salt and packet_size = 2\n", + "product has flavour = salt and packet_size = 2\n", + "Can {'Coke': {'price': 5.0, 'quantity': 500, 'volume': 5}, 'Pepsi': {'price': 4.0, 'quantity': 250, 'volume': 5}}\n", + "Chips {'Potato': {'price': 10.0, 'quantity': 100, 'flavour': 'salt', 'packet_size': 2}, 'Tapioco': {'price': 10.0, 'quantity': 100, 'flavour': 'salt', 'packet_size': 2}}\n", + "Chocolate {'Kitkat': {'price': 1.0, 'quantity': 15}, 'Hersheys': {'price': 2.0, 'quantity': 20}}\n", + "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n", + "You have chosen Chocolate Hersheys\n", + "feed in atleast 2.0 or more: \n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + " 6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Here is the balance : 4.0\n", + "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n", + "Thank you for choosing this vending machine\n", + "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n", + "Can {'Coke': {'price': 5.0, 'quantity': 500, 'volume': 5}, 'Pepsi': {'price': 4.0, 'quantity': 250, 'volume': 5}}\n", + "Chips {'Potato': {'price': 10.0, 'quantity': 100, 'flavour': 'salt', 'packet_size': 2}, 'Tapioco': {'price': 10.0, 'quantity': 100, 'flavour': 'salt', 'packet_size': 2}}\n", + "Chocolate {'Kitkat': {'price': 1.0, 'quantity': 15}, 'Hersheys': {'price': 2.0, 'quantity': 19}}\n" + ] + } + ], + "source": [ + "from abc import ABC, abstractmethod\n", + "\n", + "class Stock:\n", + " counter = 0\n", + " stock_set = set()\n", + "\n", + " def __init__(self, stock_code, desc, price, available):\n", + " self.stock_code = stock_code\n", + " self.desc = desc\n", + " self.price = price\n", + " self.available = available\n", + " Stock.counter += 1\n", + " Stock.stock_set.add(self.desc)\n", + "\n", + " def sell_price(self):\n", + " sp = round(self.price, 2)\n", + " print('cost = $', sp)\n", + "\n", + " def label(self):\n", + " print(f\"{self.desc} costs you {self.price}\")\n", + "\n", + " def sale(self, discount):\n", + " new_sp = round(self.price * (1 - discount), 2)\n", + " print('the discounted price of {} is $ {}'.format((self.stock_code, self.desc), new_sp))\n", + " return new_sp\n", + "\n", + "\n", + "class Chocolate(Stock):\n", + "\n", + " category = 'chocolate'\n", + "\n", + " def __init__(self, stock_code, desc, price, available):\n", + " super().__init__(stock_code, desc, price, available)\n", + "\n", + " def __repr__(self):\n", + " print(\"You have chosen = {self.label}\")\n", + "\n", + "\n", + "class Chips(Stock):\n", + " category = 'chips'\n", + "\n", + " def __init__(self, stock_code, desc, price, packet_size):\n", + " Stock.__init__(self, stock_code, desc, price, packet_size)\n", + "\n", + " def __repr__(self):\n", + " print(\"You have chosen = {self.label}\")\n", + "\n", + " def one_for_one(self):\n", + " # buy one get one offer\n", + " sp = round(self.price, 2)\n", + " print('Buy two packets of {} and get one free. You pay {}'.format(self.packet_size, sp))\n", + "\n", + "\n", + "class Canned(Stock):\n", + " category = 'cans'\n", + "\n", + " def __init__(self, stock_code, desc, price, available):\n", + " Stock.__init__(self, stock_code, desc, price, available)\n", + "\n", + " def label(self):\n", + " print(f'{self.desc}')\n", + " self.sell_price()\n", + "\n", + " def apply_discount(self, discount):\n", + " new_sp = self.sale(discount)\n", + " print('Price reduced ! you save = $ ', self.price - new_sp)\n", + "\n", + "class VendingMachine:\n", + " \n", + " products = {}\n", + " \n", + " def __init__(self):\n", + " self.money = 0\n", + " \n", + " def display_vending_machine(self):\n", + " for k, v in self.products.items():\n", + " print(k, v)\n", + "\n", + " \n", + "class StockManipulation(ABC):\n", + " \n", + " @abstractmethod\n", + " def update_stock(self, product):\n", + " pass\n", + " \n", + " \n", + "class StockManipulation_Print(ABC):\n", + " \n", + " @abstractmethod\n", + " def update_stock(self, product):\n", + " pass\n", + " \n", + " @abstractmethod\n", + " def print_special_attribute(self, product):\n", + " pass\n", + "\n", + "\n", + " \n", + "class StockDetailsCan(StockManipulation_Print, VendingMachine):\n", + " \n", + " def __init__(self, volume):\n", + " self.volume = volume\n", + "\n", + "\n", + " def update_stock(self, product):\n", + " d2 = {product.desc: {}}\n", + " d2[product.desc]['price'] = product.price\n", + " d2[product.desc]['quantity'] = product.available\n", + " d2[product.desc]['volume'] = self.volume \n", + " self.products.setdefault(product.stock_code, {}).update(d2)\n", + " \n", + " def print_special_attribute(self):\n", + " print(\"product has volume = \", self.volume)\n", + "\n", + "class StockDetailsChips(StockManipulation_Print, VendingMachine):\n", + "\n", + " def __init__(self, flavour, packet_size):\n", + " self.flavour = flavour\n", + " self.packet_size = packet_size \n", + " \n", + " def update_stock(self, product):\n", + " d2 = {product.desc: {}}\n", + " d2[product.desc]['price'] = product.price\n", + " d2[product.desc]['quantity'] = product.available\n", + " d2[product.desc]['flavour'] = self.flavour\n", + " d2[product.desc]['packet_size'] = self.packet_size\n", + " self.products.setdefault(product.stock_code, {}).update(d2)\n", + " \n", + " def print_special_attribute(self):\n", + " print(f\"product has flavour = {self.flavour } and packet_size = {self.packet_size}\")\n", + "\n", + "class StockDetailsChocolate(StockManipulation, VendingMachine):\n", + "\n", + " \n", + " def update_stock(self, product):\n", + " d2 = {product.desc: {}}\n", + " d2[product.desc]['price'] = product.price\n", + " d2[product.desc]['quantity'] = product.available\n", + " self.products.setdefault(product.stock_code, {}).update(d2)\n", + "\n", + "\n", + "def choose_product(product):\n", + " product_name = product.stock_code\n", + " product_count = product.available\n", + " print(\"$\"*100)\n", + " print(f\"You have chosen {product_name} {product.desc}\")\n", + " if product_count != 0:\n", + " print(f\"feed in atleast {product.price} or more: \")\n", + " money = float(input())\n", + " if money > product.price:\n", + " balance = money - product.price\n", + " print(f\"Here is the balance : {balance}\")\n", + " product.available -= 1\n", + " elif money == product.price:\n", + " print(\"Perfect Change - Thank you\")\n", + " product.available -= 1\n", + " else:\n", + " print(\"Feed in more cash/coin\")\n", + " print(\"$\"*100)\n", + " print(\"Thank you for choosing this vending machine\")\n", + " print(\"$\"*100)\n", + "\n", + "print('****************************************************************')\n", + "\n", + "ca_1 = Canned('Can', 'Coke', 5.00, 500)\n", + "ca_2 = Canned('Can', 'Pepsi', 4.00, 250)\n", + "ch_1 = Chips('Chips', 'Potato', 10.00, 100)\n", + "ch_2 = Chips('Chips', 'Tapioco', 10.00, 100)\n", + "chl_1 = Chocolate('Chocolate', 'Kitkat', 1.00, 15)\n", + "chl_2 = Chocolate('Chocolate', 'Hersheys', 2.00, 20)\n", + "print(\"Total number of products available: \",Stock.counter)\n", + "print(\"products = \", Stock.stock_set)\n", + "vm = VendingMachine()\n", + "can_obj = StockDetailsCan(5)\n", + "can_obj.update_stock(ca_1)\n", + "can_obj.print_special_attribute()\n", + "can_obj.update_stock(ca_2)\n", + "can_obj.print_special_attribute()\n", + "chips_obj = StockDetailsChips('salt', 2)\n", + "chips_obj.update_stock(ch_1)\n", + "chips_obj.print_special_attribute()\n", + "chips_obj.update_stock(ch_2)\n", + "chips_obj.print_special_attribute()\n", + "choco_obj = StockDetailsChocolate()\n", + "choco_obj.update_stock(chl_1)\n", + "choco_obj.update_stock(chl_2)\n", + "vm = VendingMachine()\n", + "vm.display_vending_machine()\n", + "choose_product(chl_2)\n", + "choco_obj.update_stock(chl_2)\n", + "vm.display_vending_machine()" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "id": "9f20f53f-2a42-4ec7-9fa7-6c0b1203ea9d", + "metadata": {}, + "outputs": [], + "source": [ + "# D" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "d9882275-a2d2-4258-8990-7ac09dc3543c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "****************************************************************\n", + "Total number of products available: 6\n", + "products = {'Kitkat', 'Potato', 'Pepsi', 'Coke', 'Tapioco', 'Hersheys'}\n", + "product has volume = 5\n", + "product has volume = 5\n", + "product has flavour = salt and packet_size = 2\n", + "product has flavour = salt and packet_size = 2\n", + "Can {'Coke': {'price': 5.0, 'quantity': 500, 'volume': 5}, 'Pepsi': {'price': 4.0, 'quantity': 250, 'volume': 5}}\n", + "Chips {'Potato': {'price': 10.0, 'quantity': 100, 'flavour': 'salt', 'packet_size': 2}, 'Tapioco': {'price': 10.0, 'quantity': 100, 'flavour': 'salt', 'packet_size': 2}}\n", + "Chocolate {'Kitkat': {'price': 1.0, 'quantity': 15}, 'Hersheys': {'price': 2.0, 'quantity': 20}}\n", + "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n", + "You have chosen Can Coke\n", + "feed in atleast 5.0 or more: \n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + " 6\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "type of money = coin\n", + "Here is the balance : 1.0\n", + "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n", + "Thank you for choosing this vending machine\n", + "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n", + "Can {'Coke': {'price': 5.0, 'quantity': 499, 'volume': 5}, 'Pepsi': {'price': 4.0, 'quantity': 250, 'volume': 5}}\n", + "Chips {'Potato': {'price': 10.0, 'quantity': 100, 'flavour': 'salt', 'packet_size': 2}, 'Tapioco': {'price': 10.0, 'quantity': 100, 'flavour': 'salt', 'packet_size': 2}}\n", + "Chocolate {'Kitkat': {'price': 1.0, 'quantity': 15}, 'Hersheys': {'price': 2.0, 'quantity': 20}}\n" + ] + } + ], + "source": [ + "from abc import ABC, abstractmethod\n", + "\n", + "class Stock:\n", + " counter = 0\n", + " stock_set = set()\n", + "\n", + " def __init__(self, stock_code, desc, price, available):\n", + " self.stock_code = stock_code\n", + " self.desc = desc\n", + " self.price = price\n", + " self.available = available\n", + " Stock.counter += 1\n", + " Stock.stock_set.add(self.desc)\n", + "\n", + " def sell_price(self):\n", + " sp = round(self.price, 2)\n", + " print('cost = $', sp)\n", + "\n", + " def label(self):\n", + " print(f\"{self.desc} costs you {self.price}\")\n", + "\n", + " def sale(self, discount):\n", + " new_sp = round(self.price * (1 - discount), 2)\n", + " print('the discounted price of {} is $ {}'.format((self.stock_code, self.desc), new_sp))\n", + " return new_sp\n", + "\n", + "\n", + "class Chocolate(Stock):\n", + "\n", + " category = 'chocolate'\n", + "\n", + " def __init__(self, stock_code, desc, price, available):\n", + " super().__init__(stock_code, desc, price, available)\n", + "\n", + " def __repr__(self):\n", + " print(\"You have chosen = {self.label}\")\n", + "\n", + "\n", + "class Chips(Stock):\n", + " category = 'chips'\n", + "\n", + " def __init__(self, stock_code, desc, price, packet_size):\n", + " Stock.__init__(self, stock_code, desc, price, packet_size)\n", + "\n", + " def __repr__(self):\n", + " print(\"You have chosen = {self.label}\")\n", + "\n", + " def one_for_one(self):\n", + " # buy one get one offer\n", + " sp = round(self.price, 2)\n", + " print('Buy two packets of {} and get one free. You pay {}'.format(self.packet_size, sp))\n", + "\n", + "\n", + "class Canned(Stock):\n", + " category = 'cans'\n", + "\n", + " def __init__(self, stock_code, desc, price, available):\n", + " Stock.__init__(self, stock_code, desc, price, available)\n", + "\n", + " def label(self):\n", + " print(f'{self.desc}')\n", + " self.sell_price()\n", + "\n", + " def apply_discount(self, discount):\n", + " new_sp = self.sale(discount)\n", + " print('Price reduced ! you save = $ ', self.price - new_sp)\n", + "\n", + "class VendingMachine:\n", + " \n", + " products = {}\n", + " money = 0\n", + " \n", + " def display_vending_machine(self):\n", + " for k, v in self.products.items():\n", + " print(k, v)\n", + "\n", + " \n", + "class StockManipulation(ABC):\n", + " \n", + " @abstractmethod\n", + " def update_stock(self, product):\n", + " pass\n", + " \n", + " \n", + "class StockManipulation_Print(ABC):\n", + " \n", + " @abstractmethod\n", + " def update_stock(self, product):\n", + " pass\n", + " \n", + " @abstractmethod\n", + " def print_special_attribute(self, product):\n", + " pass\n", + " \n", + "\n", + "class MoneyDependency(ABC):\n", + " \n", + " @abstractmethod\n", + " def do_you_have_enough_money(self):\n", + " pass\n", + " \n", + "\n", + "class CashMoney(MoneyDependency, VendingMachine):\n", + " \n", + " def do_you_have_enough_money(self):\n", + " return True\n", + " \n", + " def print_type_of_money(self):\n", + " print(\"type of money = cash\")\n", + "\n", + "class CoinMoney(MoneyDependency, VendingMachine): \n", + " \n", + " def do_you_have_enough_money(self):\n", + " return True\n", + " \n", + " def print_type_of_money(self):\n", + " print(\"type of money = coin\")\n", + "\n", + " \n", + "class StockDetailsCan(StockManipulation_Print, VendingMachine):\n", + " \n", + " def __init__(self, volume):\n", + " self.volume = volume\n", + "\n", + "\n", + " def update_stock(self, product):\n", + " d2 = {product.desc: {}}\n", + " d2[product.desc]['price'] = product.price\n", + " d2[product.desc]['quantity'] = product.available\n", + " d2[product.desc]['volume'] = self.volume \n", + " self.products.setdefault(product.stock_code, {}).update(d2)\n", + " \n", + " def print_special_attribute(self):\n", + " print(\"product has volume = \", self.volume)\n", + "\n", + "class StockDetailsChips(StockManipulation_Print, VendingMachine):\n", + "\n", + " def __init__(self, flavour, packet_size):\n", + " self.flavour = flavour\n", + " self.packet_size = packet_size \n", + " \n", + " def update_stock(self, product):\n", + " d2 = {product.desc: {}}\n", + " d2[product.desc]['price'] = product.price\n", + " d2[product.desc]['quantity'] = product.available\n", + " d2[product.desc]['flavour'] = self.flavour\n", + " d2[product.desc]['packet_size'] = self.packet_size\n", + " self.products.setdefault(product.stock_code, {}).update(d2)\n", + " \n", + " def print_special_attribute(self):\n", + " print(f\"product has flavour = {self.flavour } and packet_size = {self.packet_size}\")\n", + "\n", + "class StockDetailsChocolate(StockManipulation, VendingMachine):\n", + "\n", + " \n", + " def update_stock(self, product):\n", + " d2 = {product.desc: {}}\n", + " d2[product.desc]['price'] = product.price\n", + " d2[product.desc]['quantity'] = product.available\n", + " self.products.setdefault(product.stock_code, {}).update(d2)\n", + "\n", + "\n", + "def choose_product(product):\n", + " \n", + " product_name = product.stock_code\n", + " product_count = product.available\n", + " print(\"$\"*100)\n", + " print(f\"You have chosen {product_name} {product.desc}\")\n", + " if product_count != 0:\n", + " print(f\"feed in atleast {product.price} or more: \")\n", + " money = float(input())\n", + " cash_money = CashMoney()\n", + " coin_money = CoinMoney()\n", + " if money < 10:\n", + " coin_money.print_type_of_money()\n", + " else:\n", + " cash_money.print_type_of_money() \n", + " \n", + " if money > product.price:\n", + " balance = money - product.price\n", + " print(f\"Here is the balance : {balance}\")\n", + " product.available -= 1\n", + " elif money == product.price:\n", + " print(\"Perfect Change - Thank you\")\n", + " product.available -= 1\n", + " else:\n", + " print(\"Feed in more cash/coin\")\n", + " print(\"$\"*100)\n", + " print(\"Thank you for choosing this vending machine\")\n", + " print(\"$\"*100)\n", + "\n", + "print('****************************************************************')\n", + "\n", + "ca_1 = Canned('Can', 'Coke', 5.00, 500)\n", + "ca_2 = Canned('Can', 'Pepsi', 4.00, 250)\n", + "ch_1 = Chips('Chips', 'Potato', 10.00, 100)\n", + "ch_2 = Chips('Chips', 'Tapioco', 10.00, 100)\n", + "chl_1 = Chocolate('Chocolate', 'Kitkat', 1.00, 15)\n", + "chl_2 = Chocolate('Chocolate', 'Hersheys', 2.00, 20)\n", + "print(\"Total number of products available: \",Stock.counter)\n", + "print(\"products = \", Stock.stock_set)\n", + "vm = VendingMachine()\n", + "can_obj = StockDetailsCan(5)\n", + "can_obj.update_stock(ca_1)\n", + "can_obj.print_special_attribute()\n", + "can_obj.update_stock(ca_2)\n", + "can_obj.print_special_attribute()\n", + "chips_obj = StockDetailsChips('salt', 2)\n", + "chips_obj.update_stock(ch_1)\n", + "chips_obj.print_special_attribute()\n", + "chips_obj.update_stock(ch_2)\n", + "chips_obj.print_special_attribute()\n", + "choco_obj = StockDetailsChocolate()\n", + "choco_obj.update_stock(chl_1)\n", + "choco_obj.update_stock(chl_2)\n", + "vm = VendingMachine()\n", + "vm.display_vending_machine()\n", + "choose_product(ca_1)\n", + "can_obj.update_stock(ca_1)\n", + "vm.display_vending_machine()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b81ea01c-42e3-46d2-b228-99c5b9804387", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/hw_6/stock.py b/hw_6/stock.py new file mode 100644 index 0000000..7411fc7 --- /dev/null +++ b/hw_6/stock.py @@ -0,0 +1,138 @@ +class Stock: + counter = 0 + stock_set = set() + + def __init__(self, stock_code, desc, price, available): + self.stock_code = stock_code + self.desc = desc + self.price = price + self.available = available + Stock.counter += 1 + Stock.stock_set.add(self.desc) + + def sell_price(self): + sp = round(self.price, 2) + print('cost = $', sp) + + def label(self): + print(f"{self.desc} costs you {self.price}") + + def sale(self, discount): + new_sp = round(self.price * (1 - discount), 2) + print('the discounted price of {} is $ {}'.format((self.stock_code, self.desc), new_sp)) + return new_sp + + +class Chocolate(Stock): + + category = 'chocolate' + + def __init__(self, stock_code, desc, price, available): + super().__init__(stock_code, desc, price, available) + + def __repr__(self): + print("You have chosen = {self.label}") + + +class Chips(Stock): + category = 'chips' + + def __init__(self, stock_code, desc, price, packet_size, flavour, available): + Stock.__init__(self, stock_code, desc, price, available) + self.packet_size = packet_size + self.flavour = flavour + + def __repr__(self): + print("You have chosen = {self.label}") + + def one_for_one(self): + # buy one get one offer + sp = round(self.price, 2) + print('Buy two packets of {} and get one free. You pay {}'.format(self.packet_size, sp)) + + +class Canned(Stock): + category = 'cans' + + def __init__(self, stock_code, desc, price, volume, available): + Stock.__init__(self, stock_code, desc, price, available) + self.volume = volume + + def label(self): + print(f'{self.desc}') + self.sell_price() + + def apply_discount(self, discount): + new_sp = self.sale(discount) + print('Price reduced ! you save = $ ', self.price - new_sp) + + +def choose_product(product): + product_name = product.stock_code + product_count = product.available + print("$"*100) + print(f"You have chosen {product_name} {product.desc}") + if product_count != 0: + print(f"feed in atleast {product.price} or more: ") + money = float(input()) + if money > product.price: + balance = money - product.price + print(f"Here is the balance : {balance}") + product.available -= 1 + elif money == product.price: + print("Perfect Change - Thank you") + product.available -= 1 + else: + print("Feed in more cash/coin") + print("$"*100) + print("Thank you for choosing this vending machine") + print("$"*100) + + +class VendingMachine: + def __init__(self): + self.money = 0 + self.products = {} + + def stock_details(self, product): + d2 = {product.desc: {}} + d2[product.desc]['price'] = product.price + d2[product.desc]['quantity'] = product.available + if product.stock_code == 'Can': + d2[product.desc]['volume'] = product.volume + self.products.setdefault(product.stock_code, {}).update(d2) + elif product.stock_code == 'Chips': + d2[product.desc]['flavour'] = product.flavour + d2[product.desc]['packet_size'] = product.packet_size + self.products.setdefault(product.stock_code, {}).update(d2) + else: + self.products.setdefault(product.stock_code, {}).update(d2) + + def display_vending_machine(self): + for i, v in self.products.items(): + print(i, v) + + +print('****************************************************************') + +final_product_set = () +ca_1 = Canned('Can', 'Coke', 5.00, 500, 5) +ca_2 = Canned('Can', 'Pepsi', 4.00, 250, 4) +ch_1 = Chips('Chips', 'Potato', 10.00, 100, 'salt', 2) +ch_2 = Chips('Chips', 'Tapioco', 10.00, 100, 'chilly', 3) +chl_1 = Chocolate('Chocolate', 'Kitkat', 1.00, 15) +chl_2 = Chocolate('Chocolate', 'Hersheys', 2.00, 20) +print("Total number of products available: ",Stock.counter) +print("products = ", Stock.stock_set) +vm = VendingMachine() +vm.stock_details(ca_1) +vm.stock_details(ca_2) +vm.stock_details(ch_1) +vm.stock_details(ch_2) +vm.stock_details(chl_1) +vm.stock_details(chl_2) +vm.display_vending_machine() +choose_product(ch_1) +vm.stock_details(ch_1) +vm.display_vending_machine() +