Advertisement
Guest User

Untitled

a guest
Feb 18th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.67 KB | None | 0 0
  1. #include "structs.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. void printHorizontalLine( int width, char border_char);
  10. void printHeading( string title, int width );
  11.  
  12.  
  13. int main()
  14. {
  15.     cout << "This is Assignment 4 (Member Functions) - Zookeeper" << endl;
  16.  
  17.     //Objects
  18.     Mammal myMammal;
  19.     Reptile myReptile;
  20.     Bird myBird;
  21.     Personnel myPersonnel;
  22.  
  23.     /**
  24.     Declaring the input streams for each file
  25.     **/
  26.     ifstream inFileMammal;
  27.     ifstream inFileReptile;
  28.     ifstream inFileBird;
  29.     ifstream inFilePersonnel;
  30.  
  31.     /**
  32.     Opening the files. You can either hardcode the name of the files or ask the user to give the names
  33.     **/
  34.     inFileMammal.open("/Projects/Structs/Mammals.txt");
  35.     inFileReptile.open("/Projects/Structs/Reptiles.txt");
  36.     inFileBird.open("/Projects/Structs/Birds.txt");
  37.     inFilePersonnel.open("/Projects/Structs/Personnel.txt");
  38.  
  39.     /**
  40.     If the any of the file cannot be opened then the program terminates displaying
  41.     the error message
  42.     **/
  43.     if (!inFileMammal)
  44.     {
  45.         cout << "The Mammals input file does not exist. Program terminates!" << endl;
  46.         return 1;
  47.     }
  48.     if (!inFileBird)
  49.     {
  50.         cout << "The Birds input file does not exist. Program terminates!" << endl;
  51.         return 1;
  52.     }
  53.     if (!inFileReptile)
  54.     {
  55.         cout << "The Reptiles input file does not exist. Program terminates!" << endl;
  56.         return 1;
  57.     }
  58.     if (!inFilePersonnel)
  59.     {
  60.         cout << "The Personnel input file does not exist. Program terminates!" << endl;
  61.         return 1;
  62.     }
  63.  
  64.  
  65.     /**
  66.     Display the prompt and do the requested action. Keep repeating the prompt until exit.
  67.     If the number entered is not an option, just repeat the prompt.
  68.     **/
  69.     int chosenOption;
  70.  
  71.     do{
  72.         cout << "\n\n\nSelect which option you would like to see \n"
  73.          << "1. Print all Mammals \n"
  74.          << "2. Print all Birds \n"
  75.          << "3. Print all Reptiles \n"
  76.          << "4. Print all Personnel \n"
  77.          << "5. Exit \n"
  78.          << "\n"
  79.          << "Enter Option (1-5): ";
  80.         cin >> chosenOption;
  81.  
  82.         /**
  83.         Do the correct action according to the chosenOption
  84.         **/
  85.         string exhibitNameToFind;
  86.  
  87.         switch(chosenOption)
  88.         {
  89.         case 1:
  90.             /**
  91.             Mammals
  92.             Function call to read data from input file. That function then calls a print Function
  93.             **/
  94.             myMammal.getMammalsData(inFileMammal);
  95.             inFileMammal.close();
  96.             inFileMammal.open("Mammals.txt");
  97.             break;
  98.         case 2:
  99.             /**
  100.             Birds
  101.             Function call to read data from input file. That function then calls a print Function
  102.             **/
  103.             myBird.getBirdsData(inFileBird);
  104.             inFileBird.close();
  105.             inFileBird.open("Birds.txt");
  106.  
  107.             break;
  108.         case 3:
  109.             /**
  110.             Reptiles
  111.             Function call to read data from input file. That function then calls a print Function
  112.             **/
  113.             myReptile.getReptilesData(inFileReptile);
  114.             inFileReptile.close();
  115.             inFileReptile.open("Reptiles.txt");
  116.  
  117.             break;
  118.         case 4:
  119.             /**
  120.             Personnel
  121.             Function call to read data from input file. That function then calls a print Function
  122.             **/
  123.             myPersonnel.getPersonnelData(inFilePersonnel);
  124.             inFilePersonnel.close();
  125.             inFilePersonnel.open("Personnel.txt");
  126.             break;
  127.         default:
  128.             break;
  129.         }
  130.  
  131.     }while(chosenOption != 5);
  132.  
  133.     return 0;
  134. }
  135.  
  136. void printHorizontalLine( int width, char border_char){
  137.     cout.fill( border_char );
  138.     cout << setw( width ) << border_char << "\n";
  139.     cout.fill(' ');
  140. }
  141.  
  142. void printHeading( string title, int width ){
  143.     //Declare Variables
  144.     int magic_width = 0;
  145.  
  146.     magic_width =  (width/2) - (title.length()/2) + title.length();
  147.  
  148.     cout << "\n";
  149.     cout << left  << setfill('=') << setw( width ) << "=" << "\n";
  150.     cout << right << setfill(' ') << setw( magic_width ) << title << "\n";
  151.     cout << right << setfill('=') << setw( width ) << "=" << endl;
  152.  
  153.     //reset count
  154.     cout.clear();
  155.     cout.fill(' ');
  156.  
  157.     //VOID functions do NOT return a value
  158. }
  159.  
  160. /**
  161. Using the input stream sent as parameter we are reading the content from the Mammals.txt and storing it in the mammals struct array
  162. **/
  163. void Mammal::getMammalsData(ifstream& inFile){
  164.  
  165.     int totalMammals;
  166.     inFile >> totalMammals;
  167.  
  168.     Mammal mammals[totalMammals];
  169.  
  170.     char decimal;
  171.  
  172.     for(int i = 0; i < totalMammals; i++){
  173.         inFile >> mammals[i].species;
  174.         inFile >> mammals[i].birthDate.month >> decimal >> mammals[i].birthDate.day >> decimal >> mammals[i].birthDate.year;
  175.         inFile >> mammals[i].weight;
  176.         inFile >> mammals[i].sizeofenclosure.length >> decimal >> mammals[i].sizeofenclosure.width >> decimal >> mammals[i].sizeofenclosure.height;
  177.         inFile >> mammals[i].exhibitName;
  178.     }
  179.  
  180.     inFile.close();
  181.  
  182.     printHeading("Mammals", 60);
  183.  
  184.     displayMammals(mammals,totalMammals);
  185. }
  186.  
  187. /**
  188. Using the input stream sent as parameter we are reading the content from the Reptiles.txt and storing it in the reptiles struct array
  189. **/
  190. void Reptile::getReptilesData(ifstream& inFile){
  191.  
  192.     int totalReptiles;
  193.     inFile >> totalReptiles;
  194.  
  195.     Reptile reptiles[totalReptiles];
  196.  
  197.     char decimal;
  198.  
  199.     for(int i = 0; i < totalReptiles; i++){
  200.         inFile >> reptiles[i].species;
  201.         inFile >> reptiles[i].dangerAssess.venoumous >> decimal >> reptiles[i].dangerAssess.agression;
  202.         inFile >> reptiles[i].birthDate.month >> decimal >> reptiles[i].birthDate.day >> decimal >> reptiles[i].birthDate.year;
  203.         inFile >> reptiles[i].sizeofenclosure.length >> decimal >> reptiles[i].sizeofenclosure.width >> decimal >> reptiles[i].sizeofenclosure.height;
  204.         inFile >> reptiles[i].exhibitName;
  205.     }
  206.  
  207.     inFile.close();
  208.  
  209.     printHeading("Reptiles", 60);
  210.  
  211.     displayReptiles(reptiles,totalReptiles);
  212. }
  213.  
  214.  
  215. /**
  216. Using the input stream sent as parameter we are reading the content from the Birds.txt and storing it in the birds struct array
  217. **/
  218. void Bird::getBirdsData(ifstream& inFile){
  219.  
  220.     int totalBirds;
  221.     inFile >> totalBirds;
  222.  
  223.     Bird birds[totalBirds];
  224.  
  225.     char decimal;
  226.  
  227.     for(int i = 0; i < totalBirds; i++){
  228.         inFile >> birds[i].species;
  229.         inFile >> birds[i].birthDate.month >> decimal >> birds[i].birthDate.day >> decimal >> birds[i].birthDate.year;
  230.         inFile >> birds[i].weight;
  231.         inFile >> birds[i].canFly;
  232.         inFile >> birds[i].exhibitName;
  233.     }
  234.  
  235.     inFile.close();
  236.  
  237.     printHeading("Birds", 60);
  238.  
  239.     displayBirds(birds,totalBirds);
  240. }
  241.  
  242. /**
  243. Using the input stream sent as parameter we are reading the content from the Personnel.txt and storing it in the personnel struct array
  244. **/
  245. void Personnel::getPersonnelData(ifstream& inFile){
  246.  
  247.     int totalPersonnel;
  248.     inFile >> totalPersonnel;
  249.  
  250.     Personnel personnel[totalPersonnel];
  251.  
  252.     char decimal;
  253.  
  254.     for(int i = 0; i < totalPersonnel; i++){
  255.         inFile >> personnel[i].name;
  256.         inFile >> personnel[i].hireDate.month >> decimal >> personnel[i].hireDate.day >> decimal >> personnel[i].hireDate.year;
  257.         inFile >> personnel[i].jobTitle;
  258.         inFile >> personnel[i].exhibitName;
  259.     }
  260.  
  261.     inFile.close();
  262.  
  263.     printHeading("Personnel", 60);
  264.  
  265.     displayPersonnel(personnel,totalPersonnel);
  266. }
  267.  
  268.  
  269. /**
  270. Displaying the content from the mammals struct array on the monitor
  271. **/
  272. void Mammal::displayMammals(Mammal mammals[], int totalMammals){
  273.    cout << setw(10) << "Species" << "  | "
  274.         << "Date of Birth" << "  | "
  275.         << "Weight" << "  | "
  276.         << "Enclosure Size" << " | "
  277.         << "Exhibit Name" <<  endl;
  278.  
  279.     printHorizontalLine(80,'-');
  280.  
  281.     for(int i = 0; i < totalMammals; i++){
  282.         cout << left << setw(9) << mammals[i].species << "   | "
  283.              << 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 << " | "
  284.              << right <<setw(9) << setfill(' ') << mammals[i].weight << "     | "
  285.              << right <<setw(3) << setfill(' ') << mammals[i].sizeofenclosure.length <<":"<< setw(3) << mammals[i].sizeofenclosure.width << ":" << setw(3) << mammals[i].sizeofenclosure.height << "  | "
  286.              << right << setw(9) << setfill(' ') << mammals[i].exhibitName << " "
  287.              << setfill(' ') << endl;
  288.     }
  289. }
  290.  
  291. /**
  292. Displaying the content from the Reptiles struct array on the monitor
  293. **/
  294. void Reptile::displayReptiles(Reptile reptiles[], int totalReptiles){
  295.    cout << setw(8) << "Species" << " | "
  296.         << "Danger Assessment" << " | "
  297.         << "Date of Birth" << " | "
  298.         << "Enclosure Size" << " | "
  299.         << "Exhibit Name" <<  endl;
  300.  
  301.     printHorizontalLine(80,'-');
  302.  
  303.     for(int i = 0; i < totalReptiles; i++){
  304.         cout << left << setw(9) << setfill(' ') << reptiles[i].species << " | "
  305.              << right << setw(4) << reptiles[i].dangerAssess.venoumous << ":" << setw(2) << reptiles[i].dangerAssess.agression << "   |   "
  306.              << 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 << "   |    "
  307.              << 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 << "  | "
  308.              << right << setw(9) << setfill(' ') << reptiles[i].exhibitName << " "
  309.              << setfill(' ') << endl;
  310.     }
  311. }
  312.  
  313.  
  314. /**
  315. Displaying the content from the Birds struct array on the monitor
  316. **/
  317. void Bird::displayBirds(Bird birds[], int totalBirds){
  318.    cout << setw(10) << "Species" << "  | "
  319.         << "Date of Birth" << "  | "
  320.         << "Weight" << "  | "
  321.         << "Flying" << " | "
  322.         << "Exhibit Name" <<  endl;
  323.  
  324.     printHorizontalLine(80,'-');
  325.  
  326.     for(int i = 0; i < totalBirds; i++){
  327.         cout << left  << setw(9) << birds[i].species << "   |   "
  328.              << 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 << "   |  "
  329.              << right << setw(2) << birds[i].weight << "    |  "
  330.              << right << setw(4) << setfill(' ') << birds[i].canFly << "  |"
  331.              << right << setw(10) << birds[i].exhibitName << " "
  332.              << setfill(' ') << endl;
  333.     }
  334. }
  335.  
  336. /**
  337. Displaying the content from the Personnel struct array on the monitor
  338. **/
  339. void Personnel::displayPersonnel(Personnel personnel[], int totalPersonnel){
  340.    cout << setw(10) << "Name" << " | "
  341.         << "Date of Hire" << " | "
  342.         << "Job Title" << " | "
  343.         << "Exhibit Name" <<  endl;
  344.  
  345.     printHorizontalLine(80,'-');
  346.  
  347.     for(int i = 0; i < totalPersonnel; i++){
  348.         cout << left  << setw(9) << personnel[i].name << "   |  "
  349.              << 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 << "  | "
  350.              << right << setw(13) << setfill(' ') << personnel[i].jobTitle << "  | "
  351.              << right << setw(9) << setfill(' ') << personnel[i].exhibitName << " "
  352.              << setfill(' ') << endl;
  353.  
  354.  
  355.     }
  356. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement