Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "structs.h"
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- #include <string>
- using namespace std;
- void printHorizontalLine( int width, char border_char);
- void printHeading( string title, int width );
- int main()
- {
- cout << "This is Assignment 4 (Member Functions) - Zookeeper" << endl;
- //Objects
- Mammal myMammal;
- Reptile myReptile;
- Bird myBird;
- Personnel myPersonnel;
- /**
- Declaring the input streams for each file
- **/
- ifstream inFileMammal;
- ifstream inFileReptile;
- ifstream inFileBird;
- ifstream inFilePersonnel;
- /**
- Opening the files. You can either hardcode the name of the files or ask the user to give the names
- **/
- inFileMammal.open("/Projects/Structs/Mammals.txt");
- inFileReptile.open("/Projects/Structs/Reptiles.txt");
- inFileBird.open("/Projects/Structs/Birds.txt");
- inFilePersonnel.open("/Projects/Structs/Personnel.txt");
- /**
- If the any of the file cannot be opened then the program terminates displaying
- the error message
- **/
- if (!inFileMammal)
- {
- cout << "The Mammals input file does not exist. Program terminates!" << endl;
- return 1;
- }
- if (!inFileBird)
- {
- cout << "The Birds input file does not exist. Program terminates!" << endl;
- return 1;
- }
- if (!inFileReptile)
- {
- cout << "The Reptiles input file does not exist. Program terminates!" << endl;
- return 1;
- }
- if (!inFilePersonnel)
- {
- cout << "The Personnel input file does not exist. Program terminates!" << endl;
- return 1;
- }
- /**
- Display the prompt and do the requested action. Keep repeating the prompt until exit.
- If the number entered is not an option, just repeat the prompt.
- **/
- int chosenOption;
- do{
- cout << "\n\n\nSelect which option you would like to see \n"
- << "1. Print all Mammals \n"
- << "2. Print all Birds \n"
- << "3. Print all Reptiles \n"
- << "4. Print all Personnel \n"
- << "5. Exit \n"
- << "\n"
- << "Enter Option (1-5): ";
- cin >> chosenOption;
- /**
- Do the correct action according to the chosenOption
- **/
- string exhibitNameToFind;
- switch(chosenOption)
- {
- case 1:
- /**
- Mammals
- Function call to read data from input file. That function then calls a print Function
- **/
- myMammal.getMammalsData(inFileMammal);
- inFileMammal.close();
- inFileMammal.open("Mammals.txt");
- break;
- case 2:
- /**
- Birds
- Function call to read data from input file. That function then calls a print Function
- **/
- myBird.getBirdsData(inFileBird);
- inFileBird.close();
- inFileBird.open("Birds.txt");
- break;
- case 3:
- /**
- Reptiles
- Function call to read data from input file. That function then calls a print Function
- **/
- myReptile.getReptilesData(inFileReptile);
- inFileReptile.close();
- inFileReptile.open("Reptiles.txt");
- break;
- case 4:
- /**
- Personnel
- Function call to read data from input file. That function then calls a print Function
- **/
- myPersonnel.getPersonnelData(inFilePersonnel);
- inFilePersonnel.close();
- inFilePersonnel.open("Personnel.txt");
- break;
- default:
- break;
- }
- }while(chosenOption != 5);
- return 0;
- }
- void printHorizontalLine( int width, char border_char){
- cout.fill( border_char );
- cout << setw( width ) << border_char << "\n";
- cout.fill(' ');
- }
- void printHeading( string title, int width ){
- //Declare Variables
- int magic_width = 0;
- magic_width = (width/2) - (title.length()/2) + title.length();
- cout << "\n";
- cout << left << setfill('=') << setw( width ) << "=" << "\n";
- cout << right << setfill(' ') << setw( magic_width ) << title << "\n";
- cout << right << setfill('=') << setw( width ) << "=" << endl;
- //reset count
- cout.clear();
- cout.fill(' ');
- //VOID functions do NOT return a value
- }
- /**
- Using the input stream sent as parameter we are reading the content from the Mammals.txt and storing it in the mammals struct array
- **/
- void Mammal::getMammalsData(ifstream& inFile){
- int totalMammals;
- inFile >> totalMammals;
- Mammal mammals[totalMammals];
- char decimal;
- for(int i = 0; i < totalMammals; i++){
- inFile >> mammals[i].species;
- inFile >> mammals[i].birthDate.month >> decimal >> mammals[i].birthDate.day >> decimal >> mammals[i].birthDate.year;
- inFile >> mammals[i].weight;
- inFile >> mammals[i].sizeofenclosure.length >> decimal >> mammals[i].sizeofenclosure.width >> decimal >> mammals[i].sizeofenclosure.height;
- inFile >> mammals[i].exhibitName;
- }
- inFile.close();
- printHeading("Mammals", 60);
- displayMammals(mammals,totalMammals);
- }
- /**
- Using the input stream sent as parameter we are reading the content from the Reptiles.txt and storing it in the reptiles struct array
- **/
- void Reptile::getReptilesData(ifstream& inFile){
- int totalReptiles;
- inFile >> totalReptiles;
- Reptile reptiles[totalReptiles];
- char decimal;
- for(int i = 0; i < totalReptiles; i++){
- inFile >> reptiles[i].species;
- inFile >> reptiles[i].dangerAssess.venoumous >> decimal >> reptiles[i].dangerAssess.agression;
- inFile >> reptiles[i].birthDate.month >> decimal >> reptiles[i].birthDate.day >> decimal >> reptiles[i].birthDate.year;
- inFile >> reptiles[i].sizeofenclosure.length >> decimal >> reptiles[i].sizeofenclosure.width >> decimal >> reptiles[i].sizeofenclosure.height;
- inFile >> reptiles[i].exhibitName;
- }
- inFile.close();
- printHeading("Reptiles", 60);
- displayReptiles(reptiles,totalReptiles);
- }
- /**
- Using the input stream sent as parameter we are reading the content from the Birds.txt and storing it in the birds struct array
- **/
- void Bird::getBirdsData(ifstream& inFile){
- int totalBirds;
- inFile >> totalBirds;
- Bird birds[totalBirds];
- char decimal;
- for(int i = 0; i < totalBirds; i++){
- inFile >> birds[i].species;
- inFile >> birds[i].birthDate.month >> decimal >> birds[i].birthDate.day >> decimal >> birds[i].birthDate.year;
- inFile >> birds[i].weight;
- inFile >> birds[i].canFly;
- inFile >> birds[i].exhibitName;
- }
- inFile.close();
- printHeading("Birds", 60);
- displayBirds(birds,totalBirds);
- }
- /**
- Using the input stream sent as parameter we are reading the content from the Personnel.txt and storing it in the personnel struct array
- **/
- void Personnel::getPersonnelData(ifstream& inFile){
- int totalPersonnel;
- inFile >> totalPersonnel;
- Personnel personnel[totalPersonnel];
- char decimal;
- for(int i = 0; i < totalPersonnel; i++){
- inFile >> personnel[i].name;
- inFile >> personnel[i].hireDate.month >> decimal >> personnel[i].hireDate.day >> decimal >> personnel[i].hireDate.year;
- inFile >> personnel[i].jobTitle;
- inFile >> personnel[i].exhibitName;
- }
- inFile.close();
- printHeading("Personnel", 60);
- displayPersonnel(personnel,totalPersonnel);
- }
- /**
- Displaying the content from the mammals struct array on the monitor
- **/
- void Mammal::displayMammals(Mammal mammals[], int totalMammals){
- cout << setw(10) << "Species" << " | "
- << "Date of Birth" << " | "
- << "Weight" << " | "
- << "Enclosure Size" << " | "
- << "Exhibit Name" << endl;
- printHorizontalLine(80,'-');
- for(int i = 0; i < totalMammals; i++){
- cout << left << setw(9) << mammals[i].species << " | "
- << right <<setw(2) << setfill('0') << mammals[i].birthDate.month <<":" << setw(2) << setfill('0') << mammals[i].birthDate.day <<":" << setw(2) << setfill('0') << mammals[i].birthDate.year << " | "
- << right <<setw(9) << setfill(' ') << mammals[i].weight << " | "
- << right <<setw(3) << setfill(' ') << mammals[i].sizeofenclosure.length <<":"<< setw(3) << mammals[i].sizeofenclosure.width << ":" << setw(3) << mammals[i].sizeofenclosure.height << " | "
- << right << setw(9) << setfill(' ') << mammals[i].exhibitName << " "
- << setfill(' ') << endl;
- }
- }
- /**
- Displaying the content from the Reptiles struct array on the monitor
- **/
- void Reptile::displayReptiles(Reptile reptiles[], int totalReptiles){
- cout << setw(8) << "Species" << " | "
- << "Danger Assessment" << " | "
- << "Date of Birth" << " | "
- << "Enclosure Size" << " | "
- << "Exhibit Name" << endl;
- printHorizontalLine(80,'-');
- for(int i = 0; i < totalReptiles; i++){
- cout << left << setw(9) << setfill(' ') << reptiles[i].species << " | "
- << right << setw(4) << reptiles[i].dangerAssess.venoumous << ":" << setw(2) << reptiles[i].dangerAssess.agression << " | "
- << right <<setw(2) << setfill('0') << reptiles[i].birthDate.month <<":" << setw(2) << setfill('0') << reptiles[i].birthDate.day <<":" << setw(2) << setfill('0') << reptiles[i].birthDate.year << " | "
- << right <<setw(2) << setfill('0') << reptiles[i].sizeofenclosure.length <<":"<< setw(2) << setfill('0') << reptiles[i].sizeofenclosure.width << ":" << setw(2) << setfill('0') << reptiles[i].sizeofenclosure.height << " | "
- << right << setw(9) << setfill(' ') << reptiles[i].exhibitName << " "
- << setfill(' ') << endl;
- }
- }
- /**
- Displaying the content from the Birds struct array on the monitor
- **/
- void Bird::displayBirds(Bird birds[], int totalBirds){
- cout << setw(10) << "Species" << " | "
- << "Date of Birth" << " | "
- << "Weight" << " | "
- << "Flying" << " | "
- << "Exhibit Name" << endl;
- printHorizontalLine(80,'-');
- for(int i = 0; i < totalBirds; i++){
- cout << left << setw(9) << birds[i].species << " | "
- << right << setw(2) << setfill('0') << birds[i].birthDate.month <<":" << setw(2) << setfill('0') << birds[i].birthDate.day <<":" << setw(2) << setfill('0') << birds[i].birthDate.year << " | "
- << right << setw(2) << birds[i].weight << " | "
- << right << setw(4) << setfill(' ') << birds[i].canFly << " |"
- << right << setw(10) << birds[i].exhibitName << " "
- << setfill(' ') << endl;
- }
- }
- /**
- Displaying the content from the Personnel struct array on the monitor
- **/
- void Personnel::displayPersonnel(Personnel personnel[], int totalPersonnel){
- cout << setw(10) << "Name" << " | "
- << "Date of Hire" << " | "
- << "Job Title" << " | "
- << "Exhibit Name" << endl;
- printHorizontalLine(80,'-');
- for(int i = 0; i < totalPersonnel; i++){
- cout << left << setw(9) << personnel[i].name << " | "
- << right << setw(2) << setfill('0') << personnel[i].hireDate.month <<":" << setw(2) << setfill('0') << personnel[i].hireDate.day <<":" << setw(2) << setfill('0') << personnel[i].hireDate.year << " | "
- << right << setw(13) << setfill(' ') << personnel[i].jobTitle << " | "
- << right << setw(9) << setfill(' ') << personnel[i].exhibitName << " "
- << setfill(' ') << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement