Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- #include <cstring>
- using namespace std;
- class Product{
- int code;
- int value;
- int price;
- char* name;
- Product *next;
- public:
- Product(int ncode, int nvalue, int nprice,const char* nname);
- ~Product();
- void print(){cout<<"\n"<< code <<"\t"<< value <<"\t"<< price <<"\t"<< name <<" \n";}
- ///accesors
- int getCode(){return code;}
- void modCode(int ncode){code=ncode;}
- int getValue(){return value;}
- void modValue(int nvalue){value=nvalue;}
- void addValue(int avalue){value+=avalue;}
- int getPrice(){return price;}
- int &returnPrice(){return price;}
- void modPrice(int nprice){price=nprice;}
- const char*getName(){return name;}
- void modName(const char*nname){
- char* del=name;
- delete[] del;
- char* c=new char[strlen(nname)+1];
- strcpy(c,nname);
- name=c;
- }
- void modNext(Product*nnext){next=nnext;}
- Product *getNext(){return next;}
- /// /accesors
- };
- class ProductList{
- private:
- Product *head;
- Product *tmp;
- Product *n;
- public:
- ProductList();
- ProductList(const ProductList &p);
- ~ProductList();
- ProductList& operator=(const ProductList& rhs);
- int addProduct(int code, int value, int price,const char* name);
- int removeProduct(int code);
- void print();
- int& modify(int code,const char *name);
- };
- /////////////////////////////
- Product::Product(int ncode, int nvalue, int nprice,const char* nname){
- code = ncode;
- value = nvalue;
- price = nprice;
- char* c=new char[strlen(nname)+1];
- strcpy(c,nname);
- name=c;
- print();
- }
- Product::~Product(){
- print();
- delete[] name;
- }
- ProductList::ProductList(){
- head=NULL;
- tmp=NULL;
- n=NULL;
- }
- ProductList::~ProductList(){
- n=head;
- while(n){
- n = n->getNext();
- //delete[] head->getName();
- delete head;
- head = n;
- }
- }
- /// /////////////////////////////////////////////////
- ProductList::ProductList(const ProductList &p){
- /* tmp=p.head;
- while(tmp!=NULL){
- addProduct(tmp->getCode(),tmp->getValue(),tmp->getPrice(),tmp->getName());
- tmp=tmp->getNext();
- }
- */
- *this = p;
- }
- ProductList& ProductList::operator=(const ProductList& rhs){
- Product* t=rhs.head;
- this->head=NULL;
- this->tmp=rhs.head;
- this->n=NULL;
- if (this!=&rhs){
- n=this->head;
- while(n){
- n = n->getNext();
- //delete[] head->getName();
- delete this->head;
- this->head = n;
- }
- this->head=NULL;
- this->tmp=rhs.head;
- while(t!=NULL){
- this->addProduct(t->getCode(),t->getValue(),t->getPrice(),t->getName());
- t = t->getNext();
- }
- }
- return *this;
- }
- /// ///////////////////////////////////////////////////////
- /////////////////////////////
- ///MAIN//////////////////
- int main(){
- ProductList ListA;
- ProductList* ListB = new ProductList;
- ListA.removeProduct(1);
- ListA.addProduct(1,2,3,"test_name_1");
- ListA.print();
- ListA.addProduct(2,2,3,"test_name_2");
- ListA.addProduct(1,2,3,"test_name_1");
- ListA.addProduct(1,2,3,"test_name_1");
- ListA.modify(2,"testtest");
- ListA.removeProduct(1);
- ListA.print();
- ListA.addProduct(1,2,3,"test_name_1");
- ListA.print();
- ProductList ListC(ListA);
- /*
- ListB->removeProduct(1);
- ListB->addProduct(1,2,3,"2test_name_1");
- ListB->print();
- ListB->addProduct(2,2,3,"2test_name_2");
- ListB->addProduct(1,2,3,"2test_name_1");
- ListB->addProduct(1,2,3,"2test_name_1");
- ListB->modify(2,"2testtest");
- ListB->removeProduct(1);
- ListB->print();
- ListB->addProduct(1,2,3,"2test_name_1");
- ListB->print();
- delete ListB;
- */
- return 0;
- }
- /// END OF MAIN///////////////////////////////////////////////////////////////////////////////
- int ProductList::addProduct(int code, int value, int price,const char* name){
- cout<<"Adding: "<<code<<"\t"<<value<<"\t"<<price<<"\t"<<name;
- if (head == NULL || head->getCode() > code){
- n = new Product(code, value, price, name);
- // n = new Product();
- // n->modCode(code);
- // n->modValue(value);
- // n->modPrice(price);
- // n->modName(name);
- n->modNext(head);
- head = n;
- }
- else{
- tmp = head;
- while (tmp->getNext() && tmp->getNext()->getCode() < code){
- tmp = tmp->getNext();
- }
- //cout<<tmp->code<<endl;
- if (tmp->getCode() == code){ //if there is a product of this code
- if((!strcmp(tmp->getName(),name)) && ((tmp->getPrice())==price)){ //if code and name is the same change value
- tmp->addValue(value);
- cout<<endl;
- return 1;
- }
- else{ //if only code is the same update everything
- // tmp->value=value;
- // tmp->price=price;
- //strcpy(tmp->name,name);
- cout<<" Failed\n";
- return 0;
- }
- }
- else{
- n = new Product(code, value, price, name);
- // n = new Product();
- // n->modCode(code);
- // n->modValue(value);
- // n->modPrice(price);
- // n->modName(name);
- n->modNext(tmp->getNext());
- tmp->modNext(n);
- }
- }
- cout<<endl;
- return 1;
- }
- int ProductList::removeProduct(int code){
- Product* curr;
- Product* del=NULL;
- tmp=head;
- curr=head;
- while(curr!=NULL && curr->getCode()!=code){
- tmp=curr;
- curr=curr->getNext();
- }
- if(curr==NULL){
- cout<<"Product was not in the list\n";
- return 0;
- }
- else{
- del=curr;
- curr=curr->getNext();
- tmp->modNext(curr);
- if(del==head){
- head=head->getNext();
- tmp=NULL;
- }
- cout<<"Product " << del->getCode() << " was deleted\n";
- delete del;
- return 1;
- }
- }
- void ProductList::print(){
- tmp=head;
- cout<<"---------------------------\n";
- cout<<"Code\t"<<"Value\t"<<"Price\t"<<"Name\t\n";
- while(tmp!=NULL){
- //cout<< tmp->getCode() <<"\t"<< tmp->getValue() <<"\t"<< tmp->getPrice() <<"\t"<< tmp->getName() <<" \n";
- tmp->print();
- tmp=tmp->getNext();
- }
- cout<<"---------------------------\n";
- }
- int& ProductList::modify(int code,const char *name){
- if (head!=NULL){
- tmp = head;
- if (tmp->getCode()!=code){
- while (tmp->getNext()!=NULL && tmp->getNext()->getCode() == code){
- tmp = tmp->getNext();
- }
- }
- if (tmp->getCode() == code){
- if (!strcmp(tmp->getName(),name)){
- cout<<"Modify("<< code << ", "<<name<<")\n";
- return tmp->returnPrice();
- }
- else{
- //strcpy(tmp->name,name);
- /*delete[] tmp->getName();
- char* c=new char[strlen(name)+1];
- strcpy(c,name);
- tmp->modName(c);*/
- tmp->modName(name);
- //////////////////////////
- cout<<"Modify("<< code << ", "<<name<<")\n";
- return tmp->returnPrice();
- }
- }
- }
- addProduct(code, 0, 0, name);
- tmp = head;
- while (tmp->getNext()!=NULL && tmp->getNext()->getCode() == code){
- tmp = tmp->getNext();
- }
- cout<<"Modify("<< code << ", "<<name<<")\n";
- return tmp->returnPrice();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement