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

📄 customer.cpp

📁 标准C++编写的小小CRM软件,无任何平台依赖.采用标准XML作为数据库.只需重新纺译,可在任何平台上运行.目前测试过在GCC和VC下都可以编译通过
💻 CPP
字号:
#include "Customer.h"

#define wordsize	16
#define bufsize		128

Customer::Customer(void)
{
	initFormat();
	init();
}

Customer::~Customer(void)
{
	save();
}

bool Customer::init()
{
	string buf,word;
	string first;
	ifstream dbFile;
	struct stuMan man; 
	typedef pair<string, string> str_Pair;

	dbFile.open( "customer.xml" );
	if( dbFile.fail() ){	
		cerr<<"customer database cann't found."<<endl;
		ready = false;
	}
	while( getline( dbFile, buf, '>' ) ){
		if( buf.substr( buf.find( '<' ) ) == "<Customer_Database" ){	// match keyword
			while( getline( dbFile, buf, '>' ) ){
				buf.erase( 0, buf.find( '<' ) + 1 );
				istrstream bufStream( buf.data(), buf.size() );
				bufStream >> word;
				if( word == "Customer" ){
					/* read Customer's attribute. */
					while( bufStream>>word ){
						if( word.find( "id" )==0 ){	// find 'id' in first position.
							word.erase( 0, word.find('\"')+1 );
							word.erase( word.find('\"'), word.length() );
							man.id=toInt( word);
							if( man.id > maxID ){	maxID = man.id;	}
						}
						else if( word.find( "weight" )==0 ){	// find 'weight' in first position.
							word.erase( 0, word.find('\"')+1 );
							word.erase( word.find('\"'), word.length() );
							man.weight=toInt( word );
						}
						else
						{	cerr<<"unknown attribute"<<endl;	}
					}
					/* read customer's message */
					while( getline( dbFile, buf, '>' ) ){
						buf.erase( 0, buf.find( '<' ) + 1 );
						if( buf == "/Customer" )
						{	break;	}
						first=buf;
						if( getline( dbFile, buf, '<' ) ){
							man.msgMap.insert( str_Pair( first,buf ) );	}
						getline( dbFile, buf, '<' );	// goto next message.
					}
					CMan.push_back( man );
					man.msgMap.clear();
				}
			}
		}
	}

	return ready;
}

bool Customer::initFormat()
{
	ifstream formatFile( "customerFormat.xml" );
	string buf;
	string first;
	typedef pair<string, string> str_Pair;

	if( formatFile.fail() ){
		cerr<<"Open format define file error."<<endl;
		return false;
	}
	while( getline( formatFile,buf,'>' ) ){
		if( buf.substr( buf.find('<') ) == "<Customer_Database_Format_Define"  ){
			while( getline( formatFile, buf, '>' ) ){
				first=buf.substr( buf.find( '<' ) + 1 );
				if( first == "/Customer_Database_Format_Define" )
				{	return true;	}		// reading complete
				else{
					if( getline( formatFile, buf, '<' ) )
					{	formatMap.insert( str_Pair( first,buf ) );	}
					else
					{	cerr<<"unmatch format."<<endl;	}
				}
				getline( formatFile, buf, '<' );
			}
		}
	}

	return false;
}

bool Customer::save()
/* storage database file and format define file back to disk.	*
 * success return true, else return false.						*/
{
	ofstream formatFile( "customerFormat.xml" );
	ofstream dbFile( "customer.xml" );
	typedef pair<string, string> str_Pair;
	list < stuMan >::const_iterator man_Iter;
	map<string,string>::const_iterator str_Iter;

	if( formatFile.fail() || dbFile.fail() ){
		cerr<<"Open file error."<<endl;
		return false;
	}
	else{
		/* save datebase file */
		dbFile << "<?xml version=\"1.0\" encoding=\"gb2312\"?>" << endl;
		dbFile << "<Customer_Database>" << endl;
		for( man_Iter = CMan.begin(); man_Iter != CMan.end(); man_Iter++ )
		{
			dbFile << "<Customer id=\"" << man_Iter->id 
				<< "\" weight=\"" << man_Iter->weight << "\">" <<endl;
			for( str_Iter = ( man_Iter->msgMap ).begin(); 
				str_Iter != ( man_Iter->msgMap ).end(); str_Iter++ )	{
					dbFile << '<' << str_Iter->first << '>' << str_Iter->second
						<< "</" << str_Iter->first << '>' <<endl;
			}
			dbFile << "</Customer>" << endl;
		}
		dbFile << "</Customer_Database>";
		/* save fomat define file */
		formatFile << "<?xml version=\"1.0\" encoding=\"gb2312\"?>" << endl;
		formatFile << "<Customer_Database_Format_Define>" << endl;
		for( str_Iter = formatMap.begin(); str_Iter != formatMap.end(); str_Iter++ ){
			cout << '<' << str_Iter->first << '>' << str_Iter->second
				<< "</" << str_Iter->first << '>' <<endl;
		}
		formatFile << "</Customer_Database_Format_Define>";
	}
	return true;
}

bool Customer::add()					
/* add a customer to database, success return true,		*
 * else return false.									*/ 
{
	struct stuMan man;
	string  first,second;
	typedef pair<string, string> str_Pair;

	man.id = ++maxID;
	man.weight = defaultWeight ;
	cout << " please Input user's Arttribute with Value. end with\" no more\"" << endl;
	while( cin >> first >> second ){
		if( first == "no" && second == "more" )
		{	break;		}
		man.msgMap.insert( str_Pair( first, second ) );	
	}

	if( man.msgMap.empty() ){
		cout << " you input nothing " << endl;
		return false;
	}
	else
	{	return add( man );	}
}

bool Customer::add( const map<string,string>& attr_Map ){
	struct stuMan man;

	if( attr_Map.empty() ){
		cout << "There are no message in you input. " << endl;
		return false;
	}
	else{
		man.msgMap = attr_Map;
		man.id = ++maxID;
		man.weight = defaultWeight;
		return add( man );
	}
}

bool Customer::add( const struct stuMan& man )	
/* add a customer to database, success return true,		*
 * else return false.									*/ 
{
	list< stuMan >::iterator man_Iter = find( man.id );

	if( man_Iter == end() ){
		CMan.push_back( man );	
		return true;
	}
	else{
		cout << "Sorry, but the ID have been in used" <<endl;
		return false;
	}
}


bool Customer::chg( int id )
/* change a customer's message, success return true.	*
 * else return false.									*/
{
	list< stuMan >::iterator man_Iter = find( id );
	string  attribute, value;
	map< string, string >::iterator str_Iter;
	typedef pair<string, string> str_Pair;

	if( man_Iter == end() ){
		cout << "Sorry, but customer can't find. "<<endl;
		return false;
	}
	else{
		cout << "which attribute you want to change: ";
		cin >> attribute;
		cout << "please input new value: ";
		cin >> value;
		str_Iter = ( man_Iter->msgMap ).find( attribute );
		if( str_Iter == ( man_Iter->msgMap ).end() )	// can't find this attribute
		{	( man_Iter->msgMap ).insert( str_Pair( attribute, value ) );	}	// add attrubute
		else{	
			( man_Iter->msgMap ).erase( str_Iter );
			( man_Iter->msgMap ).insert( str_Pair( attribute,value ) );
		}
		return true;
	}
}

bool Customer::del( int id )
/* delete a customer to database, success return true,	*
 * else return false.									*/ 
{
	list< stuMan >::iterator man_Iter = find( id );

	if( man_Iter == end() ){
		cout << "Sorry, but customer can't find. "<<endl;
		return true;
	}
	else{
		CMan.erase( man_Iter );
		return true;
	}
 }

Customer::Iterator Customer::begin()
{	return CMan.begin();	}

Customer::Iterator Customer::end()
{	return CMan.end();		}

bool Customer::display( int id )				// dispaly the usr with this ID's message
{
	list< stuMan >::const_iterator man_Iter;
	map< string, string >::const_iterator msg_Iter;


	for( man_Iter = CMan.begin(); man_Iter != CMan.end(); man_Iter++ ){
		if( man_Iter->id == id ){
			cout.setf( ios::left );
			cout << setw(16) << "id" 
				<< man_Iter->id << endl;
			cout <<  setw(16) << "weightiness" 
				<< man_Iter->weight << endl;
			for( msg_Iter = (man_Iter->msgMap).begin(); 
				msg_Iter != (man_Iter->msgMap).end(); msg_Iter++ )	{
					cout <<  setw(16) << msg_Iter->first 
						<< msg_Iter->second <<endl;
				}
				cout << endl;
		}
	}

	return true;	
}

bool Customer::display( const map<string,string>& cnd_Map )	
/*  display the meesage of user who fix the condition */
{
	list< stuMan >::const_iterator man_Iter;
	map< string, string >::const_iterator msg_Iter1,msg_Iter2;
	bool findSign = false;

	for( man_Iter = CMan.begin(); man_Iter != CMan.end(); man_Iter++ ){
		for( msg_Iter1 = cnd_Map.begin(); msg_Iter1 != cnd_Map.end(); msg_Iter1++ )	{
			msg_Iter2 = ( man_Iter->msgMap ).find ( msg_Iter1->first );
			if( msg_Iter2->second != msg_Iter1->second )
			{	break;		}		// condition unfix.
		}
		if( msg_Iter1 == cnd_Map.end() ){	// all cindition fix
			findSign = true;
			display( man_Iter->id );	
		}
	}

	return findSign;	
}

bool Customer::display( const string& name )	// display this usr's message.
{
	list< stuMan >::const_iterator man_Iter;
	map< string, string >::const_iterator msg_Iter;
	bool findSign = false;

	for( man_Iter = CMan.begin(); man_Iter != CMan.end(); man_Iter++ ){
		if(  ( man_Iter->msgMap ).find( "name" )->second == name ){
			findSign = true;
			display( man_Iter->id );
		}
		/* 
		msg_Iter = ( man_Iter->msgMap ).find( "name" );
		if( msg_Iter->second == name ){	// find customer
			findSign = true;
			display( man_Iter->id );
		} */
	}

	if( findSign == false )
	{	cout << "Sorry, but this customer can't be find." << endl;	}
	return findSign;	
}

bool Customer::displayf( const string& name )
/* display the message of usrs whose name look like as the name.	*
 * success return true, else reuturn false.							*/
{
	list< stuMan >::const_iterator man_Iter;
	map< string, string >::const_iterator msg_Iter;
	bool findSign = false;

	for( man_Iter = CMan.begin(); man_Iter != CMan.end(); man_Iter++ ){
		msg_Iter = ( man_Iter->msgMap ).find( "name" );
		if( msg_Iter != ( man_Iter->msgMap ).end() ){	// find customer
			if( msg_Iter->second.find( name ) != -1 ){			
				findSign = true;
				display( man_Iter->id );	
			}
		}
	}
	if( findSign == false )
	{	cout << "Sorry, but this customer can't be find." << endl;	}

	return findSign;	
}

Customer::Iterator Customer::find( int id )
/* check the customer exist in database */
{
	list< stuMan >::iterator man_Iter;

	for( man_Iter = CMan.begin(); man_Iter != CMan.end(); man_Iter++ ){
		if( man_Iter->id == id )
		{	break;		}
	}

	return man_Iter;
}

⌨️ 快捷键说明

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