C++ 购物车代码

//good.h
#ifndef _GOOD_H
#define _GOOD_H

#include <string>

using std::string;

class good{
	private:
		string goodname; //唯一标识商品
		double price;	 //价格
		double rate;	 //折扣率
	public:
		good(){ //无参构造函数
			goodname = "";
			price = 0.0;
			rate = 1.0;
		}
		good(string s, double p){//无折扣率构造函数
			goodname = s;
			price = p;
			rate = 1.0;
		}
		good(string s, double p, double r){//有折扣率构造函数
			goodname = s;
			price = p;
			rate = r;
		}
		string getGoodname()const {
			return goodname;
		}
		double getPrice()const{
			return price;
		}
		double getRate()const {
			return rate;
		}
		void setGoodname(string const name){
			goodname = name;
		}
		void setPrice(const double p){
			price = p;
		}
		void setRate(const double r){
			rate = r;
		}
};
#endif
//**************************************
//版本:简易版2.0
//作者:snowboy
//释:
//此版本函数一次只操作购物车中的一个商品
//此版本函数仅提供通过商品名字的删除操作
//***************************************


//cart.h
#ifndef _CART_H
#define _CART_H


#include "good.h"
#include <vector>
#include <iostream>


using std::cout;
using std::endl;


struct item{ //存放商品及其数量
<span style="white-space:pre">	</span>good g;
<span style="white-space:pre">	</span>int amount;
};


class cart{
private:
<span style="white-space:pre">	</span>std::vector<struct item> glist;//订单列表
public:
<span style="white-space:pre">	</span>cart(){}
<span style="white-space:pre">	</span>void add(good g);//添加商品
<span style="white-space:pre">	</span>void deleteByGoodname(string goodname);//删除商品
<span style="white-space:pre">	</span>double total();
<span style="white-space:pre">	</span>void display();//购物列表展示
<span style="white-space:pre">	</span>


<span style="white-space:pre">	</span>double changeTotal();//TODO,改变总价
<span style="white-space:pre">	</span>void changeRateByGoodname(string);//TODO,改变商品折扣率
<span style="white-space:pre">	</span>void changPriceByGoodname(string);//TODO,改变商品价格


};




void cart::add(good g){
<span style="white-space:pre">	</span>std::vector<struct item>::iterator iter=glist.begin();
<span style="white-space:pre">	</span>for(;iter!=glist.end();++iter){
<span style="white-space:pre">		</span>if(iter->g.getGoodname() == g.getGoodname()){
<span style="white-space:pre">			</span>(*iter).amount++;
<span style="white-space:pre">			</span>return;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>item t;
<span style="white-space:pre">	</span>t.g = g;
<span style="white-space:pre">	</span>t.amount = 1;
<span style="white-space:pre">	</span>glist.push_back(t);
}


void cart::deleteByGoodname(string goodname){//按名字删除一个商品
<span style="white-space:pre">	</span>std::vector<struct item>::iterator iter=glist.begin();
<span style="white-space:pre">	</span>for(;iter!=glist.end();++iter){
<span style="white-space:pre">		</span>if(iter->g.getGoodname() == goodname){
<span style="white-space:pre">			</span>iter->amount--;
<span style="white-space:pre">			</span>if(iter->amount == 0){
<span style="white-space:pre">				</span>glist.erase(iter);
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>return;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>cout<<"no this good"<<endl;
}


void cart::display(){
<span style="white-space:pre">	</span>cout<<"********* good list *********"<<endl;
<span style="white-space:pre">	</span>if(glist.size()){
<span style="white-space:pre">		</span>std::vector<struct item>::const_iterator iter=glist.begin();
<span style="white-space:pre">		</span>for(;iter!=glist.end();++iter){
<span style="white-space:pre">			</span>cout<<"goodname: "<<iter->g.getGoodname()<<endl
<span style="white-space:pre">				</span><<"price   : "<<iter->g.getPrice()<<endl
<span style="white-space:pre">				</span><<"rate    : "<<iter->g.getRate()*100<<"%"<<endl
<span style="white-space:pre">				</span><<"amount  : "<<iter->amount<<endl
<span style="white-space:pre">				</span><<"total:  : "<<total()<<endl<<endl;
<span style="white-space:pre">		</span>}


<span style="white-space:pre">	</span>}else{
<span style="white-space:pre">		</span>cout<<"empty list"<<endl<<endl;
<span style="white-space:pre">	</span>}
}


double cart::total(){
<span style="white-space:pre">	</span>if(glist.size()){
<span style="white-space:pre">		</span>std::vector<struct item>::const_iterator iter=glist.begin();
<span style="white-space:pre">		</span>double sum=0.0;
<span style="white-space:pre">		</span>for(;iter!=glist.end();++iter){
<span style="white-space:pre">			</span>sum += iter->g.getPrice() * iter->g.getRate() * iter->amount;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>return sum;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>return 0;
}


#endif
//test.cpp


#include "cart.h"


int main()
{
<span style="white-space:pre">	</span>good g("apple", 76);
<span style="white-space:pre">	</span>cart c;
<span style="white-space:pre">	</span>c.display();
<span style="white-space:pre">	</span>c.add(g);
<span style="white-space:pre">	</span>c.display();
<span style="white-space:pre">	</span>c.add(g);
<span style="white-space:pre">	</span>c.display();
<span style="white-space:pre">	</span>c.deleteByGoodname(g.getGoodname());
<span style="white-space:pre">	</span>c.display();
<span style="white-space:pre">	</span>c.deleteByGoodname(g.getGoodname());
<span style="white-space:pre">	</span>c.display();
<span style="white-space:pre">	</span>c.deleteByGoodname(g.getGoodname());
<span style="white-space:pre">	</span>c.display();
<span style="white-space:pre">	</span>c.deleteByGoodname(g.getGoodname());
<span style="white-space:pre">	</span>c.display();
<span style="white-space:pre">	</span>return 0;
}

网上超市购物管理系统,具体功能如下: 1、商品基本信息 1)商品代码,商品名称,商品价格,商品库存。 2)设计约定:可以假定超市有 N 种商品,在系统启动时将这些商品信息加载到商品库中。 2、顾客信息 1)普通顾客属性:ID(顾客代码)(随机生成或流水号) 2)普通会员:ID(会员代码),会员姓名,会员电话,会员积分。 3)高级会员:ID(会员代号),会员姓名,会员电话,会员积分,副卡 ID。 3、购物管理 1)为每一个普通顾客生成临时顾客代码。 2)普通会员购买商品时,可享受 9.8 折,普通顾客在单次购物满 1000 元,添加个人基本信息后,可成为普通会员。 3)高级会员购买商品时,可享受 9.5 折,普通顾客在单次购物满 2000 元,添加个人基本信息后,或普通会员单次购物满 1000 元可成为高级会员。普通会员和高级会员可查询 1 年内的购物详单,会员购物积分按照 1 元 1 分计。 4)每次购物后,输出购物清单。包括顾客代号(会员代码)、商品的名称、代号、单价、折扣、数量、价格以及合计价格、购买时间。 4、店铺管理 1)实现商品信息的添加、修改、删除、查询的功。可查询库存少于一定数量的商品。 2)实现会员资料的查询、修改、删除;实现会员整理的功能,对于 1 年内无购物的会员,进行自动删除。 3)可以按照时间区间统计,包括:销售总额;分类统计商品的销售情况;统计对会员的让利情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值