-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserInterface.cpp
406 lines (392 loc) · 12.7 KB
/
UserInterface.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//Jeff Henebury
//implementation of User Interface class
/*
doing the UI via a series of loops:
1.An initial 'choose your device or quit' loop
2. and then, within each device, a 'choose what you want to do with that device' loop
*/
#pragma once
#include "UserInterface.h"
#include <iostream>
#include <algorithm> //for transform
#include <string>
using namespace std;
using std::cout;
using std::cin;
UserInterface::UserInterface()
{
//point the pointers to the respective class instances
pMyThermostat = &myThermostat;
pMyTelevision = &myTelevision;
pMyLights = &myLights;
pMySecSystem = &mySecSystem;
pMyVac = &myVac;
//fill the Device vector with POINTERS to all the device objects
allDevices.push_back(pMyThermostat);
allDevices.push_back(pMyTelevision);
allDevices.push_back(pMyLights);
allDevices.push_back(pMySecSystem);
allDevices.push_back(pMyVac);
}
UserInterface::~UserInterface() //destructor
{
}
void UserInterface::getInput(){
//before you begin, load the current device schedule from file
loadRecords(allDevices);
while (true) {
std::cout << "Welcome to the Henebury Device Manager. We take care of all your devices!\n";
std::cout << "Here is the list of connected Devices:\n";
std::cout << "1.Thermostat\n2.Television\n3.Lights\n4.Security System\n5.Vacuum\n6.Save Updates to Device Schedules\n7.Exit program\n";
std::cout << "\nWhich device would you like to access? (Enter 1-6, or 7 to exit): ";
//exception catch: if it's not a number btw. 1 and 7, catch the error, dump back to the main menu
int input = testTheInput(1, 7);
if (input == 0) { //base class Device, just for testing purposes
showOptionsForDevice();
continue;
}
if (input == 1) { //thermostat
showOptionsForThermostat();
continue;
}
if (input == 2) { //TV
showOptionsForTV();
continue;
}
if (input == 3) { //lights
showOptionsForLights();
continue;
}
if (input == 4) { //security system
showOptionsForSecurity();
continue;
}
if (input == 5) { //vacuum
showOptionsForVacuum();
continue;
}
if (input == 6) { //save schedule to file
saveRecords(allDevices);
continue;
}
if (input == 7) { //exit
cout << "\nThank you for using the Henebury Device Manager. Goodbye!\n";
break;
}
};
}
void UserInterface::showOptionsForThermostat()
{
cout << "Great, you chose Thermostat.\n";
while (true)
{
cout << "\n****\n";
cout <<"Enter the number (1 - 6) of the option you'd like to select:\n";
cout << "1.Check current power status\n2.Turn the system on or off\n3.Change the temperature\n"
<< "4.Get current schedule\n5.Set schedule\n6.Quit to go back\n";
//exception catch: if it's not a number, catch the error, dump back to the main menu
int deviceInput = testTheInput(1, 6);
if (deviceInput == 1) { //check power status
pMyThermostat->getPowerStatus();
};
if (deviceInput == 2) {
//cout << "TEST, you got to CHANGE POWER STATUS\N";
pMyThermostat->onOrOff();
};
if (deviceInput == 3) {
pMyThermostat->changeTheTemp();
};
if (deviceInput == 4) { //Getting current scheduling
pMyThermostat->getSchedule();
HoursOn(myThermostat);
};
if (deviceInput == 5) { //setting scheduling
pMyThermostat->setSchedule();
HoursOn(myThermostat);
};
if (deviceInput == 6) {
cout << "Back to Main Menu...\n";
break;
};
}
}
void UserInterface::showOptionsForLights()
{
cout << "Great, you chose Lights.\n";
while (true)
{
cout << "\n****\n";
cout << "Enter the number (1 - 6) of the option you'd like to select:\n";
cout << "1.Check current power status\n2.Turn the lights on or off\n3.Get a list of light-connected rooms\n"
<< "4.Get current schedule\n5.Set schedule\n6.Quit to go back\n";
/*cout << "What would you like to do?\n";
cin >> deviceInput;*/
int deviceInput = testTheInput(1, 6);
if (deviceInput == 1) { //check power status
pMyLights->getPowerStatus();
};
if (deviceInput == 2) {
pMyLights->onOrOff();
};
if (deviceInput == 3) { //get a list of rooms, see if they're on or off
pMyLights->printRooms();
};
if (deviceInput == 4) { //Getting current scheduling
pMyLights->getSchedule();
HoursOn(myLights);
};
if (deviceInput == 5) { //setting scheduling
pMyLights->setSchedule();
HoursOn(myLights);
};
if (deviceInput == 6) {
cout << "Back to Main Menu...\n";
break;
};
}
}
void UserInterface::showOptionsForSecurity()
{
cout << "Great, you chose Security System.\n";
while (true)
{
int newPower;
cout << "Enter the number (1-4) of the option you'd like to select:\n";
cout << "1.Check power status\n2.Turn the power off or on\n3.Get current schedule\n4.Set schedule\n5.Change security sensitivity level\n6.Quit to go back\n";
cout << "What would you like to do?\n";
//exception catch: if it's not a number, catch the error, dump back to the main menu
int deviceInput = testTheInput(1, 6);
if (deviceInput == 1) { //check power
pMySecSystem->getPowerStatus();
};
if (deviceInput == 2) { //turn on or off
pMySecSystem->onOrOff();
};
if (deviceInput == 3) { //get current schedule
pMySecSystem->getSchedule();
HoursOn(mySecSystem);
};
if (deviceInput == 4) { //get current schedule
pMySecSystem->setSchedule();
HoursOn(mySecSystem);
};
if (deviceInput == 5) { //chnge sensitivy level
cout << "What would you like the security system's sensitivity level to be set to? (1 lowest, 5 highest): ";
cin >> newPower;
pMySecSystem->setLightSensitivity(newPower);
};
if (deviceInput == 6) {
cout << "Back to Main Menu...\n";
break;
};
}
}
void UserInterface::showOptionsForTV()
{
cout << "Great, you chose TV.\n";
while (true)
{
cout << "\n****\n";
cout << "Enter the number (1-6) of the option you'd like to select:\n";
cout << "1.Check current power status\n2.Turn the TV on or off\n3.Check the current channel\n4.Change the channel\n"
<< "5.Get current schedule\n6.Set new schedule\n7.Quit to go back\n";
cout << "What would you like to do?\n";
//exception catch: if it's not a number, catch the error, dump back to the main menu
int deviceInput = testTheInput(1, 7);
if (deviceInput == 1) { //check power status
pMyTelevision->getPowerStatus();
};
if (deviceInput == 2) { //set new power status
pMyTelevision->onOrOff();
};
if (deviceInput == 3) { //check current channel
cout << "The current channel is set to: " << pMyTelevision->getCurrentChannel() << ".(I love this show!)\n";
};
if (deviceInput == 4) { //change the channel
int newChannel;
int oldChannel = pMyTelevision->getCurrentChannel();
cout << "The current channel is set to: " << oldChannel << ".\n";
cout << "What would you like to change it to?\n";
cin >> newChannel;
pMyTelevision->setCurrentChannel(newChannel);
if (oldChannel < newChannel) {
cout << "Great, you've turned the channel up to: " << pMyTelevision->getCurrentChannel() << ". Hope something good is on!\n";
}
else if (oldChannel > newChannel) {
cout << "Great, you've turned the channel down to: " << pMyTelevision->getCurrentChannel() << ". Hope something good is on!\n";
}
};
if (deviceInput == 5) { //get current schedule
pMyTelevision->getSchedule();
HoursOn(myTelevision);
};
if (deviceInput == 6) { //change the schedule
pMyTelevision->setSchedule();
HoursOn(myTelevision);
};
if (deviceInput == 7) { //back to main menu
cout << "Back to Main Menu...\n";
break;
};
}
}
void UserInterface::showOptionsForVacuum() {
cout << "Great, you chose Vacuum.\n";
while (true)
{
cout << "\n****\n";
cout << "Enter the number (1 - 6) of the option you'd like to select:\n";
cout << "1.Check current power status\n2.Turn the vacuum on or off\n3.Get a list of vacuum-connected rooms\n"
<< "4.Get current schedule\n5.Set schedule\n6.Quit to go back\n";
cout << "What would you like to do?\n";
//exception catch: if it's not a number, catch the error, dump back to the main menu
int deviceInput = testTheInput(1, 6);
if (deviceInput == 1) { //check power status
pMyVac->getPowerStatus();
};
if (deviceInput == 2) {
pMyVac->onOrOff();
};
if (deviceInput == 3) { //get a list of rooms, see if they're on or off
pMyVac->printRooms();
};
if (deviceInput == 4) { //Getting current scheduling
pMyVac->getSchedule();
HoursOn(myVac);
};
if (deviceInput == 5) { //setting scheduling
pMyVac->setSchedule();
HoursOn(myVac);
};
if (deviceInput == 6) {
cout << "Back to Main Menu...\n";
break;
};
}
}
//use Exception Handling to check the input. If it's not one of the options, send error message, clear input, redo
int UserInterface::testTheInput(int lowest, int highest)
{
int deviceInput;
while (true)
{
cout << "\nYour Input: ";
cin >> deviceInput;
try
{
if ((deviceInput < lowest) || (deviceInput > highest)) {
throw 101;
}
else {
return deviceInput;
}
}
catch (int badInput) {
cout << "Error: please enter a digit between " << lowest << " and " <<highest << endl;
cin.clear(); //clear inputted value
while (cin.get() != '\n'); // empty the loop, start over
continue;
}
}
}
//don't really need this one, just testing out the class to see if it works...
void UserInterface:: showOptionsForDevice()
{
cout << "Great, you chose the secret testing option: Device.\n";
while (true)
{
cout << "Enter the number (1-4) of the option you'd like to select:\n";
cout << "1.Check power status\n2.Change the power status\n3.View the schedule\n4.Set schedule\n5.Quit to go back\n";
cout << "What would you like to do?\n";
//exception catch: if it's not a number, catch the error, dump back to the main menu
int deviceInput = testTheInput(1, 6);
if (deviceInput == 1) {
cout << "TEST, you got to POWER STATUS\n";
myDevice.getPowerStatus();
};
if (deviceInput == 2) {
cout << "TEST, you got to CHANGE POWER STATUS\n";
myDevice.onOrOff();
};
if (deviceInput == 3) {
cout << "TEST, you got to VIEW SCHEDULE\n";
myDevice.getSchedule();
HoursOn(myDevice);
};
if (deviceInput == 4) {
cout << "TEST, you got to CHANGE SCHEDULE\n";
myDevice.setSchedule();
HoursOn(myDevice);
};
if (deviceInput == 5) {
cout << "Back to Main Menu...\n";
break;
};
}
}
//these are for saving the file
//save the schedule for all devices
void UserInterface::saveRecords(vector<Device*> allDevices)
{
//open file for writing
try {
cout << "\nWriting contents to file...";
//open file for writing
ofstream fw("Devices.txt", std::ofstream::out);
//check if file was opened for writing
if (fw.is_open())
{
//store contents to text file
for (int i = 0; i < allDevices.size(); i++) {
//save schedule for just one day, since all days have the same schedule for now...
fw << allDevices[i]->DeviceSchedule.weeklySchedule[0].turnOnTime_day << "\n";
fw << allDevices[i]->DeviceSchedule.weeklySchedule[0].turnOffTime_day << "\n";
fw << allDevices[i]->DeviceSchedule.weeklySchedule[0].turnOnTime_evening << "\n";
fw << allDevices[i]->DeviceSchedule.weeklySchedule[0].turnOffTime_evening << "\n";
//this works to get the power status:
//fw << allDevices[i]->power<< "\n";
}
fw.close();
}
else cout << "Problem with opening file";
}
catch (const char* msg) {
cerr << msg << endl;
}
cout << "\nDone!\n";
}
//load in the schedules for all devices
void UserInterface::loadRecords(vector<Device*> allDevices)
{
ifstream theReadInFile;
string sReadLine;
int iReadLine;
theReadInFile.open("Devices.txt", std::ofstream::out); //open file
if (!theReadInFile)
{
cout << "Error opening the file.\n";
}
else {
//get the schedule for each device in the vector, plop in the line from the file
for (int i = 0; i < allDevices.size(); i++) {
//get 4 lines of input, since we have to fill in 4 lines for each day for each device (time on-morning, time-on-night, etc.)
//we're just saving the first day, so get just the first day per device...
std::getline(theReadInFile, sReadLine);
stringstream(sReadLine) >> iReadLine; //convert file string to int
int turnOnTime_day = iReadLine;
std::getline(theReadInFile, sReadLine);
stringstream(sReadLine) >> iReadLine;
int turnOffTime_day = iReadLine;
std::getline(theReadInFile, sReadLine);
stringstream(sReadLine) >> iReadLine;
int turnOnTime_evening = iReadLine;
std::getline(theReadInFile, sReadLine);
stringstream(sReadLine) >> iReadLine;
int turnOffTime_evening = iReadLine;
// ... and then based off of that first day, set the schedule for the rest of the week for that device, then move on to next device
allDevices[i]->DeviceSchedule.setWeeklySchedule(turnOnTime_day, turnOffTime_day, turnOnTime_evening, turnOffTime_evening);
}
theReadInFile.close();
cout << "\nFile successfully loaded!\n\n";
}
}