Example #1
0
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.")
Example #2
0
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")