-
Notifications
You must be signed in to change notification settings - Fork 0
/
room.cpp
153 lines (135 loc) · 3.53 KB
/
room.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "room.h"
#include "house.h"
#include "devicefactory.h"
#include "appliancefactory.h"
#include "lightfactory.h"
#include <iostream>
#include <algorithm>
using namespace std;
Room::Room(std::string roomName)
{
_name = roomName;
cout << "Added " << _name << endl;
}
Room::~Room()
{
}
void Room::addComponent(std::string componentName)
{
House *h = House::instance();
std::vector<Component*> comp = h->getComponents();
for(std::vector<Component*>::iterator it = comp.begin(); it != comp.end(); ++it)
{
Component *c = *it;
if(c->getName() == componentName)
{
cout << "Added " << c->getName() << " to " << this->getName() << endl;
_components.push_back(c);
}
}
}
void Room::removeComponent(std::string componentName)
{
for(std::vector<Component*>::iterator it = _components.begin(); it != _components.end(); ++it)
{
Component *c = *it;
if(c->getName() == componentName)
{
cout << "Removing " << c->getName() << " from " << this->getName() << endl;
_components.erase(std::remove(_components.begin(), _components.end(), c));
c->~Component();
}
}
}
/*
*This method is called by the "Setup Component Called 'Y'" Command
*/
void Room::setupComponent(TYPES::COMPONENTS componentType, string componentName)
{
Factory *factory;
Component *comp;
switch(componentType){
case TYPES::DEVICE :
factory = new DeviceFactory();
comp = factory->create(componentName);
_components.push_back(comp);
break;
case TYPES::APPLIANCE :
factory = new ApplianceFactory();
comp = factory->create(componentName);
_components.push_back(comp);
break;
case TYPES::LIGHT :
factory = new LightFactory();
comp = factory->create(componentName);
_components.push_back(comp);
break;
default:
cerr << "Currently unsupported component" << endl;
break;
}
}
void Room::turnOnComponent(std::string componentName)
{
for(std::vector<Component*>::iterator it = _components.begin(); it != _components.end(); ++it)
{
Component *c = *it;
if(c->getName().compare(componentName))
{
cout << "Turning on " << c->getName() << endl;
c->on();
break;
}
}
}
void Room::turnOnComponents()
{
for(std::vector<Component*>::iterator it = _components.begin(); it != _components.end(); ++it)
{
Component *c = *it;
cout << "Turning on " << c->getName() << endl;
c->on();
}
}
void Room::turnOffComponent(std::string componentName)
{
for(std::vector<Component*>::iterator it = _components.begin(); it != _components.end(); ++it)
{
Component *c = *it;
if(c->getName().compare(componentName))
{
cout << "Turning off " << c->getName() << endl;
c->off();
break;
}
}
}
void Room::turnOffComponents()
{
for(std::vector<Component*>::iterator it = _components.begin(); it != _components.end(); ++it)
{
Component *c = *it;
cout << "Turning off " << c->getName() << endl;
c->off();
}
}
std:: string Room::getName()
{
return _name;
}
void Room::setName(std::string newName)
{
_name = newName;
}
Memento *Room::createMemento()
{
return new Memento(_components);
}
void Room::reinstateMemento(Memento *mem)
{
_components = mem->_state;
}
std::vector<Component *> Room::getComponents()
{
return _components;
}