Professional Documents
Culture Documents
Er
Er
#include <cstring>
#include <vector>
#include <algorithm>
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;
}
public:
Appliance(const char* n, float p, double w, double pow, Color col, int ts)
: AbstractAppliance(n, p, w, pow, col), temperatureSetting(ts) {}
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 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;
}
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 << "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);
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;
}
}
}
}