Advertisement
SimeonTs

SUPyF Exam 24.03.2019 - 04. Agency

Aug 13th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.11 KB | None | 0 0
  1. """
  2. Basics OOP Principles
  3. Check your solution: https://judge.softuni.bg/Contests/Practice/Index/1590#3
  4.  
  5. SUPyF Exam 24.03.2019 - 04. Agency
  6.  
  7. Problem:
  8. Input / Constraints
  9. Our task is to make a database for an agency which sells apartments.
  10. A basic apartment will have:
  11. • id -  string
  12. • rooms  – integer number
  13. • baths - integer number
  14. • square_meters – floating point number
  15. • price – floating point number
  16. There are only two types of apartments the agency can manage: LivingApartments and OfficeApartments.
  17. Both types has the same parameters as a basic.
  18. You will start receiving input data in format:
  19. * LivingApartment(“{id}”, {rooms}, {baths }, {square_meters }, {price})
  20. -if there is no fifth parameter you should print “__init__() missing 1 required positional argument: 'price'”
  21. In this case you do not add the apartment in DB
  22. * OfficeApartment(“{id}”, {rooms}, {baths }, {square_meters }, {price})
  23. -if there is no fifth parameter you should print “__init__() missing 1 required positional argument: 'price'”
  24. In this case you do not add the apartment in DB
  25. Is it possible a request for basic apartment to reach the database:
  26. *Apartment (“{id}”, {rooms}, {baths }, {square_meters }, {price})
  27. You must print “Can't instantiate abstract class Apartment with abstract methods __str__”
  28. and you must  not add it in the DB.
  29.  
  30. The core difference between OfficeApartments and LivingApartments is that the LivingApartment’s price is for buying and
  31. these apartment could not be hired and the OfficeApartmnets could only be hired not bought.
  32. When you receive the command ‘start_selling’ you should stop adding apartments to the DB.
  33. From this moment you will start receive a commands in format:
  34. {buy}/{rent} {id}
  35. Where {id} is the id of the apartment that the client wants to rent/buy.
  36. • If the DB is not an apartment with the given id print “Apartment with id - {id} does not exist!”
  37. • If there is such apartment, but it is already taken print “Apartment with id - {id} is already taken!”
  38. • If the apartment is available but the command is ‘rent’ and the apartment is of type LivingApartment print “Apartment with id - {id} is only for selling!”
  39. • If the apartment is available but the command is ‘hire’ and the apartment is of type OfficeApartment print “Apartment with id - {id} is only for renting!”
  40. • If none of the above cases are true, then just mark the apartment as taken.
  41.  
  42. Output
  43. When you receive ‘free’ or ‘taken’ you should stop selling apartments and make report.
  44. • If you have received the ‘taken’ command you should print just the taken apartments sorted first by price ascending and then by square meters descending
  45. • If you have received the ‘free’ command you should print just the free apartments sorted first by price descending and then by square meters ascending
  46. In both cases you should print the sorted apartments in the following format:
  47. {rooms} rooms place with {bathrooms} bathroom/s.
  48. {square_meters} sq. meters for {price} lv.
  49. Where {rooms} are the count of the rooms in the apartment, {bathrooms} are the count of bathrooms in the apartments. {square_meters} – they are its square meters and {price} is the price .
  50.  
  51. • In case there is no apartments according the query print “No information for this query”.
  52.  
  53. Examples:
  54.    Input:
  55.        Apartment("00A", 2, 1, 150, 100000)
  56.        OfficeApartment("00O", 2, 1, 500)
  57.        LivingApartment("00L", 3, 1, 180, 100000)
  58.        start_selling
  59.        rent 00L
  60.        taken
  61.    Output:
  62.        Can't instantiate abstract class Apartment with abstract methods __str__
  63.        __init__() missing 1 required positional argument: 'price'
  64.        Apartment with id - 00L is only for selling!
  65.        No information for this query
  66.    Input:
  67.        OfficeApartment("00O", 2, 1, 150, 500)
  68.        LivingApartment("00L", 3, 1, 180, 100000)
  69.        LivingApartment("01L", 3, 1, 190, 100000)
  70.        start_selling
  71.        buy 00L
  72.        buy 01L
  73.        rent 00O
  74.        taken
  75.    Output:
  76.        2 rooms place with 1 bathroom/s.
  77.        150.0 sq. meters for 500.0 lv.
  78.        3 rooms place with 1 bathroom/s.
  79.        190.0 sq. meters for 100000.0 lv.
  80.        3 rooms place with 1 bathroom/s.
  81.        180.0 sq. meters for 100000.0 lv.
  82. """
  83. # Here I have 3 different ways to solve the problem:
  84.  
  85.  
  86. class Apartment:
  87.     def __init__(self, ap_type, ids, rooms, baths, square_meters, price, available=True):
  88.         self.ap_type = ap_type
  89.         self.ids = ids
  90.         self.rooms = int(rooms)
  91.         self.baths = int(baths)
  92.         self.square_meters = float(square_meters)
  93.         self.price = float(price)
  94.         self.available = available
  95.  
  96.     def __str__(self):
  97.         return f"{self.rooms} rooms place with {self.baths} bathroom/s."+"\n"+f"{self.square_meters} sq. meters for {self.price} lv."
  98.  
  99.  
  100. all_apartments = []
  101.  
  102. while True:
  103.     a = input()
  104.     if a == "start_selling":
  105.         break
  106.     else:
  107.         a = a.split("(")
  108.         if a[0] == "Apartment":
  109.             print("Can't instantiate abstract class Apartment with abstract methods __str__")
  110.             continue
  111.  
  112.         data = [letter for letter in a[1] if letter.isalnum() or letter == " " or letter == "."]
  113.         data = ("".join(data))
  114.         data = [item for item in data.split(" ")]
  115.         if len(data) < 5:
  116.             print("__init__() missing 1 required positional argument: 'price'")
  117.             continue
  118.         ap = Apartment(ap_type=a[0], ids=data[0], rooms=data[1], baths=data[2], square_meters=data[3], price=data[4])
  119.         all_apartments += [ap]
  120.  
  121.  
  122. def free():
  123.     free_apartments = [apa for apa in all_apartments if apa.available]
  124.     free_apartments = sorted(sorted(free_apartments, key=lambda xa: xa.square_meters), key=lambda xa: xa.price, reverse=True)
  125.     if len(free_apartments) > 0:
  126.         for x in free_apartments:
  127.             print(x)
  128.     else:
  129.         print(f"No information for this query")
  130.  
  131.  
  132. def taken():
  133.     taken_apartments = [apa for apa in all_apartments if not apa.available]
  134.     taken_apartments = sorted(sorted(taken_apartments, key=lambda xa: xa.square_meters, reverse=True), key=lambda xa: xa.price)
  135.     if len(taken_apartments) > 0:
  136.         for x in taken_apartments:
  137.             print(x)
  138.     else:
  139.         print(f"No information for this query")
  140.  
  141.  
  142. while True:
  143.     command = input()
  144.  
  145.     if command == "free" or command == "taken":
  146.         if command == "free":
  147.             free()
  148.         elif command == "taken":
  149.             taken()
  150.         break
  151.  
  152.     action, ap_id = command.split(" ")
  153.  
  154.     check_if_ap_in_db = False
  155.     for apartment in all_apartments:
  156.         if ap_id == apartment.ids:
  157.             check_if_ap_in_db = True
  158.             if not apartment.available:
  159.                 print(f"Apartment with id - {ap_id} is already taken!")
  160.             elif apartment.available and action == "rent" and apartment.ap_type == "LivingApartment":
  161.                 print(f"Apartment with id - {ap_id} is only for selling!")
  162.             elif apartment.available and action == "hire" and apartment.ap_type == "OfficeApartment":
  163.                 print(f"Apartment with id - {ap_id} is only for renting!")
  164.             else:
  165.                 apartment.available = False
  166.     if not check_if_ap_in_db:
  167.         print(f"Apartment with id - {ap_id} does not exist!")
  168.  
  169.  
  170. # This is the way to solve the problem with 3 classes using the abstract class:
  171. """
  172. from abc import ABC, abstractmethod
  173.  
  174.  
  175. class Apartment(ABC):
  176.    def __init__(self, ids: str, rooms: int, baths: int, square_meters: float, price: float):
  177.        self.ids = str(ids)
  178.        self.rooms = int(rooms)
  179.        self.baths = int(baths)
  180.        self.square_meters = float(square_meters)
  181.        self. price = float(price)
  182.        self.available = True
  183.  
  184.    @abstractmethod
  185.    def __str__(self):
  186.        return f"{self.rooms} rooms place with {self.baths} bathroom/s.\n{self.square_meters} sq. meters for {self.price} lv."
  187.  
  188.  
  189. class LivingApartment(Apartment):
  190.    def __init__(self, ids, rooms, baths, square_meters, price):
  191.        Apartment.__init__(self, ids, rooms, baths, square_meters, price)
  192.  
  193.    def __str__(self):
  194.        return Apartment.__str__(self)
  195.  
  196.  
  197. class OfficeApartment(Apartment):
  198.    def __init__(self, ids, rooms, baths, square_meters, price):
  199.        Apartment.__init__(self, ids, rooms, baths, square_meters, price)
  200.  
  201.    def __str__(self):
  202.        return Apartment.__str__(self)
  203.  
  204.  
  205. all_apartments = []
  206.  
  207. while True:
  208.    input_data = input()
  209.    if input_data == "start_selling":
  210.        break
  211.    try:
  212.        current_apartment = eval(input_data)
  213.        all_apartments += [current_apartment]
  214.    except Exception as exception:
  215.        print(exception)
  216.  
  217. while True:
  218.    input_data = input()
  219.    if input_data == "free" or input_data == "taken":
  220.  
  221.        if input_data == "taken":
  222.            taken_apartments = [ap for ap in all_apartments if not ap.available]
  223.            taken_apartments = sorted(sorted(taken_apartments, key=lambda xa: xa.square_meters, reverse=True), key=lambda xa: xa.price)
  224.            if len(taken_apartments) > 0:
  225.                for apartment in taken_apartments:
  226.                    print(apartment)
  227.            else:
  228.                print("No information for this query")
  229.  
  230.        elif input_data == "free":
  231.            free_apartments = [apa for apa in all_apartments if apa.available]
  232.            free_apartments = sorted(sorted(free_apartments, key=lambda xa: xa.square_meters), key=lambda xa: xa.price, reverse=True)
  233.            if len(free_apartments) > 0:
  234.                for x in free_apartments:
  235.                    print(x)
  236.            else:
  237.                print(f"No information for this query")
  238.        break
  239.  
  240.    action, ap_id = input_data.split()
  241.  
  242.    try:
  243.        selected_apartment = [ap for ap in all_apartments if ap.ids == ap_id][0]
  244.        if not selected_apartment.available:
  245.            print(f"Apartment with id - {ap_id} is already taken!")
  246.        elif selected_apartment.__class__.__name__ == "LivingApartment" and action == "rent":
  247.            print(f"Apartment with id - {ap_id} is only for selling!")
  248.  
  249.        elif selected_apartment.__class__.__name__ == "OfficeApartment" and action == "buy":
  250.            print(f"Apartment with id - {ap_id} is only for renting!")
  251.        else:
  252.            selected_apartment.available = False
  253.  
  254.    except IndexError:
  255.        print(f"Apartment with id - {ap_id} does not exist!")
  256. """
  257.  
  258.  
  259. # This is the third variant, made by out teacher Ines Ivanova during the classes:
  260. """
  261. from abc import ABC, abstractmethod
  262.  
  263. class Apartment(ABC):
  264.    def __init__(self, id, rooms, baths, square_meters, price):
  265.        self.id = id
  266.        self.rooms = int(rooms)
  267.        self.baths = int(baths)
  268.        self.square_meters = float(square_meters)
  269.        self.price = float(price)
  270.        self.is_taken = False
  271.  
  272.    @abstractmethod
  273.    def __str__(self):
  274.        return f"{self.rooms} rooms place with {self.baths} bathroom/s.\n{self.square_meters} sq. meters for {self.price} lv."
  275.  
  276.  
  277. class LivingApartment(Apartment):
  278.    def __init__(self, id, rooms, baths, square_meters, price):
  279.        Apartment.__init__(self, id, rooms, baths, square_meters, price)
  280.  
  281.    def __str__(self):
  282.        return Apartment.__str__(self)
  283.  
  284.  
  285. class OfficeApartment(Apartment):
  286.    def __init__(self, id, rooms, baths, square_meters, price):
  287.        Apartment.__init__(self, id, rooms, baths, square_meters, price)
  288.  
  289.    def __str__(self):
  290.        return Apartment.__str__(self)
  291.  
  292.  
  293. data = input()
  294. apartments_list = []
  295.  
  296. while not data == "start_selling":
  297.    try:
  298.        current_app = eval(data)
  299.        apartments_list.append(current_app)
  300.    except Exception as ex:
  301.        print(ex)
  302.  
  303.    data = input()
  304.  
  305. data_list = input().split()
  306. ids_list = list(map(lambda a: a.id, apartments_list))
  307.  
  308. while not (data_list[0] == "free" or data_list[0] == "taken"):
  309.    command = data_list[0]
  310.    id = data_list[1]
  311.  
  312.    if id in ids_list:
  313.        current_app: Apartment = list(filter(lambda a: a.id == id, apartments_list))[0]
  314.        if current_app.is_taken:
  315.            print(f"Apartment with id - {id} is already taken!")
  316.        elif command == "rent" and isinstance(current_app, LivingApartment):
  317.            print(f"Apartment with id - {id} is only for selling!")
  318.        elif command == "buy" and isinstance(current_app, OfficeApartment):
  319.            print(f"Apartment with id - {id} is only for renting!")
  320.        else:
  321.            current_app.is_taken = True
  322.    else:
  323.        print(f"Apartment with id - {id} does not exist!")
  324.  
  325.  
  326.    data_list = input().split()
  327.  
  328.  
  329. if data_list[0] == 'taken':
  330.    taken_ap_list = list(filter(lambda a: a.is_taken == True, apartments_list))
  331.    for apartment in sorted(taken_ap_list, key=lambda a: (a.price, -a.square_meters)):
  332.        print(apartment)
  333.  
  334. elif data_list[0] == 'free':
  335.    taken_ap_list = list(filter(lambda a: a.is_taken == False, apartments_list))
  336.    for apartment in sorted(taken_ap_list, key=lambda a: (-a.price, a.square_meters)):
  337.        print(apartment)
  338.  
  339. if len(taken_ap_list) == 0:
  340.    print("No information for this query")
  341. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement