LaYatiX

Untitled

Dec 12th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <string>
  4. #include <random>
  5. #include <ctime>
  6. #include <sstream>
  7. #include <vector>
  8.  
  9. using namespace std;
  10.  
  11. //https://stackoverflow.com/questions/13445688/how-to-generate-a-random-number-in-c
  12. std::random_device dev;
  13. std::mt19937 rng(dev());
  14. std::uniform_int_distribution<std::mt19937::result_type> cenaRnd(5000, 150000);
  15.  
  16. std::random_device dev2;
  17. std::mt19937 rng2(dev2());
  18. std::uniform_int_distribution<std::mt19937::result_type> carsRnd(1, 3);
  19.  
  20. void wypisz();
  21.  
  22. class Car {
  23.     friend class Customer;
  24.     friend void wypisz();
  25.  
  26.     string brand;
  27.     string model;
  28.     int price;
  29.  
  30.     random_device random;
  31.  
  32. public:
  33.  
  34.     Car() { price = -1; };
  35.  
  36.     Car(string a, string b) {
  37.         brand = a;
  38.         model = b;
  39.         price = cenaRnd(rng);
  40.     }
  41.  
  42.     Car(const Car& car) {
  43.         brand = car.brand;
  44.         model = car.model;
  45.         price = cenaRnd(rng);
  46.     }
  47.    
  48.     Car& operator=(const Car &car) noexcept // copy/move constructor is called to construct arg
  49.     {
  50.         brand = car.brand;
  51.         model = car.model;
  52.         price = car.price;
  53.         return *this;
  54.     } // destructor of arg is called to release the resources formerly held by *this
  55.  
  56.     string getBrand() {
  57.         return brand;
  58.     };
  59.  
  60.     string getModel() {
  61.         return model;
  62.     };
  63.  
  64.     double getPrice() {
  65.         return price;
  66.     };
  67.  
  68.     void setBrand(string brand) {
  69.         this->brand = brand;
  70.     };
  71.  
  72.     void setModel(string model) {
  73.         this->model = model;
  74.     };
  75.  
  76.     void setPrice(double price) {
  77.         this->price = price;
  78.     };
  79. };
  80.  
  81. class Customer {
  82. private:
  83.     friend void wypisz();
  84.     string dupa = "dupa";
  85.     int ID;
  86. public:
  87.     static int counter;
  88.     int limit = 3;
  89.     int carsAmount = 0;
  90.     Car* cars = nullptr;
  91.  
  92.     Customer(Car cars[]) {
  93.         ID = counter;
  94.         counter++;
  95.         cout << "\nWywolano konstruktor sparametryzowany\n";
  96.         cout << "Customer ID: " << ID << endl;
  97.         this->cars = new Car[limit];
  98.  
  99.  
  100.         for (int i = 0; i < limit; i++) {
  101.             this->cars[i].brand = cars[i].brand;
  102.             this->cars[i].model = cars[i].model;
  103.             this->cars[i].price = cars[i].price;
  104.         };
  105.     };
  106.  
  107.     Customer() {
  108.         ID = counter;
  109.         counter++;
  110.         cout << "\nWywolano konstruktor domyslny\n";
  111.         cout << "Customer ID: " << ID << endl;
  112.         this->cars = new Car[limit];
  113.     };
  114.  
  115.     Customer(const Customer& customer) {
  116.         ID = counter;
  117.         counter++;
  118.         cout << "\nWywolano konstruktor kopiujacy\n";
  119.         cout << "Customer ID: " << ID << endl;
  120.         this->cars = new Car[limit];
  121.  
  122.         //const short carsAmount = (sizeof(customer.cars) / sizeof(*customer.cars));
  123.         /*cout << carsAmount << "asdsadsadasdsadas" << "\n"; */
  124.         for (int i = 0; i < limit; i++) {
  125.             this->cars[i].brand = customer.cars[i].brand;
  126.             this->cars[i].model = customer.cars[i].model;
  127.             this->cars[i].price = customer.cars[i].price;
  128.         };     
  129.     }
  130.  
  131.     ~Customer() {
  132.         counter--;
  133.         cout << "\nWywolano destruktor ";
  134.         cout << ID;
  135.         delete[] this->cars;
  136.     };
  137.  
  138.     void addCar(Car& car) {
  139.         this->cars[0].brand = car.brand;
  140.         this->cars[0].model = car.model;
  141.         this->cars[0].price = car.price;
  142.     }
  143.  
  144.     void addCar(Car& car, int i) {
  145.         this->cars[i].brand = car.brand;
  146.         this->cars[i].model = car.model;
  147.         this->cars[i].price = car.price;
  148.     }
  149.  
  150.     void modifyPrice() {
  151.         for (int i = 0; i < limit; i++) {
  152.             this->cars[i].setPrice(cenaRnd(rng));
  153.         };
  154.     }
  155.  
  156.     int getId() {
  157.         return ID;
  158.     };
  159.  
  160.     void coutAllInfo() {
  161.         cout << "Customer ID: " << ID << endl;
  162.         coutCars();
  163.     }
  164.  
  165.     void coutCars() {
  166.         for (int i = 0; i < limit; i++) {
  167.             if(cars[i].getPrice() != -1)
  168.                 cout << "Brand: " << cars[i].getBrand() << " Model: " << cars[i].getModel() << " price: " << cars[i].getPrice() << "$" << endl;
  169.         };
  170.     };
  171. };
  172.  
  173. int Customer::counter = 0;
  174.  
  175. //Zmienne to przetestowania dostępu do prywantch pól dla zaprzyjaźniownej funkcji wypisz()
  176. Customer* Kupiec_0 = new Customer();
  177. Car* Auto_0 = new Car();
  178.  
  179. int main() {
  180.  
  181.  
  182.     int test;
  183.     Car c1("Fiacik", "Bravo");
  184.     cout << c1.getPrice();
  185.     string model[10] = { "Fiacik","Audi","BMW","Ford","Kia","Alfa Rome", "Seat","Peugeot","Renault","Jaguar"};
  186.     string marka[10] = {"Bravo","A4","3","Focus","Rio","159","Ibiza","307","307","XE"};
  187.     Car cars[10] { {"Fiacik", "Bravo"}, {"Audi", "A4"}, {"BMW", "3"}, {"Ford", "Focus"}, {"Kia", "Rio"},
  188.     {"Alfa Romeo", "159"}, {"Seat", "Ibiza"}, {"Peugeot", "307"}, {"Renault", "307"}, {"Jaguar", "XE"} };
  189.  
  190.     Car first3cars[3];
  191.     Customer *Kupiec_1 = new Customer();
  192.     Customer *Kupiec_2 = new Customer(cars);
  193.     Customer *Kupiec_3 = new Customer(*Kupiec_2);
  194.  
  195.     cout << "Ilosc klientow: " << Customer::counter << "\n";
  196.  
  197.     Kupiec_2->coutAllInfo();
  198.     Kupiec_3->coutAllInfo();
  199.  
  200.     //Kupiec_2->coutAllInfo();
  201.     //customer3.coutAllInfo();
  202.  
  203.     Kupiec_1->addCar(c1);
  204.  
  205.     //customer1.coutAllInfo()
  206.  
  207.     Kupiec_2->modifyPrice();
  208.  
  209.     Kupiec_1->coutAllInfo();
  210.     Kupiec_2->coutAllInfo();
  211.     Kupiec_3->coutAllInfo();
  212.  
  213.     srand((int)time(0));
  214.  
  215.     delete Kupiec_1;
  216.     delete Kupiec_2;
  217.     delete Kupiec_3;
  218.  
  219.     cout << "Ilosc klientow: " << Customer::counter;
  220.  
  221.     Customer *customers = new Customer[100];
  222.  
  223.     for (int i = 0; i < 100; i++) {
  224.         int r = (rand() % 4);
  225.         for (int j = 0; j < r; j++) {
  226.             int markaModelRnd = (rand() % 10);
  227.             Car* car = new Car(model[markaModelRnd], marka[markaModelRnd]);
  228.             customers[i].addCar(*car, j);
  229.         }
  230.     };
  231.  
  232.     for (int j = 0; j < 100; j++) {
  233.         customers[j].coutAllInfo();
  234.     }
  235.  
  236.     delete Kupiec_0;
  237.  
  238.     delete[] customers;
  239.     cout << "Ilosc klientow: " << Customer::counter << "\n";
  240. }
  241.  
  242.  
  243. void wypisz()
  244. {
  245.     Kupiec_0->ID;
  246.     Auto_0->model;
  247. }
Add Comment
Please, Sign In to add comment