Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.37 KB | None | 0 0
  1. from abc import ABC, abstractmethod
  2.  
  3. class Item(ABC):
  4.     @abstractmethod
  5.     def __init__(self, id: str, os: str, price: float):
  6.         self.id = id
  7.         self.os = os
  8.         self.price = price
  9.  
  10.     @property
  11.     def id(self):
  12.         return self.__id
  13.  
  14.     @id.setter
  15.     def id(self, value):
  16.         if not value.isalpha() or len(value) < 8:
  17.             raise Exception('Invalid id!')
  18.         else:
  19.             self.__id = value
  20.  
  21.     @property
  22.     def price(self):
  23.         return self.__price
  24.  
  25.     @price.setter
  26.     def price(self, value):
  27.         if value <= 0:
  28.             raise Exception('Invalid price!')
  29.         else:
  30.             self.__price = value
  31.  
  32.     def print_item(self):
  33.         return f'Item id: {self.id}\nOperating system: {self.os}\nPrice: {self.price}'
  34.  
  35. class Phone(Item):
  36.     @abstractmethod
  37.     def __init__(self, id, os, price):
  38.         Item.__init__(self, id, os, price)
  39.  
  40.     def make_call(self):
  41.         print('Making call...')
  42.  
  43.     def receive_call(self):
  44.         print('Receiving a call!')
  45.  
  46.     def send_message(self):
  47.         print('Sending message...')
  48.  
  49.     def receive_message(self):
  50.         print('Receiving a message!')
  51.  
  52. class Tablet(Item):
  53.     def __init__(self, id, os, price):
  54.         Item.__init__(self, id, os, price)
  55.  
  56.     def stream_movie(self):
  57.         print('Streaming movie...')
  58.  
  59. class CellPhone(Phone):
  60.     def __init__(self, id, os, price):
  61.         Phone.__init__(self, id, os, price)
  62.  
  63. class SmartPhone(Phone):
  64.     def __init__(self, id, os, price, apps: list):
  65.         Phone.__init__(self, id, os, price)
  66.         self.apps = apps
  67.  
  68.     def browse_internet(self):
  69.         print('Browsing...')
  70.  
  71.     def print_item(self):
  72.         return f'Item id: {self.id}\nOperating system: {self.os}\nPrice: {self.price}\nApplications: {", ".join(self.apps)}'
  73.  
  74.     @property
  75.     def apps(self):
  76.         return self.__apps
  77.  
  78.     @apps.setter
  79.     def apps(self, value):
  80.         if len(value) < 5:
  81.             raise Exception('Invalid number of applications!')
  82.         else:
  83.             self.__apps = value
  84.  
  85. items_dict = {'SmartPhone': [], 'CellPhone': [], 'Tablet': []}
  86.  
  87. def test_item(command_list, items_dict):
  88.     id = command_list[1]
  89.     functionality = command_list[2]
  90.     found = False
  91.     for search_item in items_dict.values():
  92.         for one_item in search_item:
  93.             if id == one_item.id:
  94.                 if functionality in dir(one_item):
  95.                     format = f'one_item.{functionality}()'
  96.                     eval(format)
  97.                     found = True
  98.     if not found:
  99.         print('Invalid request has been made!')
  100.  
  101. def report_item(command_list, items_dict):
  102.     kind = command_list[1]
  103.     if kind in items_dict.keys():
  104.         for item in items_dict.get(kind):
  105.             print(item.print_item())
  106.     else:
  107.         print('Invalid request has been made!')
  108.  
  109.  
  110. while True:
  111.     command = input()
  112.     if command == 'end':
  113.         break
  114.     try:
  115.         type = command.split('(')[0]
  116.         new_obect = eval(command)
  117.         items_dict.get(type).append(new_obect)
  118.     except Exception as ex:
  119.         print(ex)
  120.  
  121. while True:
  122.     command_list = input().split()
  123.     if command_list[0] == 'end':
  124.         exit(0)
  125.     if command_list[0] == 'test':
  126.         test_item(command_list, items_dict)
  127.     elif command_list[0] == 'report':
  128.         report_item(command_list, items_dict)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement