Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 10

#include <iostream>

#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

enum Color { BLACK, GRAY, BLUE, PINK };

class AbstractAppliance {
protected:
char* name;
float price;
double weight;
double power;
Color color;

public:
AbstractAppliance(const char* n, float p, double w, double pow, Color col)
: price(p), weight(w), color(col), power(pow)
{
name = new char[strlen(n) + 1];
strcpy_s(name, strlen(n) +1, n);
}

virtual ~AbstractAppliance() {
delete[] name;
}

char* getName() {
return name;
}

float getPrice() {
return price;
}

double getPower() {
return power;
}

virtual void work() = 0;

virtual void print() = 0;


};

class Appliance : public AbstractAppliance {


protected:
int temperatureSetting;
static int operatingVoltage;
static int maxTemperature;
static int minTemperature;

public:
Appliance(const char* n, float p, double w, double pow, Color col, int ts)
: AbstractAppliance(n, p, w, pow, col), temperatureSetting(ts) {}

void work() override {


if (temperatureSetting < maxTemperature && temperatureSetting >
minTemperature) {
cout << "Working..." << endl;
return;
}
cout << "Cannot work. Appliance is not ready." << endl;
}

void setTemperature(int newTemp) {


if (newTemp < minTemperature || newTemp > maxTemperature) {
cout << "Invalid temperature setting. Temperature not changed." <<
endl;
}
else {
temperatureSetting = newTemp;
cout << "Temperature set to " << newTemp << " degrees." << endl;
}
}

void print() override {


cout << "Name: " << name << endl;
cout << "Price: " << price << endl;
cout << "Weight: " << weight << endl;
cout << "Power: " << power << endl;
cout << "Color: ";
switch (color) {
case Color::BLACK:
cout << "Black" << endl;
break;
case Color::GRAY:
cout << "Gray" << endl;
break;
case Color::BLUE:
cout << "Blue" << endl;
break;
case Color::PINK:
cout << "Pink" << endl;
break;
default:
cout << "Unknown" << endl;
}
cout << "Temperature Setting: " << temperatureSetting << endl;
cout << "Operating Voltage: " << operatingVoltage << "V" << endl;
cout << "Minimum Temperature: " << minTemperature << " degrees" << endl;
cout << "Maximum Temperature: " << maxTemperature << " degrees" << endl;
}

static void setOperatingVoltage(int ov) {


operatingVoltage = ov;
cout << "Operating voltage set to " << ov << "V." << endl;
}

static int getOperatingVoltage() {


return operatingVoltage;
}

static void setMinTemperature(int mnt) {


minTemperature = mnt;
cout << "Minimum temperature set to " << mnt << " degrees." << endl;
}
static int getMinTemperature() {
return minTemperature;
}

static void setMaxTemperature(int mxt) {


maxTemperature = mxt;
cout << "Maximum temperature set to " << mxt << " degrees." << endl;
}

static int getMaxTemperature() {


return maxTemperature;
}
};

enum KettleMode { KEEP_WARM, BOILING };

class Kettle : public Appliance {


private:
double maxWaterLevel;
double currentWaterLevel;
bool isBoiling;
KettleMode mode;

public:
Kettle(const char* n, float p, double w, double pow, Color col, int ts, double
maxWL)
: Appliance(n, p, w, pow, col, ts), maxWaterLevel(maxWL),
currentWaterLevel(0.0),
isBoiling(false), mode(KEEP_WARM) {}

void fillWaterTank(double amount) {


if (amount <= 0) {
cout << "Invalid amount. \nWater level not changed." << endl;
} else if (currentWaterLevel + amount > maxWaterLevel) {
cout << "Cannot add water. \nTank already full." << endl;
} else {
currentWaterLevel += amount;
cout << "\nWater tank filled with " << amount << " ml of water." <<
endl;
}
}

void startBoiling() {
if (currentWaterLevel <= 0 || temperatureSetting > maxTemperature ||
temperatureSetting < minTemperature) {
cout << "Cannot start boiling. Water tank is empty." << endl;
} else {
cout << "Starting to boil water..." << endl;
isBoiling = true;
mode = BOILING;
cout << "Water is now boiling." << endl;
}
}

void stopBoiling() {
cout << "Stopping boiling..." << endl;
isBoiling = false;
mode = KEEP_WARM;
cout << "Boiling stopped." << endl;
}

void print() override {


Appliance::print();
cout << "Maximum Water Level: " << maxWaterLevel << " ml" << endl;
cout << "Current Water Level: " << currentWaterLevel << " ml" << endl;
cout << "Is Boiling: " << (isBoiling ? "Yes" : "No") << endl;
cout << "Mode: " << (mode == KEEP_WARM ? "Keep Warm" : "Boiling") << endl;
}
};

int Appliance::operatingVoltage = 220;


int Appliance::maxTemperature = 200;
int Appliance::minTemperature = 100;

int main() {
// int newVoltage;
// int newMinTemp;
// int newMaxTemp;
//
// cout << "Enter the operating voltage:";
// cin >> newVoltage;
// Appliance::setOperatingVoltage(newVoltage);
//
// cout << "Enter the minimum temperature:";
// cin >> newMinTemp;
// Appliance::setMinTemperature(newMinTemp);
//
// cout << "Enter the maximum temperature:";
// cin >> newMaxTemp;
// Appliance::setMaxTemperature(newMaxTemp);

vector<Kettle*> kettleList;

while (true) {
cout << "\nAvailable operations:\n";
cout << "1. Add a Kettle\n";
cout << "2. Print\n";
cout << "3. Work with a Kettle\n";
cout << "4. Sort Kettles by Name\n";
cout << "5. Search Kettles\n";
cout << "6. Exit\n";

int choice;
cout << "\nEnter your choice:";
cin >> choice;

switch (choice) {
case 1: {
char name[50];
float price;
double weight, power;
Color color;
double capacity;
int temperatureSetting;

cout << "\nEnter details for the Kettle:\n";


cout << "Name:";
cin.ignore();
cin.getline(name, 50);

cout << "Price:";


while (!(cin >> price) || price <= 0) {
cout << "Invalid price. \nPlease enter a valid positive
price:";
cin.clear();
cin.ignore(1000, '\n');
}

cout << "Weight:";


while (!(cin >> weight) || weight <= 0) {
cout << "Invalid weight. \nPlease enter a valid positive
weight:";
cin.clear();
cin.ignore(1000, '\n');
}

cout << "Power:";


while (!(cin >> power) || power <= 0) {
cout << "Invalid power. \nPlease enter a valid positive
power:";
cin.clear();
cin.ignore(1000, '\n');
}

cout << "Color (0 for Black, 1 for Gray, \n2 for Blue, 3 for
Pink):";
int input_color;
while (!(cin >> input_color) || input_color < 0 || input_color > 3)
{
cout << "Invalid color. \nPlease enter 0 for Black, 1 for Gray,
2 for Blue, or 3 for Pink:";
cin.clear();
cin.ignore(1000, '\n');
}
color = Color(input_color);

cout << "Capacity:";


while (!(cin >> capacity) || capacity <= 0) {
cout << "Invalid capacity. \nPlease enter a valid positive
capacity:";
cin.clear();
cin.ignore(1000, '\n');
}

cout << "Temperature Setting:";


while (!(cin >> temperatureSetting) || temperatureSetting <
Appliance::getMinTemperature() ||
temperatureSetting > Appliance::getMaxTemperature()) {
cout << "Invalid temperature setting. \nPlease enter a value
between "
<< Appliance::getMinTemperature()
<< " and " << Appliance::getMaxTemperature() << ":";
cin.clear();
cin.ignore(1000, '\n');
}
int position;
cout << "Enter the position to insert (1 for beginning, \n2 for
end, 3 for a specific position): ";
cin >> position;

switch (position) {
case 1:
kettleList.insert(kettleList.begin(), new Kettle(name,
price, weight, power,
color,
temperatureSetting, capacity));
break;
case 2:
kettleList.push_back(new Kettle(name, price, weight, power,
color, temperatureSetting,
capacity));
break;
default: {
int index;
cout << "Enter the specific position (1 - " <<
kettleList.size() + 1 << "):";
while (!(cin >> index) || index < 1 || index >
kettleList.size() + 1) {
cout << "Invalid position. Please enter a value between
1 and " << kettleList.size() + 1
<< ":";
cin.clear();
cin.ignore(1000, '\n');
}
kettleList.insert(kettleList.begin() + index - 1, new
Kettle(name, price, weight, power,

color, temperatureSetting,

capacity));
}
}
cout << "Kettle added successfully!" << endl;
break;
}

case 2: {
if (kettleList.empty()) {
cout << "No kettles created" << endl;
} else {
int applianceIndex;
cout << endl;
for (int i = 0; i < kettleList.size(); i++) {
cout << "Name of appliance " << i+1 << ": " <<
kettleList[i]->getName() << endl;
}
cout << "Enter the number of the appliance to print" << "(1-"
<< kettleList.size() <<"):";
cin >> applianceIndex;
applianceIndex--;
if (applianceIndex >= 0 && applianceIndex < kettleList.size())
{
if (applianceIndex >= 0 && applianceIndex <
kettleList.size()) {
kettleList[applianceIndex]->print();
} else {
cout << "Invalid kettle number." << endl;
}
}
}
break;
}

case 3: {
if (kettleList.empty()) {
cout << "No kettles created" << endl;
} else {
cout << "\nAvailable functions:\n";
cout << "1. Work\n";
cout << "2. Set Temperature\n";
cout << "3. Fill Water Tank\n";
cout << "4. Start Boiling\n";
cout << "5. Stop Boiling\n";
cout << "6. Get Number of Kettles\n";

int workChoice;
cout << "\nEnter your choice:";
cin >> workChoice;

switch (workChoice) {
case 1: {
int kettleIndex;
cout << endl;
for (int i = 0; i < kettleList.size(); i++) {
cout << "Name of kettle " << i + 1 << ": " <<
kettleList[i]->getName() << endl;
}
cout << "Enter the number of the kettle to work" <<
"(1-" << kettleList.size() << "):";
cin >> kettleIndex;
kettleIndex--;
if (kettleIndex >= 0 && kettleIndex <
kettleList.size()) {
kettleList[kettleIndex]->work();
} else {
cout << "Invalid kettle number." << endl;
}
break;
}

case 2: {
if (kettleList.empty()) {

}
int applianceIndex;
cout << endl;
for (int i = 0; i < kettleList.size(); i++) {
cout << "Name of appliance " << i+1 << ": " <<
kettleList[i]->getName() << endl;
}
cout << "Enter the number of the appliance to print" <<
"(1-" << kettleList.size() <<"):";
cin >> applianceIndex;
applianceIndex--;
if (applianceIndex >= 0 && applianceIndex <
kettleList.size()) {
if (applianceIndex >= 0 && applianceIndex <
kettleList.size()) {
kettleList[applianceIndex]->print();
} else {
cout << "Invalid kettle number." << endl;
}
break;
}
}

case 3: {
int kettleIndex;
double amount;
cout << endl;
for (int i = 0; i < kettleList.size(); i++) {
cout << "Name of kettle " << i + 1 << ": " <<
kettleList[i]->getName() << endl;
}
cout << "Enter the number of the kettle to fill water
tank" << "(1-"
<< kettleList.size()
<< "):";
cin >> kettleIndex;
kettleIndex--;
if (kettleIndex >= 0 && kettleIndex <
kettleList.size()) {
cout << "Enter amount of water to fill water
tank:";
cin >> amount;
kettleList[kettleIndex]->fillWaterTank(amount);
} else {
cout << "Invalid kettle number." << endl;
}
break;
}

case 4: {
int kettleIndex;
cout << endl;
for (int i = 0; i < kettleList.size(); i++) {
cout << "Name of kettle " << i + 1 << ": " <<
kettleList[i]->getName() << endl;
}
cout << "Enter the number of the kettle to start
boiling" << "(1-" << kettleList.size()
<< "):";
cin >> kettleIndex;
kettleIndex--;
if (kettleIndex >= 0 && kettleIndex <
kettleList.size()) {
kettleList[kettleIndex]->startBoiling();
} else {
cout << "Invalid kettle number." << endl;
}
break;
}
case 5: {
int kettleIndex;
cout << endl;
for (int i = 0; i < kettleList.size(); i++) {
cout << "Name of kettle " << i + 1 << ": " <<
kettleList[i]->getName() << endl;
}
cout << "Enter the number of the kettle to stop
boiling" << "(1-" << kettleList.size()
<< "):";
cin >> kettleIndex;
kettleIndex--;
if (kettleIndex >= 0 && kettleIndex <
kettleList.size()) {
kettleList[kettleIndex]->stopBoiling();
} else {
cout << "Invalid kettle number." << endl;
}
break;
}

case 6:
cout << "Number of Kettles:" << kettleList.size() <<
endl;
break;

default: {
cout << "Invalid choice. Please try again." << endl;
}
}
}
break;
}

case 4: {
if (kettleList.empty()) {
cout << "No kettles created" << endl;
} else {
int sortChoice;
cout << "How would you like to sort the kettles?\n";
cout << "1. By Name\n";
cout << "2. By Price\n";
cout << "3. By Power\n";
cout << "Enter your choice:";
cin >> sortChoice;

switch (sortChoice) {
case 1:
sort(kettleList.begin(), kettleList.end(), [](Kettle
*a, Kettle *b) {
return strcmp(a->getName(), b->getName()) < 0;
});
cout << "Kettles sorted by name." << endl;
break;
case 2:
sort(kettleList.begin(), kettleList.end(), [](Kettle
*a, Kettle *b) {
return a->getPrice() < b->getPrice();
});
cout << "Kettles sorted by price." << endl;
break;
case 3:
sort(kettleList.begin(), kettleList.end(), [](Kettle
*a, Kettle *b) {
return a->getPower() < b->getPower();
});
cout << "Kettles sorted by power." << endl;
break;
default:
cout << "Invalid choice. Kettles will not be sorted."
<< endl;
break;
}
}
break;
}

case 5: {
if (kettleList.empty()) {
cout << "No kettles created" << endl;
} else {
char searchName[50];
cout << "Enter the name to search for: ";
cin.ignore();
cin.getline(searchName, 50);
bool found = false;
for (auto kettle : kettleList) {
if (strcmp(kettle->getName(), searchName) == 0) {
kettle->print();
found = true;
}
}
if (!found) {
cout << "Kettle with name \"" << searchName << "\" not
found." << endl;
}
}
break;
}

case 6: {
cout << "Exiting..." << endl;
for (auto kettle: kettleList) {
delete kettle;
}
return 0;
}

default: {
cout << "Invalid choice. Please try again." << endl;
}
}
}
}

You might also like