Advertisement
metalni

Servers Stefan Andonov

Jul 18th, 2020
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.43 KB | None | 0 0
  1. #include <cstring>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. // Your Code goes here
  6. class Server{
  7.     protected:
  8.         char ip[46];
  9.         char name[101];
  10.         const void copy_obj(const Server &copy){
  11.             strcpy(this->ip, copy.ip);
  12.             strcpy(this->name, copy.name);
  13.         }
  14.     public:
  15.         Server(const char * ip="0.0.0.0", const char * name="NoName"){
  16.             strcpy(this->ip, ip);
  17.             strcpy(this->name, name);
  18.         }
  19.         Server(const Server &copy){
  20.             this->copy_obj(copy);
  21.         }
  22.         Server &operator=(const Server &copy){
  23.             if(this != &copy)
  24.                 this->copy_obj(copy);          
  25.             return *this;
  26.         }
  27.         ~Server(){}
  28.         virtual const double total_price() const {
  29.             return 0;
  30.         }
  31. };
  32.  
  33. class DedicatedServer : public Server {
  34.     private:
  35.         double basePrice;
  36.         int ram;
  37.         int * hdds;
  38.         int noHdds;
  39.         const void copy_obj(const DedicatedServer &copy){
  40.             this->basePrice = copy.basePrice;
  41.             this->ram = copy.ram;
  42.             this->hdds = new int[copy.noHdds + 1];
  43.             for(int i=0; i<copy.noHdds; i++)
  44.                 this->hdds[i] = copy.hdds[i];
  45.             this->noHdds = copy.noHdds;
  46.         }
  47.     public:
  48.         DedicatedServer(const char * ip="0.0.0.0", const char * name="NoName", const int basePrice=0, const int ram=0) : Server(ip, name){
  49.             this->basePrice = basePrice;
  50.             this->ram = ram;
  51.             this->hdds = new int[0];
  52.             this->noHdds = 0;        
  53.         }
  54.         DedicatedServer(const DedicatedServer &copy) : Server(copy){
  55.             this->copy_obj(copy);
  56.         }
  57.         DedicatedServer &operator=(const DedicatedServer &copy){
  58.             if(this != &copy){
  59.                 delete [] this->hdds;
  60.                 Server::operator=(copy);
  61.                 this->copy_obj(copy);
  62.             }
  63.             return *this;
  64.         }
  65.         ~DedicatedServer(){
  66.             delete [] this->hdds;
  67.         }
  68.         const int hddSpace() const {
  69.             int total=0;
  70.             for(int i=0; i<this->noHdds; i++){
  71.                 total += this->hdds[i];
  72.             }
  73.             return total;
  74.         }
  75.         const double total_price() const {
  76.             return this->basePrice + this->hddSpace() * 0.5 + this->ram * 20;
  77.         }
  78.         DedicatedServer &operator+= (int &add){
  79.             int * tmp = new int[this->noHdds + 1];
  80.             for(int i=0; i<this->noHdds; i++)
  81.                 tmp[i] = this->hdds[i];
  82.             tmp[this->noHdds++] = add;
  83.             delete [] this->hdds;
  84.             this->hdds = tmp;
  85.  
  86.             return *this;
  87.         }
  88.         DedicatedServer operator++(int){
  89.             this->ram++;
  90.             return *this;
  91.         }
  92.         friend ostream &operator<<(ostream &os, DedicatedServer &orig){
  93.             os << orig.name << ": " << orig.ip << "\n";
  94.             os << orig.basePrice << ", " << orig.ram << "\n";
  95.             os << orig.noHdds << ", " << orig.hddSpace() << "\n";
  96.  
  97.             return os;
  98.         }
  99. };
  100.  
  101. class VirtualServer : public Server {
  102.     private:
  103.         int noCores;
  104.         int bandwidth;
  105.         const void copy_obj(const VirtualServer &copy){
  106.             this->noCores = copy.noCores;
  107.             this->bandwidth = copy.bandwidth;
  108.         }
  109.     public:
  110.         VirtualServer(const char * ip="0.0.0.0", const char * name="NoName", const int noCores=0, const int bandwidth=0) : Server(ip, name){
  111.             this->noCores = noCores;
  112.             this->bandwidth = bandwidth;
  113.         }
  114.         VirtualServer(const VirtualServer &copy) : Server(copy){
  115.             this->copy_obj(copy);
  116.         }
  117.         VirtualServer &operator=(const VirtualServer &copy){
  118.             if(this != &copy){
  119.                 Server::operator=(copy);
  120.                 this->copy_obj(copy);
  121.             }
  122.             return *this;
  123.         }
  124.         ~VirtualServer(){}
  125.         const double total_price() const{
  126.             return this->noCores * 5 + this->bandwidth * 10;
  127.         }
  128.         VirtualServer &operator+=(int add){
  129.             this->noCores += add;
  130.             return *this;
  131.         }
  132.         VirtualServer operator++(int){
  133.             this->bandwidth++;
  134.             return *this;
  135.         }
  136.         friend ostream &operator << (ostream &os, VirtualServer &orig){
  137.             os << orig.name << ": " << orig.ip << "\n";
  138.             os << orig.noCores << ", " << orig.bandwidth << "\n";
  139.  
  140.             return os;
  141.         }
  142. };
  143.  
  144. bool operator>(Server * left, Server &right){
  145.     if(left->total_price() > right.total_price())
  146.         return true;
  147.     return false;
  148. }
  149.  
  150. double totalCost(Server ** servers, int n){
  151.     double total = 0.0;
  152.     for(int i=0; i<n; i++)
  153.         total += servers[i]->total_price();
  154.     return total;
  155. }
  156.  
  157. Server &biggestInvoice(Server ** servers, int n){
  158.     double max=servers[0]->total_price();
  159.     int index = 0;
  160.     for(int i=0; i<n; i++){
  161.         if(servers[i]->total_price() > max){
  162.             max = servers[i]->total_price();
  163.             index = i;
  164.         }
  165.     }
  166.     return *servers[index];
  167. }
  168.  
  169. // Testing
  170. int main() {
  171.   int test_case;
  172.   char hostname[101];
  173.   char ip[46];
  174.   int ram;
  175.   int basic_price;
  176.   int disk_space;
  177.   int num_cores;
  178.   int bandwith;
  179.  
  180.   int num_inc;
  181.  
  182.   cin >> test_case;
  183.   if (test_case == 1) {
  184.     // Test Case Business Invoice - Constructor, operator <<
  185.     cin >> ip >> hostname;
  186.     cin >> basic_price >> ram;
  187.     DedicatedServer ds(ip, hostname, basic_price, ram);
  188.     cout << ds;
  189.   } else if (test_case == 2) {
  190.     // Test Case Business Invoice - Constructor, operators <<, ++
  191.     cin >> ip >> hostname;
  192.     cin >> basic_price >> ram;
  193.     DedicatedServer ds(ip, hostname, basic_price, ram);
  194.     cout << ds;
  195.  
  196.     cin >> num_inc;
  197.     for (int i = 0; i < num_inc; ++i) {
  198.       ds++;
  199.     }
  200.     cout << ds;
  201.   } else if (test_case == 3) {
  202.     // Test Case Business Invoice - Constructor, total_price, operators <<,
  203.     cin >> ip >> hostname;
  204.     cin >> basic_price >> ram;
  205.     DedicatedServer ds(ip, hostname, basic_price, ram);
  206.     cout << ds;
  207.  
  208.     cin >> num_inc;
  209.     for (int i = 0; i < num_inc; ++i) {
  210.       ds++;
  211.     }
  212.  
  213.     cin >> num_inc;
  214.     for (int i = 0; i < num_inc; ++i) {
  215.       cin >> disk_space;
  216.       ds += disk_space;
  217.     }
  218.  
  219.     cout << ds;
  220.   } else if (test_case == 4) {
  221.     cin >> ip >> hostname;
  222.     cin >> basic_price >> ram;
  223.     DedicatedServer ds(ip, hostname, basic_price, ram);
  224.     cout << ds;
  225.     cout << ds.total_price()<< endl;
  226.     cin >> num_inc;
  227.     for (int i = 0; i < num_inc; ++i) {
  228.       ds++;
  229.     }
  230.  
  231.     cin >> num_inc;
  232.     for (int i = 0; i < num_inc; ++i) {
  233.       cin >> disk_space;
  234.       ds += disk_space;
  235.     }
  236.  
  237.     cout << ds;
  238.     cout << ds.total_price();
  239.  
  240.   } else if (test_case == 5) {
  241.     cin >> ip >> hostname;
  242.     cin >> num_cores >> bandwith;
  243.     VirtualServer vs(ip, hostname, num_cores, bandwith);
  244.     cout << vs;
  245.   } else if (test_case == 6) {
  246.     cin >> ip >> hostname;
  247.     cin >> num_cores >> bandwith;
  248.     VirtualServer vs(ip, hostname, num_cores, bandwith);
  249.     cout << vs;
  250.     cin >> num_inc;
  251.     for (int i = 0; i < num_inc; ++i) {
  252.       vs++;
  253.     }
  254.  
  255.     cin >> num_inc;
  256.     vs += num_inc;
  257.  
  258.     cout << vs;
  259.  
  260.   } else if (test_case == 7) {
  261.     cin >> ip >> hostname;
  262.     cin >> num_cores >> bandwith;
  263.     VirtualServer vs(ip, hostname, num_cores, bandwith);
  264.     cout << vs;
  265.     cout << vs.total_price() << endl;
  266.  
  267.     cin >> num_inc;
  268.     for (int i = 0; i < num_inc; ++i) {
  269.       vs++;
  270.     }
  271.  
  272.     cin >> num_inc;
  273.     vs += num_inc;
  274.     cout << vs;
  275.     cout << vs.total_price();
  276.   } else if (test_case == 8) {
  277.  
  278.     int num_servers;
  279.     int server_type;
  280.  
  281.     cin >> num_servers;
  282.     Server **s = new Server *[num_servers];
  283.     for (int j = 0; j < num_servers; ++j) {
  284.  
  285.       cin >> server_type;
  286.       if (server_type == 1) {
  287.         cin >> ip >> hostname;
  288.         cin >> basic_price >> ram;
  289.         DedicatedServer *dsp =
  290.             new DedicatedServer(ip, hostname, basic_price, ram);
  291.  
  292.         cin >> num_inc;
  293.         for (int i = 0; i < num_inc; ++i) {
  294.           (*dsp)++;
  295.         }
  296.  
  297.         cin >> num_inc;
  298.         for (int i = 0; i < num_inc; ++i) {
  299.           cin >> disk_space;
  300.           (*dsp) += disk_space;
  301.         }
  302.  
  303.         cout << *(dsp);
  304.         cout << dsp->total_price() << endl;
  305.  
  306.         s[j] = dsp;
  307.       }
  308.       if (server_type == 2) {
  309.  
  310.         cin >> ip >> hostname;
  311.         cin >> num_cores >> bandwith;
  312.         VirtualServer *vsp =
  313.             new VirtualServer(ip, hostname, num_cores, bandwith);
  314.  
  315.         cin >> num_inc;
  316.         for (int i = 0; i < num_inc; ++i) {
  317.           (*vsp)++;
  318.         }
  319.  
  320.         cin >> num_inc;
  321.         (*vsp) += num_inc;
  322.  
  323.         cout << (*vsp);
  324.         cout << vsp->total_price() << endl;
  325.         s[j] = vsp;
  326.       }
  327.     }
  328.  
  329.     cout << "The cost of all servers is:\n";
  330.     cout << totalCost(s, num_servers);
  331.     cout << endl;
  332.  
  333.     cout << "Biggest invoice:\n";
  334.     Server &server = biggestInvoice(s, num_servers);
  335.  
  336.     Server *ss = &server;
  337.     DedicatedServer *bip;
  338.     VirtualServer *pip;
  339.     if (dynamic_cast<DedicatedServer *>(ss)) {
  340.       bip = dynamic_cast<DedicatedServer *>(ss);
  341.       cout << *bip << bip->total_price();
  342.     }
  343.     if (dynamic_cast<VirtualServer *>(ss)) {
  344.       pip = dynamic_cast<VirtualServer *>(ss);
  345.       cout << *pip << pip->total_price();
  346.     }
  347.   }
  348.   return 0;
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement