Advertisement
dc5553

Building Object C++

Jul 20th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.74 KB | None | 0 0
  1. /*
  2. Imagine you are working for an in-house software team for a company that wants
  3. to monitor network access points in a building. Each building has a specified
  4. number of floors, a certain number of rooms per floor, and some number of network
  5. access points in a room. Each network access point is labeled with its state as
  6. being on or off and if it is on it has the month it was turned on. Design,
  7. implement, and test classes that represent buildings, floors, and network access
  8.  points. Your classes should provide suitable observer methods that allow you to
  9.  determine how many floors are in a building, how many rooms are on a floor, how
  10.  many access points are in a room, and if an access point is on or off and which
  11.  month it was turned on. You should also provide mutator methods that allow you
  12. to turn a network access point on or off and to set the month if it is turned on.
  13. You should use composition as a key strategy in designing these classes—thus, a
  14. building should contain floors, floors should contain rooms, and rooms should
  15. contain network access points. Your program should allow a user to add additional
  16.  floors, rooms, and access points as well as turn access points on and off. Your
  17.  program should also report errors using exceptions if a room, floor, or access
  18. point is chosen that does not exist.
  19.  */
  20.  
  21. #include <iostream>
  22. #include <string>
  23. #include <vector>
  24. #include <ctype.h>
  25.  
  26. using namespace std;
  27.  
  28. enum MONTH {
  29.     Null = 0, January, February, March, April, May, June, July, August, September, October, November, December
  30. };
  31.  
  32. class AccessPoint {
  33. public:
  34.  
  35.     AccessPoint() {
  36.         isPoweredOn = false;
  37.         monthTurnedOn = 0;
  38.     };
  39.  
  40.     AccessPoint(bool power, MONTH month) {
  41.         isPoweredOn = power;
  42.         monthTurnedOn = month;
  43.     };
  44.  
  45.     AccessPoint(bool power, int month) {
  46.         isPoweredOn = power;
  47.         monthTurnedOn = month;
  48.     };
  49.  
  50.     virtual ~AccessPoint() {
  51.     };
  52.  
  53.     void PowerOn() {
  54.         isPoweredOn = true;
  55.     };
  56.  
  57.     void PowerOff() {
  58.         isPoweredOn = false;
  59.     };
  60.  
  61.     void setMonthTurnedOn(MONTH month) {
  62.         monthTurnedOn = month;
  63.     };
  64.  
  65.     string getPowerStatus() {
  66.         return isPoweredOn ? "On" : "Off";
  67.     };
  68.  
  69.     int getMonthTurnedOn() {
  70.         return monthTurnedOn;
  71.     };
  72.  
  73. private:
  74.     bool isPoweredOn;
  75.     int monthTurnedOn;
  76.     friend ostream& operator<<(ostream&, const AccessPoint&);
  77. };
  78.  
  79. class Room {
  80. public:
  81.     vector<AccessPoint> accessPoints;
  82.  
  83.     Room() {
  84.     };
  85.  
  86.     Room(int numberOfAccessPoints) {
  87.         for (int i = 0; i < numberOfAccessPoints; i++) {
  88.             AccessPoint *ap1;
  89.             ap1 = new AccessPoint;
  90.             accessPoints.push_back(*ap1);
  91.         }
  92.     };
  93.  
  94.     ~Room() {
  95.     };
  96.  
  97.     void addAccessPoint() {
  98.         AccessPoint *ap1;
  99.         ap1 = new AccessPoint;
  100.         accessPoints.push_back(*ap1);
  101.     };
  102.  
  103.     int getNumberOfAccessPoints() {
  104.         return accessPoints.size();
  105.     };
  106.  
  107. private:
  108. };
  109.  
  110. class Floor {
  111. public:
  112.     vector<Room> rooms;
  113.  
  114.     Floor() {
  115.     };
  116.  
  117.     Floor(int numberOfRooms) {
  118.         for (int i = 0; i < numberOfRooms; i++) {
  119.             Room *room1;
  120.             room1 = new Room;
  121.             rooms.push_back(*room1);
  122.         }
  123.     };
  124.  
  125.     ~Floor() {
  126.     };
  127.  
  128.     void addRoom() {
  129.         Room *room1;
  130.         room1 = new Room;
  131.         rooms.push_back(*room1);
  132.     };
  133.  
  134.     int getNumberOfRooms() {
  135.         return rooms.size();
  136.     };
  137.  
  138. private:
  139.  
  140. };
  141.  
  142. class Building {
  143. public:
  144.     vector<Floor> floors;
  145.  
  146.     Building() {
  147.     };
  148.  
  149.     Building(int numberOfFloors) {
  150.         for (int i = 0; i < numberOfFloors; i++) {
  151.             Floor *floor1;
  152.             floor1 = new Floor;
  153.             floors.push_back(*floor1);
  154.         }
  155.     };
  156.  
  157.     ~Building() {
  158.     };
  159.  
  160.     void addFloor() {
  161.         Floor *floor1;
  162.         floor1 = new Floor;
  163.         floors.push_back(*floor1);
  164.     }
  165.  
  166.     int getNumberOfFloors() {
  167.         return floors.size();
  168.     };
  169.  
  170. private:
  171. };
  172.  
  173. class BuildingFactory {
  174. public:
  175.  
  176.     BuildingFactory() {
  177.  
  178.     };
  179.  
  180.     void build() {
  181.         cout << "##############################################################" << endl;
  182.         cout << "# This program will create a building to your specifications #" << endl;
  183.         cout << "##############################################################" << endl;
  184.  
  185.         cout << "How many floors will be in your building?" << endl;
  186.         cin >> numberOfFloors;
  187.         for (int x = 0; x < numberOfFloors; ++x) {
  188.             building.addFloor();
  189.             cout << "How many rooms on floor number " << x + 1 << "?" << endl;
  190.             cin >> numberOfRooms;
  191.             for (int y = 0; y < numberOfRooms; ++y) {
  192.                 building.floors.at(x).addRoom();
  193.                 cout << "How many access points are in room " << y + 1 << " on floor " << x + 1 << "?" << endl;
  194.                 cin >> numberOfAccessPoints;
  195.                 for (int z = 0; z < numberOfAccessPoints; ++z) {
  196.                     building.floors.at(x).rooms.at(y).addAccessPoint();
  197.                 }
  198.             }
  199.         }
  200.         cout << "##################" << endl;
  201.         cout << "# Done Building  #" << endl;
  202.         cout << "##################" << endl;
  203.     }
  204.  
  205.     Building deliver() {
  206.         return building;
  207.     }
  208.  
  209. private:
  210.     int numberOfFloors;
  211.     int numberOfRooms;
  212.     int numberOfAccessPoints;
  213.     int ans2;
  214.     char ans;
  215.     Building building;
  216. };
  217.  
  218. class BuildingManager {
  219. public:
  220.  
  221.     BuildingManager(Building building) {
  222.         bldg = building;
  223.     };
  224.  
  225.     void getStatus() {
  226.         cout << "[-] Status of the building is: " << endl;
  227.         cout << "[-] The building has " << bldg.getNumberOfFloors() << " floors" << endl;
  228.         for (int x = 0; x < bldg.getNumberOfFloors(); x++) {
  229.             cout << "[-] Floor " << x + 1 << " has " << bldg.floors.at(x).getNumberOfRooms() << " rooms" << endl;
  230.             for (int y = 0; y < bldg.floors.at(x).getNumberOfRooms(); y++) {
  231.                 cout << "[-] Room number " << y + 1 << " on floor " << x + 1 << " has " << bldg.floors.at(x).rooms.at(y).getNumberOfAccessPoints() << " access points" << endl;
  232.                 cout << "[-] Of those access points here are their statuses: " << endl;
  233.                 for (int z = 0; z < bldg.floors.at(x).rooms.at(y).getNumberOfAccessPoints(); z++){
  234.                     cout << bldg.floors.at(x).rooms.at(y).accessPoints.at(z) << endl;
  235.                 }
  236.             }
  237.         }
  238.     };
  239.  
  240.     void powerOnAP(int floor, int room, int ap, MONTH month) {
  241.         if (ap < bldg.floors.at(floor).rooms.at(room).getNumberOfAccessPoints()) {
  242.             bldg.floors.at(floor).rooms.at(room).accessPoints.at(ap).PowerOn();
  243.             bldg.floors.at(floor).rooms.at(room).accessPoints.at(ap).setMonthTurnedOn(month);
  244.         }
  245.     };
  246.  
  247.     void powerOffAP(int floor, int room, int ap) {
  248.         if (ap < bldg.floors.at(floor).rooms.at(room).getNumberOfAccessPoints()) {
  249.             bldg.floors.at(floor).rooms.at(room).accessPoints.at(ap).PowerOff();
  250.             bldg.floors.at(floor).rooms.at(room).accessPoints.at(ap).setMonthTurnedOn(Null);
  251.         }
  252.     };
  253.  
  254. private:
  255.     Building bldg;
  256. };
  257.  
  258. ostream& operator<<(ostream &strm, const AccessPoint &ap) {
  259.     return strm << "[-] AccessPoint (Power: " << ap.isPoweredOn << "/ Month Powered On: " << ap.monthTurnedOn << ")";
  260. };
  261.  
  262. int main() {
  263.     BuildingFactory bf;
  264.     bf.build();
  265.     BuildingManager bm(bf.deliver());
  266.     bm.powerOnAP(1, 1, 1, December);
  267.     bm.powerOnAP(0, 0, 0, January);
  268.     bm.powerOnAP(0, 1, 1, February);
  269.     bm.powerOnAP(1, 0, 0, March);
  270.    
  271.     bm.getStatus();
  272.     cout << endl;
  273.     bm.powerOffAP(1, 0, 0);
  274.     cout << "#####################" << endl;
  275.     bm.getStatus();
  276.  
  277.     return 0;
  278.  
  279.  
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement