C++项目实战(一)——简单商品销售系统实现

本项目通过使用win32控制台应用程序实现一个非常简单的商品销售系统,主要涉及的知识点包含有:类的设计与使用、文件流操作、标准模板库的使用。

简单商品销售系统:
需求分析:
我们需要实现一个 能进货、能售货的简单商品销售系统,根据面向对象的理念,思路如下:
1、设计一个 commodity 类,用来实现 商品的基本属性、获取商品基本属性的接口。(即    包含有:商品的种类、 商品的库存量、商品的进货价、商品的售出价,获取商品基本属
  性的接口等)
2、设计一个 trade 类,用来实现 商品的数据链表、获取商品操作以及保存信息的接口。(即
  包含有:商品数据链表,获取本地商品信息、商品进货及售出交易信息、更新商品库存信
  息等)
3、在 main 主程序中使用 win32 控制台实现用户与操作系统间的界面交互,主要为:各种商
  品操作提示信息,运用以上两个类操作简单商品销售系统等。

代码实现:
头文件:
1、commodity.h

#include <string.h>

class commodity
{
	private:
		int static kinds; //商品种类
		int ID; //商品编号
		int stock; //商品库存量
		float buyValue; //商品进货单价
		float sellValue; //商品售出单价
		char name[30]; //商品名称

	public:
		int getID(); //获取商品编号
		int getStock(); //获取商品库存量
		float getBuyValue(); //获取进货单价
		float getsellValue(); //获取售出单价
		char* getName(); //获取商品名称
		
		void UpdateStock(int n); //更新商品库存量

		friend class trade; //将自身数据提供给友元 trade 类使用

    //两个有参构造函数
		commodity(char name[], float buyValue, float sellValue) //构造函数1:加入一种全新的商品
		{
			kinds++;
			ID = kinds;
			this->stock = 0;
			this->buyValue = buyValue;
			this->sellValue = sellValue;
			strcpy(this->name, name);
		}

		commodity(int ID, char name[], float buyValue, float sellValue, int stock) //构造函数2保留
		{
			kinds++;
			this->ID = ID;
			this->stock = stock;
			this->buyValue = buyValue;
			this->sellValue = sellValue;
			strcpy(this->name, name);
		}
};

2、trade.h

#include "stdafx.h"
#include <list>
#include "commodity.h" //加入commodity.h

using std::list; //使用 list 命名空间

class trade
{
	struct record //商品交易记录结构体
	{
		char name[30]; //商品名称
		int count; //交易数量
		char sTime[70]; //交易时间

		record(char* name, int count, char* time)
		{
			strcpy(this->name, name);
			this->count = count;
			strcpy(sTime, time);
		}
	};

	//成员变量
	private:
		list<commodity> dataList; //使用 commodity类 实现一个商品数据链表
		list<record> buyRecordList; //使用 record结构体 实现一个商品进货记录链表
		list<record> sellRecordList; //使用 record结构体 实现一个商品售货记录链表

	//成员函数
	public:
		bool GetInformathion(int index); //获取并输出商品信息
		void GetIndex(); //获取并输出商品目录
		bool init(); //从本地文件获取商品信息
		void save(); //将商品信息保存到本地文件中
		bool Buy(int ID, int count); //购买商品的操作与数据检查
		bool Sell(int n, int ID); //售出商品的操作与数据检查
		void AddNew(char name[], float buyValue, float sellValue); //添加新的商品

		/*获得商品的购入和销售记录*/
		void getSellRecord();
		void getBuyRecord();
};

至此,上述完成了两个类的声明部分,下main在源文件中实现类的定义,以及main函数实现简单商品销售系统的实现。

源文件:
1、commodity.cpp

#include
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值