Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstring>
- using namespace std;
- // vasiot kod za klasite ovde
- class StockRecord{
- private:
- char id[12];
- char company[50];
- int priceBought;
- int priceNow;
- int sharesBought;
- public:
- StockRecord(){};
- StockRecord(char *id, char *company, int priceBought, int sharesBought){
- strcpy(this->id, id);
- strcpy(this->company, company);
- this->priceBought = priceBought;
- this->sharesBought = sharesBought;
- }
- void setNewPrice (double c){
- priceNow = c;
- }
- double value(){
- return sharesBought * priceNow;
- }
- double profit(){
- return value() - sharesBought * priceBought;
- }
- friend ostream &operator << (ostream &o, StockRecord &p){
- o<<p.company<<" "<<p.sharesBought<<" "<<p.priceBought<<" "<<p.priceNow<<" "<<p.profit()<<endl;
- return o;
- }
- };
- class Client{
- private:
- char name[60];
- int id;
- StockRecord *companies;
- int companiesNo;
- /*void copy(const Client &p){
- strcpy(name, p.name);
- id = p.id;
- companiesNo = p.companiesNo;
- companies = new StockRecord [companiesNo];
- for(int i=0;i<companiesNo;i++){
- companies[i] = p.companies[i];
- }
- }*/
- public:
- Client(char *name, int id){
- strcpy(this->name, name);
- this->id = id;
- companies = NULL;
- companiesNo = 0;
- }
- /*Client(const Client &p){
- copy(p);
- }
- Client &operator = (const Client &p){
- if(this == &p)
- return *this;
- delete [] companies;
- copy(p);
- return *this;
- }*/
- double totalValue(){
- double total = 0;
- for(int i=0;i<companiesNo;i++){
- total+=companies[i].value();
- }
- return total;
- }
- Client &operator += (StockRecord &p){
- StockRecord *temp = new StockRecord [companiesNo];
- for(int i=0;i<companiesNo;i++){
- temp[i] = companies[i];
- }
- temp[companiesNo++] = p;
- delete [] companies;
- companies = temp;
- return *this;
- }
- friend ostream &operator << (ostream &o, Client &p){
- o<<p.id<<" "<<p.totalValue()<<endl;
- for(int i=0;i<p.companiesNo;i++){
- o<<p.companies[i];
- }
- return o;
- }
- };
- // ne menuvaj vo main-ot
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement