Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //GSM.h
- #pragma once
- class GSM
- {
- private:
- unsigned long SKU;
- char *brand;
- char *model;
- float price;
- unsigned int count;
- bool dualSIM;
- public:
- GSM();
- GSM(unsigned long SKU,
- const char *brand,
- const char *model,
- float price,
- unsigned int count,
- bool dualSIM);
- GSM(const GSM &m);
- unsigned long getSKU() const;
- void setSKU(unsigned long SKU);
- char *getBrand() const;
- void setBrand(const char *brand);
- char *getModel() const;
- void setModel(const char *model);
- float getPrice() const;
- void setPrice(float price);
- unsigned int getCount() const;
- void setCount(unsigned int count);
- bool getDualSIM() const;
- void setDualSIM(bool dualSIM);
- ~GSM();
- };
- ----------------------------------------------------------------------
- //store.h
- #pragma once
- #include "GSM.h"
- class Store
- {
- private:
- GSM **listGSM;
- size_t countGSM;
- size_t maxCountGSM;
- size_t IncrementCountGSM();
- public:
- Store();
- size_t getCountGSM() const;
- size_t getMaxCountGSM() const;
- void addGSM(const GSM &m);
- bool deleteGSM(size_t index);
- bool changeGSM(const GSM &m, size_t index);
- void displayGSM() const;
- ~Store();
- };
- --------------------------------------------------------------------------------
- //GSM.cpp
- #include "GSM.h"
- #include <iostream>
- #include <cstring>
- GSM::GSM() :SKU(0), price(0), count(0), dualSIM(false), brand(NULL), model(NULL)
- {
- setBrand("");
- setModel("");
- }
- GSM::GSM(unsigned long SKU,
- const char * brand,
- const char * model,
- float price,
- unsigned int count,
- bool dualSIM) : brand(NULL), model(NULL)
- {
- setSKU(SKU);
- setBrand(brand);
- setModel(model);
- setPrice(price);
- setCount(count);
- setDualSIM(dualSIM);
- }
- GSM::GSM(const GSM & m) :
- GSM(m.SKU,
- m.brand,
- m.model,
- m.price,
- m.count,
- m.dualSIM)
- {
- }
- unsigned long GSM::getSKU() const
- {
- return SKU;
- }
- void GSM::setSKU(unsigned long SKU)
- {
- this->SKU = SKU;
- }
- char * GSM::getBrand() const
- {
- return brand;
- }
- void GSM::setBrand(const char * brand)
- {
- if (this->brand) delete[] this->brand;
- size_t size = strlen(brand) + 1;
- this->brand = new char[size];
- strcpy_s(this->brand, size, brand);
- }
- char * GSM::getModel() const
- {
- return model;
- }
- void GSM::setModel(const char * model)
- {
- if (this->model)delete[] this->model;
- size_t size = strlen(model) + 1;
- this->model = new char[size];
- strcpy_s(this->model, size, model);
- }
- float GSM::getPrice() const
- {
- return price;
- }
- void GSM::setPrice(float price)
- {
- this->price = price;
- }
- unsigned int GSM::getCount() const
- {
- return count;
- }
- void GSM::setCount(unsigned int count)
- {
- this->count = count;
- }
- bool GSM::getDualSIM() const
- {
- return dualSIM;
- }
- void GSM::setDualSIM(bool dualSIM)
- {
- this->dualSIM = dualSIM;
- }
- GSM::~GSM()
- {
- delete[] brand;
- delete[] model;
- }
- ---------------------------------------------------------------------------
- //store.cpp
- #include "Store.h"
- #include <iostream>
- size_t Store::IncrementCountGSM()
- {
- countGSM++;
- return getCountGSM();
- }
- Store::Store() : countGSM(0), maxCountGSM(4)
- {
- listGSM = new GSM*[maxCountGSM];
- }
- size_t Store::getCountGSM() const
- {
- return countGSM;
- }
- size_t Store::getMaxCountGSM() const
- {
- return maxCountGSM;
- }
- void Store::addGSM(const GSM &m)
- {
- if (getCountGSM() >= getMaxCountGSM())
- {
- maxCountGSM *= 2;
- GSM** newListGSM = new GSM*[getMaxCountGSM()];
- for (size_t i = 0; i < getCountGSM(); i++)
- {
- newListGSM[i] = listGSM[i];
- }
- delete[] listGSM;
- listGSM = newListGSM;
- }
- GSM* pGSM = new GSM(m);
- listGSM[getCountGSM()] = pGSM;
- IncrementCountGSM();
- }
- bool Store::deleteGSM(size_t index)
- {
- if (index >= getCountGSM())return false;
- delete listGSM[index];
- countGSM--;
- for (size_t i = index; i < getCountGSM(); i++)
- {
- listGSM[i] = listGSM[i + 1];
- }
- return true;
- }
- bool Store::changeGSM(const GSM &m, size_t index)
- {
- if (index >= getCountGSM())
- {
- return false;
- }
- delete listGSM[index];
- listGSM[index] = new GSM(m);
- return true;
- }
- void Store::displayGSM() const
- {
- for (size_t i = 0; i < getCountGSM(); i++)
- {
- GSM* m = listGSM[i];
- std::cout << "GSM number: " << i << std::endl;
- std::cout << " brand: " << m->getBrand() << ' ';
- std::cout << "quantity: " << m->getCount() << ' ';
- std::cout << "dualSIM: " << m->getDualSIM() << ' ';
- std::cout << "model: " << m->getModel() << ' ';
- std::cout << "price: " << m->getPrice() << ' ';
- std::cout << "SKU: " << m->getSKU() << std::endl;
- }
- }
- Store::~Store()
- {
- for (size_t i = 0; i < getCountGSM(); i++)
- {
- delete listGSM[i];
- }
- delete[] listGSM;
- }
- ---------------------------------------------------------------------------
- //main.cpp
- #include "Store.h"
- #include "GSM.h"
- #include <iostream>
- int main()
- {
- Store s;
- s.addGSM({ 123456, "Nokia", "3310", 50.50, 12, false });
- s.addGSM({ 234598, "iPhone", "8", 950.50, 50, true });
- s.addGSM({ 897608, "Samsung", "galaxy", 850.50, 15, false });
- s.addGSM({ 876343, "iPhone", "6 Plus", 430.50, 3, true });
- while (1)
- {
- char command;
- std::cin >> command;
- if (command == 'Q')
- {
- break;
- }
- switch (command)
- {
- case 'A':
- {
- std::cout << "You are add to change GSM. ";
- unsigned long SKU;
- std::cout << "SKU : ";
- std::cin >> SKU;
- char brand[64];
- std::cout << "brand : ";
- std::cin >> brand;
- char model[64];
- std::cout << "model : ";
- std::cin >> model;
- float price;
- std::cout << "price : ";
- std::cin >> price;
- unsigned int count;
- std::cout << "count : ";
- std::cin >> count;
- bool dualSIM;
- std::cout << "dualSIM(true/false) : ";
- std::cin >> dualSIM;
- GSM m(SKU, brand, model, price, count, dualSIM);
- s.addGSM(m);
- break;
- }
- case 'X':
- {
- std::cout << "You are about to remove GSM. ";
- size_t index;
- std::cin >> index;
- s.deleteGSM(index);
- break;
- }
- case 'C':
- {
- std::cout << "You are about to change GSM. ";
- unsigned long SKU;
- std::cout << "SKU : ";
- std::cin >> SKU;
- char brand[64];
- std::cout << "brand : ";
- std::cin >> brand;
- char model[64];
- std::cout << "model : ";
- std::cin >> model;
- float price;
- std::cout << "price : ";
- std::cin >> price;
- unsigned int count;
- std::cout << "count : ";
- std::cin >> count;
- bool dualSIM;
- std::cout << "dualSIM(true/false) : ";
- std::cin >> dualSIM;
- size_t index;
- std::cout << "index : ";
- std::cin >> index;
- GSM m(SKU, brand, model, price, count, dualSIM);
- s.changeGSM(m, index);
- break;
- }
- case 'D':
- {
- std::cout << "Printing all GSMs";
- s.displayGSM();
- break;
- }
- case ' ':
- case '\n':
- break;
- default:
- std::cout << "invalid command" << std::endl;
- }
- }
- int x;
- std::cin >> x;
- }
- ---------------------------------------
- end. имаш два хедъра - GSM и store и съответно същите .cpp файла, както и един мейн. Всичко работи
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement