Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- class StockRecord {
- private:
- char ID[12];
- char company[50];
- double bought_price;
- double moment_price;
- int shares;
- public:
- StockRecord(const char *ID="",const char *company="",double bought_price=0.0,int shares=0)
- {
- strcpy(this->ID,ID);
- strcpy(this->company,company);
- this->bought_price=bought_price;
- this->shares=shares;
- }
- StockRecord(StockRecord &sr)
- {
- strcpy(this->ID,sr.ID);
- strcpy(this->company,sr.company);
- this->bought_price=sr.bought_price;
- this->moment_price=sr.moment_price;
- this->shares=sr.shares;
- }
- void setNewPrice(double c)
- {
- this->moment_price=c;
- }
- double value()
- {
- return shares * moment_price;
- }
- double profit()
- {
- return shares * (moment_price - bought_price);
- }
- friend ostream &operator <<(ostream & out, StockRecord &sr)
- {
- return out<<sr.company<<" "<<sr.shares<<" "<<sr.bought_price<<" "<<sr.moment_price<<" "<<sr.profit()<<endl;
- }
- float get_moment()
- {
- return this->moment_price;
- }
- };
- class Client{
- private:
- char fullname[60];
- int identity;
- StockRecord *companies;
- int n;
- public:
- Client(const char *fullname="",int identity=0,StockRecord *companies=NULL,int n=0)
- {
- strcpy(this->fullname,fullname);
- this->identity=identity;
- this->companies = new StockRecord[n];
- this->n=n;
- for(int i=0;i<n;i++)
- {
- this->companies=companies;
- }
- }
- Client(Client &c)
- {
- strcpy(this->fullname,c.fullname);
- this->identity=c.identity;
- this->companies = new StockRecord[n];
- this->n = c.n;
- for(int i=0;i<n;i++)
- {
- companies=c.companies;
- }
- }
- Client &operator=(Client &c)
- {
- if(this!=&c)
- {
- delete [] companies;
- strcpy(this->fullname,c.fullname);
- this->identity=c.identity;
- this->n = c.n;
- this->companies = new StockRecord[n];
- for(int i=0;i<n;i++)
- {
- companies=c.companies;
- }
- }
- return *this;
- }
- double totalvalue()
- {
- double total=0;
- for(int i=0;i<n;i++)
- {
- total+=companies[i].value();
- }
- return total;
- }
- Client &operator +=(const StockRecord &sr)
- {
- StockRecord *tmp = new StockRecord[n+1];
- for(int i=0;i<n;i++)
- {
- tmp[i]=this->companies[i];
- }
- tmp[this->n++]=sr;
- delete companies;
- this->companies=tmp;
- return *this;
- }
- friend ostream &operator <<(ostream &out,Client &c)
- {
- out<<c.identity<<" "<<c.totalvalue()<<endl;
- for(int i=0;i<c.n;i++)
- {
- out<<c.companies[i];
- }
- return out;
- }
- };
- int main(){
- int test;
- cin >> test;
- if(test == 1){
- double price;
- cout << "=====TEST NA KLASATA StockRecord=====" << endl;
- StockRecord sr("1", "Microsoft", 60.0, 100);
- cout << "Konstruktor OK" << endl;
- cin >> price;
- sr.setNewPrice(price);
- cout << "SET metoda OK" << endl;
- }
- else if(test == 2){
- cout << "=====TEST NA METODITE I OPERATOR << OD KLASATA StockRecord=====" << endl;
- char id[12], company[50];
- double price, newPrice;
- int n, shares;
- cin >> n;
- for(int i = 0; i < n; ++i){
- cin >> id;
- cin >> company;
- cin >> price;
- cin >> newPrice;
- cin >> shares;
- StockRecord sr(id, company, price, shares);
- sr.setNewPrice(newPrice);
- cout << sr.value() << endl;
- cout << sr;
- }
- }
- else if(test == 3){
- cout << "=====TEST NA KLASATA Client=====" << endl;
- char companyID[12], companyName[50], clientName[50];
- int clientID, n, shares;
- double oldPrice, newPrice;
- bool flag = true;
- cin >> clientName;
- cin >> clientID;
- cin >> n;
- Client c(clientName, clientID);
- cout << "Konstruktor OK" << endl;
- for(int i = 0; i < n; ++i){
- cin >> companyID;
- cin >> companyName;
- cin >> oldPrice;
- cin >> newPrice;
- cin >> shares;
- StockRecord sr(companyID, companyName, oldPrice, shares);
- sr.setNewPrice(newPrice);
- c += sr;
- if(flag){
- cout << "Operator += OK" << endl;
- flag = false;
- }
- }
- cout << c;
- cout << "Operator << OK" << endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment