horozov86

Unit_tests_plantation_27.07.2023

Aug 2nd, 2023
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. class Plantation:
  2.     def __init__(self, size: int):
  3.         self.size = size
  4.         self.plants = {}
  5.         self.workers = []
  6.  
  7.     @property
  8.     def size(self):
  9.         return self.__size
  10.  
  11.     @size.setter
  12.     def size(self, value: int):
  13.         if value < 0:
  14.             raise ValueError("Size must be positive number!")
  15.         self.__size = value
  16.  
  17.     def hire_worker(self, worker: str):
  18.         if worker in self.workers:
  19.             raise ValueError("Worker already hired!")
  20.  
  21.         self.workers.append(worker)
  22.         return f"{worker} successfully hired."
  23.  
  24.     def __len__(self):
  25.         count_of_plants = 0
  26.         for plants_planted in self.plants.values():
  27.             count_of_plants += len(plants_planted)
  28.         return count_of_plants
  29.  
  30.     def planting(self, worker: str, plant: str):
  31.         if worker not in self.workers:
  32.             raise ValueError(f"Worker with name {worker} is not hired!")
  33.  
  34.         elif len(self) >= self.size:
  35.             raise ValueError("The plantation is full!")
  36.         if worker in self.plants.keys():
  37.             self.plants[worker].append(plant)
  38.             return f"{worker} planted {plant}."
  39.         self.plants[worker] = [plant]
  40.         return f"{worker} planted it's first {plant}."
  41.  
  42.     def __str__(self):
  43.         result = [f"Plantation size: {self.size}"]
  44.         result.append(f'{", ".join(self.workers)}')
  45.         for worker, plants in self.plants.items():
  46.             result.append(f"{worker} planted: {', '.join(plants)}")
  47.         return '\n'.join(result)
  48.  
  49.     def __repr__(self):
  50.         result = ''
  51.         result += f'Size: {self.size}\n'
  52.         result += f'Workers: {", ".join(self.workers)}'
  53.         return result
Add Comment
Please, Sign In to add comment