class Controller: decorations_repository: DecorationRepository aquariums: List[BaseAquarium] def __init__(self) -> None: self.decorations_repository = DecorationRepository() self.aquariums = [] def add_aquarium(self, aquarium_type: str, aquarium_name: str) -> str: aquarium_types = { 'FreshwaterAquarium': FreshwaterAquarium, 'SaltwaterAquarium': SaltwaterAquarium, } if aquarium_type not in aquarium_types: return "Invalid aquarium type." self.aquariums.append(aquarium_types[aquarium_type](aquarium_name)) return f"Successfully added {aquarium_type}." def add_decoration(self, decoration_type: str) -> str: decoration_types = { 'Ornament': Ornament, 'Plant': Plant, } if decoration_type not in decoration_types: return "Invalid decoration type." self.decorations_repository.add(decoration_types[decoration_type]()) return f"Successfully added {decoration_type}." def _find_aquarium(self, aquarium_name: str) -> Optional[BaseAquarium]: for i in self.aquariums: if i.name == aquarium_name: return i return None def _pop_first_decoration_of_type( self, decoration_type: str) -> Optional[BaseDecoration]: d = self.decorations_repository.find_by_type(decoration_type) if not isinstance(d, BaseDecoration): return None self.decorations_repository.remove(d) return d def insert_decoration(self, aquarium_name: str, decoration_type: str) -> Optional[str]: aquarium = self._find_aquarium(aquarium_name) if not aquarium: return None decoration = self._pop_first_decoration_of_type(decoration_type) if not decoration: return f"There isn't a decoration of type {decoration_type}." aquarium.add_decoration(decoration) return f'Successfully added {decoration_type} to {aquarium_name}.' def add_fish(self, aquarium_name: str, fish_type: str, fish_name: str, fish_species: str, price: float) -> Optional[str]: aquarium = self._find_aquarium(aquarium_name) if not aquarium: return None fish_types: Dict[str, Type[BaseFish]] = { 'FreshwaterFish': FreshwaterFish, 'SaltwaterFish': SaltwaterFish, } if fish_type not in fish_types: return f"There isn't a fish of type {fish_type}." fish = fish_types[fish_type](fish_name, fish_species, price) return aquarium.add_fish(fish) def feed_fish(self, aquarium_name: str) -> Optional[str]: a = self._find_aquarium(aquarium_name) if not a: return None a.feed() return f'Fish fed: {len(a.fish)}' def calculate_value(self, aquarium_name: str) -> Optional[str]: a = self._find_aquarium(aquarium_name) if not a: return None value = sum(i.price for i in chain(a.fish, a.decorations)) return f'The value of Aquarium {a.name} is {value:.2f}.' def report(self) -> str: return '\n'.join([a.str() for a in self.aquariums])
from project.aquarium.saltwater_aquarium import SaltwaterAquarium from project.controller import Controller orn = Ornament() plant = Plant() orn_2 = Ornament() # print(orn.__dict__) # print(plant.__dict__) dec_rep = DecorationRepository() dec_rep.add(orn) dec_rep.add(orn_2) dec_rep.add(plant) print(dec_rep.find_by_type("Ornament")) print(dec_rep.__dict__) dec_rep.remove(orn_2) dec_rep.remove(orn_2) fresh_water_fish = FreshwaterFish("Sladka", "Freshwater", 5.5) salt_water_fish = SaltwaterFish("Salty", "Saltwater", 8.4) fresh_water_fish_2 = FreshwaterFish("Freshy2", "FreshFish2", 7) salt_water_fish_2 = SaltwaterFish("Salty_2", "Saltwater", 4) fresh_aquarium = FreshwaterAquarium("FreshShitAquarium") salt_aquarium = SaltwaterAquarium("SaltwaterShitAquarium") print(fresh_aquarium.add_fish(fresh_water_fish)) fresh_aquarium.add_fish(salt_water_fish) print(fresh_aquarium.add_fish(fresh_water_fish_2)) salt_aquarium.add_fish(salt_water_fish) salt_aquarium.add_fish(salt_water_fish_2) salt_aquarium.remove_fish(salt_water_fish_2) salt_aquarium.add_decoration(orn)
class Controller: def __init__(self): self.decorations_repository = DecorationRepository() self.aquariums = [] def add_aquarium(self, aquarium_type: str, aquarium_name: str): if aquarium_type == "FreshwaterAquarium": aquarium = FreshwaterAquarium(aquarium_name) self.aquariums.append(aquarium) return f"Successfully added {aquarium_type}." elif aquarium_type == "SaltwaterAquarium": aquarium = SaltwaterAquarium(aquarium_name) self.aquariums.append(aquarium) return f"Successfully added {aquarium_type}." return "Invalid aquarium type." def add_decoration(self, decoration_type: str): if decoration_type == "Ornament": decoration = Ornament() self.decorations_repository.add(decoration) return f"Successfully added {decoration_type}." elif decoration_type == "Plant": decoration = Plant() self.decorations_repository.add(decoration) return f"Successfully added {decoration_type}." else: return "Invalid decoration type." def insert_decoration(self, aquarium_name: str, decoration_type: str): aquarium = [a for a in self.aquariums if a.name == aquarium_name] if aquarium: decoration = self.decorations_repository.find_by_type(decoration_type) if decoration != "None": aquarium[0].add_decoration(decoration) self.decorations_repository.remove(decoration) return f"Successfully added {decoration_type} to {aquarium_name}." return f"There isn't a decoration of type {decoration_type}." def add_fish(self, aquarium_name: str, fish_type: str, fish_name: str, fish_species: str, price: float): if fish_type == "FreshwaterFish": fish = FreshwaterFish(fish_name, fish_species, price) elif fish_type == "SaltwaterFish": fish = SaltwaterFish(fish_name, fish_species, price) else: return f"There isn't a fish of type {fish_type}." try: aquarium = [a for a in self.aquariums if aquarium_name == a.name][0] # if aquarium.water != fish.water: # return "Water not suitable." return aquarium.add_fish(fish) except IndexError: return def feed_fish(self, aquarium_name: str): try: aquarium = [a for a in self.aquariums if aquarium_name == a.name][0] aquarium.feed() return f"Fish fed: {len(aquarium.fish)}" except IndexError: return def calculate_value(self, aquarium_name: str): try: aquarium = [a for a in self.aquariums if aquarium_name == a.name][0] value = sum(f.price for f in aquarium.fish) value += sum(d.price for d in aquarium.decorations) return f"The value of Aquarium {aquarium_name} is {value:.2f}." except IndexError: return def report(self): result = [] for aquarium in self.aquariums: result.append(str(aquarium)) return "\n".join(result)
class Controller: aquarium_types = ["FreshwaterAquarium", "SaltwaterAquarium"] decoration_types = ["Ornament", "Plant"] fish_types = ["FreshwaterFish", "SaltwaterFish"] def __init__(self): self.decorations_repository = DecorationRepository() self.aquariums = [] @staticmethod def is_type_valid(given_type, valid_types): return given_type in valid_types @staticmethod def create_aquarium(aq_type, aq_name): if aq_type == "FreshwaterAquarium": return FreshwaterAquarium(aq_name) if aq_type == "SaltwaterAquarium": return SaltwaterAquarium(aq_name) @staticmethod def create_decoration(dec_type): if dec_type == "Ornament": return Ornament() if dec_type == "Plant": return Plant() @staticmethod def create_fish(fish_type, name, species, price): if fish_type == "FreshwaterFish": return FreshwaterFish(name, species, price) if fish_type == "SaltwaterFish": return SaltwaterFish(name, species, price) @staticmethod def get_decoration(dec_type, repository): decoration = repository.find_by_type(dec_type) return None if decoration == "None" else decoration @staticmethod def get_aquarium(name, aquariums): match = [aqua for aqua in aquariums if aqua.name == name] return None if not match else match[0] @staticmethod def get_total_price(elements): return sum([el.price for el in elements]) def add_aquarium(self, aquarium_type: str, aquarium_name: str): if not self.is_type_valid(aquarium_type, self.aquarium_types): return "Invalid aquarium type." aquarium = self.create_aquarium(aquarium_type, aquarium_name) self.aquariums.append(aquarium) return f"Successfully added {aquarium_type}." def add_decoration(self, decoration_type: str): if not self.is_type_valid(decoration_type, self.decoration_types): return "Invalid decoration type." decoration = self.create_decoration(decoration_type) self.decorations_repository.add(decoration) return f"Successfully added {decoration_type}." def insert_decoration(self, aquarium_name: str, decoration_type: str): decoration = self.get_decoration(decoration_type, self.decorations_repository) aquarium = self.get_aquarium(aquarium_name, self.aquariums) if not decoration: return f"There isn't a decoration of type {decoration_type}." if decoration and aquarium: aquarium.decorations.append(decoration) self.decorations_repository.remove(decoration) return f"Successfully added {decoration_type} to {aquarium_name}." def add_fish(self, aquarium_name: str, fish_type: str, fish_name: str, fish_species: str, price: float): if not self.is_type_valid(fish_type, self.fish_types): return f"There isn't a fish of type {fish_type}." fish = self.create_fish(fish_type, fish_name, fish_species, price) aquarium = self.get_aquarium(aquarium_name, self.aquariums) return aquarium.add_fish(fish) def feed_fish(self, aquarium_name: str): aquarium = self.get_aquarium(aquarium_name, self.aquariums) aquarium.feed() fed_count = len(aquarium.fish) return f"Fish fed: {fed_count}" def calculate_value(self, aquarium_name: str): aquarium = self.get_aquarium(aquarium_name, self.aquariums) fish_prices = self.get_total_price(aquarium.fish) decorations_prices = self.get_total_price(aquarium.decorations) value = fish_prices + decorations_prices return f"The value of Aquarium {aquarium_name} is {value:.2f}." def report(self): result = [] for aquarium in self.aquariums: result.append(str(aquarium)) return '\n'.join(result)
print(f"Fish numbersssss: {len(s.fish)}") print(s) print(fish3.fish_type) print(co.add_aquarium("SaltwaterAquarium", "SaltAQ")) print(co.add_aquarium("FreshwaterAquarium", "FreshAQ")) print(len(co.aquariums)) print(co.add_decoration("Plant")) print(co.add_decoration("Ornament")) print(co.add_decoration("Kurec")) print(de.decorations) de.add(Plant()) de.add(Ornament()) de.add(Plant()) print(len(de.decorations)) print(de.remove(Plant())) print(len(de.decorations)) print(de.find_by_type("Plant")) print(de.find_by_type("Ornament")) print(co.add_decoration("Plant")) print(co.add_decoration("Ornament")) print(co.add_decoration("Ornamentdas")) print(len(co.decorations_repository.decorations)) print(len(s.decorations)) print(co.insert_decoration("SaltAQ", "Plantаааааа")) print(co.decorations_repository.decorations) print(len(s.decorations)) print(len(s.fish)) print(co.feed_fish("SaltAQ")) print(co.calculate_value("SaltAQ")) print(co.report())
class Controller: def __init__(self): self.decorations_repository = DecorationRepository() self.aquariums = [] def add_aquarium(self, aquarium_type: str, aquarium_name: str): type_ = self.find_type_of_aquarium(aquarium_type) if not type_: return "Invalid aquarium type." aquarium = type_(aquarium_name) self.aquariums.append(aquarium) return f"Successfully added {aquarium_type}." @staticmethod def find_type_of_aquarium(type_): if type_ == 'FreshwaterAquarium': return FreshwaterAquarium elif type_ == 'SaltwaterAquarium': return SaltwaterAquarium else: return None def add_decoration(self, decoration_type: str): type_ = self.validate_type_decoration(decoration_type) if not type_: return "Invalid decoration type." decoration = type_() self.decorations_repository.add(decoration) return f"Successfully added {decoration_type}." @staticmethod def validate_type_decoration(type_): if type_ == 'Plant': return Plant elif type_ == 'Ornament': return Ornament else: return None def __find_aquarium(self, name): for aquarium in self.aquariums: if aquarium.name == name: return aquarium def insert_decoration(self, aquarium_name: str, decoration_type: str): aquarium = self.__find_aquarium(aquarium_name) decoration = self.decorations_repository.find_by_type(decoration_type) if decoration == "None": return f"There isn't a decoration of type {decoration_type}." if aquarium: aquarium.add_decoration(decoration) self.decorations_repository.remove(decoration) return f"Successfully added {decoration_type} to {aquarium_name}." def add_fish(self, aquarium_name: str, fish_type: str, fish_name: str, fish_species: str, price: float): type_ = self.validate_type_of_fish(fish_type) aquarium = self.__find_aquarium(aquarium_name) if not type_: return f"There isn't a fish of type {fish_type}." if aquarium.__class__.__name__ == 'FreshwaterAquarium' and fish_type == 'SaltwaterFish': return "Water not suitable." elif aquarium.__class__.__name__ == 'SaltwaterAquarium' and fish_type == 'FreshwaterFish': return "Water not suitable." fish = type_(fish_name, fish_species, price) result = aquarium.add_fish(fish) return result @staticmethod def validate_type_of_fish(type_): if type_ == 'FreshwaterFish': return FreshwaterFish elif type_ == 'SaltwaterFish': return SaltwaterFish else: return None def feed_fish(self, aquarium_name: str): aquarium = self.__find_aquarium(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(aquarium_name) sum_ = 0 for fish in aquarium.fish: sum_ += fish.price for decoration in aquarium.decorations: sum_ += decoration.price return f"The value of Aquarium {aquarium_name} is {sum_:.2f}." def report(self): result = '' for aquarium in self.aquariums: result += f'{aquarium}\n' return result
class Controller: def __init__(self): self.decorations_repository = DecorationRepository() self.aquariums = [] def add_aquarium(self, aquarium_type: str, aquarium_name: str): if aquarium_type != "FreshwaterAquarium" and aquarium_type != "SaltwaterAquarium": return "Invalid aquarium type." else: if aquarium_type == "FreshwaterAquarium": aquarium = FreshwaterAquarium(aquarium_name) else: aquarium = SaltwaterAquarium(aquarium_name) self.aquariums.append(aquarium) return f"Successfully added {aquarium_type}." def add_decoration(self, decoration_type: str): if decoration_type != "Ornament" and decoration_type != "Plant": return "Invalid decoration type." else: if decoration_type == "Ornament": decoration = Ornament() else: decoration = Plant() self.decorations_repository.add(decoration) return f"Successfully added {decoration_type}." def insert_decoration(self, aquarium_name: str, decoration_type: str): try: decoration = \ [d for d in self.decorations_repository.decorations if d.__class__.__name__ == decoration_type][0] except IndexError: return f"There isn't a decoration of type {decoration_type}." try: aquarium = \ [a for a in self.aquariums if a.name == aquarium_name][0] aquarium.add_decoration(decoration) self.decorations_repository.remove(decoration) return f"Successfully added {decoration_type} to {aquarium_name}." except IndexError: return def add_fish(self, aquarium_name: str, fish_type: str, fish_name: str, fish_species: str, price: float): if fish_type != "FreshwaterFish" and fish_type != "SaltwaterFish": return f"There isn't a fish of type {fish_type}." else: if fish_type == "FreshwaterFish": fish = FreshwaterFish(fish_name, fish_species, price) else: fish = SaltwaterFish(fish_name, fish_species, price) aquarium = \ [a for a in self.aquariums if a.name == aquarium_name][0] if "Freshwater" in aquarium.__class__.__name__ and "Freshwater" in fish.__class__.__name__: return aquarium.add_fish(fish) elif "Saltwater" in aquarium.__class__.__name__ and "Saltwater" in fish.__class__.__name__: return aquarium.add_fish(fish) else: return "Water not suitable." def feed_fish(self, aquarium_name: str): aquarium = \ [a for a in self.aquariums if a.name == aquarium_name][0] aquarium.feed() fed_count = len(aquarium.fish) return f"Fish fed: {fed_count}" def calculate_value(self, aquarium_name: str): aquarium = \ [a for a in self.aquariums if a.name == aquarium_name][0] value_fish = sum([f.price for f in aquarium.fish]) value_decorations = sum([d.price for d in aquarium.decorations]) return f"The value of Aquarium {aquarium_name} is {(value_fish + value_decorations):.2f}." def report(self): result = "\n".join([str(a) for a in self.aquariums]) return result
class Controller: _AQUARIUM_TYPES = { "FreshwaterAquarium": FreshwaterAquarium, "SaltwaterAquarium": SaltwaterAquarium, } _DECORATION_TYPES = { "Ornament": Ornament, "Plant": Plant, } _FISH_TYPES = { "FreshwaterFish": FreshwaterFish, "SaltwaterFish": SaltwaterFish, } def __init__(self): self.decorations_repository = DecorationRepository() self.aquariums = [] def find_aquarium_by_name(self, name): for aquarium in self.aquariums: if aquarium.name == name: return aquarium return False def add_aquarium(self, aquarium_type, aquarium_name): if aquarium_type not in self._AQUARIUM_TYPES: return "Invalid aquarium type." aquarium = self._AQUARIUM_TYPES[aquarium_type](aquarium_name) self.aquariums.append(aquarium) return f"Successfully added {aquarium_type}." def add_decoration(self, decoration_type): if decoration_type not in self._DECORATION_TYPES: return "Invalid decoration type." decoration = self._DECORATION_TYPES[decoration_type]() self.decorations_repository.add(decoration) return f"Successfully added {decoration_type}." def insert_decoration(self, aquarium_name, decoration_type): aquarium = self.find_aquarium_by_name(aquarium_name) decoration = self.decorations_repository.find_by_type(decoration_type) if decoration == "None": return f"There isn't a decoration of type {decoration_type}." if aquarium: aquarium.add_decoration(decoration) self.decorations_repository.remove(decoration) return f"Successfully added {decoration_type} to {aquarium_name}." def add_fish(self, aquarium_name, fish_type, fish_name, fish_species, price): if fish_type not in self._FISH_TYPES: return f"There isn't a fish of type {fish_type}." fish = self._FISH_TYPES[fish_type](fish_name, fish_species, price) aquarium = self.find_aquarium_by_name(aquarium_name) if aquarium.suitable_fish != fish_type: return "Water not suitable." return aquarium.add_fish(fish) def feed_fish(self, aquarium_name): aquarium = self.find_aquarium_by_name(aquarium_name) fed_count = aquarium.feed() return f"Fish fed: {fed_count}" def calculate_value(self, aquarium_name): aquarium = self.find_aquarium_by_name(aquarium_name) fish_prices = sum([fish.price for fish in aquarium.fish]) decoration_prices = sum( [decoration.price for decoration in aquarium.decorations]) total = fish_prices + decoration_prices return f"The value of Aquarium {aquarium_name} is {total:.2f}." def report(self): info = [str(aquarium) for aquarium in self.aquariums] return '\n'.join(info)
class Controller: def __init__(self): self.decorations_repository = DecorationRepository() self.aquariums = [] @staticmethod def get_fish(fish_type, fish_name, fish_species, price): fish = None if fish_type == 'FreshwaterFish': fish = FreshwaterFish(fish_name, fish_species, price) elif fish_type == 'SaltwaterFish': fish = SaltwaterFish(fish_name, fish_species, price) return fish def get_aquarium(self, aquarium_name): try: aquarium = [a for a in self.aquariums if a.name == aquarium_name][0] except IndexError: aquarium = None return aquarium def add_aquarium(self, aquarium_type, aquarium_name): valid_aquariums = ["FreshwaterAquarium", "SaltwaterAquarium"] if aquarium_type not in valid_aquariums: return "Invalid aquarium type." if aquarium_type == valid_aquariums[0]: aquarium = FreshwaterAquarium(aquarium_name) else: aquarium = SaltwaterAquarium(aquarium_name) self.aquariums.append(aquarium) return f"Successfully added {aquarium_type}." def add_decoration(self, decoration_type): valid_decorations = ["Ornament", "Plant"] if decoration_type not in valid_decorations: return "Invalid decoration type." if decoration_type == valid_decorations[0]: decoration = Ornament() else: decoration = Plant() self.decorations_repository.add(decoration) return f"Successfully added {decoration_type}." def insert_decoration(self, aquarium_name, decoration_type): decoration = self.decorations_repository.find_by_type(decoration_type) aquarium = self.get_aquarium(aquarium_name) if aquarium is not None and not decoration == "None": aquarium.add_decoration(decoration) if self.decorations_repository.remove(decoration): return f"Successfully added {decoration_type} to {aquarium_name}." if decoration == "None": return f"There isn't a decoration of type {decoration_type}." def add_fish(self, aquarium_name, fish_type, fish_name, fish_species, price): valid_fish = ["FreshwaterFish", "SaltwaterFish"] if fish_type not in valid_fish: return f"There isn't a fish of type {fish_type}." fish = self.get_fish(fish_type, fish_name, fish_species, price) aquarium = self.get_aquarium(aquarium_name) if not aquarium.water == fish.water: return "Water not suitable." return aquarium.add_fish(fish) def feed_fish(self, aquarium_name): aquarium = self.get_aquarium(aquarium_name) aquarium.feed() return f"Fish fed: {len(aquarium.fish)}" def calculate_value(self, aquarium_name): aquarium = self.get_aquarium(aquarium_name) fish_price = sum(fish.price for fish in aquarium.fish) decorations_price = sum(decoration.price for decoration in aquarium.decorations) total_price = fish_price + decorations_price return f"The value of Aquarium {aquarium_name} is {total_price:.2f}." def report(self): result = "" for aquarium in self.aquariums: result += aquarium.__str__() return result
class Controller: def __init__(self): self.decorations_repository = DecorationRepository() self.aquariums = [] def find_aquarium_by_name(self, name): try: aquarium = [a for a in self.aquariums if a.name == name][0] return aquarium except IndexError: return def add_aquarium(self, aquarium_type: str, aquarium_name: str): valid_type = ('FreshwaterAquarium', 'SaltwaterAquarium') if aquarium_type not in valid_type: return "Invalid aquarium type." if aquarium_type == valid_type[0]: new_aquarium = FreshwaterAquarium(aquarium_name) elif aquarium_type == valid_type[1]: new_aquarium = SaltwaterAquarium(aquarium_name) self.aquariums.append(new_aquarium) return f"Successfully added {aquarium_type}." def add_decoration(self, decoration_type: str): valid_type = ('Ornament', 'Plant') if decoration_type not in valid_type: return "Invalid decoration type." if decoration_type == valid_type[0]: new_decoration = Ornament() elif decoration_type == valid_type[1]: new_decoration = Plant() self.decorations_repository.add(new_decoration) return f"Successfully added {decoration_type}." def insert_decoration(self, aquarium_name: str, decoration_type: str): decoration = self.decorations_repository.find_by_type(decoration_type) if decoration == 'None': return f"There isn't a decoration of type {decoration_type}." aquarium = self.find_aquarium_by_name(aquarium_name) aquarium.add_decoration(decoration) self.decorations_repository.remove(decoration) return f"Successfully added {decoration_type} to {aquarium_name}." def add_fish(self, aquarium_name: str, fish_type: str, fish_name: str, fish_species: str, price: float): valid_fish_type = ('FreshwaterFish', 'SaltwaterFish') valid_aquarium_type = ('FreshwaterAquarium', 'SaltwaterAquarium') if fish_type not in valid_fish_type: return f"There isn't a fish of type {fish_type}." aquarium = self.find_aquarium_by_name(aquarium_name) if fish_type == valid_fish_type[0] and aquarium.__class__.__name__ == valid_aquarium_type[0]: new_fish = FreshwaterFish(fish_name, fish_species, price) return aquarium.add_fish(new_fish) elif fish_type == valid_fish_type[1] and aquarium.__class__.__name__ == valid_aquarium_type[1]: new_fish = SaltwaterFish(fish_name, fish_species, price) return aquarium.add_fish(new_fish) else: return f"Water not suitable." def feed_fish(self, aquarium_name: str): aquarium = self.find_aquarium_by_name(aquarium_name) aquarium.feed() return f"Fish fed: {len(aquarium.fish)}" def calculate_value(self, aquarium_name: str): aquarium = self.find_aquarium_by_name(aquarium_name) value = aquarium.total_value return f"The value of Aquarium {aquarium_name} is {value:.2f}." def report(self): result = '\n'.join([str(a) for a in self.aquariums]) return result[:-1]
class Controller: def __init__(self): self.decorations_repository = DecorationRepository() self.aquariums = [] @staticmethod def get_aquarium_type(aquarium_type): if aquarium_type == "FreshwaterAquarium": return FreshwaterAquarium elif aquarium_type == "SaltwaterAquarium": return SaltwaterAquarium @staticmethod def get_decoration_type(decoration_type): if decoration_type == "Ornament": return Ornament elif decoration_type == "Plant": return Plant @staticmethod def get_fish_type(fish_type): if fish_type == "FreshwaterFish": return FreshwaterFish elif fish_type == "SaltwaterFish": return SaltwaterFish def get_aquarium_by_name(self, name): for a in self.aquariums: if a.name == name: return a def add_aquarium(self, aquarium_type: str, aquarium_name: str): aquarium = self.get_aquarium_type(aquarium_type) if not aquarium: return "Invalid aquarium type." self.aquariums.append(aquarium(aquarium_name)) return f"Successfully added {aquarium_type}." def add_decoration(self, decoration_type: str): decoration = self.get_decoration_type(decoration_type) if not decoration: return "Invalid decoration type." self.decorations_repository.add(decoration()) return f"Successfully added {decoration_type}." def insert_decoration(self, aquarium_name: str, decoration_type: str): aquarium = self.get_aquarium_by_name(aquarium_name) decoration = [d for d in self.decorations_repository.decorations if d.__class__.__name__ == decoration_type] if not aquarium: return if not decoration: return f"There isn't a decoration of type {decoration_type}." aquarium = aquarium decoration = decoration[0] aquarium.add_decoration(decoration) self.decorations_repository.remove(decoration) return f"Successfully added {decoration_type} to {aquarium_name}." def add_fish(self, aquarium_name: str, fish_type: str, fish_name: str, fish_species: str, price: float): fish = self.get_fish_type(fish_type) aquarium = self.get_aquarium_by_name(aquarium_name) if not fish: return f"There isn't a fish of type {fish_type}." if not aquarium: return return aquarium.add_fish(fish(fish_name, fish_species, price)) def feed_fish(self, aquarium_name: str): aquarium = self.get_aquarium_by_name(aquarium_name) if not aquarium: return aquarium.feed() return f"Fish fed: {len(aquarium.fish)}" def calculate_value(self, aquarium_name: str): aquarium = self.get_aquarium_by_name(aquarium_name) if not aquarium: return price = aquarium.total_price return f"The value of Aquarium {aquarium_name} is {price:.2f}." def report(self): result = "" for a in self.aquariums: result += str(a) return result
class Controller: def __init__(self): self.decorations_repository = DecorationRepository() self.aquariums = [] def add_aquarium(self, aquarium_type: str, aquarium_name: str): if aquarium_type == 'FreshwaterAquarium': self.aquariums.append(FreshwaterAquarium(aquarium_name)) return f"Successfully added {aquarium_type}." elif aquarium_type == "SaltwaterAquarium": self.aquariums.append(SaltwaterAquarium(aquarium_name)) return f"Successfully added {aquarium_type}." else: return "Invalid aquarium type." def add_decoration(self, decoration_type: str): if decoration_type == 'Ornament': self.decorations_repository.add(Ornament()) return f"Successfully added {decoration_type}." elif decoration_type == 'Plant': self.decorations_repository.add(Plant()) return f"Successfully added {decoration_type}." else: return "Invalid decoration type." def insert_decoration(self, aquarium_name: str, decoration_type: str): try: temp_a = [a for a in self.aquariums if a.name == aquarium_name][0] temp_d = [ d for d in self.decorations_repository.decorations if d.__class__.__name__ == decoration_type ][0] temp_a.add_decoration(temp_d) self.decorations_repository.remove(temp_d) return f"Successfully added {decoration_type} to {aquarium_name}." except IndexError: return f"There isn't a decoration of type {decoration_type}." def add_fish(self, aquarium_name: str, fish_type: str, fish_name: str, fish_species: str, price: float): if fish_type not in ["FreshwaterFish", "SaltwaterFish"]: return f"There isn't a fish of type {fish_type}." try: temp_a = [a for a in self.aquariums if a.name == aquarium_name][0] if fish_type == "FreshwaterFish" and temp_a.__class__.__name__ == 'FreshwaterAquarium': return temp_a.add_fish( FreshwaterFish(fish_name, fish_species, price)) elif fish_type == "SaltwaterFish" and temp_a.__class__.__name__ == 'SaltwaterAquarium': return temp_a.add_fish( SaltwaterFish(fish_name, fish_species, price)) else: return "Water not suitable." except IndexError: pass def feed_fish(self, aquarium_name: str): fed_count = 0 for a in self.aquariums: if a.name == aquarium_name: for fishs in a.fish: fishs.eat() fed_count += 1 return f"Fish fed: {fed_count}" def calculate_value(self, aquarium_name: str): value = 0 temp_a = [a for a in self.aquariums if a.name == aquarium_name][0] value += sum([f.price for f in temp_a.fish]) value += sum([d.price for d in temp_a.decorations]) return f"The value of Aquarium {aquarium_name} is {value:.2f}." def report(self): result = '' for a in self.aquariums: result += a.__str__() return result
class Controller: def __init__(self): self.decorations_repository = DecorationRepository() self.aquariums = [] def add_aquarium(self, aquarium_type: str, aquarium_name: str): valid_types = ('FreshwaterAquarium', 'SaltwaterAquarium') if aquarium_type not in valid_types: return 'Invalid aquarium type.' if aquarium_type == valid_types[0]: new_aquarium = FreshwaterAquarium(aquarium_name) else: new_aquarium = SaltwaterAquarium(aquarium_name) self.aquariums.append(new_aquarium) return f'Successfully added {aquarium_type}.' def add_decoration(self, decoration_type: str): valid_types = ('Ornament', 'Plant') if decoration_type not in valid_types: return 'Invalid decoration type.' if decoration_type == valid_types[0]: new_decoration = Ornament() else: new_decoration = Plant() self.decorations_repository.add(new_decoration) return f'Successfully added {decoration_type}.' def insert_decoration(self, aquarium_name: str, decoration_type: str): decoration = self.decorations_repository.find_by_type(decoration_type) aquariums = [a for a in self.aquariums if a.name == aquarium_name] if decoration == 'None': return f'There isn\'t a decoration of type {decoration_type}.' if not aquariums: return aquarium = aquariums[0] aquarium.add_decoration(decoration) is_removed = self.decorations_repository.remove(decoration) if is_removed: return f'Successfully added {decoration_type} to {aquarium_name}.' def add_fish(self, aquarium_name: str, fish_type: str, fish_name: str, fish_species: str, price: float): valid_fish_types = ('FreshwaterFish', 'SaltwaterFish') valid_aquarium_types = ('FreshwaterAquarium', 'SaltwaterAquarium') if fish_type not in valid_fish_types: return f'There isn\'t a fish of type {fish_type}.' aquarium = [a for a in self.aquariums if a.name == aquarium_name][0] if fish_type == valid_fish_types[ 0] and aquarium.__class__.__name__ == valid_aquarium_types[0]: new_fish = FreshwaterFish(name=fish_name, species=fish_species, price=price) return aquarium.add_fish(new_fish) elif fish_type == valid_fish_types[ 1] and aquarium.__class__.__name__ == valid_aquarium_types[1]: new_fish = SaltwaterFish(name=fish_name, species=fish_species, price=price) return aquarium.add_fish(new_fish) else: return 'Water not suitable.' def feed_fish(self, aquarium_name: str): aquarium = [a for a in self.aquariums if a.name == aquarium_name][0] for fish in aquarium.fish: fish.eat() return f'Fish fed: {len(aquarium.fish)}' def calculate_value(self, aquarium_name: str): aquarium = [a for a in self.aquariums if a.name == aquarium_name][0] value = sum([d.price for d in aquarium.decorations]) + sum( [f.price for f in aquarium.fish]) return f'The value of Aquarium {aquarium_name} is {value:.2f}.' def report(self): result = '' for aquarium in self.aquariums: result += str(aquarium) + '\n' return result[:-1]
class Controller: def __init__(self): self.decorations_repository = DecorationRepository() self.aquariums = [] def add_aquarium(self, aquarium_type: str, aquarium_name: str): if aquarium_type not in ["FreshwaterAquarium", "SaltwaterAquarium"]: return "Invalid aquarium type." aquarium = FreshwaterAquarium(aquarium_name) if aquarium_type == 'FreshwaterAquarium' \ else SaltwaterAquarium(aquarium_name) self.aquariums.append(aquarium) return f"Successfully added {aquarium_type}." def add_decoration(self, decoration_type: str): if decoration_type not in ["Ornament", "Plant"]: return "Invalid decoration type." decoration = Ornament() if decoration_type == "Ornament" else Plant() self.decorations_repository.add(decoration) return "Successfully added {decoration_type}." def insert_decoration(self, aquarium_name: str, decoration_type: str): if aquarium_name in map(lambda a: a.name, self.aquariums): aquarium = [a for a in self.aquariums if a.name == aquarium_name][0] decoration = self.decorations_repository.find_by_type( decoration_type) if decoration == 'None': return f"There isn't a decoration of type {decoration_type}." aquarium.add_decoration(decoration) self.decorations_repository.remove(decoration) return f"Successfully added {decoration_type} to {aquarium_name}." def add_fish(self, aquarium_name: str, fish_type: str, fish_name: str, fish_species: str, price: float): valid_fish_types = ["FreshwaterFish", "SaltwaterFish"] if fish_type not in valid_fish_types: return f"There isn't a fish of type {fish_type}." fish = FreshwaterFish(fish_name, fish_species, price) if fish_type == "FreshwaterFish" \ else SaltwaterFish(fish_name, fish_species, price) if aquarium_name in map(lambda a: a.name, self.aquariums): aquarium = [a for a in self.aquariums if a.name == aquarium_name][0] aquarium.add_fish(fish) def feed_fish(self, aquarium_name: str): if aquarium_name in map(lambda a: a.name, self.aquariums): aquarium = [a for a in self.aquariums if a.name == aquarium_name][0] aquarium.feed() return f"Fish fed: {len(aquarium.fish)}" def calculate_value(self, aquarium_name: str): if aquarium_name in map(lambda a: a.name, self.aquariums): aquarium = [a for a in self.aquariums if a.name == aquarium_name][0] fish_price = sum([f.price for f in aquarium.fish]) decoration_price = sum([d.price for d in aquarium.decorations]) value = fish_price + decoration_price return f"The value of Aquarium {aquarium_name} is {value:.2f}." def report(self): return '\n'.join([str(a) for a in self.aquariums])
class Controller: def __init__(self): self.decorations_repository = DecorationRepository() self.aquariums = [] def add_aquarium(self, aquarium_type, aquarium_name): if aquarium_type == 'FreshwaterAquarium': aquarium = FreshwaterAquarium(aquarium_name) elif aquarium_type == 'SaltwaterAquarium': aquarium = SaltwaterAquarium(aquarium_name) else: return 'Invalid aquarium type.' self.aquariums.append(aquarium) return f'Successfully added {aquarium_type}.' def add_decoration(self, decoration_type): if decoration_type == 'Ornament': decoration = Ornament() elif decoration_type == 'Plant': decoration = Plant() else: return 'Invalid decoration type.' self.decorations_repository.add(decoration) return f'Successfully added {decoration_type}.' def insert_decoration(self, aquarium_name, decoration_type): aquarium_exists = self.__get_aquarium_by_name(aquarium_name) decoration_type_exists = self.decorations_repository.find_by_type( decoration_type) if aquarium_exists and (decoration_type_exists != 'None'): self.decorations_repository.remove(decoration_type_exists) aquarium_exists.add_decoration(decoration_type_exists) return f'Successfully added {decoration_type} to {aquarium_name}.' if decoration_type != 'Plant' and decoration_type != 'Ornament': return f"There isn't a decoration of type {decoration_type}." def add_fish(self, aquarium_name, fish_type, fish_name, fish_species, price): aquarium = self.__get_aquarium_by_name(aquarium_name) if fish_type == 'FreshwaterFish': fish_object = FreshwaterFish(fish_name, fish_species, price) elif fish_type == 'SaltwaterFish': fish_object = SaltwaterFish(fish_name, fish_species, price) else: return f"There isn't a fish of type {fish_type}." return aquarium.add_fish(fish_object) def feed_fish(self, aquarium_name): aquarium_exists = self.__get_aquarium_by_name(aquarium_name) aquarium_exists.feed() amount_fish = len(aquarium_exists.fish) return f'Fish fed: {amount_fish}' def calculate_value(self, aquarium_name): aquarium_exists = self.__get_aquarium_by_name(aquarium_name) decorations_cost = sum( [dec.price for dec in aquarium_exists.decorations]) fish_cost = sum([fish_obj.price for fish_obj in aquarium_exists.fish]) total = decorations_cost + fish_cost return f'The value of Aquarium {aquarium_name} is {total:.2f}.' def report(self): result = '' for aquarium in self.aquariums: result += aquarium.__str__() return result def __get_aquarium_by_name(self, aquarium_name): for aquarium in self.aquariums: if getattr(aquarium, 'name') == aquarium_name: return aquarium