0% found this document useful (0 votes)
31 views3 pages

Animal

The document defines an AnimalDatabase class that manages an array of Animal structs. The AnimalDatabase class provides methods to add, delete, find animals by ID, location, category, keeper, or dietary conditions. It also includes methods to find sick animals based on criteria, get frequency of sick animals, destroy all records, and get number of animals born between dates. The class limits the number of animals to 100 and handles adding/finding animals within that limit.

Uploaded by

f2022065342
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views3 pages

Animal

The document defines an AnimalDatabase class that manages an array of Animal structs. The AnimalDatabase class provides methods to add, delete, find animals by ID, location, category, keeper, or dietary conditions. It also includes methods to find sick animals based on criteria, get frequency of sick animals, destroy all records, and get number of animals born between dates. The class limits the number of animals to 100 and handles adding/finding animals within that limit.

Uploaded by

f2022065342
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

#include <stdexcept>

const int MAX_ANIMALS = 100;

struct Animal {
std::string id;
std::string name;
std::string offspring;
std::string cell_id;
std::string gender;
int age;
float weight;
std::string category;
};

class AnimalDatabase {
private:
Animal animals[MAX_ANIMALS];
int numAnimals;

public:
AnimalDatabase() : numAnimals(0) {}

void addAnimal(const Animal& animal) {


if (numAnimals < MAX_ANIMALS) {
animals[numAnimals++] = animal;
} else {
std::cout << "Animal database is full. Cannot add more animals." <<
std::endl;
}
}

void deleteAnimal(const std::string& id) {


for (int i = 0; i < numAnimals; i++) {
if (animals[i].id == id) {
for (int j = i; j < numAnimals - 1; j++) {
animals[j] = animals[j + 1];
}
numAnimals--;
std::cout << "Animal with ID " << id << " has been deleted." <<
std::endl;
return;
}
}
std::cout << "Animal with ID " << id << " not found." << std::endl;
}

Animal findAnimal(const std::string& id) {


for (int i = 0; i < numAnimals; i++) {
if (animals[i].id == id) {
return animals[i];
}
}
throw std::runtime_error("Animal with ID " + id + " not found.");
}

Animal* findAnimalsByLocation(const std::string& location, int& count) {


Animal* result = new Animal[MAX_ANIMALS];
count = 0;

for (int i = 0; i < numAnimals; i++) {


if (animals[i].cell_id == location) {
result[count++] = animals[i];
}
}

return result;
}

Animal* findAnimalsByCategory(const std::string& category, int& count) {


Animal* result = new Animal[MAX_ANIMALS];
count = 0;

for (int i = 0; i < numAnimals; i++) {


if (animals[i].category == category) {
result[count++] = animals[i];
}
}

return result;
}

Animal* findAnimalsByKeeper(const std::string& keeper, int& count) {


Animal* result = new Animal[MAX_ANIMALS];
count = 0;

for (int i = 0; i < numAnimals; i++) {


// Assuming the keeper information is stored in the offspring field
if (animals[i].offspring == keeper) {
result[count++] = animals[i];
}
}

return result;
}

Animal* findAnimalsByDietaryConditions(const std::string& conditions, int&


count) {
Animal* result = new Animal[MAX_ANIMALS];
count = 0;

for (int i = 0; i < numAnimals; i++) {


// Assuming the dietary conditions are stored in the name field
if (animals[i].name == conditions) {
result[count++] = animals[i];
}
}

return result;
}

Animal* findSickAnimals(const std::string& category, int age, const


std::string& gender,
const std::string& location, const std::string&
startDate,
const std::string& endDate, int& count) {
Animal* result = new Animal[MAX_ANIMALS];
count = 0;

// Implementation for finding sick animals based on criteria

return result;
}

int getFrequencyOfSickAnimals() {
int frequency = 0;

// Implementation for calculating frequency of sick animals

return frequency;
}

void destroyAllRecords() {
numAnimals = 0;
}

int getNumberOfAnimalsBorn(const std::string& startDate, const std::string&


endDate) {
int count = 0;

// Implementation for calculating the number of animals born between two


dates

return count;
}
};

You might also like