Vla_DOS

c++ street

Apr 1st, 2023
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. // Base class for houses
  9. class House {
  10. protected:
  11.     string address;
  12. public:
  13.     House() {}
  14.     House(string addr) : address(addr) {}
  15.     virtual void setFields(string input) {
  16.         cout << address;
  17.     }
  18.     virtual void printInfo() = 0;
  19.     virtual string getType() = 0;
  20.     virtual int getNumResidents() = 0;
  21.     string getAddress() { return address; }
  22.     void setAddress(string addr) { address = addr; }
  23. };
  24.  
  25. // Residential House class
  26. class ResidentialHouse : public House {
  27. private:
  28.     int numResidents;
  29. public:
  30.     ResidentialHouse() {}
  31.     ResidentialHouse(string addr, int residents) : House(addr), numResidents(residents) {}
  32.     void setFields(int input) {
  33.         numResidents = input;
  34.     }
  35.     int getNumResidents() {
  36.         return numResidents;
  37.     }
  38.     void printInfo() {
  39.         cout << endl;
  40.         cout << "Address: " << address << endl;
  41.         cout << "Type: Residential House" << endl;
  42.         cout << "Number of Residents: " << numResidents << endl;
  43.     }
  44.     string getType() { return "Residential House"; }
  45. };
  46.  
  47. // Store class
  48. class Store : public House {
  49. private:
  50.     string storeType;
  51. public:
  52.     Store() {}
  53.     Store(string addr, string type) : House(addr), storeType(type) {}
  54.     void setFields(string input) override {
  55.         House::setFields(input);
  56.         storeType = input;
  57.     }
  58.     int getNumResidents() {
  59.         return NULL;
  60.     }
  61.     void printInfo() {
  62.         cout << endl;
  63.  
  64.         cout << "Address: " << address << endl;
  65.         cout << "Type: Store" << endl;
  66.         cout << "Store Type: " << storeType << endl;
  67.     }
  68.     string getType() { return "Store"; }
  69. };
  70.  
  71. // School class
  72. class School : public House {
  73. private:
  74.     int numStudents;
  75.     string level;
  76. public:
  77.     School() {}
  78.     School(string addr, int students, string lvl) : House(addr), numStudents(students), level(lvl) {}
  79.     void setFields(string input) {
  80.  
  81.         level = input;
  82.  
  83.     }
  84.     int getNumResidents() {
  85.         return numStudents;
  86.     }
  87.     void printInfo() {
  88.         cout << endl;
  89.  
  90.         cout << "Address: " << address << endl;
  91.         cout << "Type: School" << endl;
  92.         cout << "Number of Students: " << numStudents << endl;
  93.         cout << "Level: " << level << endl;
  94.     }
  95.     string getType() { return "School"; }
  96. };
  97.  
  98. // Hospital class
  99. class Hospital : public House {
  100. private:
  101.     string hospitalType;
  102. public:
  103.     Hospital() {}
  104.     Hospital(string addr, string type) : House(addr), hospitalType(type) {}
  105.     void setFields(string input) {
  106.         hospitalType = input;
  107.     }
  108.     int getNumResidents() {
  109.         return NULL;
  110.     }
  111.     void printInfo() {
  112.         cout << endl;
  113.  
  114.         cout << "Address: " << address << endl;
  115.         cout << "Type: Hospital" << endl;
  116.         cout << "Hospital Type: " << hospitalType << endl;
  117.     }
  118.     string getType() { return "Hospital"; }
  119. };
  120. class Street {
  121. private:
  122.     vector<House*> houses;
  123.  
  124. public:
  125.     Street() {}
  126.  
  127.     void addHouse(House* house) {
  128.         houses.push_back(house);
  129.     }
  130.  
  131.     void printHouses() {
  132.         for (auto house : houses) {
  133.             house->printInfo();
  134.         }
  135.     }
  136.     void writeToFile(string fileName) {
  137.         ofstream outfile;
  138.         outfile.open(fileName);
  139.  
  140.         if (!outfile) {
  141.             cerr << "Unable to open file " << fileName << endl;
  142.             exit(1);  
  143.         }
  144.  
  145.         for (auto house : houses) {
  146.             outfile << house->getType() << endl;
  147.             outfile << house->getAddress() << endl;
  148.             if (house->getType() != "Hospital" && house->getType() != "Store")
  149.                 outfile << house->getNumResidents() << endl;
  150.         }
  151.  
  152.         outfile.close();
  153.     }
  154.     void readFromFile(string fileName) {
  155.         ifstream infile;
  156.         infile.open(fileName);
  157.  
  158.         if (!infile) {
  159.             cerr << "Unable to open file " << fileName << endl;
  160.             exit(1);   // call system to stop
  161.         }
  162.  
  163.         string type, address, data;
  164.         while (getline(infile, type)) {
  165.             getline(infile, address);
  166.             if (type != "Hospital" && type != "Store") {
  167.                 getline(infile, data);
  168.             }
  169.             else {
  170.                 data = "";
  171.             }
  172.             House* house;
  173.             if (type == "Residential House") {
  174.                 int numResidents = stoi(data);
  175.                 house = new ResidentialHouse(address, numResidents);
  176.             }
  177.             else if (type == "Store") {
  178.                 string storeType = data;
  179.                 house = new Store(address, storeType);
  180.             }
  181.             else if (type == "School") {
  182.                 string level = data;
  183.                 int numStudents = stoi(level);
  184.                 house = new School(address, numStudents, level);
  185.             }
  186.             else if (type == "Hospital") {
  187.                 string hospitalType = data;
  188.                 house = new Hospital(address, hospitalType);
  189.             }
  190.             else {
  191.                 cerr << "Unknown house type: " << type << endl;
  192.                 continue;
  193.             }
  194.             addHouse(house);
  195.         }
  196.  
  197.         infile.close();
  198.     }
  199. };
  200.  
  201. int main() {
  202.     Store shop("456 Maple Ave", "grocery");
  203.  
  204.     Hospital hospital("789 Oak St", "general");
  205.  
  206.     School school("101 Elm St", 800, "lyceum");
  207.     ResidentialHouse resH("65 Maple Ave", 456);
  208.  
  209.     Street street;
  210.     street.addHouse(&shop);
  211.     street.addHouse(&hospital);
  212.     street.addHouse(&school);
  213.     street.addHouse(&resH);
  214.  
  215.     street.printHouses();
  216.     street.writeToFile("file.txt");
  217.     street.readFromFile("file.txt");
  218.     street.printHouses();
  219.     return 0;
  220. }
Advertisement
Add Comment
Please, Sign In to add comment