Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- #include <cstring>
- using namespace std;
- ///Working with default constructors and destructors
- class Product{
- int code;
- int value;
- int price;
- char* name;
- Product *next;
- public:
- //Product();
- //Product(int code, int value, int price,const char* name);
- Product(int ncode, int nvalue, int nprice,const char* nname);
- ~Product();
- void print(){cout<<"\nProduct: "<< code <<"\t"<< value <<"\t"<< price <<"\t"<< name <<" \n";}
- 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){
- delete[] name;
- char* c=new char[strlen(nname)+1];
- strcpy(c,nname);
- name=c;
- }
- void modNext(Product*nnext){next=nnext;}
- Product *getNext(){return next;}
- /* int getName(){return name;}
- int getNext(){return next;}*/
- };
- class ProductList{
- private:
- Product *head;
- Product *tmp;
- Product *n;
- public:
- ProductList();
- ~ProductList();
- 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;
- }
- }
- 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;
- }
- 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=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);
- //////////////////////////
- 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();
- }
- /////////////////////////////
- ///MAIN//////////////////
- int main(){
- /*
- ProductList List;
- List.removeProduct(1);
- List.addProduct(1,2,3,"test_name_1");
- List.print();
- List.addProduct(2,2,3,"test_name_2");
- List.addProduct(1,2,3,"test_name_1");
- List.addProduct(1,2,3,"test_name_1");
- List.modify(2,"testtest");
- List.removeProduct(1);
- List.print();
- List.addProduct(1,2,3,"test_name_1");
- List.print();
- //List.~ProductList();
- List.print();
- */
- ProductList ListA;
- ProductList* ListB = new ProductList;
- ListA.addProduct(1,1,1,"product1");
- ListA.removeProduct(1);
- ListA.print();
- ListB->addProduct(2,1,1,"product2");
- ListB->print();
- ListB->removeProduct(2);
- delete ListB;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement