Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 28.77 KB | None | 0 0
  1. //Adam Staworowski
  2. //213B 2016/2017
  3. //Komis samochodowy
  4.  
  5. #include "stdafx.h"
  6. #include <iostream>
  7. #include <cstdio>
  8. #include <cstdlib>
  9. #include <ctime>
  10. #include <string>
  11.  
  12. using namespace std;
  13.  
  14. class Vehicle {
  15. private:
  16.     unsigned int price;
  17.     bool damaged;
  18. public:
  19.     friend class Workshop;
  20.     virtual void print() = 0;
  21.     virtual void printPrice() = 0;
  22.     virtual void initialization() = 0;
  23.     virtual string getMake() = 0;
  24.     virtual string getEngine() = 0;
  25.     virtual unsigned int getPrice() = 0;
  26.     virtual unsigned int getYear() = 0;
  27.     virtual bool getDamaged() = 0;
  28.     virtual void setMake(string m) = 0;
  29.     virtual void setEngine(string e) = 0;
  30.     virtual void setPrice(unsigned int p) = 0;
  31.     virtual void setYear(unsigned int y) = 0;
  32.     virtual void setDamaged(bool d) = 0;
  33. };
  34.  
  35. class Car :public Vehicle, public Engine
  36. {
  37. private:
  38.     string make;
  39.     string engine;
  40.     unsigned int price;
  41.     unsigned int year;
  42.     bool damaged;
  43. public:
  44.     friend class Workshop;
  45.     Car();
  46.     Car(string m, string e, unsigned int p, unsigned int y, bool d, unsigned int w);
  47.     Car(const Car& car);
  48.     ~Car();
  49.     void setMake(string m);
  50.     void setEngine(string e);
  51.     void setPrice(unsigned int p);
  52.     void setYear(unsigned int y);
  53.     void setDamaged(bool d);
  54.     string getMake();
  55.     string getEngine();
  56.     unsigned int getPrice();
  57.     unsigned int getYear();
  58.     bool getDamaged();
  59.  
  60.     friend ostream & operator<<(ostream& out, const Car& car);
  61.     friend istream & operator >> (istream& in, Car& car);
  62.     Car operator()(const Car& car);
  63.     void operator=(const Car& Car);
  64.     void operator->();
  65.     friend int operator+(const Car& car);
  66.     friend int operator-(const Car& car);
  67.  
  68.     void print();
  69.     void printPrice();
  70.     void initialization();
  71.  
  72. };
  73.  
  74. class Truck :public Car {
  75. private:
  76.     unsigned int capacity;
  77.     unsigned int totalWeight;
  78. public:
  79.     friend class Workshop;
  80.     Truck() :capacity(0), totalWeight(0), Car(){}
  81.     ~Truck();
  82.     void setCapacity(unsigned int c);
  83.     void setTotalWeight(unsigned int tw);
  84.     unsigned int getCapacity();
  85.     unsigned int getTotalWeight();
  86. };
  87.  
  88. class Motorcycle : public Car{
  89. private:
  90.     int cubicCapacity;
  91.     int HP;
  92. public:
  93.     friend class Workshop;
  94.     Motorcycle() :cubicCapacity(0), HP(0), Car(){}
  95.     ~Motorcycle();
  96.     void setCubicCapacity(unsigned int cC);
  97.     void setHP(int Hp);
  98.     unsigned int getCubicCapacity();
  99.     unsigned int getHP();
  100. };
  101.  
  102. class Engine {
  103. private:
  104.     string type;
  105.     unsigned int hp;
  106.     unsigned int fuelAmmount;
  107. public:
  108.     Engine();
  109.     Engine(string t, unsigned int hP, unsigned int fA);
  110.     ~Engine();
  111.     void fuelUp();
  112.     void startEngine();
  113. };
  114.  
  115. class Client {
  116. private:
  117.     string name;
  118.     int age;
  119.     unsigned int carAmmount;
  120. public:
  121.     friend class Workshop;
  122.     Client();
  123.     Client(string n, int a, unsigned int cA);
  124.     ~Client();
  125.     Vehicle *ownedVehicle[5];
  126.  
  127.     void setName(string n);
  128.     void setAge(int i);
  129.     string getName();
  130.     int getAge();
  131.  
  132.     void print();
  133.     void initialization();
  134.  
  135. };
  136.  
  137. class Workshop {
  138. private:
  139.     unsigned int vehicleCount;
  140.     unsigned int clientCount;
  141.     int balance;
  142.     Vehicle **myVehicle;
  143.     Client **myClient;
  144.     static Workshop *singleton;
  145. public:
  146.     class Mechanic {
  147.     private:
  148.         string name;
  149.         int age;
  150.     public:
  151.         Mechanic();
  152.         Mechanic(string n, int a);
  153.         ~Mechanic();
  154.         void repairVehicle(Vehicle*& vehicle);
  155.     };
  156.     static Workshop *getInstance() {
  157.         if (singleton == NULL) {
  158.             singleton = new Workshop;
  159.         }
  160.         return singleton;
  161.     }
  162.     Workshop();
  163.     Workshop(unsigned int cC, unsigned int clC, int b);
  164.     ~Workshop();
  165.     void viewClients();
  166.     void presentCars();
  167.     void addClient();
  168.     void addCar();
  169.     void sellCar();
  170.     void buyCar();
  171.     void repairCar();
  172. };
  173.  
  174. class MemoryManagement {
  175. public:
  176.     static void dynamicAllocation(Car*& car);
  177.     static void initialization(Car* car);
  178.     static void print(Car* car);
  179.     static void remove(Car*& car);
  180.     static void dynamicAllocation(Client*& client);
  181.     static void initialization(Client* client);
  182.     static void print(Client* client);
  183.     static void remove(Client*& client);
  184.     static void dynamicAllocation(Car*& car, int size);
  185.     static void initialization(Car* car, int size);
  186.     static void print(Car* car, int size);
  187.     static void remove(Car*& car, int size);
  188.     static void dynamicAllocation(Client*& client, int size);
  189.     static void initialization(Client* client, int size);
  190.     static void print(Client* client, int size);
  191.     static void remove(Client*& car, int size);
  192.     static void dynamicAllocation(Vehicle**& vehicle, int size);
  193.     static void initialization(Vehicle** vehicle, int size);
  194.     static void print(Vehicle** vehicle, int size);
  195.     static void remove(Vehicle** vehicle, int size);
  196.     static void dynamicAllocation(Client**& client, int size);
  197.     static void initialization(Client**& client, int size);
  198.     static void print(Client** client, int size);
  199.     static void remove(Client**& client, int size);
  200.  
  201.     static void addAtTheEnd(Car*& car, int size);
  202.     static void addAtTheEnd(Client*& client, int size);
  203.  
  204.     static void removeFromTheEnd(Car*& car, int size);
  205.     static void removeFromTheEnd(Client*& client, int size);
  206.  
  207.     static void addPtrToArray(Vehicle**& vehicle, int size);
  208.     static void addPtrToArray(Client**& client, int size);
  209.  
  210.     static void removePtrFromTheEnd(Car**& car, int size);
  211.     static void removePtrFromTheEnd(Client**& client, int size);
  212.  
  213.     static void addObjectAtNth(Car*& car, int size, int target);
  214.     static void addObjectAtNth(Client*& car, int size, int target);
  215.  
  216.     static void removeObjFromNth(Car*& car, int size, int target);
  217.     static void removeObjFromNth(Client*& client, int size, int target);
  218.  
  219.     static void addPtrToArray(Vehicle**& vehicle, int size, int target);
  220.     static void addPtrToArray(Client**& client, int size, int target);
  221.  
  222.     static void removePtrFromNth(Vehicle**& vehicle, int size, int target);
  223.     static void removePtrFromNth(Client**& client, int size, int target);
  224. };
  225.  
  226. string make[5] = { "volkswagen", "audi", "bmw", "opel", "mercedes" };
  227. string engine[2] = { "diesel", "petrol" };
  228.  
  229. int main()
  230. {
  231.     Workshop *WorkshopPtr = NULL;
  232.  
  233.     int czas;
  234.     czas = time(NULL);
  235.     srand(czas);
  236.    
  237.     WorkshopPtr = new Workshop;
  238.     WorkshopPtr->addCar();
  239.     int choice = 0;
  240.     cout << "Komis samochodowy v0.5" << endl;
  241.     cout << "wybierz jedna z dostepnych operacji" << endl;
  242.     do {
  243.         cout << "1. sprzedaj auto z naszej oferty" << endl;
  244.         cout << "2. kup auto od klienta" << endl;
  245.         cout << "3. wyswietl nasza oferte" << endl;
  246.         cout << "4. wyswietl liste kontrahentow " << endl;
  247.         cout << "5. napraw pojazd" << endl;
  248.         cout << "0. zakoncz" << endl;
  249.         cin >> choice;
  250.         switch (choice) {
  251.         case 1: {
  252.             WorkshopPtr->sellCar();
  253.             break;
  254.         }
  255.         case 2: {
  256.             WorkshopPtr->buyCar();
  257.             break;
  258.         }
  259.         case 3: {
  260.             WorkshopPtr->presentCars();
  261.             break;
  262.         }
  263.         case 4: {
  264.             WorkshopPtr->viewClients();
  265.             break;
  266.         }
  267.         case 5: {
  268.             WorkshopPtr->repairCar();
  269.             break;
  270.         }
  271.         }
  272.     } while (choice != 0);
  273.  
  274.     delete(WorkshopPtr);
  275.  
  276.     return 0;
  277. }
  278.  
  279. void MemoryManagement::dynamicAllocation(Car*& car) {
  280.     car = new Car;
  281. }
  282. void MemoryManagement::initialization(Car* car) {
  283.     /*
  284.     string make[5]{ "volkswagen", "audi", "bmw", "opel", "mercedes" };
  285.     string engine[2]{ "diesel", "petrol" };
  286.     string randomMake = make[(rand() % 4)];
  287.     string randomEngine = engine[rand() % 2];
  288.     unsigned int price = 1000 * (5 + rand() % 27);
  289.     unsigned int year = 1999 + rand() % 11;
  290.     bool damaged = rand() % 2;
  291.     car->setMake(randomMake);
  292.     car->setEngine(randomEngine);
  293.     car->setPrice(price);
  294.     car->setYear(year);
  295.     car->setDamaged(damaged);
  296.     */
  297.     car->initialization();
  298. }
  299. void MemoryManagement::print(Car* car) {
  300.     car->print();
  301. }
  302. void MemoryManagement::remove(Car*& car) {
  303.     delete car;
  304.     car = NULL;
  305. }
  306.  
  307. void MemoryManagement::dynamicAllocation(Client*& client) {
  308.     client = new Client;
  309. }
  310. void MemoryManagement::initialization(Client* client) {
  311.     /*
  312.     string engine[2]{ "diesel", "petrol" };
  313.     string randomEngine = engine[(rand() % 1)];
  314.     unsigned int price = 1000 * (5 + rand() % 27);
  315.     unsigned int priceRange = 1000 * (3 + rand() % 4);
  316.     bool damaged = rand() % 2;
  317.     client->setEngine(randomEngine);
  318.     client->setPrice(price);
  319.     client->setPriceRange(priceRange);
  320.     client->setDamaged(damaged);
  321.     */
  322.     client->initialization();
  323. }
  324. void MemoryManagement::print(Client* client) {
  325.     client->print();
  326. }
  327. void MemoryManagement::remove(Client*& client) {
  328.     delete client;
  329.     client = NULL;
  330. }
  331.  
  332. void MemoryManagement::dynamicAllocation(Car*& car, int size) {
  333.     car = new Car[size];
  334. }
  335. void MemoryManagement::initialization(Car* car, int size) {
  336.     for (int i = 0; i < size; i++) {
  337.         /*
  338.         string make[5]{ "volkswagen", "audi", "bmw", "opel", "mercedes" };
  339.         string engine[2]{ "diesel", "petrol" };
  340.         string randomMake = make[(rand() % 4)];
  341.         string randomEngine = engine[rand() % 2];
  342.         unsigned int price = 1000 * (5 + rand() % 27);
  343.         unsigned int year = 1999 + rand() % 11;
  344.         bool damaged = rand() % 2;
  345.         car[i].setMake(randomMake);
  346.         car[i].setEngine(randomEngine);
  347.         car[i].setPrice(price);
  348.         car[i].setYear(year);
  349.         car[i].setDamaged(damaged);
  350.         */
  351.         car[i].initialization();
  352.     }
  353. }
  354. void MemoryManagement::print(Car* car, int size) {
  355.     for (int i = 0; i<size; i++) {
  356.         car[i].print();
  357.     }
  358. }
  359. void MemoryManagement::remove(Car*& car, int size) {
  360.     delete[] car;
  361.     car = NULL;
  362. }
  363.  
  364. void MemoryManagement::dynamicAllocation(Client*& client, int size) {
  365.     client = new Client[size];
  366. }
  367. void MemoryManagement::initialization(Client* client, int size) {
  368.     for (int i = 0; i < size; i++) {
  369.         /*
  370.         string engine[2]{ "diesel", "petrol" };
  371.         string randomEngine = engine[(rand() % 1)];
  372.         unsigned int price = 1000 * (5 + rand() % 27);
  373.         unsigned int priceRange = 1000 * (3 + rand() % 4);
  374.         bool damaged = rand() % 2;
  375.         client[i].setEngine(randomEngine);
  376.         client[i].setPrice(price);
  377.         client[i].setPriceRange(priceRange);
  378.         client[i].setDamaged(damaged);
  379.         */
  380.         client[i].initialization();
  381.     }
  382. }
  383. void MemoryManagement::print(Client* client, int size) {
  384.     for (int i = 0; i<size; i++) {
  385.         client[i].print();
  386.     }
  387. }
  388. void MemoryManagement::remove(Client*& client, int size) {
  389.     delete[] client;
  390.     client = NULL;
  391. }
  392. void MemoryManagement::dynamicAllocation(Vehicle**&vehicle, int size) {
  393.     vehicle = new Vehicle*[size];
  394.     for (int i = 0; i< size; i++) {
  395.         vehicle[i] = new Car;
  396.     }
  397. }
  398. void MemoryManagement::initialization(Vehicle**vehicle, int size) {
  399.     for (int i = 0; i < size; i++) {
  400.         /*
  401.         string make[5]{ "volkswagen", "audi", "bmw", "opel", "mercedes" };
  402.         string engine[2]{ "diesel", "petrol" };
  403.         string randomMake = make[(rand() % 4)];
  404.         string randomEngine = engine[rand() % 2];
  405.         unsigned int price = 1000 * (5 + rand() % 27);
  406.         unsigned int year = 1999 + rand() % 11;
  407.         bool damaged = rand() % 2;
  408.         car[i]->setMake(randomMake);
  409.         car[i]->setEngine(randomEngine);
  410.         car[i]->setPrice(price);
  411.         car[i]->setYear(year);
  412.         car[i]->setDamaged(damaged);
  413.         */
  414.         vehicle[i]->initialization();
  415.     }
  416. }
  417. void MemoryManagement::print(Vehicle**vehicle, int size) {
  418.     for (int i = 0; i<size; i++) {
  419.         vehicle[i]->print();
  420.     }
  421. }
  422. void MemoryManagement::remove(Vehicle**vehicle, int size) {
  423.     for (int i = 0; i<size; i++)
  424.         delete vehicle[i];
  425.     delete[] vehicle;
  426. }
  427.  
  428. void MemoryManagement::dynamicAllocation(Client**&client, int size) {
  429.     client = new Client*[size];
  430.     for (int i = 0; i< size; i++) {
  431.         client[i] = new Client;
  432.     }
  433. }
  434. void MemoryManagement::initialization(Client**&client, int size) {
  435.     for (int i = 0; i < size; i++) {
  436.         /*
  437.         string engine[2]{ "diesel", "petrol" };
  438.         string randomEngine = engine[(rand() % 1)];
  439.         unsigned int price = 1000 * (5 + rand() % 27);
  440.         unsigned int priceRange = 1000 * (3 + rand() % 4);
  441.         bool damaged = rand() % 2;
  442.         client[i]->setEngine(randomEngine);
  443.         client[i]->setDamaged(damaged);
  444.         */
  445.         client[i]->initialization();
  446.     }
  447. }
  448. void MemoryManagement::print(Client**client, int size) {
  449.     for (int i = 0; i<size; i++) {
  450.         client[i]->print();
  451.     }
  452. }
  453. void MemoryManagement::remove(Client**& client, int size) {
  454.     for (int i = 0; i<size; i++)
  455.         delete client[i];
  456.     delete[] client;
  457. }
  458.  
  459. //1 dodaj obiekt na koncu dynamicznej tablicy obiektow w z czasie wykonania
  460. void MemoryManagement::addAtTheEnd(Car*& car, int size) {
  461.     int n = size;
  462.     Car* temp = NULL;
  463.     temp = new Car[size + 1];
  464.     for (int i = 0; i < size; i++) {
  465.         temp[i] = car[i];
  466.     }
  467.     delete[] car;
  468.  
  469.     string make[5] = { "volkswagen", "audi", "bmw", "opel", "mercedes" };
  470.     string engine[2] = { "diesel", "petrol" };
  471.     string randomMake = make[(rand() % 4)];
  472.     string randomEngine = engine[rand() % 2];
  473.     unsigned int price = 1000 * (5 + rand() % 27);
  474.     unsigned int year = 1999 + rand() % 11;
  475.     bool damaged = rand() % 2;
  476.     temp[n].setMake(randomMake);
  477.     temp[n].setEngine(randomEngine);
  478.     temp[n].setPrice(price);
  479.     temp[n].setYear(year);
  480.     temp[n].setDamaged(damaged);
  481.  
  482.     car = temp;
  483. }
  484. void MemoryManagement::addAtTheEnd(Client*&client, int size) {
  485.     int n = size;
  486.     Client* temp = NULL;
  487.     temp = new Client[size + 1];
  488.     for (int i = 0; i < size; i++) {
  489.         temp[i] = client[i];
  490.     }
  491.     delete[] client;
  492.  
  493.     temp[n].initialization();
  494.  
  495.     client = temp;
  496. }
  497.  
  498. //2 usun obiekt z konca tablicy
  499. void MemoryManagement::removeFromTheEnd(Car*& car, int size) {
  500.     Car* temp = NULL;
  501.     temp = new Car[size - 1];
  502.     for (int i = 0; i < size - 1; i++) {
  503.         temp[i] = car[i];
  504.     }
  505.     car = temp;
  506. }
  507. void MemoryManagement::removeFromTheEnd(Client*& client, int size) {
  508.     Client* temp = NULL;
  509.     temp = new Client[size - 1];
  510.     for (int i = 0; i < size - 1; i++) {
  511.         temp[i] = client[i];
  512.     }
  513.     client = temp;
  514. }
  515.  
  516. //3 dodaj wskaznik na obiekt do tablicy
  517. void MemoryManagement::addPtrToArray(Vehicle**& vehicle, int size) {
  518.     int n = size;
  519.     Vehicle** temp;
  520.     temp = new Vehicle*[size + 1];
  521.     for (int i = 0; i< size + 1; i++) {
  522.         temp[i] = new Car;
  523.  
  524.     }
  525.     for (int i = 0; i < size; i++) {
  526.         temp[i] = vehicle[i];
  527.     }
  528.     string make[5] = { "volkswagen", "audi", "bmw", "opel", "mercedes" };
  529.     string engine[2] = { "diesel", "petrol" };
  530.     string randomMake = make[(rand() % 4)];
  531.     string randomEngine = engine[rand() % 2];
  532.     unsigned int price = 1000 * (5 + rand() % 27);
  533.     unsigned int year = 1999 + rand() % 11;
  534.     bool damaged = rand() % 2;
  535.     temp[n]->setMake(randomMake);
  536.     temp[n]->setEngine(randomEngine);
  537.     temp[n]->setPrice(price);
  538.     temp[n]->setYear(year);
  539.     temp[n]->setDamaged(damaged);
  540.  
  541.     vehicle = temp;
  542. }
  543. void MemoryManagement::addPtrToArray(Client**& client, int size) {
  544.     int n = size;
  545.     Client** temp;
  546.     temp = new Client*[size + 1];
  547.     for (int i = 0; i< size + 1; i++) {
  548.         temp[i] = new Client;
  549.  
  550.     }
  551.     for (int i = 0; i < size; i++) {
  552.         temp[i] = client[i];
  553.     }
  554.  
  555.     temp[n]->initialization();
  556.  
  557.     client = temp;
  558. }
  559.  
  560. //4 usun wskaznik na obiekt z ostatniego miejsca tablicy wskaznikow
  561. void MemoryManagement::removePtrFromTheEnd(Car**& car, int size) {
  562.     int n = size;
  563.     Car** temp;
  564.     temp = new Car*[size - 1];
  565.     for (int i = 0; i< size - 1; i++) {
  566.         temp[i] = new Car;
  567.  
  568.     }
  569.     for (int i = 0; i < size - 1; i++) {
  570.         temp[i] = car[i];
  571.     }
  572.  
  573.  
  574.     car = temp;
  575. }
  576. void MemoryManagement::removePtrFromTheEnd(Client**& client, int size) {
  577.     int n = size;
  578.     Client** temp;
  579.     temp = new Client*[size - 1];
  580.     for (int i = 0; i< size - 1; i++) {
  581.         temp[i] = new Client;
  582.  
  583.     }
  584.     for (int i = 0; i < size - 1; i++) {
  585.         temp[i] = client[i];
  586.     }
  587.  
  588.  
  589.     client = temp;
  590. }
  591.  
  592. //5 wstaw obiekt do tablicy obiektow na wskazane misjce
  593. void MemoryManagement::addObjectAtNth(Car*& car, int size, int target) {
  594.     if (target > size) {
  595.         cout << "cel znajduje sie poza tablica, nie dodac";
  596.         return;
  597.     }
  598.     Car* temp = NULL;
  599.     temp = new Car[size + 1];
  600.     for (int i = 0; i < target - 1; i++) {
  601.         temp[i] = car[i];
  602.     }
  603.     string make[5] = { "volkswagen", "audi", "bmw", "opel", "mercedes" };
  604.     string engine[2] = { "diesel", "petrol" };
  605.     string randomMake = make[(rand() % 4)];
  606.     string randomEngine = engine[rand() % 2];
  607.     unsigned int price = 1000 * (5 + rand() % 27);
  608.     unsigned int year = 1999 + rand() % 11;
  609.     bool damaged = rand() % 2;
  610.     temp[target - 1].setMake(randomMake);
  611.     temp[target - 1].setEngine(randomEngine);
  612.     temp[target - 1].setPrice(price);
  613.     temp[target - 1].setYear(year);
  614.     temp[target - 1].setDamaged(damaged);
  615.  
  616.     for (int j = target; j < size + 1; j++) {
  617.         temp[j] = car[j - 1];
  618.     }
  619.     delete[]car;
  620.     car = temp;
  621. }
  622. void MemoryManagement::addObjectAtNth(Client*& client, int size, int target) {
  623.     if (target > size) {
  624.         cout << "cel znajduje sie poza tablica, nie dodac";
  625.         return;
  626.     }
  627.     Client* temp = NULL;
  628.     temp = new Client[size + 1];
  629.     for (int i = 0; i < target - 1; i++) {
  630.         temp[i] = client[i];
  631.     }
  632.  
  633.     temp[target - 1].initialization();
  634.  
  635.     for (int j = target; j < size + 1; j++) {
  636.         temp[j] = client[j - 1];
  637.     }
  638.     delete[]client;
  639.     client = temp;
  640. }
  641.  
  642. //6 usun obiekt ze wskazanego miejsca
  643. void MemoryManagement::removeObjFromNth(Car*& car, int size, int target) {
  644.     if (target>size) {
  645.         cout << "Nie mozna usunac; element znajduej sie poza tablcia" << endl;
  646.         return;
  647.     }
  648.     Car* temp = NULL;
  649.     temp = new Car[size - 1];
  650.  
  651.     for (int i = 0; i < target - 1; i++) {
  652.         temp[i] = car[i];
  653.     }
  654.  
  655.     for (int j = target; j < size; j++) {
  656.         temp[j - 1] = car[j];
  657.     }
  658.     delete[] car;
  659.     car = temp;
  660. }
  661. void MemoryManagement::removeObjFromNth(Client*& client, int size, int target) {
  662.     if (target>size) {
  663.         cout << "Nie mozna usunac; element znajduej sie poza tablcia" << endl;
  664.         return;
  665.     }
  666.     Client* temp = NULL;
  667.     temp = new Client[size - 1];
  668.  
  669.     for (int i = 0; i < target - 1; i++) {
  670.         temp[i] = client[i];
  671.     }
  672.  
  673.     for (int j = target; j < size; j++) {
  674.         temp[j - 1] = client[j];
  675.     }
  676.     delete[] client;
  677.     client = temp;
  678. }
  679.  
  680. //7 wstaw wskznik na obiekt dla wskazanego miejsca w tablicy wskaznkow na obiekt
  681. void MemoryManagement::addPtrToArray(Vehicle**& vehicle, int size, int target) {
  682.     if (target > size) {
  683.         cout << "cel znajduje sie poza tablica, nie mozna usunac";
  684.         return;
  685.     }
  686.     Vehicle** temp = NULL;
  687.     temp = new Vehicle*[size + 1];
  688.  
  689.     for (int i = 0; i < target - 1; i++) {
  690.         temp[i] = vehicle[i];
  691.     }
  692.  
  693.     string make[5] = { "volkswagen", "audi", "bmw", "opel", "mercedes" };
  694.     string engine[2] = { "diesel", "petrol" };
  695.     string randomMake = make[(rand() % 4)];
  696.     string randomEngine = engine[rand() % 2];
  697.     unsigned int price = 1000 * (5 + rand() % 27);
  698.     unsigned int year = 1999 + rand() % 11;
  699.     bool damaged = rand() % 2;
  700.     temp[target - 1]->setMake(randomMake);
  701.     temp[target - 1]->setEngine(randomEngine);
  702.     temp[target - 1]->setPrice(price);
  703.     temp[target - 1]->setYear(year);
  704.     temp[target - 1]->setDamaged(damaged);
  705.  
  706.     for (int j = target; j < size + 1; j++) {
  707.         temp[j] = vehicle[j - 1];
  708.     }
  709.     delete[]vehicle;
  710.     vehicle = temp;
  711. }
  712. void MemoryManagement::addPtrToArray(Client**& client, int size, int target) {
  713.     if (target > size) {
  714.         cout << "cel znajduje sie poza tablica, nie mozna usunac";
  715.         return;
  716.     }
  717.     Client** temp = NULL;
  718.     temp = new Client*[size + 1];
  719.     for (int i = 0; i < target - 1; i++) {
  720.         temp[i] = client[i];
  721.     }
  722.  
  723.     temp[target - 1]->initialization();
  724.  
  725.     for (int j = target; j < size + 1; j++) {
  726.         temp[j] = client[j - 1];
  727.     }
  728.     delete[]client;
  729.     client = temp;
  730.  
  731.  
  732. }
  733.  
  734. //8 usun wskaznik na obiekt ze wskazanego miejsca w tablicy wskaznikow na obiekt
  735. void MemoryManagement::removePtrFromNth(Vehicle**& vehicle, int size, int target) {
  736.     if (target>size) {
  737.         cout << "Nie mozna usunac; element znajduej sie poza tablcia" << endl;
  738.         return;
  739.     }
  740.     int n = size;
  741.     Vehicle** temp = NULL;
  742.     temp = new Vehicle*[size - 1];
  743.     for (int i = 0; i < target - 1; i++) {
  744.         temp[i] = vehicle[i];
  745.     }
  746.  
  747.     for (int j = target; j < size; j++) {
  748.         temp[j - 1] = vehicle[j];
  749.     }
  750.     delete[] vehicle;
  751.     vehicle = temp;
  752. }
  753. void MemoryManagement::removePtrFromNth(Client**& client, int size, int target) {
  754.     if (target>size) {
  755.         cout << "Nie mozna usunac; element znajduej sie poza tablcia" << endl;
  756.         return;
  757.     }
  758.     int n = size;
  759.     Client** temp = NULL;
  760.     temp = new Client*[size - 1];
  761.  
  762.     for (int i = 0; i < target - 1; i++) {
  763.         temp[i] = client[i];
  764.     }
  765.  
  766.     for (int j = target; j < size; j++) {
  767.         temp[j - 1] = client[j];
  768.     }
  769.  
  770.     delete[] client;
  771.     client = temp;
  772. }
  773.  
  774. Car::Car() {
  775.     make = "empty";
  776.     engine = "empty";
  777.     price = 0;
  778.     year = 0;
  779.     damaged = 0;
  780. }
  781. Car::Car(string m, string e, unsigned int p, unsigned int y, bool d, unsigned int w) {
  782.     make = m;
  783.     engine = e;
  784.     price = p;
  785.     year = y;
  786.     damaged = d;
  787. }
  788. Car::Car(const Car& car) {
  789.     make = car.make;
  790.     engine = car.engine;
  791.     price = car.price;
  792.     year = car.year;
  793.     damaged = car.damaged;
  794. };
  795.  
  796. Car::~Car() {
  797.  
  798. }
  799. Truck::~Truck() {
  800.  
  801. }
  802. Motorcycle::~Motorcycle() {
  803.  
  804. }
  805. void Motorcycle::setCubicCapacity(unsigned int cC) {
  806.     cubicCapacity = cC;
  807. }
  808. void Motorcycle::setHP(int HPp) {
  809.     HP = HPp;
  810. }
  811. unsigned int Motorcycle::getCubicCapacity() {
  812.     return cubicCapacity;
  813. }
  814. unsigned int Motorcycle::getHP() {
  815.     return cubicCapacity;
  816. }
  817. Client::Client() {
  818.     name = "empty";
  819.     age = 0;
  820.     carAmmount = 0;
  821. }
  822.  
  823. Client::Client(string n, int a, unsigned int cA) {
  824.     name = n;
  825.     age = a;
  826.     carAmmount = cA;
  827. }
  828. Client::~Client() {
  829.  
  830. }
  831. Workshop::Mechanic::Mechanic() {
  832.     name = "empty";
  833.     age = 0;
  834. }
  835. Workshop::Mechanic::Mechanic(string n, int a) {
  836.     name = n;
  837.     age = a;
  838. }
  839. Workshop::Mechanic::~Mechanic() {
  840.  
  841. }
  842. void Workshop::Mechanic::repairVehicle(Vehicle*&vehicle) {
  843.     if (vehicle->damaged == 0) {
  844.         cout << "pojazd nie jest uszkodzony!" << endl;
  845.     }
  846.     else {
  847.         cout << "czy chcesz naprawic pojazd?" << endl;
  848.         cout << "0.nie 1.tak" << endl;
  849.         bool choice;
  850.         cin >> choice;
  851.         if (choice == 0) return;
  852.         else {
  853.             cout << "naprawa bedzie kosztowac" << 1000 << endl;
  854.             cout << "czy chcesz dokonac naprawy?" << endl;
  855.             cout << "0.nie 1.tak" << endl;
  856.             bool choicee;
  857.             cin >> choicee;
  858.             if (choicee == 0)return;
  859.             else {
  860.                 vehicle->setDamaged(0);
  861.             }
  862.         }
  863.     }
  864. }
  865. Workshop::Workshop() {
  866.     vehicleCount = 0;
  867.     clientCount = 0;
  868.     balance = 50000;
  869. }
  870. Workshop::Workshop(unsigned int cC, unsigned int clC, int b) {
  871.     vehicleCount = cC;
  872.     clientCount = clC;
  873.     balance = b;
  874. }
  875. Workshop::~Workshop() {
  876.  
  877. }
  878. /*
  879. Car::Engine::Engine() {
  880. string type = "empty";
  881. unsigned int hp = 0;
  882. unsigned int fuelAmmount = 0;
  883. }
  884. Car::Engine::Engine(string t, unsigned int hP, unsigned int fA) {
  885. string type = t;
  886. unsigned int hp = hP;
  887. unsigned int fuelAmmount = fA;
  888. }
  889. Car::Engine::~Engine() {
  890.  
  891. }
  892. */
  893.  
  894. ostream& operator<<(ostream& out, const Car& car) {
  895.     out << car.make << "\t" << car.price << "\t" << car.year << "\t" << car.engine << "\t" << car.damaged;
  896.     return out;
  897. }
  898. istream& operator >> (istream& in, Car& car) {
  899.     cout << "marka, cena, rok produkcji, rodzaj silnika, uszkodzony?" << endl;
  900.     in >> car.make;
  901.     in >> car.price;
  902.     in >> car.year;
  903.     in >> car.engine;
  904.     in >> car.damaged;
  905.     return in;
  906. }
  907. Car Car::operator()(const Car& car) {
  908.     Car carb;
  909.     make = car.make;
  910.     engine = car.engine;
  911.     price = car.price;
  912.     year = car.year;
  913.     damaged = car.damaged;
  914.     return car;
  915. }
  916. void Car::operator=(const Car& car) {
  917.     make = car.make;
  918.     engine = car.engine;
  919.     price = car.price;
  920.     year = car.year;
  921.     damaged = car.damaged;
  922. }
  923. void Car::operator->() {
  924.     //TODO
  925. }
  926. int operator+(const Car& car) {
  927.     int suma = 0;
  928.     suma += car.price;
  929.     return suma;
  930. }
  931. int operator-(const Car& car) {
  932.     int roznica = 0;
  933.     roznica -= car.price;
  934.     return roznica;
  935. }
  936. void Car::setMake(string m) {
  937.     make = m;
  938. }
  939. void Car::setEngine(string m) {
  940.     engine = m;
  941. }
  942. void Car::setPrice(unsigned int p) {
  943.     price = p;
  944. }
  945. void Car::setYear(unsigned int y) {
  946.     year = y;
  947. }
  948. void Car::setDamaged(bool d) {
  949.     damaged = d;
  950. }
  951. string Car::getMake() {
  952.     return make;
  953. }
  954. string Car::getEngine() {
  955.     return engine;
  956. }
  957. unsigned int Car::getPrice() {
  958.     return price;
  959. }
  960. unsigned int Car::getYear() {
  961.     return year;
  962. }
  963. bool Car::getDamaged() {
  964.     return damaged;
  965. }
  966. void Truck::setCapacity(unsigned int c) {
  967.     capacity = c;
  968. }
  969. void Truck::setTotalWeight(unsigned int tw) {
  970.     totalWeight = tw;
  971. }
  972. unsigned int Truck::getCapacity() {
  973.     return capacity;
  974. }
  975. unsigned int Truck::getTotalWeight() {
  976.     return totalWeight;
  977. }
  978.  
  979. void Client::setName(string n) {
  980.     name = n;
  981. }
  982. void Client::setAge(int a) {
  983.     age = a;
  984. }
  985.  
  986. string Client::getName() {
  987.     return name;
  988. }
  989. int Client::getAge() {
  990.     return age;
  991. }
  992. void Car::print() {
  993.  
  994.     cout << make << "\t";
  995.     cout << engine << "\t";
  996.     cout << price << "\t";
  997.     cout << year << "\t";
  998.     cout << damaged << "\t";
  999.     cout << endl;
  1000.     cout << endl;
  1001. }
  1002. void Car::printPrice() {
  1003.     cout << price << endl;
  1004. }
  1005. void Car::initialization() {
  1006.     string make[5] = { "volkswagen", "audi", "bmw", "opel", "mercedes" };
  1007.     string engine[2] = { "diesel", "petrol" };
  1008.     string randomMake = make[(rand() % 4)];
  1009.     string randomEngine = engine[rand() % 2];
  1010.     Car::make = randomMake;
  1011.     Car::engine = engine[rand() % 2];
  1012.     price = 1000 * (5 + rand() % 27);
  1013.     year = 1999 + rand() % 11;
  1014.     damaged = rand() % 2;
  1015. }
  1016.  
  1017. void Client::print() {
  1018.     cout << name << "\t";
  1019.     cout << age << endl;
  1020. }
  1021. void Client::initialization() {
  1022.     cout << "podaj imie" << endl;
  1023.     cin >> name;
  1024.     cout << "podaj wiek" << endl;
  1025.     cin >> age;
  1026. }
  1027.  
  1028. void Workshop::addClient() {
  1029.     /*
  1030.     MemoryManagement::dynamicAllocation(myClient, 1);
  1031.     MemoryManagement::initialization(myClient, 1);
  1032.     */
  1033.     MemoryManagement::addPtrToArray(myClient, clientCount);
  1034.     //myClient[clientCount]->setName(name);
  1035.     clientCount++;
  1036.  
  1037. }
  1038. void Workshop::addCar() {
  1039.     cout << "podaj liczbe aut do dodania" << endl;
  1040.     cin >> vehicleCount;
  1041.     MemoryManagement::dynamicAllocation(myVehicle, vehicleCount);
  1042.     MemoryManagement::initialization(myVehicle, vehicleCount);
  1043.  
  1044. }
  1045. void Workshop::viewClients() {
  1046.     if (clientCount == 0) cout << "aktualnie nie mamy zadnych kontrahentow" << endl;
  1047.     for (int i = 0; i < clientCount; i++) {
  1048.         cout << i + 1 << "\t";
  1049.         myClient[i]->print();
  1050.     }
  1051.     cout << endl;
  1052. }
  1053. void Workshop::presentCars() {
  1054.     if (vehicleCount == 0) cout << "aktualnie nie mamy zadnych pojazdow w ofercie" << endl;
  1055.     for (int i = 0; i < vehicleCount; i++) {
  1056.         cout << i + 1 << "\t";
  1057.         myVehicle[i]->print();
  1058.     }
  1059. }
  1060. void Workshop::sellCar() {
  1061.     cout << "czy jestes juz w naszej bazie klientow? 0.nie 1.tak" << endl;
  1062.     int addingClient;
  1063.     cin >> addingClient;
  1064.     int current;
  1065.     if (addingClient == 0) {
  1066.         addClient();
  1067.         current = 1;
  1068.     }
  1069.     else {
  1070.         viewClients();
  1071.         cout << "podaj swoj nr ID" << endl;
  1072.         cin >> current;
  1073.     }
  1074.  
  1075.     if (vehicleCount == 0) {
  1076.         cout << "aktualnie nie mamy zadnych pojazdow w ofercie" << endl;
  1077.         return;
  1078.     }
  1079.     cout << "wybierz pojazd z naszej oferty\n" << endl;
  1080.     for (int i = 0; i < vehicleCount; i++) {
  1081.         cout << i + 1 << "\t";
  1082.         myVehicle[i]->print();
  1083.     }
  1084.     cout << "podaj numer pojazdu ktory Cie interesuje\n" << endl;
  1085.     int choice;
  1086.     cin >> choice;
  1087.     cout << "cena pojazdu wynosi ";
  1088.     myVehicle[choice - 1]->printPrice(); cout << "$" << endl << endl;
  1089.     cout << "czy chcesz kupic wybrany pojazd?\n 0.nie 1.tak\n" << endl;
  1090.     bool buy;
  1091.     bool retry;
  1092.     cin >> buy;
  1093.     if (buy == 1) {
  1094.         balance = balance + myVehicle[choice - 1]->getPrice();
  1095.        
  1096.         MemoryManagement::removePtrFromNth(myVehicle, vehicleCount, choice);
  1097.         vehicleCount--;
  1098.         cout << "balans po wykonanej transakcji " << balance << "$" << endl;
  1099.  
  1100.     }
  1101.     else {
  1102.         cout << "czy chcesz wybrac inny pojazd?\n 0.nie 1.tak\n" << endl;
  1103.         cin >> retry;
  1104.         if (retry == 1) {
  1105.  
  1106.             return sellCar();
  1107.         }
  1108.         else {
  1109.             return;
  1110.         }
  1111.     }
  1112.  
  1113. }
  1114. void Workshop::buyCar() {
  1115.     if (clientCount == 0) {
  1116.         addClient();
  1117.         int vehicleType;
  1118.         cout << "jaki rodzaj pojazdu chcesz sprzedac" << endl;
  1119.         cout << "1. auto 2. ciezarowka 3. motocykl" << endl;
  1120.         cin >> vehicleType;
  1121.         cout << "wprowadz parametry pojazdu ;" << endl;
  1122.         bool damaged;
  1123.  
  1124.         Car temp;
  1125.         cin >> temp;
  1126.         cout << "sprawdz dane" << endl;
  1127.         cout << temp;
  1128.         cout << "czy auto jest uszkodzone?\n 0.nie 1.tak\n" << endl;
  1129.  
  1130.         cin >> damaged;
  1131.         if (damaged == 1) {
  1132.             cout << "mozemy zaoferowac Ci " << temp.price / 2 << "$" << endl;
  1133.             cout << "czy chcesz sprzedac ten pojazd?\n 0.nie 1.tak\n" << endl;
  1134.             int choice = 0;
  1135.             cin >> choice;
  1136.             if (choice == 0) {
  1137.                 return;
  1138.             }
  1139.             else if (choice == 1) {
  1140.                 MemoryManagement::addPtrToArray(myVehicle, vehicleCount);
  1141.                 switch (vehicleType) {
  1142.                 case 1: {
  1143.                     myVehicle[vehicleCount] = new Car();
  1144.  
  1145.                     break;
  1146.                 }
  1147.                 case 2: {
  1148.                     myVehicle[vehicleCount] = new Truck();
  1149.                     //TODO
  1150.                     break;
  1151.                 }
  1152.                 case 3: {
  1153.                     myVehicle[vehicleCount] = new Motorcycle();
  1154.                     break;
  1155.                 }
  1156.                 }
  1157.                 vehicleCount++;
  1158.                 balance = balance - temp.price / 2;
  1159.                 cout << "balans po wykonanej transakcji " << balance << "$" << endl;
  1160.                 return;
  1161.             }
  1162.  
  1163.         }
  1164.         else {
  1165.             cout << "mozemy zaoferowac Ci " << 0.8*temp.price << "$" << endl;
  1166.             cout << "czy chcesz sprzedac ten pojazd?\n 0.nie 1.tak\n" << endl;
  1167.             int choice = 0;
  1168.             cin >> choice;
  1169.             if (choice == 0) {
  1170.                 return;
  1171.             }
  1172.             else if (choice == 1) {
  1173.                 MemoryManagement::addPtrToArray(myVehicle, vehicleCount);
  1174.                 switch (vehicleType) {
  1175.                 case 1: {
  1176.                     myVehicle[vehicleCount] = new Car();
  1177.                     break;
  1178.                 }
  1179.                 case 2: {
  1180.                     myVehicle[vehicleCount] = new Truck();
  1181.                     break;
  1182.                 }
  1183.                 case 3: {
  1184.                     myVehicle[vehicleCount] = new Motorcycle();
  1185.                     break;
  1186.                 }
  1187.                 }
  1188.                 vehicleCount++;
  1189.                 balance = balance - 0.8*temp.price;
  1190.                 cout << "balans po wykonanej transakcji " << balance << "$" << endl;
  1191.                 return;
  1192.  
  1193.             }
  1194.         }
  1195.         return;
  1196.     }
  1197.  
  1198. }
  1199. void Workshop::repairCar() {
  1200.     Mechanic mechanic;
  1201.     for (int i = 0; i < 5; i++) {
  1202.         mechanic.repairVehicle(myVehicle[i]);
  1203.     }
  1204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement