Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Python OOP Exam - 10 April 2021 - Aquariums
- # https://judge.softuni.org/Contests/Practice/Index/2934#1
- =============================================================================================
- # file name: base_aquarium.py
- from abc import ABC, abstractmethod
- class BaseAquarium(ABC):
- def __init__(self, name: str, capacity: int):
- self.name = name
- self.capacity = capacity
- self.decorations = [] # decorations (objects).
- self.fish = [] # all the fish (objects).
- # All passed names would be UNIQUE !!!
- # and it will not be necessary to check if a given name already exists.
- @property
- def name(self):
- return self.__name
- @name.setter
- def name(self, value):
- if value == "":
- raise ValueError("Aquarium name cannot be an empty string.")
- self.__name = value
- @abstractmethod
- def calculate_comfort(self):
- ...
- @abstractmethod
- def add_fish(self, fish): # add Fish OBJECT!
- ...
- @abstractmethod
- def remove_fish(self, fish): # remove Fish OBJECT!
- ...
- @abstractmethod
- def add_decoration(self, decoration): # remove Decoration OBJECT!
- ...
- @abstractmethod
- def feed(self): # feed all fish in the aquarium
- ...
- def __str__(self):
- ...
- =============================================================================================
- # file name: freshwater_aquarium.py
- from project.aquarium.base_aquarium import BaseAquarium
- class FreshwaterAquarium(BaseAquarium):
- INITIAL_CAPACITY = 50
- def __init__(self, name: str):
- super().__init__(name, self.INITIAL_CAPACITY)
- self.name = name
- self.capacity = self.INITIAL_CAPACITY
- def calculate_comfort(self):
- return sum([d.comfort for d in self.decorations])
- def add_fish(self, fish):
- if self.capacity == 0:
- return f"Not enough capacity."
- if type(fish).__name__ == 'FreshwaterFish':
- self.fish.append(fish)
- self.capacity -= 1
- return f"Successfully added {type(fish).__name__} to {self.name}."
- else:
- return 'Water not suitable.'
- def remove_fish(self, fish):
- if fish in self.fish:
- self.fish.remove(fish)
- def add_decoration(self, decoration):
- if decoration not in self.decorations:
- self.decorations.append(decoration)
- def feed(self):
- for fish_obj in self.fish:
- fish_obj.eat()
- def __str__(self):
- fish_names = [f.name for f in self.fish]
- if not fish_names:
- fish_names = ['none']
- result = f"{self.name}:\n"
- result += f"Fish: {' '.join(fish_names)}\n"
- result += f"Decorations: {len(self.decorations)}\n"
- result += f"Comfort: {self.calculate_comfort()}"
- return result
- =============================================================================================
- # file name: saltwater_aquarium.py
- from project.aquarium.base_aquarium import BaseAquarium
- class SaltwaterAquarium(BaseAquarium):
- INITIAL_CAPACITY = 25
- def __init__(self, name: str):
- super().__init__(name, self.INITIAL_CAPACITY)
- self.name = name
- self.capacity = self.INITIAL_CAPACITY
- def calculate_comfort(self):
- return sum([d.comfort for d in self.decorations])
- def add_fish(self, fish):
- if self.capacity == 0:
- return f"Not enough capacity."
- if type(fish).__name__ == 'SaltwaterFish':
- self.fish.append(fish)
- self.capacity -= 1
- return f"Successfully added {type(fish).__name__} to {self.name}."
- else:
- return 'Water not suitable.'
- def remove_fish(self, fish):
- if fish in self.fish:
- self.fish.remove(fish)
- def add_decoration(self, decoration):
- self.decorations.append(decoration)
- def feed(self):
- for fish_obj in self.fish:
- fish_obj.eat()
- def __str__(self):
- fish_names = [f.name for f in self.fish]
- if not fish_names:
- fish_names = ['none']
- result = f"{self.name}:\n"
- result += f"Fish: {' '.join(fish_names)}\n"
- result += f"Decorations: {len(self.decorations)}\n"
- result += f"Comfort: {self.calculate_comfort()}"
- return result
- =============================================================================================
- # file name: base_decoration.py
- from abc import ABC, abstractmethod
- class BaseDecoration(ABC):
- @abstractmethod
- def __init__(self, comfort: int, price: float):
- self.comfort = comfort
- self.price = price
- =============================================================================================
- # file name: ornament.py
- from project.decoration.base_decoration import BaseDecoration
- class Ornament(BaseDecoration):
- COMFORT = 1
- PRICE = 5
- def __init__(self):
- super().__init__(self.COMFORT, self.PRICE)
- self.comfort = self.COMFORT
- self.price = self.PRICE
- =============================================================================================
- # file name: plant.py
- from project.decoration.base_decoration import BaseDecoration
- class Plant(BaseDecoration):
- COMFORT = 5
- PRICE = 10
- def __init__(self):
- super().__init__(self.COMFORT, self.PRICE)
- self.comfort = self.COMFORT
- self.price = self.PRICE
- =============================================================================================
- # file name: decoration_repository.py
- class DecorationRepository:
- def __init__(self):
- self.decorations = [] # contain all decorations (objects).
- def add(self, decoration): # Add Obj!
- self.decorations.append(decoration)
- def remove(self, decoration): # Remove Obj!
- if decoration in self.decorations:
- self.decorations.remove(decoration)
- return True
- return False
- def find_by_type(self, decoration_type: str):
- decorations_founds = [d for d in self.decorations if type(d).__name__ == decoration_type]
- if decorations_founds:
- first_decoration_found = decorations_founds[0]
- return first_decoration_found
- return 'None'
- =============================================================================================
- # file name: base_fish.py
- from abc import ABC, abstractmethod
- class BaseFish(ABC):
- def __init__(self, name: str, species: str, size: int, price: float):
- self.name = name
- self.species = species
- self.size = size
- self.price = price
- @property
- def name(self):
- return self.__name
- @name.setter
- def name(self, value):
- if value == '':
- raise ValueError("Fish name cannot be an empty string.")
- self.__name = value
- @property
- def species(self):
- return self.__species
- @species.setter
- def species(self, value):
- if value == '':
- raise ValueError("Fish species cannot be an empty string.")
- self.__species = value
- @property
- def price(self):
- return self.__price
- @price.setter
- def price(self, value):
- if value <= 0:
- raise ValueError("Price cannot be equal to or below zero.")
- self.__price = value
- @abstractmethod
- def eat(self):
- self.size += 5
- =============================================================================================
- # file name: freshwater_fish
- from project.fish.base_fish import BaseFish
- class FreshwaterFish(BaseFish):
- # The FreshwaterFish could only live in FreshwaterAquarium!
- INITIAL_SIZE = 3
- def __init__(self, name: str, species: str, price: float):
- super().__init__(name, species, self.INITIAL_SIZE, price)
- self.name = name
- self.size = self.INITIAL_SIZE
- self.price = price
- def eat(self):
- self.size += 3
- =============================================================================================
- # file name: saltwater_fish
- from project.fish.base_fish import BaseFish
- class SaltwaterFish(BaseFish):
- # The SaltwaterFish could only live in SaltwaterAquarium!
- INITIAL_SIZE = 5
- def __init__(self, name: str, species: str, price: float):
- super().__init__(name, species, self.INITIAL_SIZE, price)
- self.name = name
- self.species = species
- self.size = self.INITIAL_SIZE
- self.price = price
- def eat(self):
- self.size += 2
- =============================================================================================
- # file name: controller.py
- from project.aquarium.freshwater_aquarium import FreshwaterAquarium
- from project.aquarium.saltwater_aquarium import SaltwaterAquarium
- from project.decoration.decoration_repository import DecorationRepository
- from project.decoration.ornament import Ornament
- from project.decoration.plant import Plant
- from project.fish.freshwater_fish import FreshwaterFish
- from project.fish.saltwater_fish import SaltwaterFish
- class Controller:
- def __init__(self):
- self.decorations_repository = self.add_repo()
- self.aquariums = [] # all aquariums (objects).
- @staticmethod
- def add_repo():
- repo_obj = DecorationRepository()
- return repo_obj
- def add_aquarium(self, aquarium_type: str, aquarium_name: str):
- aquarium_mapper = {"FreshwaterAquarium": FreshwaterAquarium, "SaltwaterAquarium": SaltwaterAquarium}
- if aquarium_type not in aquarium_mapper:
- return 'Invalid aquarium type.'
- else:
- new_aquarium = aquarium_mapper[aquarium_type](aquarium_name)
- self.aquariums.append(new_aquarium)
- return f"Successfully added {aquarium_type}."
- def add_decoration(self, decoration_type: str):
- if decoration_type == 'Ornament':
- decoration_to_add = Ornament()
- elif decoration_type == 'Plant':
- decoration_to_add = Plant()
- else:
- return "Invalid decoration type."
- self.decorations_repository.add(decoration_to_add)
- return f"Successfully added {decoration_type}."
- def find_aquarium_by_name(self, aquarium_name_):
- for aquarium_obj in self.aquariums:
- if aquarium_obj.name == aquarium_name_:
- return aquarium_obj
- def insert_decoration(self, aquarium_name: str, decoration_type: str):
- decoration = self.decorations_repository.find_by_type(decoration_type)
- aquarium = self.find_aquarium_by_name(aquarium_name)
- if decoration == 'None':
- return f"There isn't a decoration of type {decoration_type}."
- else:
- aquarium.add_decoration(decoration)
- self.decorations_repository.remove(decoration)
- return f"Successfully added {decoration_type} to {aquarium_name}."
- @staticmethod
- def create_fish_by_type(fish_type_, fish_name_, fish_species_, price_):
- if fish_type_ == 'FreshwaterFish':
- return FreshwaterFish(fish_name_, fish_species_, price_)
- elif fish_type_ == 'SaltwaterFish':
- return SaltwaterFish(fish_name_, fish_species_, price_)
- else:
- return None
- def add_fish(self, aquarium_name: str, fish_type: str, fish_name: str, fish_species: str, price: float):
- new_fish = self.create_fish_by_type(fish_type, fish_name, fish_species, price)
- if not new_fish:
- return f"There isn't a fish of type {fish_type}."
- aquarium = self.find_aquarium_by_name(aquarium_name)
- return aquarium.add_fish(new_fish)
- def feed_fish(self, aquarium_name: str):
- aquarium = self.find_aquarium_by_name(aquarium_name)
- for fish in aquarium.fish:
- fish.eat()
- return f"Fish fed: {len(aquarium.fish)}"
- def calculate_value(self, aquarium_name: str):
- aquarium = self.find_aquarium_by_name(aquarium_name)
- total_value = self.calculate_value_of_all_decorations_and_fish(aquarium)
- return f"The value of Aquarium {aquarium_name} is {total_value:.2f}."
- @staticmethod
- def calculate_value_of_all_decorations_and_fish(aquarium_obj):
- sum_fish = sum([f.price for f in aquarium_obj.fish])
- sum_decorations = sum([d.price for d in aquarium_obj.decorations])
- return sum_fish + sum_decorations
- def report(self):
- return '\n'.join([str(a) for a in self.aquariums])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement