-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOOP_PreMid_Assignment.cpp
More file actions
217 lines (181 loc) · 5.71 KB
/
Copy pathOOP_PreMid_Assignment.cpp
File metadata and controls
217 lines (181 loc) · 5.71 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <iostream>
using namespace std;
class Device{
string Name;
bool isON;
int brightness;
public:
Device(){ // Default Constructor
Name = "Unknown";
isON = false;
brightness = 50;
}
Device(string n, bool status, int b){ // Overloaded Constructor
Name = n;
isON = status;
if (b >= 0 && b<= 100){ // Validation
brightness = b;
}
else{
brightness = 50; // default value if invalid
}
}
void setName(string n){
Name = n;
}
string getName() const{
return Name;
}
void setBrightness(int b){
if (b >= 0 && b<= 100){ // Validation
brightness = b;
}
else{
cout << "Invalid brightness value. Must be 0-100." << endl;
}
}
int getBrightness() const{
return brightness;
}
void turnOn(){
isON = true;
}
void turnOff(){
isON = false;
}
bool getStatus() const{
return isON;
}
void showInfo() const{
cout << "Name of Device: " << Name
<< ", Status : " << (isON ? "ON" : "OFF")
<< ", Brightness Level: " << brightness << endl;
}
~Device(){ // Destructor
cout<<"Device "<<Name<<" is being removed."<<endl;
}
};
class Room{
string roomName;
Device devices[5];
int count; // Number of devices currently in room
public:
Room(string name) : roomName(name) , count(0){} //Constructor with member initializer list
~Room(){ // Destructor
cout << "Room " << roomName << " manager closed." << endl;
}
// Adds device to array if space is available
void addDevice(Device d){
if (count < 5){
devices[count] = d;
count++;
cout << "Device added to " << roomName << endl;
}
else{
cout << "Room is full. Cannot add device." << endl;
}
}
// Shows all devices in room
void showAllDevices() const {
if (count == 0) {
cout << "No devices in " << roomName << endl;
return;
}
cout << "Devices in " << roomName << ":" <<endl;
for (int i = 0; i < count; i++){
devices[i].showInfo();
}
}
// Reurns index of device
int findDevice(string name){
for (int i = 0; i < count; i++){
if (devices[i].getName() == name){
return i;
}
}
return -1;
}
// Turns device ON/OFF
void toggleDevice(string name){
int index = findDevice(name);
if (index == -1){
cout << "Device not found." << endl;
return;
}
if (devices[index].getStatus()){
devices[index].turnOff();
}
else{
devices[index].turnOn();
}
cout << "Device " << name << " is toggled." << endl;
}
// Changes Brightness
void setDeviceBrightness(string name, int brightness){
int index = findDevice(name);
if (index == -1){
cout << "Device not found." << endl;
return;
}
devices[index].setBrightness(brightness);
cout << "Brightness updated for " << name << endl;
}
};
int main(){
Room r("Living Room");
int choice;
do {
cout << "\n=== Smart Room Manager ===" << endl;
cout << "1. Add a Device" << endl;
cout << "2. Show All Devices" << endl;
cout << "3. Turn Device ON/OFF" << endl;
cout << "4. Change Brightness" << endl;
cout << "5. Exit" << endl;
cout << "Enter choice: ";
cin >> choice;
cin.ignore(); // clear newline in buffer so getline does not read an empty line
switch(choice) {
case 1:{
string name;
bool status;
int brightness;
cout << "Enter device name: ";
getline(cin, name);
cout << "Enter status (1 = ON, 0 = OFF): ";
cin >> status;
cout << "Enter brightness (0-100): ";
cin >> brightness;
Device d(name, status, brightness);
r.addDevice(d);
break;
}
case 2: {
r.showAllDevices();
break;
}
case 3: {
string name;
cout << "Enter device name to toggle: ";
getline(cin >> ws, name); // ws removes leadimg whitespace/newline
r.toggleDevice(name);
break;
}
case 4: {
string name;
int b;
cout << "Enter device name: ";
getline(cin >> ws, name);
cout << "Enter new brightness (0-100): ";
cin >> b;
r.setDeviceBrightness(name, b);
break;
}
case 5:
cout << "Exiting Smart Room Manager ..." << endl;
break;
default:
cout << "Invalid choice. Try again." << endl;
}
} while (choice != 5);
return 0;
}