Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

class Animal:

def __init__(self, species, name, age):

self.species = species

self.name = name

self.age = age

def __str__(self):

return f"{self.species}: {self.name}, Age: {self.age}"

class Crop:

def __init__(self, name, quantity):

self.name = name

self.quantity = quantity

def __str__(self):

return f"{self.name}: {self.quantity} units"

class Farm:

def __init__(self, name):

self.name = name

self.animals = []

self.crops = []

def add_animal(self, animal):

self.animals.append(animal)

print(f"{animal.species} {animal.name} added to the farm.")

def add_crop(self, crop):

self.crops.append(crop)
print(f"{crop.name} crop added to the farm.")

def feed_animals(self):

for animal in self.animals:

print(f"Feeding {animal.species} {animal.name}.")

def harvest_crops(self):

for crop in self.crops:

print(f"Harvesting {crop.name}.")

def display_inventory(self):

print("Farm Inventory:")

print("Animals:")

if not self.animals:

print("No animals in the farm.")

else:

for animal in self.animals:

print(animal)

print("Crops:")

if not self.crops:

print("No crops in the farm.")

else:

for crop in self.crops:

print(crop)

# Sample usage

if __name__ == "__main__":

farm = Farm("Sample Farm")

cow = Animal("Cow", "Bessie", 3)


chicken = Animal("Chicken", "Cluck", 1)

wheat = Crop("Wheat", 100)

corn = Crop("Corn", 50)

farm.add_animal(cow)

farm.add_animal(chicken)

farm.add_crop(wheat)

farm.add_crop(corn)

farm.feed_animals()

farm.harvest_crops()

farm.display_inventory()

You might also like