类的设计与运用:图书商店管理系统

需要完成的设计以及功能如下:

为一家书店编写一个简单的书籍管理程序,要求用两个类实现:

1)编写一个表示书籍信息的类Book。

1.1)用私有成员变量表示以下五项信息:标题(用string类的对象表示)、作者(用string类的对象表示)、页码、价格以及存量。

1.2)编写一个用于展示书籍信息的公有成员函数,在控制台上用一行显示上述五项信息。

1.3)为每一个私有成员变量编写一个公有成员函数,用于返回私有成员变量的值。

1.4)编写一个公有成员函数修改书籍的价格。

1.5)编写一个公有成员函数模拟入库操作,用于增加该书籍的库存数量,该函数的参数是入库量。

1.6)编写一个公有成员函数模拟销售操作,用于减少该书籍的库存数量,该函数的参数是销售量,注意:销售量不能超过库存量,函数返回实际销售的数量。

2)编写一个书店类BookShop。要求:

2.1) 用一个私有成员表示该书店的所有书籍信息。该成员变量的类型是vector<Book>。请包含头文件<vector>。

2.2)编写一个公有成员函数,为书店添加一个书籍信息(即Book对象),调用vector::push_back函数实现。

2.3)编写一个公有成员函数,展示该书店中所有书籍信息,每一行展示一条书籍记录,并在最后另起一行展示书籍总数和总价。

2.4)编写一个公有成员函数,用于在控制台上输出一个书籍记录。该函数的参数是书籍的索引号(即Book对象在vector<Book>数组中的下标),通过调用Book::show()实现。

2.5)编写一个公有成员函数,返回书店中的书籍总数。

2.6)编写一个公有成员函数,模拟销售操作。函数参数是书籍的索引号,以及销售册数,返回销售收入。在该函数中向控制台输出一条销售记录,记录内容为所销售的书名、册数以及销售额。

2.7)编写一个公有成员函数,模拟入库操作。函数参数是书籍的索引号,以及入库册数。在该函数中向控制台输出一条入库记录,记录内容为所入库的书名以及册数。

3) 在main函数中定义BookShop对象,并添加书籍记录信息、并展示书店书籍信息、执行书籍销售和入库操作。

#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Book//定义一个class,对象内容为一种书的各种属性
{
public:

	void info()
	{
		cout << "Title:" << title << ",";
		cout << "author:" << author << ",";
		cout << "page:" << page << ",";
		cout << "count:" << count << ",";
		cout << "price:" << price <<"RMB"<< endl<<endl;
	}

	Book(const string& _title, const string& _author, const int& _page, const int& _count, const double& _price) :
		title(_title), author(_author), page(_page), count(_count), price(_price) {}


	string retitle() { return title; }
	string reauthor() { return author; }
	int repage() { return page; }
	int recount() { return count; }
	double reprice() { return price; }

	void changeprice()
	{
		cout << "Please input the order of the book" << endl;
		int num;
		cin >> num;
		cout << "Please input the price after changing" << endl;
		double changep;
		cin >> changep; price = changep;
		cout << "Change successfully" << endl;

	}



	void enstore(int Count)
	{
		cout << "please input the encount" << endl;
		count += Count;
	}

	int sell(int sellnum)
	{
		if (sellnum > count)
		{
			sellnum = count;
			count = 0;
			return sellnum;
		}
		else
			count = count - sellnum;
		return sellnum;

	}

private:
	string title, author;
	int page, count;
	double price;

};


//定义一个class,对象内容是书店的各种操作
class bookshop
{


public:

	void add()
	{
		cout << "Next please add the title,author,page,count and price of the book " << endl;
		cout << "===================================" << endl;
		string titles, authors;
		int pages, counts;
		double prices;
		cin >> titles >> authors >> pages >> counts >> prices;
		bookarr.push_back(Book(titles, authors, pages, counts, prices));
	}

	void show(int i)
	{
		bookarr[i].info();
	}


	void printall()
	{
		cout << "All Books:" << endl;
		double sumprice = 0;
		int sumcount = 0;
		for (int i = 0; i < 4; i++)
		{
			show(i);
			sumcount += bookarr[i].recount();
			sumprice += bookarr[i].reprice() * bookarr[i].recount();
			cout << endl;
		}
		cout << "All numbers:" << sumcount << endl;
		cout << "All prices:" << sumprice <<"RMB"<< endl;
		cout << "===================================" << endl<<endl;
	}


	int resumnum()
	{
		int sumcount = 0;
		for (int i = 0; i < 4; i++)
		{
			sumcount += bookarr[i].recount();
			cout << endl;
		}
		return sumcount;

	}

	double sellmod(int i, int sellnum)
	{
		sellnum = bookarr[i].sell(sellnum);
		double sum = bookarr[i].reprice() * sellnum;
		cout << "Sold: " << sellnum << bookarr[i].retitle() << "," << "Sum= " << sum <<"RMB"<< endl;
		cout << "===================================" << endl;
		return sum;

	}

	void enstoremod(int i, int encount)
	{
		bookarr[i].enstore(encount);
		cout << "Restock: " << encount << bookarr[i].retitle() << endl;
		cout << "===================================" << endl;
	}

private:
	vector<Book> bookarr;
};

int main()
{
	
	bookshop shop1;//开了一个名为shop1的书店!
	vector<Book> bookarr;
	for (int i = 0; i < 4; i++)
//添加4种书,此处也可以通过while来读入书籍并添加终止符EOF等,来设计添加任意种图书
    shop1.add();
//此处我曾经写成了bookshop.add();这样是不对的,bookshop只是一个class的类型,本身一个对象,因此需要前声明的bookshop类型的shop1进行调用类里面公用成员函数。
//以下为对商店的各种操作:
	shop1.printall();
	shop1.sellmod(0, 10);
	shop1.sellmod(1, 13);
	shop1.sellmod(2, 17);
	shop1.enstoremod(1, 20);
	shop1.printall();
	return 0;

}

注:主要运用了类的设计以及vector的用法,vector用法可见:(24条消息) C++ vector的用法(整理)_一个幽默且帅气的程序员的博客-CSDN博客_c++ vector

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值