假设某书店的前台收银销售类的定义如下:


2.假设某书店的前台收银销售类的定义如下:
 Class Sale :
//前台销售类
 private :
 int buycount ;//购买书的数量
 double total ;// 总价
 BookData book [1000], buy [100];///book 数组用于存储图书信息, buy 数组//用于存储客户购买的图书的信息
 public :
 Sale ();构造函数
 void addBook();向购物车中增加书籍
 void getBuy() ; 查看购物车
 void getCash();结算
 void getData();打开书库文件并将文件中的图书信息读取到内存中
void putData(); 在实验结束前,将图书信息写入书库文件中;
请完成该类的定义,并实现如下功能:
(1)向购物车中添加书籍:用户先输入书名查找要购买的书籍,系统会查找相关书籍并询问购买数量,用户输入数量后即可添加至购物车;(2)査看购物车:系统显示用户购物车中的书籍以及价格、数量等信息;
(3)结算:系统计算出用户需要支付的价格。

2022.3.10

该程序需要有一个名字为date.text且包含1000本书的文件

才能正常运行

 

#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
using namespace std;
int i=0;
class BookDate//书的数据 
{
	public:
		string bookname;
		int booknumber;
		int bookprice;
};
class Sale//前台销售类 
{
	private:
		int buycount;
		double total;
		BookDate book[1000],buy[100];
	public:
		Sale();
		void addBook();
		void getBuy();
		void getCash();
		void getDate(fstream &ofs);
		void putDate(fstream &ofs);
};
Sale::Sale()//初始数量金额为0 
{
	buycount=0;
	total=0;
}
void Sale::addBook()//添加购物车 
{
	string name;
	int num;
	cout<<"想要购买的书:";
	cin>>name;
	int j;
	for(j=0;j<1000;j++)
	{
		if(name==book[j].bookname)
		{
			cout<<"剩余"<<book[j].booknumber<<"本"<<endl;
			cout<<"单价"<<book[j].bookprice<<"元"<<endl;
			cout<<"想要购买的数量:" ;
			cin>>num;
			if(num<=book[j].booknumber)
			{
				buy[i].bookname=book[j].bookname;
				buy[i].booknumber=num;
				buy[i].bookprice=book[j].bookprice;
				book[j].booknumber-=num;
				i++;
				cout<<"已将该书添加到购物车!"<<endl;
			}
			else
			{
				cout<<"书本数量不足!"<<endl; 
			}
			break;
		}
	}
	if(j==1000)
	{
		cout<<"无该书"<<endl;
	}
}
void Sale::getBuy()//查看购物车 
{
	int j;
	if(i==0)
	{
		cout<<"购物车为空"<<endl; 
	}
	else
	{
		for(j=0;j<i;j++)
		{
			cout<<"书名:"<<buy[j].bookname<<endl;
			cout<<"购买数量:"<<buy[j].booknumber<<endl;
			cout<<"单价:"<<buy[j].bookprice<<endl;
		}
	}
}
void Sale::getCash()//结算 
{
	int sum=0,j;
	for(j=0;j<i;j++)
	{
		sum+=buy[j].booknumber*buy[j].bookprice;
	}
	cout<<"总价:"<<sum<<"元"<<endl;
}
void Sale::getDate(fstream &ofs)//打开书库文件并将图书信息读取到内存中 
{
	int j;
	ofs.open("date.txt",ios::out|ios::in);
	if(!ofs.is_open())
	{
		cout<<"false"<<endl;
		return;
	}
	for(j=0;j<1000;j++)
	{
		ofs<<book[j].bookname<<" "<<book[j].booknumber<<" "<<book[j].bookprice;
	}
	ofs.close();
}
void Sale::putDate(fstream &ofs)//结束时将图书信息写入书库文件 
{
	ofs.open("date.txt",ios::out);
	if(!ofs.is_open())
	{
		cout<<"false"<<endl;
		return;
	}
	for(int j=0;j<1000;j++)
	{
		ofs<<book[j].bookname<<" "<<book[j].booknumber<<" "<<book[j].bookprice;
	}
	ofs.close();
}
void menu() //菜单 
{
	int ch;
	Sale t;
	fstream ofs;
	t.getDate(ofs);
	while(1)
	{
		cout<<"请选择:"<<endl;
		cout<<"1:向购物车中增加书籍"<<endl;
		cout<<"2.查看购物车"<<endl;
		cout<<"3.结算"<<endl;
		cout<<"4.退出系统"<<endl;
		cin>>ch; 
		if((ch<=4) && (ch>=1))
		{
			switch(ch)
			{
				case 1:t.addBook();break;
				case 2:t.getBuy();break;
				case 3:t.getCash();break;
				case 4:t.putDate(ofs);exit(0);break;
			}
		}
		else
		{
			cout<<"输入错误";
		}

	}
}
int main()
{
	while(1)
	{
		menu();
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值