class TestZoo(unittest.TestCase): def setUp(self): animals = [] capacity = 200 budget = 1000 self.my_zoo = Zoo(animals, capacity, budget) def test_init(self): self.assertListEqual(self.my_zoo.animals, []) self.assertEqual(self.my_zoo.capacity, 200) self.assertEqual(self.my_zoo.budget, 1000) self.assertEqual(self.my_zoo.DAILY_INCOME, 60) self.assertEqual(self.my_zoo.KILO_MEAT, 4) self.assertEqual(self.my_zoo.KILO_VEG_FOOD, 2) def test_accomodate(self): animal = Animal("Wolf", 2, "Kat", "male", 2,) self.my_zoo.accomodate(animal) self.assertListEqual(self.my_zoo.animals, [animal]) def test_calculate_incomes(self): animal = Animal("Wolf", 2, "Kat", "male", 2,) self.my_zoo.accomodate(animal) self.my_zoo.calculate_income() self.assertEqual(self.my_zoo.budget, 1060) def test_calculate_outcome(self): animal = Animal("Wolf", 2, "Kat", "male", 2) self.my_zoo.accomodate(animal) outcome = self.my_zoo.calculate_outcome() self.my_zoo.budget -= outcome self.assertEqual(self.my_zoo.budget, 999.6) def test_reproduce(self): animal = Animal("Wolf", 2, "Kat", "male", 2) self.my_zoo.accomodate(animal) animal = Animal("Wolf", 2, "Kate", "female", 3) self.my_zoo.accomodate(animal) self.my_zoo.reproduce()
class ZooGarden: def __init__(self): self.zoo = Zoo(50, 10000) def see_animals(self): if len(self.zoo.animals) != 0: for animal in self.zoo.animals: print(animal) else: print("\nThere are no animals yet. Accomodate some?") def accomodate(self, species, name, age, weight): gender = input("What gender is the animal?: ") self.zoo.accommodate(species, age, name, gender, weight) def move_to_habitat(self, species, name): has_such_animal = False for animal in self.zoo.animals: if animal.species == species and animal.name == name: print("{} is now a free {}!".format(animal.name, animal.species)) self.zoo.animals.remove(animal) Animal.SPECIES_NAMES[animal.species].remove(animal.name) has_such_animal = True if not has_such_animal: print("\nThere is no such animal in the zoo anyway.") def simulate(self, interval_of_time, period): if len(self.zoo.animals) != 0: time_in_months = self.__time_in_months(interval_of_time, period) for female in self.zoo.animals: for male in self.zoo.animals: self.zoo.reproduce(female, male) self.__grow_all_animals(time_in_months) print('\nTheese animals have grown:') self.see_animals() self.__print_dead_animals(self.zoo.dead_animals()) self.__could_budget_afford_food(time_in_months) self.__born_animals() else: print("\nThere are no animals yet. Accomodate some?") def __time_in_months(self, interval_of_time, period): if interval_of_time == 'days': return period * 1 / 30 # rough approximation elif interval_of_time == 'weeks': return period * 1 / 4 # rough approximation elif interval_of_time == 'months': return period elif interval_of_time == 'years': return period * 12 else: print("\nWrong interval of time, it should be " "days, weeks, months or years.") def __grow_all_animals(self, time_in_months): for animal in self.zoo.animals: animal.grow(time_in_months) def __print_dead_animals(self, list_dead_animals): if len(list_dead_animals) != 0: print("\nTheese animals had died:") for animal in list_dead_animals: print(animal) else: print("\nNo animals have died.") def __could_budget_afford_food(self, time_in_months): time_in_days = time_in_months * 30 # rough approximation self.zoo.budget += self.zoo.get_income() * time_in_days self.zoo.get_outcome() if self.zoo.budget >= 0: print("\nZoo's budget can afford feeding all animals:)") else: print("T\nhere aren't enough money to feed all animals." "Move some to habitat?") def __born_animals(self): babies = self.zoo.newborn_baby() if len(babies) != 0: print("\nZoo's babies are:") for baby in babies: print(baby) else: print("\nThere're no babies.") def show_database_species(self): file = open("database.json", "r") content = json.loads(file.read()) file.close() print("\nDatabase species are:") for species_key in content: print(species_key) def interface(self): print("\nWelcome to our Zoo! You can use one of the following" " commands:" "\n\nshow_database_species -> to know wich species" " are in our database" "\n\nsee_animals -> to see all current animals" "\n\naccomodate <species> <name> <age_in_months> <weight_in_" "kilos> -> to add animal in the zoo" "\n\nmove_to_habitat <species> <name> -> removes an animal from " "the Zoo and returns it to its natural habitat" "\n\nsimulate <interval_of_time> <period> -> shows what happens" " in the Zoo for that time" "\n - <interval_of_time> is 'days', 'weeks', 'months' or 'years'" "\n - <period> is a number" "\n\nexit -> to exit the program") while True: reply = input("\nYour choice: ") if reply == 'exit': break else: reply = reply.split() # make list of arguments if reply[0] == "see_animals": self.see_animals() elif reply[0] == "accomodate" and len(reply) == 5: self.accomodate(reply[1], reply[2], float(reply[3]), float(reply[4])) elif reply[0] == "move_to_habitat" and len(reply) == 3: self.move_to_habitat(reply[1], reply[2]) elif reply[0] == "simulate" and len(reply) == 3: self.simulate(reply[1], float(reply[2])) elif reply[0] == "show_database_species": self.show_database_species() else: print("Wrong command! Try again.")
def main(): print("Hello and welcome to Sofia Zoo!" + "\n" "Please, enter one of the following commands:" + "\n" "see_animals" + "\n" "accommodate <species> <name> <age> <gender> <weight>" + "\n" "move_to_habitat <species> <name>" + "\n" "simulate <interval_of_time> <period> " + "\n" "finish") sofia_zoo = Zoo(20, 200) intervals_of_time = {1: "days", 2: "weeks", 3: "months", 4: "years"} panda = Animal("panda", 32, "Pandio", "male", 50) tiger = Animal("tiger", 20, "Tonny", "male", 110) tigress = Animal("tiger", 24, "Anne", "female", 75) sofia_zoo.accomodate_new_animal(panda) sofia_zoo.accomodate_new_animal(tiger) sofia_zoo.accomodate_new_animal(tigress) command = input("Enter command > ") lst = command.split(" ") while lst[0] != "finish": if lst[0] == "see_animals": for animal in sofia_zoo.animals: print("{} : {}, {}, {}".format(animal.name, animal.species, animal.age, round(animal.weight, 2))) if lst[0] == "accommodate": sofia_zoo.accomodate_new_animal(Animal(lst[1], lst[3], lst[2], lst[4], lst[5])) if lst[0] == "move_to_habitat": sofia_zoo.remove_animal(lst[1], lst[2]) if lst[0] == "simulate": print("Simulation of the zoo for {} {}".format(lst[2], lst[1])) if lst[1] == intervals_of_time[1]: days = lst[2] elif lst[1] == intervals_of_time[2]: days = lst[2] * DAYS_IN_WEEK elif lst[1] == intervals_of_time[3]: days = lst[2] * DAYS_IN_MONTH else: days = lst[2] * DAYS_IN_YEAR for animal in sofia_zoo.animals: animal.grow(days / DAYS_IN_MONTH) print("Animals in the zoo:") for animal in sofia_zoo.animals: print("{} : {}, {}, {}".format(animal.name, animal.species, animal.age, round(animal.weight, 2))) animal.grow(days / DAYS_IN_MONTH) for day in range(days): for animal in sofia_zoo.animals: animal.eat() if sofia_zoo.die(animal): print(animal.name + " has died") sofia_zoo.daily_income() sofia_zoo.daily_outcome() if sofia_zoo.budget == 0: print("The zoo cannot affor to feed all animals") number_of_animals_in_zoo = len(sofia_zoo.animals) sofia_zoo.reproduce(choice(sofia_zoo.animals), choice(sofia_zoo.animals)) #sofia_zoo.reproduce(tiger, tigress) if len(sofia_zoo.animals) > number_of_animals_in_zoo: print("New baby is born") command = input("Enter command > ") lst = command.split(" ")
class ZooTest(unittest.TestCase): def setUp(self): self.first_animals = [Animal("tiger", 144, "DiviaLud", "male", 300), Animal("tiger", 96, "Pam", "female", 280), Animal("panda", 72, "Pom-Pom", "male", 340)] self.zoo = Zoo(self.first_animals, 100, 1200) self.animal1 = Animal("panda", 48, "Jully", "female", 350) self.animal2 = Animal("panda", 48, "Lilly", "female", 350) self.animal3 = Animal("tiger", 144, "DiviaLud", "male", 300) self.animal4 = Animal("tiger", 96, "Pam", "female", 280) def test_init(self): self.assertEqual(self.zoo.animals, self.first_animals) self.assertEqual(self.zoo.capacity, 100) self.assertEqual(self.zoo.budget, 1200) def test_names_for_species(self): self.assertEqual(self.zoo.names_for_species(self.animal3), ["DiviaLud", "Pam"]) def test_names_for_species_doesnot_have_such_species(self): self.assertEqual(self.zoo.names_for_species(Animal( "horse", 96, "Pam", "female", 280)), []) def test_accomodate(self): self.assertTrue(self.zoo.accommodate(self.animal1)) self.assertIn(self.animal1, self.zoo.animals) def test_accomodate_no_more_space(self): zoo1 = Zoo(self.first_animals, 3, 1200) self.assertEqual(zoo1.accommodate(self.animal1), "No more space!") def test_accomodate_exists_name(self): animal = Animal("panda", 48, "Pom-Pom", "female", 350) message = "There is such species with the same name. Rename your animal" self.assertEqual(self.zoo.accommodate(animal), message) def test_get_income(self): self.zoo.get_income() self.assertEqual(self.zoo.budget, 1380) def test_outcome(self): self.first_animals[0].eat("meat", 8) # tiger self.first_animals[1].eat("meat", 10) # tiger self.first_animals[2].eat("bamboo", 4) # panda # 8 * 4 + 10 * 4 + 4 * 2 = 80 self.zoo.outcome() self.assertEqual(self.zoo.budget, 1120) def test_clear_dead_animals_all_died(self): for i in range(100): self.zoo.clear_dead_animals(180) self.assertEqual(self.zoo.animals, []) def test_gender_baby(self): genders = [] for i in range(1000): genders.append(self.zoo.gender_baby()) self.assertIn("female", genders) self.assertIn("male", genders) def test_reproduce_same_gender(self): self.assertEqual(self.zoo.reproduce(self.animal1, self.animal2), "Cannot reproduce") def test_reproduce_different_species(self): self.assertEqual(self.zoo.reproduce(self.animal1, self.animal3), "Cannot reproduce") def test_reproduce_before_six_months(self): self.animal4.last_pregnancy = 48 self.assertEqual(self.zoo.reproduce(self.animal1, self.animal4), "Cannot reproduce") #################################################### def test_reproduce_female_is_second_argument(self): self.animal4.last_pregnancy = 0 self.assertNotEqual(self.zoo.reproduce(self.animal3, self.animal4), "Cannot reproduce") def test_reproduce_female_is_first_argument(self): self.animal4.last_pregnancy = 0 self.assertNotEqual(self.zoo.reproduce(self.animal4, self.animal3), "Cannot reproduce") #################################################### def test_reproduce_first_animal_too_small(self): self.animal4.last_pregnancy = 0 self.animal3 = Animal("tiger", 12, "DiviaLud", "male", 300) self.animal4 = Animal("tiger", 96, "Pam", "female", 280) self.assertEqual(self.zoo.reproduce(self.animal3, self.animal4), "Cannot reproduce. Animals are too young") def test_reproduce_second_animal_too_small(self): self.animal4.last_pregnancy = 0 self.animal3 = Animal("tiger", 12, "DiviaLud", "male", 300) self.animal4 = Animal("tiger", 96, "Pam", "female", 280) self.assertEqual(self.zoo.reproduce(self.animal4, self.animal3), "Cannot reproduce. Animals are too young") def test_reproduce_both_animals_too_small(self): self.animal4.last_pregnancy = 0 self.animal3 = Animal("tiger", 12, "DiviaLud", "male", 300) self.animal4 = Animal("tiger", 12, "Pam", "female", 280) self.assertEqual(self.zoo.reproduce(self.animal4, self.animal3), "Cannot reproduce. Animals are too young")