⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 product_mgr.h

📁 第一次上传
💻 H
字号:
#include "product_node.h"
class product_mgr{                                                                                  //商品管理链表
public:
	product_node* head;                                                                             //头指针
	product_mgr();                                                                                  //构造函数
	product_mgr(string n_mgr_product,string n_mgr_property,string c_mgr_property);                  //构造函数
	product_mgr(string n_mgr_product,string n_mgr_property,int c_mgr_property);                     //构造函数
	bool itsempty();                                                                                //链是否为空
	int num_product(string n_mgr_product);                                                   //返回名称叫n_mgr_product的商品数目
	product_node* get_prenode(product_node* p);                                              //返回P的前一个结点
	product_node* re_node_nameed(string n_mgr_product);                                      //返回名称叫n_mgr_product的商品结点
	bool add_char_property(string n_mgr_product,string n_property_product,string c_property_product);//添加属性
	bool add_int_property(string n_mgr_product,string n_property_product,int c_property_product);    //添加属性
	bool add_char_all_property(string n_property_product,string c_property_product);                                                            //给所有商品添加属性
	bool add_int_all_property(string n_property_product,int c_property_product);                                                             //给所有商品添加属性
	bool delete_char_property(string n_mgr_product,string n_property_product);               //删除属性
	bool delete_int_property(string n_mgr_product,string n_property_product);                //删除属性
	string get_char_property(string n_mgr_product,string n_property_product);                //取得字符属性值
	int get_int_property(string n_mgr_product,string n_property_product);                    //取得数值属性值
	bool add_product_1(string n_mgr_product);                                                //添加名为n_mgr_product的商品
	bool delete_product(string n_mgr_product);                                               //删除名为n_mgr_product的商品
	void display_property_product(string n_mgr_product);                                     //打印商品n_mgr_product的所有属性
	void display_product();                                                                  //输出所有商品信息
};
product_mgr::product_mgr(){
	head=new product_node();
}

product_mgr::product_mgr(string n_mgr_product,string n_mgr_property,string c_mgr_property){
	head=new product_node();
	head->next_p=new product_node(n_mgr_product,n_mgr_property,c_mgr_property);
}

product_mgr::product_mgr(string n_mgr_product,string n_mgr_property,int c_mgr_property){
	head=new product_node();
	head->next_p=new product_node(n_mgr_product,n_mgr_property,c_mgr_property);
}

bool product_mgr::itsempty(){
	if(head->next_p==NULL)
		return true;
	else
		return false;
}

product_node* product_mgr::get_prenode(product_node* p){
	product_node* temp=head->next_p;
	if(!itsempty()){
		while(temp->next_p!=p&&temp->next_p!=NULL)
			temp=temp->next_p;
		if(temp->next_p==p)
			return temp;	
	}
	return head;
}

product_node* product_mgr::re_node_nameed(string n_mgr_product){	
	if(num_product(n_mgr_product)==0)
		return NULL;
	else{
		product_node* tp=head->next_p;
		if(tp->p_data.name_product==n_mgr_product)
			return tp;
		else if(tp->next_p==NULL)
			return NULL;
		else{
			while(1){
				if(tp->p_data.name_product==n_mgr_product)					
					return tp;				
				tp=tp->next_p;
				if(tp==NULL)
					break;
			}
		}
	}
	return NULL;
}


int product_mgr::num_product(string n_mgr_product){
	int i=0;
	product_node* tp=head->next_p;
	if(!itsempty()){
		while(1){
			if(tp->p_data.name_product==n_mgr_product)
				i++;
			tp=tp->next_p;
			if(tp==NULL)
				break;
		}
	}
	return i;
}

bool product_mgr::delete_product(string n_mgr_product){
	if(num_product(n_mgr_product)==0)
		return false;
	else{
		product_node* tp=re_node_nameed(n_mgr_product);
		if(tp!=NULL){
			if(tp->many==1){
				if(tp==head->next_p){
					head->next_p=tp->next_p;
					delete tp;
					return true;
				}
				else{
					product_node* tp_pre=get_prenode(tp);
					tp_pre->next_p=tp->next_p;
					delete tp;
					return true;
				}
			}
			else{
				tp->many--;
				return true;
			}
		}
		else
			return false;
	}
}





string product_mgr::get_char_property(string n_mgr_product,string n_property_product){
	if(num_product(n_mgr_product)==0){
		string s;
		s="error";
		return s;
	}
	else{
		product_node* tp=re_node_nameed(n_mgr_product);		
		tp->p_data.char_mgr->get_property_mgr(n_property_product);
	}
}

int product_mgr::get_int_property(string n_mgr_product,string n_property_product){
	if(num_product(n_mgr_product)==0){
		return 0;
	}
	else{
		product_node* tp=re_node_nameed(n_mgr_product);		
		tp->p_data.int_mgr->get_property_mgr(n_property_product);
	}
}




bool product_mgr::add_product_1(string n_mgr_product){	
	product_node* newp=new product_node(n_mgr_product);
	if(!itsempty()){
		product_node* tp=head->next_p;
		if(num_product(n_mgr_product)==0){
			while(tp->next_p!=NULL)
				tp=tp->next_p;			
			tp->next_p=newp;
			return true;
		}
		else{
			product_node* t=re_node_nameed(n_mgr_product);
			t->many++;
			return true;
		}
	}
	else{
		head->next_p=newp;
		return true;
	}
}

bool product_mgr::add_char_property(string n_mgr_product,string n_property_product,string c_property_product){
	if(num_product(n_mgr_product)==0)
		return false;
	else{
		product_node* tp=re_node_nameed(n_mgr_product);		
		if(tp->p_data.char_mgr->add_property(n_property_product,c_property_product))
			return true;
		else
			return false;
		
	}
}

bool product_mgr::add_int_property(string n_mgr_product,string n_property_product,int c_property_product){
	if(num_product(n_mgr_product)==0)
		return false;
	else{
		product_node* tp=re_node_nameed(n_mgr_product);		
		if(tp->p_data.int_mgr->add_property(n_property_product,c_property_product))
			return true;
		else
			return false;
	}
	
}

bool product_mgr::add_char_all_property(string n_property_product,string c_property_product){
	if(!itsempty()){
		product_node* temp=head->next_p;
		while(1){
			temp->p_data.char_mgr->add_property(n_property_product,c_property_product);			
			if(temp->next_p==NULL)
				break;
			temp=temp->next_p;
		}
		return true;
	}
	else
		return false;
}

bool product_mgr::add_int_all_property(string n_property_product,int c_property_product){
	if(!itsempty()){
		product_node* temp=head->next_p;
		while(1){
			temp->p_data.int_mgr->add_property(n_property_product,c_property_product);			
			if(temp->next_p==NULL)
				break;
			temp=temp->next_p;
		}
		return true;
	}
	else
		return false;
}

bool product_mgr::delete_char_property(string n_mgr_product,string n_property_product){
	product_node* tp=re_node_nameed(n_mgr_product);
	if(tp!=NULL)
		if(tp->p_data.char_mgr->delete_property(n_property_product))			
			return true;		
	return false;
}

bool product_mgr::delete_int_property(string n_mgr_product,string n_property_product){
	product_node* tp=re_node_nameed(n_mgr_product);
	if(tp!=NULL)
		if(tp->p_data.int_mgr->delete_property(n_property_product))
			return true;
	return false;
}

void product_mgr::display_property_product(string n_mgr_product){
	product_node* tp=re_node_nameed(n_mgr_product);
	if(tp!=NULL){
		tp->p_data.char_mgr->displaynode();
		tp->p_data.int_mgr->displaynode();		
	}
	else
		cout<<"没有此商品!\n";
}

void product_mgr::display_product(){
	if(itsempty())
		cout<<"无任何商品!\n";
	else{
		product_node* tp=head->next_p;
		while(tp->next_p!=NULL){
			cout<<"商品名称:";
			cout<<tp->p_data.name_product<<"  库存"<<tp->many<<"件"<<endl;
			tp->p_data.char_mgr->displaynode();
			tp->p_data.int_mgr->displaynode();
			tp=tp->next_p;
		}
		cout<<"\n商品名称:";
        cout<<tp->p_data.name_product<<"  库存"<<tp->many<<"件"<<endl;
		tp->p_data.char_mgr->displaynode();
		tp->p_data.int_mgr->displaynode();		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -