i need to finish this code in c++ i just need the streetlight and temperature cl
ID: 3721362 • Letter: I
Question
i need to finish this code in c++ i just need the streetlight and temperature class done that’s it this is what i have so far:#define _CRT_SECURE_NO_WARNINGS #define TEMPERATURE_RANGE 100 #include <iostream> #include <string> #include <cstdlib> #include <vector>
using namespace std;
class Temperature { public: Temperature() { for (unsigned int i = 0; i < pTemperatureData.capacity(); i++) { pTemperatureData[i] = 0; } } //constructor
~Temperature() { for (unsigned int i = 0; i < pTemperatureData.size(); i++) { if (pTemperatureData[i]) { pTemperatureData[i] = 0; } } }//destructor
int getTemperatureData() { int randomData = rand() % TEMPERATURE_RANGE ; //random value generated from 0-100. This value is represented in Percentage if (randomData > 0) { pTemperatureData.push_back(randomData); //add randomData value into vector pLightData } return randomData; } void print() { for (unsigned int i = 0; i < pTemperatureData.size(); i++) { cout << pTemperatureData[i] << endl; } } static void test() { Temperature aTemperature; int randnum1 = aTemperature.getLightData(); int randnum2 = aTemperature.getLightData(); int randnum3 = aTemperature.getLightData(); int randnum4 = aTemperature.getLightData(); cout << "randnum1 = " << randnum1 << endl; cout << "randnum2 = " << randnum2 << endl; cout << "randnum3 = " << randnum3 << endl; cout << "randnum4 = " << randnum4 << endl; aTemperature.print(); } protected: vector <double> pTemperatureData; };
Explanation / Answer
NOTE:
1. The class model was missing in your question. The class model was designed, built as per class diagram
2. The temperature and streetlight classes are implemented in temperature.cpp and streetlight.cpp files.
3. The code works as follows;
It creates cities and randomly adds some streetlights to it using appropriate classes
then it submits each city to StreetLightController class which internally loops through streetlights, reads the four sensors each streetlight has and decides if LED for the particular streetlight should be ON, OFF or DIM
At the end, the LED status for every light, along with sensor data is displayed.
For testing purpose, sensor data returns a value randomly as -1,0 or 1. -1 may mean BAD or LESS. example LESS TRAFFIC, LESS SUNLIGHT, BAD WEATHER
4. Please save each file separately as mentioned below and then compile before testing it.
CODE FILES:
MAIN.CPP
#include<string>
#include <vector>
#include <iostream>
#include <iomanip>
#include <direct.h>
#include <cstdio>
#include "StreetLightController.h"
#include "City.h"
using namespace std;
City getCityData(string cityname);
int main(int argc, char **argv)
{
//list of cities that are analyzed
vector<City> _cities;
_cities.push_back(getCityData("MUMBAI"));
_cities.push_back(getCityData("NEWYORK"));
StreetLightController _streetlightcontroller;
for(std::vector<City>::size_type i = 0; i != _cities.size(); i++) {
//_streetlightcontroller.showCityLightStatus(_cities[i]);
_cities[i] = _streetlightcontroller.manageCity(_cities[i]);
_streetlightcontroller.showCityLightStatus(_cities[i]);
}
exit(0);
}//end main
City getCityData(string cityname)
{
//the program will analyze the information
//and make appropriate changes to the
//smart streetlights
City _city(cityname);
return _city;
};
_GENERIC.H
#include <stdlib.h>
#include <string>
#pragma once
using namespace std;
class _generic
{
public:
static const int led_ON=1;
static const int led_OFF=2;
static const int led_DIM=3;
static int generateRandom(int min, int max);
static int generateRandom(int range);
static void display_data(int data, string datatype);
};
CITY.H
#pragma once
#include "Streetlight.h"
#include "_generic.h"
#include <vector>
using namespace std;
class City
{
public:
City(string cityname);
virtual ~City(void);
public:
string _cityname;
//city will be use the controllers code on smart streelights
vector<Streetlight> _streetlights;
void getCityData(string cityname);
};
COMMUNICATION.H
#pragma once
class Communication
{
public:
Communication(void);
virtual ~Communication(void);
public:
void setCommunication();
};
GPS.H
#pragma once
class GPS
{
public:
GPS(void);
virtual ~GPS(void);
public:
double data[2]; /* x coordinate and y coordinate */
public:
double getGPS();
};
HUMIDITY.H
#pragma once
#include "weather.h"
class Humidity :
public Weather
{
public:
Humidity(void);
~Humidity(void);
public:
bool analyzeNdifferentiateWeatherConditions(string weatherdata);
double getWeather();
};
LIGHT.H
#pragma once
#include "_generic.h"
class Light
{
public:
Light(void);
virtual ~Light(void);
public:
double data;
public:
double getLight();
};
MOTION.H
#pragma once
#include "traffic.h"
class Motion :
public Traffic
{
public:
Motion(void);
virtual ~Motion(void);
double getTraffic();
};
SENSORS.H
#pragma once
#include "Light.h"
#include "Motion.h"
#include "Temperature.h"
#include "Humidity.h"
class Sensors
{
public:
Sensors(void);
virtual ~Sensors(void);
public:
//the street light will be able to pickup info with
//following sensors:light, weather(humidity and temperature),
// and motion
Light _light;
Motion _motion;
Temperature _temperature;
Humidity _humidity;
};
STREETLIGHT.H
#pragma once
#include "sensors.h"
#include <vector>
using namespace std;
class Streetlight
{
public:
Streetlight(void);
virtual ~Streetlight(void);
public:
//street light gather information from sensors
Sensors _sensors;
public:
//the program will have three settings for he LEDs
//the settings will depend on the analysis
// of the information by program
int ledstatus; //1=ON 2=OFF 3=DIM
public:
double getStreetlight();
};
STREETLIGHTCONTROLLER.H
#pragma once
#include "GPS.h"
#include "Streetlight.h"
#include "Motion.h"
#include "Communication.h"
#include "Light.h"
#include "Weather.h"
#include "City.h"
class StreetLightController
{
public:
StreetLightController(void);
virtual ~StreetLightController(void);
double getWeather(Sensors * _sensors);
double getLight(Sensors * _sensors);
double getTraffic(Sensors * _sensors);
double getStreetLight(Streetlight * _streetlight);
void setCommunication();
public:
GPS _gps;
Streetlight _streetlight;
/*
//street light has all these sensors
Motion _trafficMotion;
Communication _communication;
Light _light;
Temperature _weatherTemperature;
Humidity _weatherHumidity;
*/
public:
//helper functions
City manageCity(City _city);
vector<Streetlight> manageStreetlights(vector<Streetlight> _streetlights);
Streetlight manageLight(Streetlight _streetlight);
void showCityLightStatus(City _city);
};
TEMPERATURE.H
#pragma once
#include "weather.h"
class Temperature :
public Weather
{
public:
Temperature(void);
virtual ~Temperature(void);
public:
bool analyzeNdifferentiateWeatherConditions(string weatherdata);
double getWeather();
};
TRAFFIC.H
#pragma once
#include "_generic.h"
class Traffic
{
public:
Traffic(void);
virtual ~Traffic(void);
virtual double getTraffic() = 0;
public:
double data;
};
WEATHER.H
#pragma once
#include<string>
#include "_generic.h"
using namespace std;
class Weather
{
public:
Weather(void);
virtual ~Weather(void);
public:
double data;
public:
// the deriving class has to implement these
//analyzeNdifferentiateWeatherConditions
virtual bool analyzeNdifferentiateWeatherConditions(string weatherdata) = 0;
virtual double getWeather() = 0;
};
_GENERIC.CPP
#include "_generic.h"
/* generates a random number between min to max*/
int _generic::generateRandom(int min, int max)
{
int randNum = (rand() % (max-min)) + 1;
return randNum;
}
/* generates a random number between -range to +range*/
int _generic::generateRandom(int range)
{
int max = (2 * range)+2;
int min = 1;
int randNum = (rand() % (max-min))+1;
randNum = randNum-range-1;
return randNum;
}
void _generic::display_data(int data, string datatype)
{
if (datatype == "traffic")
{
printf(" ");
if ((int)data== -1)
{
printf("TRAFFIC=NONE");
}
else if ((int)data == 0)
{
printf("TRAFFIC=EXTREMELY LESS");
}
else if ((int)data == 1)
{
printf("TRAFFIC=PRESENT");
}
}
else if (datatype=="humidity")
{
printf(" ");
if ((int)data == -1)
{
printf("HUMIDITY=BAD");
}
else if ((int)data == 0)
{
printf("HUMIDITY=OK");
}
else if ((int)data == 1)
{
printf("HUMIDITY=GOOD");
}
}
else if (datatype=="temperature")
{
printf(" ");
if ((int)data == -1)
{
printf("TEMPERATURE=BAD");
}
else if ((int)data == 0)
{
printf("TEMPERATURE=OK");
}
else if ((int)data == 1)
{
printf("TEMPERATURE=GOOD");
}
}
else if (datatype == "light")
{
printf(" ");
if ((int)data== -1)
{
printf("SUNLIGHT=NONE");
}
else if ((int)data == 0)
{
printf("SUNLIGHT=LESS");
}
else if ((int)data == 1)
{
printf("SUNLIGHT=GOOD");
}
}
};//end display_data
CITY.CPP
#include "City.h"
City::City(string cityname)
{
_cityname = cityname;
//create random number of 'test' streelights for the city
int totalstreetlights = _generic::generateRandom(1,10) ;
Streetlight *_streetlight;
for(int i =0;i<totalstreetlights;i++)
{
_streetlight = new Streetlight();
_streetlights.push_back(*_streetlight);
}
}
City::~City(void)
{
}
COMMUNICATION.CPP
#include "Communication.h"
Communication::Communication(void)
{
}
Communication::~Communication(void)
{
}
void Communication::setCommunication()
{
;
}
GPS.CPP
#include "GPS.h"
GPS::GPS(void)
{
data[0] = -1;
data[1] = -1;
}
GPS::~GPS(void)
{
}
double GPS::getGPS()
{
return -1;
}
HUMIDITY.CPP
#include "Humidity.h"
Humidity::Humidity(void)
{
data=-1;
}
Humidity::~Humidity(void)
{
}
bool Humidity::analyzeNdifferentiateWeatherConditions(string weatherdata)
{
return true;
}
double Humidity::getWeather()
{
data = _generic::generateRandom(1);
//_generic::display_data(data,"humidity");
return data;
}
LIGHT.CPP
#include "Light.h"
Light::Light(void)
{
data=-1;
}
Light::~Light(void)
{
}
double Light::getLight(void)
{
data = _generic::generateRandom(1);
//_generic::display_data(data,"light");
return data;
}
MOTION.CPP
#include "Motion.h"
Motion::Motion(void)
{
data=-1;
}
Motion::~Motion(void)
{
}
double Motion::getTraffic()
{
data = _generic::generateRandom(1);
//_generic::display_data(data,"traffic");
return data;
}
SENSORS.CPP
#include "Sensors.h"
Sensors::Sensors(void)
{
}
Sensors::~Sensors(void)
{
}
STREETLIGHT.CPP
#include "Streetlight.h"
Streetlight::Streetlight(void)
{
ledstatus = _generic::led_OFF; //keep it off by default
}
Streetlight::~Streetlight(void)
{
}
double Streetlight::getStreetlight()
{
//returning the current led status of streetlight
return ledstatus;
}
STREETLIGHTCONTROLLER.CPP
#include "StreetLightController.h"
#include <iostream>
using namespace std;
StreetLightController::StreetLightController(void)
{
}
StreetLightController::~StreetLightController(void)
{
}
//Reduce Light Pollution
//Reduce Energy Consumption
//Keep roads safe
//Keep roads visible
City StreetLightController::manageCity(City _city)
{
//manage streetlights
_city._streetlights = manageStreetlights(_city._streetlights);
return _city;
}
vector<Streetlight> StreetLightController::manageStreetlights(vector<Streetlight> _streetlights)
{
for(std::vector<City>::size_type i = 0; i != _streetlights.size(); i++) {
_streetlights[i] = manageLight(_streetlights[i]);
}
return _streetlights;
}
Streetlight StreetLightController::manageLight(Streetlight _streetlight)
{
getStreetLight(&_streetlight);
return _streetlight;
}
double StreetLightController::getStreetLight(Streetlight * _streetlight)
{
double dWeatherCondition=-1;
double dLightCondition=-1;
double dTrafficCondition=-1;
int ledstatus = _generic::led_OFF; //off by default
dWeatherCondition = getWeather(&(_streetlight->_sensors));
dLightCondition = getLight(&(_streetlight->_sensors));
dTrafficCondition = getTraffic(&(_streetlight->_sensors));
//if there is some traffic. 0 is extremely less traffic
if ((int)dTrafficCondition == 1 || (int)dTrafficCondition == 0)
{
//traffic is on. so light has to be on or dim
//if there is no sunlight light has to be on
if ((int)dLightCondition == -1)
{
ledstatus = _generic::led_ON;
}
else if ((int)dLightCondition >=0 )
{
// if there is some sunlight or lot of sunlight
//if weather conditions are good then light has to be dim
if ((int)dWeatherCondition == 1)
{
ledstatus = _generic::led_DIM;
}
else
{
//in bad weather conditions light has to be on
ledstatus = _generic::led_ON;
}
}
else
{
//in sunlight, make sure the light is off
ledstatus = _generic::led_OFF;
}
}
else
{
//no traffic. means light has to be off
ledstatus = _generic::led_OFF;
}
/*if (ledstatus == _generic::led_ON)
{
printf (" LED ON");
}
else if (ledstatus == _generic::led_OFF)
{
printf (" LED OFF");
}
else if (ledstatus == _generic::led_DIM)
{
printf (" LED DIM");
}*/
_streetlight->ledstatus = ledstatus;
return ledstatus;
}
double StreetLightController::getWeather(Sensors * _sensors)
{
double humiditydata, temperaturedata;
double weatherdata = -1;
humiditydata = _sensors->_humidity.getWeather();
_sensors->_humidity.data = humiditydata;
temperaturedata =_sensors->_temperature.getWeather();
_sensors->_temperature.data = temperaturedata ;
if (temperaturedata == 1 && humiditydata == 1)
{
weatherdata = 1;
}
else if (temperaturedata == 0 && humiditydata == 0)
{
weatherdata = 0;
}
return weatherdata;
}
double StreetLightController::getLight(Sensors * _sensors)
{
double data;
data = _sensors->_light.getLight();
_sensors->_light.data = data;
return data;
}
double StreetLightController::getTraffic(Sensors * _sensors)
{
double data;
data = _sensors->_motion.getTraffic();
_sensors->_motion.data = data;
return data;
}
void StreetLightController::setCommunication()
{
;
}
void StreetLightController::showCityLightStatus(City _city)
{
double humiditydata,lightdata,motiondata,temperaturedata;
int ledstatus;
string sledstatus;
printf(" ");
cout << "CITY " << _city._cityname;
printf(" _______________________");
for(std::vector<City>::size_type i = 0; i != _city._streetlights.size(); i++) {
Streetlight _streetlight = _city._streetlights[i];
printf(" ");
printf("Street light #%d",i+1);
printf(" _______________________");
humiditydata = _streetlight._sensors._humidity.data ;
lightdata = _streetlight._sensors._light.data;
motiondata = _streetlight._sensors._motion.data ;
temperaturedata = _streetlight._sensors._temperature.data ;
if (_streetlight.ledstatus == _generic::led_ON)
{
sledstatus = "ON";
}
else if (_streetlight.ledstatus == _generic::led_OFF)
{
sledstatus = "OFF";
}
else if (_streetlight.ledstatus == _generic::led_DIM)
{
sledstatus = "DIM";
}
printf(" ");
_generic::display_data(humiditydata,"humidity");
_generic::display_data(temperaturedata,"temperature");
_generic::display_data(lightdata,"light");
_generic::display_data(motiondata,"traffic");
cout << " LED=" << sledstatus;
}
}
TEMPERATURE.CPP
#include "Temperature.h"
Temperature::Temperature(void)
{
data=-1;
}
Temperature::~Temperature(void)
{
}
bool Temperature::analyzeNdifferentiateWeatherConditions(string weatherdata)
{
return true;
}
double Temperature::getWeather()
{
data = _generic::generateRandom(1);
//_generic::display_data(data,"temperature");
return data;
}
TRAFFIC.CPP
#include "Traffic.h"
Traffic::Traffic(void)
{
}
Traffic::~Traffic(void)
{
}
WEATHER.CPP
#include "Weather.h"
Weather::Weather(void)
{
}
Weather::~Weather(void)
{
}
OUTPUT
D:UsersjdsDocumentsVisual Studio 2010ProjectsStreetLightManagementDebug>S
treetLightManagement
CITY MUMBAI
_______________________
Street light #1
_______________________
HUMIDITY=OK
TEMPERATURE=OK
SUNLIGHT=GOOD
TRAFFIC=EXTREMELY LESS
LED=ON
Street light #2
_______________________
HUMIDITY=BAD
TEMPERATURE=BAD
SUNLIGHT=LESS
TRAFFIC=PRESENT
LED=ON
Street light #3
_______________________
HUMIDITY=GOOD
TEMPERATURE=GOOD
SUNLIGHT=LESS
TRAFFIC=NONE
LED=OFF
Street light #4
_______________________
HUMIDITY=OK
TEMPERATURE=GOOD
SUNLIGHT=LESS
TRAFFIC=PRESENT
LED=ON
Street light #5
_______________________
HUMIDITY=BAD
TEMPERATURE=BAD
SUNLIGHT=NONE
TRAFFIC=NONE
LED=OFF
Street light #6
_______________________
HUMIDITY=GOOD
TEMPERATURE=BAD
SUNLIGHT=LESS
TRAFFIC=EXTREMELY LESS
LED=ON
CITY NEWYORK
_______________________
Street light #1
_______________________
HUMIDITY=BAD
TEMPERATURE=GOOD
SUNLIGHT=GOOD
TRAFFIC=PRESENT
LED=ON
Street light #2
_______________________
HUMIDITY=GOOD
TEMPERATURE=BAD
SUNLIGHT=GOOD
TRAFFIC=NONE
LED=OFF
Street light #3
_______________________
HUMIDITY=BAD
TEMPERATURE=OK
SUNLIGHT=GOOD
TRAFFIC=EXTREMELY LESS
LED=ON
Street light #4
_______________________
HUMIDITY=OK
TEMPERATURE=BAD
SUNLIGHT=GOOD
TRAFFIC=NONE
LED=OFF
Street light #5
_______________________
HUMIDITY=GOOD
TEMPERATURE=BAD
SUNLIGHT=NONE
TRAFFIC=PRESENT
LED=ON
Street light #6
_______________________
HUMIDITY=BAD
TEMPERATURE=OK
SUNLIGHT=GOOD
TRAFFIC=EXTREMELY LESS
LED=ON
Street light #7
_______________________
HUMIDITY=GOOD
TEMPERATURE=GOOD
SUNLIGHT=LESS
TRAFFIC=NONE
LED=OFF
Street light #8
_______________________
HUMIDITY=BAD
TEMPERATURE=OK
SUNLIGHT=GOOD
TRAFFIC=NONE
LED=OFF
Street light #9
_______________________
HUMIDITY=OK
TEMPERATURE=OK
SUNLIGHT=LESS
TRAFFIC=PRESENT
LED=ON
D:UsersjdsDocumentsVisual Studio 2010ProjectsStreetLightManagementDebug>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.