Advertisement
chasnasestra

Untitled

Mar 18th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.78 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4.  
  5. class StockRecord {
  6.    
  7.     private:
  8.         char id[12];
  9.         char ime[50];
  10.         float kupenaCena;
  11.         float momentalnaCena;
  12.         int brAkcii;
  13.    
  14.     public:
  15.     StockRecord(const char *id="", const char *ime="", float momentalnaCena=0, int brAkcii=0){
  16.         strcpy(this->id, id);
  17.         strcpy(this->ime, ime);
  18.         this->momentalnaCena = momentalnaCena;
  19.         this->brAkcii = brAkcii;
  20.     }
  21.    
  22.     void setNewPrice(double c){
  23.         this->kupenaCena = c;
  24.     }
  25.    
  26.     double value(){
  27.         return brAkcii*kupenaCena;
  28.     }
  29.    
  30.     double profit(){
  31.         return brAkcii*(momentalnaCena - kupenaCena);
  32.     }
  33.    
  34.     friend ostream &operator<< (ostream &o, const StockRecord &rhs){
  35.     o << rhs.ime << " " << rhs.brAkcii << " " << rhs.momentalnaCena << " " << rhs.kupenaCena << " " <<
  36.         rhs.brAkcii*(rhs.momentalnaCena - rhs.kupenaCena)*-1 << endl; return o;
  37.     }
  38. };
  39.  
  40. class Client{
  41.    
  42.     private:
  43.         char imePrezime[60];
  44.         int id;
  45.         StockRecord *sr;
  46.         int brAkcii;
  47.    
  48.     public:
  49.     Client(const char *imePrezime="", int id=0){
  50.             strcpy(this->imePrezime, imePrezime);
  51.             brAkcii = 0;
  52.             this->id = id;
  53.         }
  54.  
  55.     ~Client(){ delete [] sr; }
  56.    
  57.     double totalValue(){
  58.         double sum=0;
  59.         for(int i=0; i<brAkcii; i++){
  60.             sum+=sr[i].value();
  61.         }
  62.         return sum;
  63.     }
  64.    
  65.       Client& operator+=(const StockRecord &c) {
  66.       StockRecord *temp = new StockRecord[brAkcii+1];
  67.       for(int i=0; i<brAkcii; i++)
  68.           temp[i]=sr[i];
  69.       temp[brAkcii]=c;
  70.       sr = temp;
  71.       brAkcii++;
  72.       return *this;
  73.       }
  74.    
  75.     friend ostream &operator<< (ostream &o, Client &rhs){
  76.         o << rhs.id << " " << rhs.totalValue() << endl;
  77.         for(int i=0; i<rhs.brAkcii; i++){
  78.             o << rhs.sr[i];
  79.         }
  80.         return o;
  81.     }    
  82.    
  83. };
  84.  
  85. // ne menuvaj vo main-ot
  86.  
  87. int main(){
  88.    
  89.     int test;
  90.     cin >> test;
  91.    
  92.     if(test == 1){
  93.         double price;
  94.         cout << "=====TEST NA KLASATA StockRecord=====" << endl;
  95.         StockRecord sr("1", "Microsoft", 60.0, 100);
  96.         cout << "Konstruktor OK" << endl;
  97.         cin >> price;
  98.         sr.setNewPrice(price);
  99.         cout << "SET metoda OK" << endl;
  100.     }
  101.     else if(test == 2){
  102.         cout << "=====TEST NA METODITE I OPERATOR << OD KLASATA StockRecord=====" << endl;
  103.         char id[12], company[50];
  104.         double price, newPrice;
  105.         int n, shares;
  106.         cin >> n;
  107.         for(int i = 0; i < n; ++i){
  108.             cin >> id;
  109.             cin >> company;
  110.             cin >> price;
  111.             cin >> newPrice;
  112.             cin >> shares;
  113.             StockRecord sr(id, company, price, shares);
  114.             sr.setNewPrice(newPrice);
  115.             cout << sr.value() << endl;
  116.             cout << sr;
  117.         }
  118.     }
  119.     else if(test == 3){
  120.         cout << "=====TEST NA KLASATA Client=====" << endl;
  121.         char companyID[12], companyName[50], clientName[50];
  122.         int clientID, n, shares;
  123.         double oldPrice, newPrice;
  124.         bool flag = true;
  125.         cin >> clientName;
  126.         cin >> clientID;
  127.         cin >> n;
  128.         Client c(clientName, clientID);
  129.         cout << "Konstruktor OK" << endl;
  130.         for(int i = 0; i < n; ++i){
  131.             cin >> companyID;
  132.             cin >> companyName;
  133.             cin >> oldPrice;
  134.             cin >> newPrice;
  135.             cin >> shares;
  136.             StockRecord sr(companyID, companyName, oldPrice, shares);
  137.             sr.setNewPrice(newPrice);
  138.             c += sr;
  139.             if(flag){
  140.                 cout << "Operator += OK" << endl;
  141.                 flag = false;
  142.             }
  143.         }
  144.         cout << c;
  145.         cout << "Operator << OK" << endl;
  146.     }
  147.     return 0;
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement