Advertisement
Axeer

Tractor Madness

May 27th, 2022
568
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifndef OOP_MODULE_H
  2. #define OOP_MODULE_H
  3.  
  4. #include "algorithm"
  5. #include "string"
  6. #include <cstdlib>     //stdlib.h
  7. #include <ctime>       /* time.h */
  8.  
  9. using namespace std;
  10.  
  11. typedef double Liters;
  12. typedef double Rubles;
  13.  
  14. double fRand(double fMin, double fMax)
  15. {
  16.     double f = (double)rand() / RAND_MAX;
  17.     return fMin + f * (fMax - fMin);
  18. }
  19.  
  20. struct Cylinder
  21. {
  22.     double bore;
  23.     double stroke;
  24.  
  25.     Cylinder(double bore = 0, double stroke = 0)
  26.     {
  27.         this->bore = bore, this->stroke = stroke;
  28.     }
  29. };
  30.  
  31. struct EngineFamily
  32. {
  33.     string configuration;
  34.     string family_name;
  35.     int valves;
  36. };
  37.  
  38. struct Engine : public EngineFamily
  39. {
  40.     double brake_horsepower;
  41.     double displacement;
  42.     bool turbochargers;
  43.     string model_name;
  44.     Cylinder cylinder;
  45.     int rpm_max;
  46.  
  47.     Engine(string family_name = string("template_model"),
  48.             string configuration = string("template_configuration"),
  49.             int valves = 0,
  50.             string model_name = string("template_name"),
  51.             Cylinder cylinder = Cylinder(),
  52.             bool turbochargers = false,
  53.             double displacement = 0,
  54.             int rpm_max = 0,
  55.             double brake_horsepower = 0) :
  56.             brake_horsepower(brake_horsepower),
  57.             rpm_max(rpm_max),
  58.             displacement(displacement),
  59.             turbochargers(turbochargers),
  60.             cylinder(cylinder),
  61.             model_name(model_name),
  62.             EngineFamily{ configuration, family_name, valves }
  63.     {
  64.     }
  65. };
  66.  
  67.  
  68. struct EngineList
  69. {
  70.     int total_configurations;
  71.     Engine** engines;
  72.  
  73.     void extend()
  74.     {
  75.         Engine** tmp = new Engine* [total_configurations + 1]{};
  76.         std::copy_n(this->engines, total_configurations, tmp);
  77.         delete[] this->engines;
  78.         engines = tmp;
  79.     }
  80.  
  81.     void add(Engine engine)
  82.     {
  83.         extend();
  84.         engines[total_configurations] = new Engine(engine);
  85.         total_configurations++;
  86.     }
  87.  
  88.     void add(Engine* engine)
  89.     {
  90.         extend();
  91.         engines[total_configurations] = engine;
  92.         total_configurations++;
  93.     }
  94.  
  95.     EngineList(Engine engine)
  96.     {
  97.         this->total_configurations = 1;
  98.         this->engines = new Engine* [1]{ &engine };
  99.     }
  100.  
  101.     EngineList()
  102.     {
  103.         this->total_configurations = 0;
  104.         this->engines = 0;
  105.     }
  106. };
  107.  
  108. struct TractorConfiguration
  109. {
  110.     int wheels;
  111.     Engine* engine;
  112.     int id;
  113.     int additional_components;
  114. protected:
  115.     Rubles price;
  116. public:
  117.     TractorConfiguration(int wheels, Engine* engine, int id, int additional_components = 0) : wheels(wheels),
  118.                                                                                               engine(engine), price(0),
  119.                                                                                               id(id),
  120.                                                                                               additional_components(
  121.                                                                                                       additional_components)
  122.     {
  123.     }
  124.     TractorConfiguration() : wheels(0), engine(), id(0), additional_components(0){}
  125. };
  126.  
  127. struct Tractor : public TractorConfiguration
  128. {
  129.     Tractor(int wheels, Engine* engine, int id = 0) : TractorConfiguration(wheels, engine, id)
  130.     {
  131.         price = getPrice();
  132.     }
  133.  
  134.     Rubles getPrice()
  135.     {
  136.         price = (120 * wheels) + engine->brake_horsepower + (additional_components * 250);
  137.         return price;
  138.     }
  139.  
  140.     void addDetail()
  141.     {
  142.         additional_components++;
  143.     }
  144.  
  145.     Tractor() : TractorConfiguration()
  146.     {
  147.     }
  148.  
  149. };
  150.  
  151. struct Catalog{
  152.     Tractor** tractors;
  153.     int n;
  154.     Catalog(): n(0), tractors(0)
  155.     {}
  156.  
  157.     void add(Tractor* tractor){
  158.         Tractor** tmp = new Tractor*[n+1]{};
  159.         std::copy_n(tractors, n, tmp);
  160.         tractors = tmp;
  161.         tractors[n++] = tractor;
  162.     }
  163. };
  164.  
  165. class Client
  166. {
  167.     Rubles total_cash;
  168. public:
  169.     int id;
  170.     Rubles cash_spent;
  171.     Tractor* own_tractor;
  172.     Tractor* order;
  173.  
  174.     void buy(Tractor* tractor)
  175.     {
  176.         if (tractor->getPrice() <= total_cash)
  177.         {
  178.             cash_spent += tractor->getPrice();
  179.             total_cash -= tractor->getPrice();
  180.             own_tractor = tractor;
  181.         }
  182.         else
  183.         {
  184.             throw (std::exception("client haven't enough cash"));
  185.         }
  186.     }
  187.     void chooseOrder(Catalog* catalog){
  188.         this->order = catalog->tractors[rand()%(catalog->n - 1)];
  189.     }
  190.     Client(Rubles total_cash, const int id) : total_cash(total_cash), id(id), cash_spent(0), own_tractor(0)
  191.     {
  192.     };
  193.  
  194.     Client()
  195.     {
  196.         id = 0;
  197.         total_cash = fRand(600, 2000);
  198.         cash_spent = 0;
  199.         own_tractor = 0;
  200.     }
  201.  
  202.     Client(Catalog* catalog){
  203.         *this = Client();
  204.         chooseOrder(catalog);
  205.     }
  206.  
  207. };
  208.  
  209. class TractorFactory
  210. {
  211. public:
  212.     Tractor* produce(const Tractor* tractor)
  213.     {
  214.         static int total_assembled = 0;
  215.         Tractor* production = new Tractor(tractor->wheels, tractor->engine, total_assembled++);
  216.         //copy_n(tractor, 1, production);
  217.         //production->id = ;
  218.         return production;
  219.     }
  220. };
  221.  
  222. template<class T>
  223. class Queue
  224. {
  225.     T mem[100]{};
  226.  
  227.     int head;
  228.     int tail;
  229. public:
  230.     Queue()
  231.     {
  232.         head = tail = 0;
  233.     };
  234.  
  235.     void push(T obj)
  236.     {
  237.         mem[head++] = obj;
  238.         if (head >= 100) head = 0;
  239.     }
  240.  
  241.     T pull()
  242.     {
  243.         T tmp = mem[tail];
  244.         tail++;
  245.         if (tail >= 100) tail = 0;
  246.         return tmp;
  247.     }
  248.  
  249.     int size()
  250.     {
  251.         int result = 0;
  252.         if (head > tail)
  253.         {
  254.             return head - tail;
  255.         }
  256.         else
  257.         {
  258.             return tail - head;
  259.         }
  260.     }
  261.  
  262.     int available()
  263.     {
  264.         return 100 - size();
  265.     }
  266. };
  267.  
  268. class Retail
  269. {
  270.     Queue<Client> client_queue;
  271.     Queue<Tractor> tractor_orders;
  272.     int total_purchased;
  273.     TractorFactory* linked_factory;
  274. public:
  275.     Retail()
  276.     {
  277.         total_purchased = 0;
  278.     }
  279.  
  280.     void add(Client client)
  281.     {
  282.         client_queue.push(client);
  283.         tractor_orders.push(*client.order);
  284.     }
  285.  
  286.     void add(Tractor tractor)
  287.     {
  288.         tractor_orders.push(tractor);
  289.     }
  290.  
  291.     void link(TractorFactory* factory)
  292.     {
  293.         this->linked_factory = factory;
  294.     }
  295.  
  296.     void serve()
  297.     {
  298.         Client client = client_queue.pull();
  299.         client.id = total_purchased++;
  300.         Tractor* t = linked_factory->produce(client.order);
  301.         client.buy(t);
  302.     }
  303.  
  304.     void serveAll()
  305.     {
  306.         while (client_queue.size())
  307.         {
  308.             serve();
  309.         }
  310.     }
  311. };
  312.  
  313.  
  314. #endif //OOP_MODULE_H
  315.  
Advertisement
RAW Paste Data Copied
Advertisement