Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- using namespace std;
- class Product
- {
- int code;
- int value;
- int price;
- char *name;
- public: Product* next;
- Product(int ncode,int nvalue,int nprice,char* nname); //prints all data of the product
- ~Product(); //prints data of the product
- void print(); //prints data of the product
- int getCode();
- int getValue();
- int getPrice();
- char* getName();
- int modCode(int ncode);
- int modValue(int nvalue);
- int modPrice(int nprice);
- char* modName(char *nnmae);
- // Can add more methods
- };
- class ProductList
- {
- Product* head;
- public: ProductList(); //creates an empty list
- ~ProductList(); //deletes the whole list
- int addProduct(int code,int value,int price,char *name); //adding in a simmilar way, that we have added in task 1 !!!CODE UNIQUE!!!
- void removeProduct(int code); //removing a product with an unique code
- void print(); //print the whole list
- };
- void Product::print()
- {
- cout<<"Code: "<<code<<endl;
- cout<<"Value: "<<value<<endl;
- cout<<"Price: "<<price<<endl;
- cout<<"Name: "<<name<<endl;
- }
- Product::Product(int ncode,int nvalue,int nprice,char* nname)
- {
- code=ncode;
- value=nvalue;
- price=nprice;
- if(name!=NULL)
- {
- name=new char[strlen(nname)+1];
- strcpy(name,nname);
- }
- next=NULL;
- print();
- }
- Product::~Product()
- {
- delete[](name);
- print();
- }
- int Product::getCode()
- {
- return code;
- }
- int Product::getValue()
- {
- return value;
- }
- int Product::getPrice()
- {
- return price;
- }
- char* Product::getName()
- {
- return name;
- }
- int Product::modCode(int ncode)
- {
- code=ncode;
- }
- int Product::modValue(int nvalue)
- {
- value=nvalue;
- }
- int Product::modPrice(int nprice)
- {
- price=nprice;
- }
- char* Product::modName(char* nname)
- {
- char* n=new char[strlen(nname)+1];
- strcpy(n,nname);
- name=n;
- }
- ProductList::ProductList()
- {
- head=new Product(0,0,0,NULL);
- }
- ProductList::~ProductList()
- {
- while(head != NULL)
- {
- Product *n=head->next;
- //delete[](head->name);
- delete head;
- head = n;
- }
- }
- void ProductList::removeProduct(int code)
- {
- head=new Product(0,0,0,NULL);
- ProductList();
- int c=head->getCode();
- if(c==code)
- {
- head=head->next;
- }
- Product* curr;
- Product* nex;
- curr=head;
- nex=curr->next;
- while(nex!=NULL)
- {
- int n=nex->getCode();
- if(n==code)
- {
- curr->next=nex->next;
- //delete[](nex->name);
- delete nex;
- nex=curr->next;
- }
- else
- {
- curr=nex;
- nex=nex->next;
- }
- }
- }
- void ProductList::print()
- {
- Product *current=head;
- while(current!=NULL)
- {
- int c=current->getCode();
- int v=current->getValue();
- int p=current->getPrice();
- cout<<"Code: "<<c<<endl;
- cout<<"Value: "<<v<<endl;
- cout<<"Price: "<<p<<endl;
- //cout<<"Name: "<<current->name<<endl<<endl;
- current=current->next;
- }
- }
- int ProductList::addProduct(int code,int value,int price,char* name)
- {
- Product* curr=head;
- Product* tmp=NULL;
- while(curr!=NULL)
- {
- int c=curr->getCode();
- if(code==c)
- {
- int p=curr->getPrice();
- char* s=curr->getName();
- if(p==price&&strcmp(name,s)==0)
- {
- int nval=curr->getValue();
- curr->modValue(value+nval);
- return 1;
- }
- else return 0;
- }
- curr=curr->next;
- }
- if(head==NULL)
- {
- head=new Product(code,value,price,name);
- head->next=NULL;
- }
- else
- {
- Product *tmp;
- tmp=head;
- while(tmp->next!=NULL)
- {
- tmp=tmp->next;
- }
- tmp->next=new Product(0,0,0,NULL);
- tmp=tmp->next;
- tmp=new Product(code,value,price,name);
- tmp->next=NULL;
- }
- int g=0,s=0;
- Product* sizer=head;
- while(sizer!=NULL)
- {
- s++;
- sizer=sizer->next;
- }
- while(g!=s)
- {
- Product* sorter=head;
- Product* nex=sorter->next;
- while(nex!=NULL)
- {
- int sc=sorter->getCode();
- int sv=sorter->getValue();
- int sp=sorter->getPrice();
- char* sn=sorter->getName();
- int nc=nex->getCode();
- int np=nex->getPrice();
- int nv=nex->getValue();
- char* nn=nex->getName();
- if(sc>nc)
- {
- swap(nc,sc);
- swap(nv,sv);
- swap(np,sp);
- swap(nn,sn);
- }
- else
- {
- sorter=nex;
- nex=nex->next;
- }
- }
- g++;
- }
- }
- int main()
- {
- ProductList List;
- //List.removeProduct(1);
- List.addProduct(2,3,2,"B");
- //ProductList::addProduct(1,1,3,"C");
- List.~ProductList();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement