#include <iostream>
#include <list>
#include "Goods.h"
#include "ShoppingCar.h"
#include "InitialGoods.h"
#include "Master.h"
/**
* C++项目:原创级超市系统
* ShopSystem
*
* 技术的应用:
* 面向对象,list集合排序,泛型,封装,继承,
* 覆盖,遍历,exit()应用,系统年月日时分秒应用
*
* 功能:
* 管理员登入功能,
* 管理员对商品的增、删、改、查功能,
* 商品排序功能,商品库存及时更新功能
* 用户购买商品功能,记录库存功能,
* 支付系统功能,打印票据功能
*/
int main()
{
//创建集合,里面放超市里的商品
list<Goods> L;
//创建集合,里面放用户购买的商品
list<ShoppingCar> listCar;
//加载最初的超市商品,放入ArrayList<List>里面
InitialGoods initialGoods;
initialGoods.initialGoods(L);
//创建管理员对象
Master master;
//创建购物车对象
ShoppingCar shoppingCar;
while (true) {
cout<<"你是管理员还是消费者?请输入管理员或消费者 若输入exit则退出超市系统\n";
string str;
cin >> str;
if (str.compare("exit") == 0) {
cout << "系统已经安全退出,再见";
return 0;
}
if (str.compare("管理员") == 0) {
//管理员的登入方法
master.SignIn();
//展示超市商品清单的方法
master.displayGoods(L);
//展示管理员操作商品的方法
master.displayFounction();
//管理员对商品的增、删、改、查方法
master.displayFounctionCase(L);
}
else if (str.compare("消费者") == 0) {
cout<<"欢迎光临,下表为商品信息\n";
//展示超市商品清单的方法
master.displayGoods(L);
/**
* 该方法是一个庞大的系统,模拟现实购物及结账过程,
* 它负责用户将购买的商品,
* 放入购物车和支付价钱的大系统.
*/
shoppingCar.addGoodsOfCarAndCountPriceSystem(L, listCar);
//打印最终的购物票据
shoppingCar.PrintFinalGoodsMessage(listCar);
//用户购买商品后,商品库存会及时更新,管理员可以随时增加商品库存
cout<<"商品的库存已更新,如下\n";
master.displayGoods(L);
return 0;
}
else
{
cout<<"非法输入,请重新输入\n";
}
}
}
#pragma once
#include <string>
using namespace std;
class Goods
{
public:
//商品编号
string id;
//商品名字
string name;
//商品单价
double price;
//商品库存
int number;
//创建有参无参构造方法
Goods();
Goods(string id, string name, double price, int number);
};
#pragma once
#include "Goods.h"
#include<list>
class InitialGoods
{
public:
//初始化商品
void initialGoods(list<Goods> &L);
};
#pragma once
#include <string>
#include "Goods.h"
#include<list>
using namespace std;
class Master
{
public:
//管理员姓名
string name;
//管理员密码
string password;
Master();
Master(string name, string password);
//登入方法
void SignIn();
//添加商品方法
void addGoods(list<Goods>& L);
//删除商品功能
void deleteGoods(list<Goods>& L);
//修改商品功能
void setGoods(list<Goods>& L);
//查询商品功能
void seekGoods(list<Goods>& L);
//展示商品信息功能
void displayGoods(list<Goods>& L);
//展示管理员的管理功能
void displayFounction();
//实现管理员的功能
void displayFounctionCase(list<Goods>& L);
};
#pragma once
#include <string>
#include<list>
#include "Goods.h"
using namespace std;
class ShoppingCar
{
public:
//购物车商品编号
string id;
//购物车商品名字
string name;
//购物车商品单价
double price;
//购物车商品数量
int amount;
ShoppingCar();
ShoppingCar(string id, string name, double price, int amount);
//展示购物车商品功能
void displayGoodsCar(list<ShoppingCar> &listCar);
//打印商品票据
void PrintFinalGoodsMessage(list<ShoppingCar> &listCar);
/**
* 该方法是一个庞大的系统,模拟现实购物及结账过程,
* 它负责用户将购买的商品,
* 放入购物车和支付价钱的大系统.
*/
void addGoodsOfCarAndCountPriceSystem(list<Goods>& L, list<ShoppingCar>& listCar);
};
#include "Goods.h"
//无参有参构造函数
Goods::Goods()
{
}
Goods::Goods(string id, string name, double price, int number)
{
this->id = id;
this->name = name;
this->price = price;
this->number = number;
}
#include "InitialGoods.h"
//初始化商品
void InitialGoods::initialGoods(list<Goods>& L)
{
Goods g1("105", "苹果", 2.3, 814);
Goods g2("107", "葡萄", 3.7, 546);
Goods g3("104", "草莓", 4.5, 438);
Goods g4("108", "菠萝", 1.4, 936);
Goods g5("106", "旺仔", 5.2, 833);
Goods g6("109", "伊利", 5.6, 783);
Goods g7("112", "方便面", 2.1, 642);
Goods g8("110", "小面包", 1.6, 754);
Goods g9("115", "牛皮糖", 1.0, 358);
Goods g10("113", "火腿肠", 4.3, 521);
Goods g11("114", "散龙卫", 1.5, 249);
L.push_back(g1);
L.push_back(g2);
L.push_back(g3);
L.push_back(g4);
L.push_back(g5);
L.push_back(g6);
L.push_back(g7);
L.push_back(g8);
L.push_back(g9);
L.push_back(g10);
L.push_back(g11);
}
#include "Master.h"
#include<string>
#include<list>
#include <iostream>
using namespace std;
//管理员默认登入账号密码
Master::Master()
{
this->name = "123";
this->password = "123";
}
Master::Master(string name, string password)
{
this->name = name;
this->password = password;
}
//登入功能
void Master::SignIn()
{
cout << "欢迎进入超市系统,请注意,你只有三次登入的机会\n";
int count = 3;
while (true) {
count--;
cout << "请输入账号:";
string str1;
cin >> str1;
cout << "请输入密码:";
string str2;
cin >> str2;
Master master;
Master master1(str1, str2);
if (master.name == master1.name && master.password == master1.password) {
cout << "登入成功,欢迎进入超市系统\n";
break;
}
else if (count != 0) {
cout << "账号或密码错误,请重新输入,你还有" << count << "次机会\n";
}
else {
cout << "三次机会用完,退出系统\n";
exit(0);
}
}
}
//添加商品
void Master::addGoods(list<Goods>& L)
{
cout<<"-----添加商品-----\n";
cout<<"输入商品编号:";
string id;
cin >> id;
for (list<Goods>::iterator it = L.begin(); it != L.end(); it++) {
if (id.compare(it->id) == 0) {
cout << "该商品存在,请添加其他商品\n";
return;
}
}
cout << "输入商品名字:";
string name;
cin >> name;
cout << "输入商品单价:";
double price;
cin >> price;
cout << "输入商品库存:";
int number;
cin >> number;
Goods g(id,name,price,number);
L.push_back(g);
cout<<"添加成功\n";
}
//删除商品
void Master::deleteGoods(list<Goods>& L)
{
Master master ;
cout<<"-----删除商品-----\n";
cout<<"如下是商品清单\n";
master.displayGoods(L);
cout<<"请输入商品编号:";
string str ;
cin >> str;
for (list<Goods>::iterator it = L.begin(); it != L.end(); it++) {
if (str.compare(it->id) == 0) {
L.erase(it);
cout << "删除成功\n";
return;
}
}
cout << "该商品不存在,请删除其他商品\n";
}
//修改商品
void Master::setGoods(list<Goods>& L)
{
Master master;
cout << "-----修改商品-----\n";
cout << "如下是商品清单\n";
master.displayGoods(L);
cout << "请输入要修改的商品编号:";
string str;
cin >> str;
for (list<Goods>::iterator it = L.begin(); it != L.end(); it++) {
if (str.compare(it->id) == 0) {
L.erase(it);
cout << "输入修改后商品名字:";
string name;
cin >> name;
cout << "输入修改后商品单价:";
double price;
cin >> price;
cout << "请输入修改后商品库存:";
int number;
cin >> number;
Goods g(str, name, price, number);
L.push_back(g);
cout << "修改成功\n";
return;
}
}
cout << "该商品不存在,请修改其他商品\n";
}
//查询商品
void Master::seekGoods(list<Goods>& L)
{
cout << "-----查找商品-----\n";
cout << "请输入编号或商品名字:";
string str;
cin >> str;
for (list<Goods>::iterator it = L.begin(); it != L.end(); it++) {
if (str.compare(it->id) == 0 || str.compare(it->name) == 0) {
cout << "商品信息如下所示\n";
cout << it->id << " " << it->name << " " << it->price << " " << it->number << endl;
return;
}
}
cout << "该商品不存在,请查找其他商品\n";
}
//展示商品信息功能
bool compare(Goods& g1, Goods& g2) {
return g1.id < g2.id;
}
void Master::displayGoods(list<Goods>& L)
{
cout<<"编号 商品 单价 库存\n";
//将集合里的商品按照编号由小到大排序
L.sort(compare);
for (list<Goods>::iterator it = L.begin(); it != L.end(); it++) {
cout << it->id << " " << it->name << " " << it->price << " " << it->number << endl;
}
}
void Master::displayFounction()
{
cout << "增加商品请输入1" << endl;
cout << "删除商品请输入2" << endl;
cout << "修改商品请输入3" << endl;
cout << "查询商品请输入4" << endl;
cout << "展示商品请输入5" << endl;
cout << "退出管理商品系统请输入0" << endl;
}
void Master::displayFounctionCase(list<Goods>& L)
{
//创建管理员对象
Master master ;
while (true) {
cout<<"请输入0/1/2/3/4/5:";
int n ;
cin >> n;
if (n == 0) {
break;
}
switch (n) {
case 1: {
//增加商品
master.addGoods(L);
break;
}
case 2: {
//删除商品
master.deleteGoods(L);
break;
}
case 3: {
//修改商品
master.setGoods(L);
break;
}
case 4: {
//查询商品
master.seekGoods(L);
break;
}
case 5: {
//展示商品信息
master.displayGoods(L);
break;
}
default:
cout<<"无效数字,请重新输入\n";
}
}
}
#include "ShoppingCar.h"
#include<iostream>
#include<Windows.h>
using namespace std;
ShoppingCar::ShoppingCar()
{
}
ShoppingCar::ShoppingCar(string id, string name, double price, int amount)
{
this->id = id;//购物车商品编号
this->name = name;//商品名
this->price = price;//商品单价
this->amount = amount;//购买商品数量
}
//展示购物车商品功能
bool compare2(ShoppingCar& s1, ShoppingCar& s2) {
return s1.id < s2.id;
}
void ShoppingCar::displayGoodsCar(list<ShoppingCar>& listCar)
{
cout<<"编号 商品 单价 购买数量\n";
//对购物车集合里的商品按照id进行由小到大排序
listCar.sort(compare2);
for (list<ShoppingCar>::iterator it = listCar.begin(); it != listCar.end(); it++) {
cout << it->id << " " << it->name << " " << it->price << " " << it->amount << endl;
}
}
//打印商品票据
void ShoppingCar::PrintFinalGoodsMessage(list<ShoppingCar>& listCar)
{
ShoppingCar shoppingCar;
cout << "你的打印单:\n";
//展示购物车美食信息
shoppingCar.displayGoodsCar(listCar);
//获取系统时间
SYSTEMTIME sys;
GetLocalTime(&sys);
printf("打印于:%4d/%02d/%02d %02d:%02d:%02d\n", sys.wYear, sys.wMonth, sys.wDay, sys.wHour, sys.wMinute, sys.wSecond);
cout << "购物愉快,欢迎下次光临\n";
}
/**
* 该方法是一个庞大的系统,模拟现实购物及结账过程,
* 它负责用户将购买的商品,
* 放入购物车和支付价钱的大系统.
*/
void ShoppingCar::addGoodsOfCarAndCountPriceSystem(list<Goods>& L, list<ShoppingCar>& listCar)
{
ShoppingCar shoppingCar;
double sum = 0;
cout << "请选择你要购买的商品名字及数量,输入格式:\n商品编号 \n数量\n输入0表示不再购物,进入结账系统,请按格式输入:\n";
while (true) {
string id;
cin >> id;
if (id.compare("0") == 0) {
break;
}
int amount;
cin >> amount;
for (list<Goods>::iterator it = L.begin(); it != L.end(); it++) {
if (id.compare(it->id) == 0) {
if (it->number >= amount) {
//如果购买的数量在库存内,可以购买该数量的商品,
//否则提示库存不足,重新输入
ShoppingCar s(it->id, it->name, it->price, amount);
listCar.push_back(s);
//计算此商品的价钱
sum += it->price * amount;
//更新此商品的库存
it->number = it->number - amount;
}
else {
cout << "库存不足,请重新输入\n";
break;
}
}
}
}
cout << "您买的商品信息如下\n";
//展示购物车商品信息
shoppingCar.displayGoodsCar(listCar);
printf("需要支付%.1f元,请支付:", sum);
double money;
cin >> money;
if (money > sum) {
printf("支付成功,找你%.1f元\n", money - sum);
}
else if (money == sum) {
cout << "支付成功\n";
}
else {
double ca = sum - money;
while (ca > 0) {
printf("你支付钱过少,你需要再支付%.1f元,请支付", ca);
double money1;
cin >> money1;
ca = ca - money1;
if (ca == 0.0) {
cout << "支付成功\n";
break;
}
if (ca < 0) {
printf("支付成功,找你%.1f元\n", -1 * ca);
}
}
}
}
摘要
科技水平发展日新月异,人们深刻认识到计算机的强大功能,如今,我们生活的地方到处都充斥着计算机的身影,计算机信息管理是计算机学科的重要组成部分,与传统的纸张记录相比有着相当大的优势。目前对于超市这种大型企业,拥有一个计算机超市商品信息管理系统是非常有必要的。因此本文开发了一个针对商品信息管理的软件来提高信息的传递效率。
该系统名为超市商品信息管理系统,系统开发环境基于C++语言,借助于 vs2022进行开发。该系统由一个包Shop System和Goods、Master、InitialGoods、ShoppingCart组成,各司其职,完成系统要求。该系统应用了十一项c++技术并有十二项功能。这些技术有面向对象,集合排序,实现接口,泛型,封装,继承,覆盖,遍历,系统时间应用等。功能有管理员登入功能,管理员对商品的增、删、改、查功能,商品排序功能,商品库存及时更新功能,用户购买商品功能,记录库存功能,支付系统功能,打印票据功能,修复输入环节故障功能。