Advertisement
Guest User

Untitled

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