#include #include #include #include #include using namespace std; void showMenu(); //////////////////////////////////////////// // // Puppy class definition class puppy{ friend ostream &operator<<(ostream&, puppy); friend istream &operator>>(istream&, puppy &); public: puppy(); string getName(); void setName(string); string getID(); bool setID(string); float getWeight(); void setWeight(float); string getColor(); bool setColor(string); string getSex(); bool setSex(string); bool isNeutered(); void Neuter(); char getStatus(); bool setStatus(char); void inputPuppy(); ~puppy(); private: string name; string id; float weight; string color; string sex; bool neutered; char status; }; // puppy constructor puppy::puppy(){ name ="-"; id = "-"; weight = 0; color = "-"; sex = "-"; neutered = false; status ='-'; } // puppy destructor // Don't need to do anything special here puppy::~puppy(){ // empty destructor } // find out what the puppy is named string puppy::getName(){ return name; } // give the puppy an name of its own void puppy::setName(string newname){ name = newname; } // Each puppy has an ID chip inserted under its skin // and a number tattooed on its ear string puppy::getID(){ return id; } // set the puppies initial ID bool puppy::setID(string newid){ if (newid.length() == 5) { id = newid; return true; } else { cerr << "ID length should be 5" << endl; return false; } } // find the puppies current weight float puppy::getWeight(){ return weight; } // set the puppies current weight void puppy::setWeight(float newweight){ weight = newweight; } // what color is this puppy? string puppy::getColor(){ return color; } // set the puppies color // yellow, black, brown and brindle are the possibilites bool puppy::setColor(string newcolor){ if (newcolor == "black"||newcolor == "Black"|| newcolor =="BLACK"){ color = "Black"; return true; } else if (newcolor == "yellow"|| newcolor == "Yellow" ||newcolor =="YELLOW") { color = "Yellow"; return true; } else if (newcolor == "brown"|| newcolor == "Brown" ||newcolor =="BROWN"){ color = "Brown"; return true; } else if (newcolor == "brindle"|| newcolor == "Brindle" || newcolor =="BRINDLE" ){ color = "Brindle"; return true; } else { cerr << "Illegal color code. Bad code: " << newcolor << endl; return false; } } // What sex is this puppy? string puppy::getSex(){ return sex; } // Set this puppies sex bool puppy::setSex(string newsex){ if (newsex == "male" || newsex == "Male" || newsex == "MALE" || newsex == "M" || newsex =="m") { sex = "Male"; return true; } else if (newsex == "female" || newsex == " Female" || newsex == "FEMALE" || newsex == "f" || newsex =="F") { sex = "Female"; return true; } else { cerr << "Illegal sex code. Bad code: " << newsex << endl; return false; } } // Some puppies are kept for breeding stock, // and are not neutered bool puppy::isNeutered(){ return neutered; } // Other puppies are kept out of trouble void puppy::Neuter(){ neutered = true; } // Keep track of what happened to indivdual pups char puppy::getStatus(){ return status; } // Set the puppy status bool puppy::setStatus(char newstatus){ // Status is one of: // w : puppy in breeding center being whelped // p : released prior to raising // r : released from raiser // i : failed IFT, released // a : picked up by ATF // g : sucessful guide // b : breeding stock if (newstatus == 'w'||newstatus =='W') { status = 'W'; return true; } else if (newstatus == 'p'||newstatus == 'P' ){ status = 'P'; return true; } else if (newstatus == 'r'||newstatus == 'R') { status = 'R'; return true; } else if (newstatus == 'i'||newstatus == 'I' ) { status = 'I'; return true; } else if (newstatus == 'a'||newstatus == 'A' ){ status = 'A'; return true; } else if (newstatus == 'g'||newstatus == 'G' ){ status = 'G'; return true; } else if (newstatus == 'b'||newstatus == 'B') { status = 'B'; return true; } else { cerr << "Error in setting status code. Bad code: " << newstatus << endl; return false; } } // Make it easy to input puppy information from the keyboard void puppy::inputPuppy(){ string newname, newcolor, newsex, eoln; char neuter, newstatus; cout << "Please enter the puppy information: " << endl; cout << "Puppy name: "; getline (cin, name); do { cout << "Puppy ID: "; getline (cin, id); } while (!setID(id)); cout << "Puppy birth weight: "; cin >> weight; getline (cin, eoln); do { cout << "Puppy color: "; getline (cin, newcolor); } while (!setColor(newcolor)); do { cout << "Puppy sex: "; getline (cin, newsex); } while (!setSex(newsex)); cout << "Is the pup neutered? (y/n) "; cin >> neuter; if (neuter == 'y'||neuter == 'Y') neutered = true; else neutered = false; cout << "Puppy status code: " << endl; cout << "w : puppy in breeding center being whelped" << endl; cout << " p : released prior to raising" << endl; cout << " r : released from raiser" << endl; cout << " i : failed IFT, released" << endl; cout << " a : picked up by ATF" << endl; cout << " g : sucessful guide " << endl; cout << " b : breeding stock " << endl; do { cout << "Puppy status code: "; cin >> newstatus; } while (!setStatus(newstatus)); getline (cin, eoln); } // overload the output operator ostream &operator<<(ostream &output, puppy p) { string ntd; if (p.neutered) ntd = "Neutered"; else ntd = "Intact"; output << p.name << " " << p.id << " " << p.weight << " " << p.color << " " << p.sex << " " << ntd << " " << p.status; return output; } // overload the input operator istream &operator>>(istream &input, puppy &p){ string ntd; input >> p.name >> p.id >> p.weight >> p.color >> p.sex >> ntd >> p.status; if (ntd == "Intact") p.neutered = false; else p.neutered = true; return input; } // // end the puppy class definition //////////////////////////////////////////// //////////////////////////////////////////// // litter class definition // class litter{ friend ostream &operator<<(ostream&, litter); friend istream &operator>>(istream&, litter &); public: litter(); string getFatherName(); string getMotherName(); int getBirthDay(); int getBirthMonth(); int getBirthYear(); void setupLitter(); float getSuccessRate(); void saveLitter(string); void loadLitter(string); void print(); void clear(); int getLitterSize(); ~litter(); private: string fathername; string mothername; int birth_day; int birth_month; int birth_year; vector puppies; }; litter::litter(){ fathername ="-"; mothername ="-"; birth_day = 0; birth_month = 0; birth_year = 0; puppies.clear(); } // what is the father's name? string litter::getFatherName(){ return fathername; } // what is the mother's name? string litter::getMotherName(){ return mothername; } // return information about birth dates int litter::getBirthDay(){ return birth_day; } int litter::getBirthMonth(){ return birth_month; } int litter::getBirthYear(){ return birth_year; } // This method takes information about // the litter from the keyboard void litter::setupLitter(){ int number_of_pups; puppy temp_pup; string eoln; cout << "Please enter the following information about the litter:" << endl; cout << "Father's name: "; getline (cin, fathername); cout << "Mother's name: "; getline (cin, mothername); cout << "Day of birth: "; cin >> birth_day; cout << "Month of birth: "; cin >> birth_month; cout << "Year of birth: "; cin >> birth_year; cout << "Number of puppies: "; cin >> number_of_pups; getline(cin, eoln); for (int i = 0; i < number_of_pups; i++){ temp_pup.inputPuppy(); puppies.push_back(temp_pup); } } // This method computes the success rate of puppies in the litter // The success rate is the percentage of pups who are guides or // breeding stock. float litter::getSuccessRate(){ float result = 0; int successful_pups = 0; for (int i = 0; i < puppies.size(); i++) { if (puppies[i].getStatus() == 'G' || puppies[i].getStatus() == 'B') successful_pups ++; } result = static_cast(successful_pups)/ puppies.size(); return result; } // this method write the litter information // out to a file void litter::saveLitter(string litterfile){ ofstream outfile; outfile.open(litterfile.c_str()); if (!outfile) { cerr << "Could not open file: "<< litterfile << " for output." << endl; exit(1); } outfile << fathername << endl; outfile << mothername << endl; outfile << birth_day << endl; outfile << birth_month << endl; outfile << birth_year << endl; outfile << puppies.size() << endl; for (int i = 0; i < puppies.size(); i++) outfile << puppies[i] << endl; outfile.close(); } // this method loads information about a litter from a file void litter::loadLitter(string litterfile){ string eoln; puppy temp_pup; int litter_size; ifstream infile; infile.open(litterfile.c_str()); if (!infile) { cerr << "Could not open file: "<< litterfile << " for input." << endl; exit(1); } getline(infile, fathername); getline(infile, mothername); infile >> birth_day; infile >> birth_month; infile >> birth_year; infile >> litter_size; getline(infile, eoln); for (int i = 0; i < litter_size; i++){ infile >> temp_pup; puppies.push_back(temp_pup); } infile.close(); } void litter::clear(){ fathername ="-"; mothername ="-"; birth_day = 0; birth_month = 0; birth_year = 0; puppies.clear(); } void litter::print(){ char sl = '/'; cout << endl; cout << "Fathername: " << fathername << endl; cout << "Mother's name: " << mothername << endl; cout << "Birthdate: " << birth_month<< sl << birth_day << sl << birth_year << endl; for (int i = 0; i < getLitterSize(); i++) cout << puppies[i] << endl; cout << endl; } int litter::getLitterSize(){ return puppies.size(); } litter::~litter(){ // empty deconstructor } // overload the output operator ostream &operator<<(ostream &output, litter l) { output << l.fathername << " " << l.mothername << " " << l.birth_day << " " << l.birth_month << " " << l.birth_year << " " << l.getLitterSize() << endl; for (int i = 0; i < l.getLitterSize(); i++) output << l.puppies[i] << endl; return output; } // overload the input operator istream &operator>>(istream &input, litter &l){ int num_pups; puppy temp_pup; input >> l.fathername >> l.mothername >> l.birth_day >> l.birth_month >> l.birth_year >> num_pups; for (int i = 0; i < num_pups; i++) { input >> temp_pup; l.puppies.push_back(temp_pup); } return input; } // // end the litter class definition //////////////////////////////////////////// //////////////////////////////////////////// // Main funtion // int main(){ bool finished = false; char choice; string filename, eoln; litter bunch_of_cute_puppies; while (!finished){ showMenu(); cin >> choice; getline(cin, eoln); switch (choice){ case 'N': case 'n': bunch_of_cute_puppies.setupLitter(); break; case 'L': case 'l': cout << "Enter filename to load: "; getline (cin, filename); bunch_of_cute_puppies.clear(); bunch_of_cute_puppies.loadLitter(filename); break; case 'P': case 'p': bunch_of_cute_puppies.print(); break; case 'S': case 's': cout << "Enter filename to save to: "; getline (cin, filename); bunch_of_cute_puppies.saveLitter(filename); break; case 'C': case 'c': cout << endl << "The success rate of litter is: " << bunch_of_cute_puppies.getSuccessRate() << endl << endl; break; case 'E': case 'e': finished = true; break; default: cout << endl << "Unknown choice. Please try again" << endl; } } } void showMenu(){ cout << "Litter management menu options:" << endl; cout << " N) Input new litter from keyboard." << endl; cout << " L) Load litter information from file." << endl; cout << " P) Print current litter information to screen." << endl; cout << " S) Save current litter to file." << endl; cout << " C) Show current litter success rate" << endl; cout << " E) Exit the program" << endl; cout << endl; cout << "Your choice:"; }