小程序练习...“微小书店”程序...

/*
	author: marrywindy
	purpose: 小书店管理
	data:     2011-1-19
*/
#include "stdafx.h"
#include <iostream>
#include <map>
#include <vector>
#include <string>
using namespace std;

// 图书管理类,复杂图书的入库,根据ISBN查询图书的价格,统计书库中书籍的总册数,显示书库中的全部图书,售出书时要立即更新库存
class Books
{
public:
	Books()
	{
		mISBN="";
		mPrice=0.0;
		mCount=0;
	}
	~Books()
	{

	}
	// 图书入库
	void pushBooks(string strISBN, double price)
	{
		mMap.insert(make_pair<string,double>(strISBN, price));
		mCount++;
	}
	// 显示书库中全部的图书信息
	void printInfo()
	{
		cout<<"当前书库的全部信息"<<endl;
		map<string, double>::iterator iter;
		for (iter=mMap.begin() ; iter!=mMap.end(); iter++)
		{
			cout<<iter->first<<"  ";
			cout<<iter->second<<endl;
		}
		cout<<"Total numbers of the books: "<<mCount<<endl;
	}
	// 根据ISBN返回这个书的价格
	double getPrice(string strISBN)
	{
		map<string , double>::iterator iter=mMap.begin();
		for ( ; iter!=mMap.end();iter++)
		{
			if((iter->first)==strISBN)
			{
				return (iter->second);
			}
		}
	}
	// 根据售出书的情况来更新库存图书信息
	void updateBooks(string strISBN)
	{
		map<string , double>::iterator iter=mMap.begin();
		while (iter!=mMap.end())
		{
			if ((iter->first) == strISBN)
			{
				mMap.erase(iter);
				mCount--;
				break;
			}
			else
				iter++;
		}
	}
private:
	string mISBN;
	double   mPrice;
	int      mCount;
	map<string, double> mMap;
private:
	Books(const Books& );
	Books& operator=(const Books& );
};

// 顾客类 顾客可以买书,书籍用ISBN来唯一标识
class Customer
{
public:
	Customer(Books*& books)
	{
		mSum=0.0;
		mCount=0;
		mBooks=books;
	}
	~Customer()
	{

	}
	// 顾客喜欢的书,直接放入篮子里
	void buyBooks(string strISBN)
	{
		mMap.insert(pair<string , double>(strISBN , mBooks->getPrice(strISBN)));
		mSum+=mBooks->getPrice(strISBN);
		mBooks->updateBooks(strISBN);
		mCount++;
	}
	// 顾客结账
	void calculate()
	{
		cout<<"顾客所购买的书:"<<endl;
		map<string , double>::iterator iter=mMap.begin();
		for ( ; iter!=mMap.end(); iter++)
		{
			cout<<iter->first<<"   "<<iter->second<<endl;
		}
		cout<<"The amouts of saled books:"<<mCount<<endl;
		cout<<"The total price is:"<<mSum<<endl;
		cout<<"The average price is: "<<(double)(mSum/mCount)<<endl;
	}
private:
	Books* mBooks;
	map<string , double> mMap;
	double  mSum;
	int        mCount;
private:
	Customer(const Customer& );
	Customer& operator=(const Customer& );
};

int _tmain(int argc, _TCHAR* argv[])
{
	// 图书入库
    Books* books=new Books();
	books->pushBooks("isbn001", 12.2);
	books->pushBooks("isbn002", 15.7);
	books->pushBooks("isbn003", 15.2);
	books->pushBooks("isbn004", 28.5);
	books->pushBooks("isbn005", 34.6);
	books->pushBooks("isbn006", 50.6);
	books->pushBooks("isbn007", 45.3);
	books->printInfo();
	//顾客购书
	Customer* cust=new Customer(books);
	cust->buyBooks("isbn002");
	cust->buyBooks("isbn005");
	cust->calculate();
	//显示更新后的书库信息
	books->printInfo();
	delete books;
	delete cust;
	return 0;
}




posted on 2011-01-19 11:00  marrywindy  阅读(487)  评论(0编辑  收藏  举报

导航