LegoDrifter

OOP - Akcii

Jun 23rd, 2020
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. class StockRecord {
  8. private:
  9.     char ID[12];
  10.     char company[50];
  11.     double bought_price;
  12.     double moment_price;
  13.     int   shares;
  14. public:
  15.   StockRecord(const char *ID="",const char *company="",double bought_price=0.0,int shares=0)
  16.  {
  17.     strcpy(this->ID,ID);
  18.     strcpy(this->company,company);
  19.     this->bought_price=bought_price;
  20.    
  21.     this->shares=shares;
  22.  }
  23.  StockRecord(StockRecord &sr)
  24.  {
  25.     strcpy(this->ID,sr.ID);
  26.     strcpy(this->company,sr.company);
  27.     this->bought_price=sr.bought_price;
  28.     this->moment_price=sr.moment_price;
  29.     this->shares=sr.shares;
  30.  }
  31.     void setNewPrice(double c)
  32.     {
  33.         this->moment_price=c;
  34.     }
  35.     double value()
  36.     {
  37.         return shares * moment_price;
  38.     }
  39.     double profit()
  40.     {
  41.         return shares * (moment_price - bought_price);
  42.     }
  43.     friend ostream &operator <<(ostream & out, StockRecord &sr)
  44.     {
  45.         return out<<sr.company<<" "<<sr.shares<<" "<<sr.bought_price<<" "<<sr.moment_price<<" "<<sr.profit()<<endl;
  46.     }
  47.     float get_moment()
  48.     {
  49.         return this->moment_price;
  50.     }
  51.  
  52.  
  53. };
  54.  
  55. class Client{
  56. private:
  57.     char fullname[60];
  58.     int identity;
  59.     StockRecord *companies;
  60.     int n;
  61. public:
  62.     Client(const char *fullname="",int identity=0,StockRecord *companies=NULL,int n=0)
  63.     {
  64.         strcpy(this->fullname,fullname);
  65.         this->identity=identity;
  66.         this->companies = new StockRecord[n];
  67.         this->n=n;
  68.         for(int i=0;i<n;i++)
  69.         {
  70.             this->companies=companies;
  71.         }
  72.     }
  73.     Client(Client &c)
  74.     {
  75.         strcpy(this->fullname,c.fullname);
  76.         this->identity=c.identity;
  77.         this->companies = new StockRecord[n];
  78.         this->n = c.n;
  79.         for(int i=0;i<n;i++)
  80.         {
  81.             companies=c.companies;
  82.         }
  83.     }
  84.     Client &operator=(Client &c)
  85.     {
  86.         if(this!=&c)
  87.         {
  88.         delete [] companies;
  89.         strcpy(this->fullname,c.fullname);
  90.         this->identity=c.identity;
  91.         this->n = c.n;
  92.         this->companies = new StockRecord[n];
  93.         for(int i=0;i<n;i++)
  94.         {
  95.             companies=c.companies;
  96.         }
  97.         }
  98.         return *this;
  99.  
  100.     }
  101.     double totalvalue()
  102.     {
  103.         double total=0;
  104.         for(int i=0;i<n;i++)
  105.         {
  106.             total+=companies[i].value();
  107.         }
  108.         return total;
  109.     }
  110.     Client &operator +=(const StockRecord &sr)
  111.     {
  112.         StockRecord *tmp = new StockRecord[n+1];
  113.         for(int i=0;i<n;i++)
  114.             {
  115.                 tmp[i]=this->companies[i];
  116.             }
  117.         tmp[this->n++]=sr;
  118.         delete companies;
  119.         this->companies=tmp;
  120.         return *this;
  121.  
  122.     }
  123.     friend ostream &operator <<(ostream &out,Client &c)
  124.     {
  125.         out<<c.identity<<" "<<c.totalvalue()<<endl;
  126.         for(int i=0;i<c.n;i++)
  127.         {
  128.             out<<c.companies[i];
  129.         }
  130.         return out;
  131.     }
  132.  
  133.    
  134.  
  135. };
  136.  
  137. int main(){
  138.    
  139.     int test;
  140.     cin >> test;
  141.    
  142.     if(test == 1){
  143.         double price;
  144.         cout << "=====TEST NA KLASATA StockRecord=====" << endl;
  145.         StockRecord sr("1", "Microsoft", 60.0, 100);
  146.         cout << "Konstruktor OK" << endl;
  147.         cin >> price;
  148.         sr.setNewPrice(price);
  149.         cout << "SET metoda OK" << endl;
  150.     }
  151.     else if(test == 2){
  152.         cout << "=====TEST NA METODITE I OPERATOR << OD KLASATA StockRecord=====" << endl;
  153.         char id[12], company[50];
  154.         double price, newPrice;
  155.         int n, shares;
  156.         cin >> n;
  157.         for(int i = 0; i < n; ++i){
  158.             cin >> id;
  159.             cin >> company;
  160.             cin >> price;
  161.             cin >> newPrice;
  162.             cin >> shares;
  163.             StockRecord sr(id, company, price, shares);
  164.             sr.setNewPrice(newPrice);
  165.             cout << sr.value() << endl;
  166.             cout << sr;
  167.         }
  168.     }
  169.     else if(test == 3){
  170.         cout << "=====TEST NA KLASATA Client=====" << endl;
  171.         char companyID[12], companyName[50], clientName[50];
  172.         int clientID, n, shares;
  173.         double oldPrice, newPrice;
  174.         bool flag = true;
  175.         cin >> clientName;
  176.         cin >> clientID;
  177.         cin >> n;
  178.         Client c(clientName, clientID);
  179.         cout << "Konstruktor OK" << endl;
  180.         for(int i = 0; i < n; ++i){
  181.             cin >> companyID;
  182.             cin >> companyName;
  183.             cin >> oldPrice;
  184.             cin >> newPrice;
  185.             cin >> shares;
  186.             StockRecord sr(companyID, companyName, oldPrice, shares);
  187.             sr.setNewPrice(newPrice);
  188.             c += sr;
  189.             if(flag){
  190.                 cout << "Operator += OK" << endl;
  191.                 flag = false;
  192.             }
  193.         }
  194.         cout << c;
  195.         cout << "Operator << OK" << endl;
  196.     }
  197.     return 0;
  198.  
  199. }
Add Comment
Please, Sign In to add comment