古之立大事者,不惟有超世之才,亦必有坚忍不拔之志。——苏轼
-----------------------------------------
学编程的bird的博客,邀您一起学习歷
----------------凌------------省-------------很高兴你打开了这篇博客。
★如有疑问不解或者说有想问的地方,都可以在下方评论留言,博主看到会尽快回复的。
★当然,期待你的点赞+关注哦!
————————————————
版权声明:本文为CSDN博主「学编程的bird」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接: 超市管理系统 C++_学编程的小bird的博客-CSDN博客https://blog.csdn.net/m0_64069699/article/details/125065247?spm=1001.2014.3001.5502
设计内容
超市中商品分为四类,分别是食品、化妆品、日用品和饮料。每种商品都包含商品名称、价格、库存量和生产厂家、品牌等信息。主要完成对商品的销售、统计和简单管理。本系统的软件界面是使用C++编译生成的一个系统操作菜单。操作菜单界面总共有七个选项可以供用户操作选择,分别为购买商品、添加商品、删除商品、修改商品、查询商品、统计商品、退出系统。菜单页面下方有可供用户选择操作项目的指令,当用户选择并输入0-6中的任意数字,系统便会进入相应的功能操作。如果出现错误操作,系统会该处输入错误请从新输入等提示或者自动跳出回到主菜单界面。
相关功能
(1)销售功能。购买商品时,先输入类别,然后输入商品名称,并在库存中查找该商品的相关信息。如果有库存量,输入购买的数量,进行相应计算。如果库存量不够,给出提示信息,结束购买。
(2)商品简单管理功能。
添加功能:主要完成商品信息的添加。
查询功能:可按商品类别、商品名称、生产厂家进行查询。若存在相应信息,输出所查询的信息,若不存在该记录,则提示“该记录不存在!”。
修改功能:可根据查询结果对相应的记录进行修改。
删除功能:主要完成商品信息的删除。先输入商品类别,再输入要删除的商品名称,根据查询结果删除该物品的记录,如果该商品不在物品库中,则提示“该商品不存在”。
(3)统计功能。
输出当前库存中所有商品的总数及详细信息;可按商品的价格、库存量、生产厂家进行统计,输出统计信息时,要按从大到小排序。
框图
提示:
(1) 类的设计关系
(2) 主功能函数
源代码
#include
#include
#include //头文件引入
#include
#include
using namespace std;
class Objects //大类
{
public:
Objects()
{
}
char Id[20];
char kind [20];//种类
char name[20];//名字
char place[10];//生产地
char brand[10];//品牌
float buyprice; //进货价;
float Purchase; //卖出价;
int amount; //存货数量;
int amount1; //售货数量;
int money; //花销;
Objects* Next;
void InputAll()
{
InputName();
InputKind();
InputOther();
}
void InputKind()
{
cout << "tt请选择种类:"; cin >> kind;
}
void InputName()
{
cout << "tt请输入商品的名称:"; cin >> name;
}
void InputBuyprice()
{
cout << "tt请输入进货价:"; cin >> buyprice;
}
void InputPurchase()
{
cout << "tt请输入售出价:"; cin >> Purchase;
}
void InputAmount()
{
cout << "tt请输入剩余商品数量:"; cin >> amount;
}
void Inputmoney()
{
cout << "tt请输入花销:"; cin >> money;
}
void InputPlace()
{
cout << "tt请输入商品产地:"; cin >> place;
}
void Inputbrand()
{
cout << "tt请输入生产商品牌:"; cin >> brand;
}
void InputOther()
{
cout << "tt请输入进货价:"; cin >> buyprice;
cout << "tt请输入售出价:"; cin >> Purchase;
cout << "tt请输入存货数量:"; cin >> amount;
cout << "tt请输入商品的产地:"; cin >> place;
cout << "tt请输入生产商品牌:"; cin >> brand;
money = 0;
}
void ReadFile(istream& in) //将输入的东西存入文件
{
in >> name >> kind >> Id >> buyprice >> Purchase >> amount >> place >> brand >> money;
}
void Show()
{
cout << "商品名: " << name << endl << "种类:" << kind << endl << "进货价: " << buyprice << endl << "售出价: " << Purchase << endl << "剩余商品数量: " <<
amount << endl << "花销:" << money << endl << "商品的产地: " << place << endl << "生产商品牌: " << brand << endl << endl << endl;
}
void Show2()
{
cout << "商品名: " << name << endl << "种类:" << kind << endl << "进货价: " << buyprice << endl << "售出价: " << Purchase << endl << "剩余商品数量: " <<
amount << endl << "商品的产地: " << place << endl << "生产商品牌: " << brand << endl << endl << endl;
}
void Show3()
{
cout << "商品名: " << name << endl << "种类:" << kind << endl << "售出价: " << Purchase << endl << "剩余商品数量: " <<
amount << endl << "花销:" << money << endl << "商品的产地: " << place << endl << "生产商品牌: " << brand << endl << endl << endl;
}
};
class ObjectsInformation : public Objects // ObjectsInformation类
{
public:
ObjectsInformation();
~ObjectsInformation();
void showMenu(int n);
void Find();
void Save();
void ModifyItem();
void RemoveItem();
void Swap(Objects*, Objects*);
void my_Sort();
void Purchase();
int ListCount();
void Display()
{
system("cls");
i = 0;
for (Objects* p = Head->Next; p != End; p = p->Next)
{
p->Show();
i++;
}
cout << "共有" << i << "个商品" << "n" << endl;
cout << "输入任意字符!继续……";
_getch();
}
void Display2()
{
system("cls");
i = 0;
for (Objects* p = Head->Next; p != End; p = p->Next)
{
p->Show2();
i++;
}
cout << "共有" << i << "个商品" << "n" << endl;
cout << "输入任意字符!继续……";
_getch();
}
void AddItem()//从键盘输入商品信息
{
system("cls");
End->InputName();
showMenu(1);
End->InputKind();
End->InputOther();
End->Next = new Objects;
End = End->Next;
cout << "添加成功!" << endl;
Save();
cout << "输入任意字符!继续……";
_getch();
}
private:
Objects* Head, * End;
int i;
ifstream in;
ofstream out;
Objects* FindItem(char* name)
{
for (Objects* p = Head; p->Next != End; p = p->Next)//匹配成功则返回上一个指针,不成功就返回空
if (!strcmp(p->Next->name, name))return p;
return NULL;
}
Objects* Findkind(char* kind)
{
for (Objects* p = Head; p->Next != End; p = p->Next)//匹配成功则返回上一个指针,不成功就返回空
if (!strcmp(p->Next->kind, kind))return p;
return NULL;
}
Objects* Findbrand(char* brand)
{
for (Objects* p = Head; p->Next != End; p = p->Next)//匹配成功则返回上一个指针,不成功就返回空
if (!strcmp(p->Next->brand, brand))return p;
return NULL;
}
};
ObjectsInformation::ObjectsInformation() //构造函数
{
Head = new Objects;
Head->Next = new Objects;
End = Head->Next;
in.open("supermarket.txt");
if (!in)
cout <<" " <ReadFile(in);
if (End->name[0] == ' ')break;
End->Next = new Objects;
End = End->Next;
}
in.close();
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << "tttt超市里面还有一些库存,可以看看" << "n" << endl;
}
}
ObjectsInformation::~ObjectsInformation() //析构函数
{
//Save();
for (Objects* temp; Head->Next != End;)
{
temp = Head->Next;
Head->Next = Head->Next->Next;
delete temp;
}
delete Head, End;
}
void ObjectsInformation::showMenu(int n)//菜单 //菜单
{
switch (n)
{
case 1:
{
cout << "▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉n"
<< " 1. 食品 2. 化妆品 3. 日用品 4. 饮料 n"
<< "▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉n" << endl;
break; }
case 2:
{
system("cls");
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ 超 市 管 理 系 统 ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆" << endl;
cout << " " << endl; cout << " " << endl;
cout << " ************* 1.增添商品 ************* " << endl;
cout << " " << endl;
cout << " ************* 2.统计商品 ************* " << endl;
cout << " " << endl;
cout << " ************* 3.查询商品 ************* " << endl;
cout << " " << endl;
cout << " ************* 4.删除商品 ************* " << endl;
cout << " " << endl;
cout << " ************* 5.修改商品 ************* " << endl;
cout << " " << endl;
cout << " ************* 6.购买商品 ************* " << endl;
cout << " " << endl;
cout << " ************* 0.退出系统 ************* " << endl;
cout << "ttntttt 请选择:0-6" << endl;
break; }
case 3:
{
system("cls");
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " ************* 1.修改种类 ************* n ";
cout << " " << endl;
cout << " ************* 2.修改名称 ************* n";
cout << " " << endl;
cout << " ************* 3.修改进货价 ************* n";
cout << " " << endl;
cout << " ************* 4.修改售出价 ************* n";
cout << " " << endl;
cout << " ************* 5.修改剩余量 ************* n ";
cout << " " << endl;
cout << " ************* 6.修改产地 ************* n ";
cout << " " << endl;
cout << " ************* 7.修改品牌 ************* n ";
cout << " " << endl;
cout << " ************* 8.修改全部信息 ************* " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " ************* 请选择:0-8进行相关操作" << endl;
break;
}
case 4:
{
system("cls");
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << "*************************************************************************************n"
<< "t 1. 按商品价排序 2. 按库存量排序 2. 按生产品牌排序 "
<< "**************************************************************************************" << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << "ttntttt 请选择:0-3" << endl;
break;
}
case 5:
{
system("cls");
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << "*************************************************************************************n"
<< "t 1. 按商品名称查询 2. 按商品种类查询 3. 按商品厂家查询 "
<< "**************************************************************************************" << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << "ttntttt 请选择:0-3" << endl;
}
}
}
void ObjectsInformation::Find() //查找函数
{
system("cls");
char name[20], Id[10];
int x;
Objects* p = NULL;
showMenu(5);
cin >> x;
switch (x)
{
case 1: {cout << "tt请输入要查找的商品的名称:"; cin >> name;
if (p = FindItem(name))
{
p->Next->Show2();
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到该名称的商品!" << 'n' << endl;
cout << "输入任意字符!继续……";
_getch();
}
}break;
case 2: {cout << "tt请输入要查找的商品的种类:"; cin >> name;
if (p = Findkind(kind))
{
p->Next->Show2();
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到该种类的商品!" << 'n' << endl;
cout << "输入任意字符!继续……";
_getch();
}
}break;
case 3: {cout << "tt请输入要查找的厂家的名称:"; cin >> name;
if (p = Findbrand(brand))
{
p->Next->Show2();
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到该厂家的商品!" << 'n' << endl;
cout << "输入任意字符!继续……";
_getch();
}
}break;
}
}
void ObjectsInformation::ModifyItem() //修改商品信息
{
showMenu(3);
int x;
cin >> x;
switch (x)
{
case 1:
{
char Id[20];
Objects* p = NULL;
cout << "tt请输入要修改的商品的名称:"; cin >> Id;
if (p = FindItem(Id))
{
cout << "tt已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputKind();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 2:
{
char Id[20];
Objects* p = NULL;
cout << "tt请输入要修改的商品的名称:"; cin >> Id;
if (p = FindItem(Id))
{
cout << "tt已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputName();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 3:
{
char Id[20];
Objects* p = NULL;
cout << "tt请输入要修改的商品的名称:"; cin >> Id;
if (p = FindItem(Id))
{
cout << "tt已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputBuyprice();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 4:
{
char Id[20];
Objects* p = NULL;
cout << "tt请输入要修改的商品的名称:"; cin >> Id;
if (p = FindItem(Id))
{
cout << "tt已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputPurchase();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 5:
{
char Id[20];
Objects* p = NULL;
cout << "tt请输入要修改的商品的名称:"; cin >> Id;
if (p = FindItem(Id))
{
cout << "tt已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputAmount();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 6:
{
char Id[20];
Objects* p = NULL;
cout << "tt请输入要修改的商品的名称:"; cin >> Id;
if (p = FindItem(Id))
{
cout << "tt已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputPlace();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 7:
{
char Id[20];
Objects* p = NULL;
cout << "tt请输入要修改的商品的名称:"; cin >> Id;
if (p = FindItem(Id))
{
cout << "tt已找到商品的信息,请输入新的信息!" << endl;
p->Next->Inputbrand();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 8:
{
char Id[20];
Objects* p = NULL;
cout << "tt请输入要修改的商品的名称:"; cin >> Id;
if (p = FindItem(Id))
{
cout << "tt已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputAll();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
}
}
}
void ObjectsInformation::RemoveItem() //删除商品信息
{
system("cls");
char Id[20];
Objects* p = NULL, * temp = NULL;
cout << "tt请输入要删除的商品的名称:" << endl; cin >> Id;
if (p = FindItem(Id))
{
temp = p->Next;
p->Next = p->Next->Next;
delete temp;
cout << "tt删除成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
}
void ObjectsInformation::Purchase() //购买商品
{
system("cls");
char Id[20]; int i;
Objects* p = NULL;
Objects* temp = NULL;
cout << "tt请输入要购买的商品的名称和数量:"; cin >> Id; cin >> i;
if (p = FindItem(Id))
{
if (p->Next->amount > i)
{
p->Next->amount -= i;
p->Next->amount1 += i;
p->Next->money += i * (p->Next->Purchase);
cout << "商品购买成功!" << "n";
cout << "购买商品名称: " << Id << "t" << "数量: " << i << "n";
cout << "n";
cout << "购买后商品信息: " << endl;
p->Next->Show3();
Save();
p->Next->money = 0;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "商品数量不够,不能购买" << endl;
cout << "输入任意字符!继续……";
_getch();
Purchase();
}
}
else
{
cout << "无此种商品,不能购买" << endl;
cout << "输入任意字符!继续……";
_getch();
showMenu(2);
}
}
void ObjectsInformation::Swap(Objects* p1, Objects* p2) //交换两个商品的数据
{
Objects* temp = new Objects;
strcpy_s(temp->name, p1->name);
strcpy_s(temp->kind, p1->kind);
strcpy_s(temp->Id, p1->Id);
strcpy_s(temp->place, p1->place);
temp->Purchase = p1->Purchase;
temp->buyprice = p1->buyprice;
temp->amount = p1->amount;
temp->money = p1->money;
strcpy_s(temp->brand, p1->brand);
strcpy_s(p1->name, p2->name);
strcpy_s(p1->kind, p2->kind);
strcpy_s(p1->Id, p2->Id);
strcpy_s(p1->place, p2->place);
p1->Purchase = p2->Purchase;
p1->buyprice = p2->buyprice;
p1->amount = p2->amount;
p1->money = p2->money;
strcpy_s(p1->brand, p2->brand);
strcpy_s(p2->name, temp->name);
strcpy_s(p2->kind, temp->kind);
strcpy_s(p2->Id, temp->Id);
strcpy_s(p2->place, temp->place);
p2->Purchase = temp->Purchase;
p2->buyprice = temp->buyprice;
p2->amount = temp->amount;
p2->money = temp->money;
strcpy_s(p2->brand, temp->brand);
}
int ObjectsInformation::ListCount() //统计当前链表的记录总数,返回一个整数
{
if (!Head)
return 0;
int n = 0;
for (Objects* p = Head->Next; p != End; p = p->Next)
{
n++;
}
return n;
}
void ObjectsInformation::my_Sort() //排序商品
{
showMenu(4);
int x;
cin >> x;
switch (x)
{
case 1:
{
system("cls");
cout << "tt排序中..." << endl;
cout << "n";
Objects* p = NULL, * p1 = NULL, * k = NULL;
int n = ObjectsInformation::ListCount();
if (n < 2)
return;
for (p = Head->Next; p != End; p = p->Next)
for (k = p->Next; k != End; k = k->Next)
{
if (p->buyprice > k->buyprice)
{
ObjectsInformation::Swap(p, k);
}
}
Display2();
out.open("进货价.txt");
for (Objects* q = Head->Next; q != End; q = q->Next)
out << q->name << "t" << q->kind << "t" << q->Id << "t" << q->buyprice << "t" << q->Purchase << "t" << q->amount << "t" << q->place << "t" << q->brand << "t" << q->money << 'n';
out.close();
cout << "保存信息成功" << endl;
cout << "排序完成!" << endl;
_getch();
return;
}
case 2:
{
system("cls");
cout << "tt排序中..." << endl;
cout << "n";
Objects* p = NULL, * p1 = NULL, * k = NULL;
int n = ObjectsInformation::ListCount();
if (n < 2)
return;
for (p = Head->Next; p != End; p = p->Next)
for (k = p->Next; k != End; k = k->Next)
{
if (p->amount > k->amount)
{
ObjectsInformation::Swap(p, k);
}
}
Display2();
out.open("库存量.txt");
for (Objects* q = Head->Next; q != End; q = q->Next)
out << q->name << "t" << q->kind << "t" << q->Id << "t" << q->buyprice << "t" << q->Purchase << "t" << q->amount << "t" << q->place << "t" << q->brand << "t" << q->money << 'n';
out.close();
cout << "保存信息成功" << endl;
cout << "排序完成!" << endl;
_getch();
return;
}
case 3:
{
system("cls");
cout << "tt排序中..." << endl;
cout << "n";
Objects* p = NULL, * p1 = NULL, * k = NULL;
int n = ObjectsInformation::ListCount();
if (n < 2)
return;
for (p = Head->Next; p != End; p = p->Next)
for (k = p->Next; k != End; k = k->Next)
{
if (p->money > k->money)
{
ObjectsInformation::Swap(p, k);
}
}
Display2();
out.open("生产品牌.txt");
for (Objects* q = Head->Next; q != End; q = q->Next)
out << q->name << "t" << q->kind << "t" << q->Id << "t" << q->buyprice << "t" << q->Purchase << "t" << q->amount << "t" << q->place << "t" << q->brand << "t" << q->money << 'n';
out.close();
cout << "保存信息成功" << endl;
cout << "排序完成!" << endl;
_getch();
return;
}
}
}
void ObjectsInformation::Save() //保存商品信息到文件函数
{
out.open("supermarket.txt");
for (Objects* p = Head->Next; p != End; p = p->Next)
out << p->name << "t" << p->kind << "t" << p->Id << "t" << p->buyprice << "t" << p->Purchase << "t" << p->amount << "t" << p->place << "t" << p->brand << "t" << p->money << 'n';
out.close();
cout << "保存信息成功" << endl;
}
int main()
{
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " ◢ █ █ █ █ █ █ █ █ █ █ █ █ ◣" << endl;
cout << " █ █ █ █ █ █ █ █ █ █ █ █ █ █ " << endl;
cout << " █ █ ◥ █ █ ◤ █ █ " << endl;
cout << " ◢ █ █ █ ◥ ◤ █ █ ◣" << endl;
cout << " ▊ ▎ █ █ ◣ ◢ █ ▊ ▊" << endl;
cout << " ▊ ▎ █ █ ◤ ● ● ◥ █ ▊ ▊" << endl;
cout << " ▊ █ █ █ ▊ ▊" << endl;
cout << " ◥ ▇ █ █ ▊ ▊ █ ▇ ◤ 嗨嗨嗨,欢迎光临老八秘制小超市" << endl;
cout << " █ █ ◥▆ ▄ ▄ ▄ ▄ ▆ ◤ █ ▊ ◢▇ ▇◣" << endl;
cout << " ◢ █ █◥◥▆ ▅ ▄ ▂ ▂ ▂ ▂ ▄ ▅ ▆ ◢█" << endl;
cout << " █ ╳ ╳ █ ◢◤" << endl;
cout << " ◥ █ ◣ ˙ ˙ ◢ █ ◢◤ " << endl;
cout << " ▊ ▊ █" << endl;
cout << " ▊ ▊ ◢◤" << endl;
cout << " ▊ ⊕ █ ▇ ▇ ▇ ◤ " << endl;
cout << " ◢ █ ▇ ▆ ▆ ▆ ▅ ▅ ▅ ▅ ▆ ▆ ▆ ▇ █ " << endl;
cout << " ▊ ▊ " << endl;
ObjectsInformation Grade;
cout << " 温馨提示:按任意键进入超市后台";
_getch();
int x;
bool quit = false;
while (!quit)
{
Grade.showMenu(2);
cin >> x;
switch (x)
{
case 0: {quit = true; break; }
case 1: {Grade.AddItem(); break; }
case 2: {Grade.my_Sort(); break; }
case 3: {Grade.Find(); break; }
case 4: {Grade.RemoveItem(); break; }
case 5: {Grade.ModifyItem(); break; }
case 6: {Grade.Purchase(); break; }
}
}
return 0;
}
系统内容展示:
超市中商品分为四类,分别是食品、化妆品、日用品和饮料。每种商品都包含商品名称、价格、库存量和生产厂家、品牌等信息。主要完成对商品的销售、统计和简单管理。本系统的软件界面是使用C++编译生成的一个系统操作菜单。操作菜单界面总共有七个选项可以供用户操作选择,分别为购买商品、添加商品、删除商品、修改商品、查询商品、统计商品、退出系统。菜单页面下方有可供用户选择操作项目的指令,当用户选择并输入0-6中的任意数字,系统便会进入相应的功能操作。如果出现错误操作,系统会该处输入错误请从新输入等提示或者自动跳出回到主菜单界面。
相关功能
(1)销售功能。购买商品时,先输入类别,然后输入商品名称,并在库存中查找该商品的相关信息。如果有库存量,输入购买的数量,进行相应计算。如果库存量不够,给出提示信息,结束购买。
(2)商品简单管理功能。
添加功能:主要完成商品信息的添加。
查询功能:可按商品类别、商品名称、生产厂家进行查询。若存在相应信息,输出所查询的信息,若不存在该记录,则提示“该记录不存在!”。
修改功能:可根据查询结果对相应的记录进行修改。
删除功能:主要完成商品信息的删除。先输入商品类别,再输入要删除的商品名称,根据查询结果删除该物品的记录,如果该商品不在物品库中,则提示“该商品不存在”。
(3)统计功能。
输出当前库存中所有商品的总数及详细信息;可按商品的价格、库存量、生产厂家进行统计,输出统计信息时,要按从大到小排序。
框图
提示:
(1) 类的设计关系
(2) 主功能函数
源代码
#include
#include
#include //头文件引入
#include
#include
using namespace std;
class Objects //大类
{
public:
Objects()
{
}
char Id[20];
char kind [20];//种类
char name[20];//名字
char place[10];//生产地
char brand[10];//品牌
float buyprice; //进货价;
float Purchase; //卖出价;
int amount; //存货数量;
int amount1; //售货数量;
int money; //花销;
Objects* Next;
void InputAll()
{
InputName();
InputKind();
InputOther();
}
void InputKind()
{
cout << "tt请选择种类:"; cin >> kind;
}
void InputName()
{
cout << "tt请输入商品的名称:"; cin >> name;
}
void InputBuyprice()
{
cout << "tt请输入进货价:"; cin >> buyprice;
}
void InputPurchase()
{
cout << "tt请输入售出价:"; cin >> Purchase;
}
void InputAmount()
{
cout << "tt请输入剩余商品数量:"; cin >> amount;
}
void Inputmoney()
{
cout << "tt请输入花销:"; cin >> money;
}
void InputPlace()
{
cout << "tt请输入商品产地:"; cin >> place;
}
void Inputbrand()
{
cout << "tt请输入生产商品牌:"; cin >> brand;
}
void InputOther()
{
cout << "tt请输入进货价:"; cin >> buyprice;
cout << "tt请输入售出价:"; cin >> Purchase;
cout << "tt请输入存货数量:"; cin >> amount;
cout << "tt请输入商品的产地:"; cin >> place;
cout << "tt请输入生产商品牌:"; cin >> brand;
money = 0;
}
void ReadFile(istream& in) //将输入的东西存入文件
{
in >> name >> kind >> Id >> buyprice >> Purchase >> amount >> place >> brand >> money;
}
void Show()
{
cout << "商品名: " << name << endl << "种类:" << kind << endl << "进货价: " << buyprice << endl << "售出价: " << Purchase << endl << "剩余商品数量: " <<
amount << endl << "花销:" << money << endl << "商品的产地: " << place << endl << "生产商品牌: " << brand << endl << endl << endl;
}
void Show2()
{
cout << "商品名: " << name << endl << "种类:" << kind << endl << "进货价: " << buyprice << endl << "售出价: " << Purchase << endl << "剩余商品数量: " <<
amount << endl << "商品的产地: " << place << endl << "生产商品牌: " << brand << endl << endl << endl;
}
void Show3()
{
cout << "商品名: " << name << endl << "种类:" << kind << endl << "售出价: " << Purchase << endl << "剩余商品数量: " <<
amount << endl << "花销:" << money << endl << "商品的产地: " << place << endl << "生产商品牌: " << brand << endl << endl << endl;
}
};
class ObjectsInformation : public Objects // ObjectsInformation类
{
public:
ObjectsInformation();
~ObjectsInformation();
void showMenu(int n);
void Find();
void Save();
void ModifyItem();
void RemoveItem();
void Swap(Objects*, Objects*);
void my_Sort();
void Purchase();
int ListCount();
void Display()
{
system("cls");
i = 0;
for (Objects* p = Head->Next; p != End; p = p->Next)
{
p->Show();
i++;
}
cout << "共有" << i << "个商品" << "n" << endl;
cout << "输入任意字符!继续……";
_getch();
}
void Display2()
{
system("cls");
i = 0;
for (Objects* p = Head->Next; p != End; p = p->Next)
{
p->Show2();
i++;
}
cout << "共有" << i << "个商品" << "n" << endl;
cout << "输入任意字符!继续……";
_getch();
}
void AddItem()//从键盘输入商品信息
{
system("cls");
End->InputName();
showMenu(1);
End->InputKind();
End->InputOther();
End->Next = new Objects;
End = End->Next;
cout << "添加成功!" << endl;
Save();
cout << "输入任意字符!继续……";
_getch();
}
private:
Objects* Head, * End;
int i;
ifstream in;
ofstream out;
Objects* FindItem(char* name)
{
for (Objects* p = Head; p->Next != End; p = p->Next)//匹配成功则返回上一个指针,不成功就返回空
if (!strcmp(p->Next->name, name))return p;
return NULL;
}
Objects* Findkind(char* kind)
{
for (Objects* p = Head; p->Next != End; p = p->Next)//匹配成功则返回上一个指针,不成功就返回空
if (!strcmp(p->Next->kind, kind))return p;
return NULL;
}
Objects* Findbrand(char* brand)
{
for (Objects* p = Head; p->Next != End; p = p->Next)//匹配成功则返回上一个指针,不成功就返回空
if (!strcmp(p->Next->brand, brand))return p;
return NULL;
}
};
ObjectsInformation::ObjectsInformation() //构造函数
{
Head = new Objects;
Head->Next = new Objects;
End = Head->Next;
in.open("supermarket.txt");
if (!in)
cout <<" " <ReadFile(in);
if (End->name[0] == ' ')break;
End->Next = new Objects;
End = End->Next;
}
in.close();
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << "tttt超市里面还有一些库存,可以看看" << "n" << endl;
}
}
ObjectsInformation::~ObjectsInformation() //析构函数
{
//Save();
for (Objects* temp; Head->Next != End;)
{
temp = Head->Next;
Head->Next = Head->Next->Next;
delete temp;
}
delete Head, End;
}
void ObjectsInformation::showMenu(int n)//菜单 //菜单
{
switch (n)
{
case 1:
{
cout << "▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉n"
<< " 1. 食品 2. 化妆品 3. 日用品 4. 饮料 n"
<< "▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉n" << endl;
break; }
case 2:
{
system("cls");
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ 超 市 管 理 系 统 ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆" << endl;
cout << " " << endl; cout << " " << endl;
cout << " ************* 1.增添商品 ************* " << endl;
cout << " " << endl;
cout << " ************* 2.统计商品 ************* " << endl;
cout << " " << endl;
cout << " ************* 3.查询商品 ************* " << endl;
cout << " " << endl;
cout << " ************* 4.删除商品 ************* " << endl;
cout << " " << endl;
cout << " ************* 5.修改商品 ************* " << endl;
cout << " " << endl;
cout << " ************* 6.购买商品 ************* " << endl;
cout << " " << endl;
cout << " ************* 0.退出系统 ************* " << endl;
cout << "ttntttt 请选择:0-6" << endl;
break; }
case 3:
{
system("cls");
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " ************* 1.修改种类 ************* n ";
cout << " " << endl;
cout << " ************* 2.修改名称 ************* n";
cout << " " << endl;
cout << " ************* 3.修改进货价 ************* n";
cout << " " << endl;
cout << " ************* 4.修改售出价 ************* n";
cout << " " << endl;
cout << " ************* 5.修改剩余量 ************* n ";
cout << " " << endl;
cout << " ************* 6.修改产地 ************* n ";
cout << " " << endl;
cout << " ************* 7.修改品牌 ************* n ";
cout << " " << endl;
cout << " ************* 8.修改全部信息 ************* " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " ************* 请选择:0-8进行相关操作" << endl;
break;
}
case 4:
{
system("cls");
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << "*************************************************************************************n"
<< "t 1. 按商品价排序 2. 按库存量排序 2. 按生产品牌排序 "
<< "**************************************************************************************" << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << "ttntttt 请选择:0-3" << endl;
break;
}
case 5:
{
system("cls");
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << "*************************************************************************************n"
<< "t 1. 按商品名称查询 2. 按商品种类查询 3. 按商品厂家查询 "
<< "**************************************************************************************" << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << "ttntttt 请选择:0-3" << endl;
}
}
}
void ObjectsInformation::Find() //查找函数
{
system("cls");
char name[20], Id[10];
int x;
Objects* p = NULL;
showMenu(5);
cin >> x;
switch (x)
{
case 1: {cout << "tt请输入要查找的商品的名称:"; cin >> name;
if (p = FindItem(name))
{
p->Next->Show2();
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到该名称的商品!" << 'n' << endl;
cout << "输入任意字符!继续……";
_getch();
}
}break;
case 2: {cout << "tt请输入要查找的商品的种类:"; cin >> name;
if (p = Findkind(kind))
{
p->Next->Show2();
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到该种类的商品!" << 'n' << endl;
cout << "输入任意字符!继续……";
_getch();
}
}break;
case 3: {cout << "tt请输入要查找的厂家的名称:"; cin >> name;
if (p = Findbrand(brand))
{
p->Next->Show2();
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到该厂家的商品!" << 'n' << endl;
cout << "输入任意字符!继续……";
_getch();
}
}break;
}
}
void ObjectsInformation::ModifyItem() //修改商品信息
{
showMenu(3);
int x;
cin >> x;
switch (x)
{
case 1:
{
char Id[20];
Objects* p = NULL;
cout << "tt请输入要修改的商品的名称:"; cin >> Id;
if (p = FindItem(Id))
{
cout << "tt已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputKind();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 2:
{
char Id[20];
Objects* p = NULL;
cout << "tt请输入要修改的商品的名称:"; cin >> Id;
if (p = FindItem(Id))
{
cout << "tt已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputName();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 3:
{
char Id[20];
Objects* p = NULL;
cout << "tt请输入要修改的商品的名称:"; cin >> Id;
if (p = FindItem(Id))
{
cout << "tt已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputBuyprice();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 4:
{
char Id[20];
Objects* p = NULL;
cout << "tt请输入要修改的商品的名称:"; cin >> Id;
if (p = FindItem(Id))
{
cout << "tt已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputPurchase();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 5:
{
char Id[20];
Objects* p = NULL;
cout << "tt请输入要修改的商品的名称:"; cin >> Id;
if (p = FindItem(Id))
{
cout << "tt已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputAmount();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 6:
{
char Id[20];
Objects* p = NULL;
cout << "tt请输入要修改的商品的名称:"; cin >> Id;
if (p = FindItem(Id))
{
cout << "tt已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputPlace();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 7:
{
char Id[20];
Objects* p = NULL;
cout << "tt请输入要修改的商品的名称:"; cin >> Id;
if (p = FindItem(Id))
{
cout << "tt已找到商品的信息,请输入新的信息!" << endl;
p->Next->Inputbrand();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 8:
{
char Id[20];
Objects* p = NULL;
cout << "tt请输入要修改的商品的名称:"; cin >> Id;
if (p = FindItem(Id))
{
cout << "tt已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputAll();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
}
}
}
void ObjectsInformation::RemoveItem() //删除商品信息
{
system("cls");
char Id[20];
Objects* p = NULL, * temp = NULL;
cout << "tt请输入要删除的商品的名称:" << endl; cin >> Id;
if (p = FindItem(Id))
{
temp = p->Next;
p->Next = p->Next->Next;
delete temp;
cout << "tt删除成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
}
void ObjectsInformation::Purchase() //购买商品
{
system("cls");
char Id[20]; int i;
Objects* p = NULL;
Objects* temp = NULL;
cout << "tt请输入要购买的商品的名称和数量:"; cin >> Id; cin >> i;
if (p = FindItem(Id))
{
if (p->Next->amount > i)
{
p->Next->amount -= i;
p->Next->amount1 += i;
p->Next->money += i * (p->Next->Purchase);
cout << "商品购买成功!" << "n";
cout << "购买商品名称: " << Id << "t" << "数量: " << i << "n";
cout << "n";
cout << "购买后商品信息: " << endl;
p->Next->Show3();
Save();
p->Next->money = 0;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "商品数量不够,不能购买" << endl;
cout << "输入任意字符!继续……";
_getch();
Purchase();
}
}
else
{
cout << "无此种商品,不能购买" << endl;
cout << "输入任意字符!继续……";
_getch();
showMenu(2);
}
}
void ObjectsInformation::Swap(Objects* p1, Objects* p2) //交换两个商品的数据
{
Objects* temp = new Objects;
strcpy_s(temp->name, p1->name);
strcpy_s(temp->kind, p1->kind);
strcpy_s(temp->Id, p1->Id);
strcpy_s(temp->place, p1->place);
temp->Purchase = p1->Purchase;
temp->buyprice = p1->buyprice;
temp->amount = p1->amount;
temp->money = p1->money;
strcpy_s(temp->brand, p1->brand);
strcpy_s(p1->name, p2->name);
strcpy_s(p1->kind, p2->kind);
strcpy_s(p1->Id, p2->Id);
strcpy_s(p1->place, p2->place);
p1->Purchase = p2->Purchase;
p1->buyprice = p2->buyprice;
p1->amount = p2->amount;
p1->money = p2->money;
strcpy_s(p1->brand, p2->brand);
strcpy_s(p2->name, temp->name);
strcpy_s(p2->kind, temp->kind);
strcpy_s(p2->Id, temp->Id);
strcpy_s(p2->place, temp->place);
p2->Purchase = temp->Purchase;
p2->buyprice = temp->buyprice;
p2->amount = temp->amount;
p2->money = temp->money;
strcpy_s(p2->brand, temp->brand);
}
int ObjectsInformation::ListCount() //统计当前链表的记录总数,返回一个整数
{
if (!Head)
return 0;
int n = 0;
for (Objects* p = Head->Next; p != End; p = p->Next)
{
n++;
}
return n;
}
void ObjectsInformation::my_Sort() //排序商品
{
showMenu(4);
int x;
cin >> x;
switch (x)
{
case 1:
{
system("cls");
cout << "tt排序中..." << endl;
cout << "n";
Objects* p = NULL, * p1 = NULL, * k = NULL;
int n = ObjectsInformation::ListCount();
if (n < 2)
return;
for (p = Head->Next; p != End; p = p->Next)
for (k = p->Next; k != End; k = k->Next)
{
if (p->buyprice > k->buyprice)
{
ObjectsInformation::Swap(p, k);
}
}
Display2();
out.open("进货价.txt");
for (Objects* q = Head->Next; q != End; q = q->Next)
out << q->name << "t" << q->kind << "t" << q->Id << "t" << q->buyprice << "t" << q->Purchase << "t" << q->amount << "t" << q->place << "t" << q->brand << "t" << q->money << 'n';
out.close();
cout << "保存信息成功" << endl;
cout << "排序完成!" << endl;
_getch();
return;
}
case 2:
{
system("cls");
cout << "tt排序中..." << endl;
cout << "n";
Objects* p = NULL, * p1 = NULL, * k = NULL;
int n = ObjectsInformation::ListCount();
if (n < 2)
return;
for (p = Head->Next; p != End; p = p->Next)
for (k = p->Next; k != End; k = k->Next)
{
if (p->amount > k->amount)
{
ObjectsInformation::Swap(p, k);
}
}
Display2();
out.open("库存量.txt");
for (Objects* q = Head->Next; q != End; q = q->Next)
out << q->name << "t" << q->kind << "t" << q->Id << "t" << q->buyprice << "t" << q->Purchase << "t" << q->amount << "t" << q->place << "t" << q->brand << "t" << q->money << 'n';
out.close();
cout << "保存信息成功" << endl;
cout << "排序完成!" << endl;
_getch();
return;
}
case 3:
{
system("cls");
cout << "tt排序中..." << endl;
cout << "n";
Objects* p = NULL, * p1 = NULL, * k = NULL;
int n = ObjectsInformation::ListCount();
if (n < 2)
return;
for (p = Head->Next; p != End; p = p->Next)
for (k = p->Next; k != End; k = k->Next)
{
if (p->money > k->money)
{
ObjectsInformation::Swap(p, k);
}
}
Display2();
out.open("生产品牌.txt");
for (Objects* q = Head->Next; q != End; q = q->Next)
out << q->name << "t" << q->kind << "t" << q->Id << "t" << q->buyprice << "t" << q->Purchase << "t" << q->amount << "t" << q->place << "t" << q->brand << "t" << q->money << 'n';
out.close();
cout << "保存信息成功" << endl;
cout << "排序完成!" << endl;
_getch();
return;
}
}
}
void ObjectsInformation::Save() //保存商品信息到文件函数
{
out.open("supermarket.txt");
for (Objects* p = Head->Next; p != End; p = p->Next)
out << p->name << "t" << p->kind << "t" << p->Id << "t" << p->buyprice << "t" << p->Purchase << "t" << p->amount << "t" << p->place << "t" << p->brand << "t" << p->money << 'n';
out.close();
cout << "保存信息成功" << endl;
}
int main()
{
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " ◢ █ █ █ █ █ █ █ █ █ █ █ █ ◣" << endl;
cout << " █ █ █ █ █ █ █ █ █ █ █ █ █ █ " << endl;
cout << " █ █ ◥ █ █ ◤ █ █ " << endl;
cout << " ◢ █ █ █ ◥ ◤ █ █ ◣" << endl;
cout << " ▊ ▎ █ █ ◣ ◢ █ ▊ ▊" << endl;
cout << " ▊ ▎ █ █ ◤ ● ● ◥ █ ▊ ▊" << endl;
cout << " ▊ █ █ █ ▊ ▊" << endl;
cout << " ◥ ▇ █ █ ▊ ▊ █ ▇ ◤ 嗨嗨嗨,欢迎光临老八秘制小超市" << endl;
cout << " █ █ ◥▆ ▄ ▄ ▄ ▄ ▆ ◤ █ ▊ ◢▇ ▇◣" << endl;
cout << " ◢ █ █◥◥▆ ▅ ▄ ▂ ▂ ▂ ▂ ▄ ▅ ▆ ◢█" << endl;
cout << " █ ╳ ╳ █ ◢◤" << endl;
cout << " ◥ █ ◣ ˙ ˙ ◢ █ ◢◤ " << endl;
cout << " ▊ ▊ █" << endl;
cout << " ▊ ▊ ◢◤" << endl;
cout << " ▊ ⊕ █ ▇ ▇ ▇ ◤ " << endl;
cout << " ◢ █ ▇ ▆ ▆ ▆ ▅ ▅ ▅ ▅ ▆ ▆ ▆ ▇ █ " << endl;
cout << " ▊ ▊ " << endl;
ObjectsInformation Grade;
cout << " 温馨提示:按任意键进入超市后台";
_getch();
int x;
bool quit = false;
while (!quit)
{
Grade.showMenu(2);
cin >> x;
switch (x)
{
case 0: {quit = true; break; }
case 1: {Grade.AddItem(); break; }
case 2: {Grade.my_Sort(); break; }
case 3: {Grade.Find(); break; }
case 4: {Grade.RemoveItem(); break; }
case 5: {Grade.ModifyItem(); break; }
case 6: {Grade.Purchase(); break; }
}
}
return 0;
}
系统内容展示:
(1)销售功能。购买商品时,先输入类别,然后输入商品名称,并在库存中查找该商品的相关信息。如果有库存量,输入购买的数量,进行相应计算。如果库存量不够,给出提示信息,结束购买。
(2)商品简单管理功能。
添加功能:主要完成商品信息的添加。
查询功能:可按商品类别、商品名称、生产厂家进行查询。若存在相应信息,输出所查询的信息,若不存在该记录,则提示“该记录不存在!”。
修改功能:可根据查询结果对相应的记录进行修改。
删除功能:主要完成商品信息的删除。先输入商品类别,再输入要删除的商品名称,根据查询结果删除该物品的记录,如果该商品不在物品库中,则提示“该商品不存在”。
(3)统计功能。
输出当前库存中所有商品的总数及详细信息;可按商品的价格、库存量、生产厂家进行统计,输出统计信息时,要按从大到小排序。
提示:
(1) 类的设计关系
(2) 主功能函数
源代码
#include
#include
#include //头文件引入
#include
#include
using namespace std;
class Objects //大类
{
public:
Objects()
{
}
char Id[20];
char kind [20];//种类
char name[20];//名字
char place[10];//生产地
char brand[10];//品牌
float buyprice; //进货价;
float Purchase; //卖出价;
int amount; //存货数量;
int amount1; //售货数量;
int money; //花销;
Objects* Next;
void InputAll()
{
InputName();
InputKind();
InputOther();
}
void InputKind()
{
cout << "tt请选择种类:"; cin >> kind;
}
void InputName()
{
cout << "tt请输入商品的名称:"; cin >> name;
}
void InputBuyprice()
{
cout << "tt请输入进货价:"; cin >> buyprice;
}
void InputPurchase()
{
cout << "tt请输入售出价:"; cin >> Purchase;
}
void InputAmount()
{
cout << "tt请输入剩余商品数量:"; cin >> amount;
}
void Inputmoney()
{
cout << "tt请输入花销:"; cin >> money;
}
void InputPlace()
{
cout << "tt请输入商品产地:"; cin >> place;
}
void Inputbrand()
{
cout << "tt请输入生产商品牌:"; cin >> brand;
}
void InputOther()
{
cout << "tt请输入进货价:"; cin >> buyprice;
cout << "tt请输入售出价:"; cin >> Purchase;
cout << "tt请输入存货数量:"; cin >> amount;
cout << "tt请输入商品的产地:"; cin >> place;
cout << "tt请输入生产商品牌:"; cin >> brand;
money = 0;
}
void ReadFile(istream& in) //将输入的东西存入文件
{
in >> name >> kind >> Id >> buyprice >> Purchase >> amount >> place >> brand >> money;
}
void Show()
{
cout << "商品名: " << name << endl << "种类:" << kind << endl << "进货价: " << buyprice << endl << "售出价: " << Purchase << endl << "剩余商品数量: " <<
amount << endl << "花销:" << money << endl << "商品的产地: " << place << endl << "生产商品牌: " << brand << endl << endl << endl;
}
void Show2()
{
cout << "商品名: " << name << endl << "种类:" << kind << endl << "进货价: " << buyprice << endl << "售出价: " << Purchase << endl << "剩余商品数量: " <<
amount << endl << "商品的产地: " << place << endl << "生产商品牌: " << brand << endl << endl << endl;
}
void Show3()
{
cout << "商品名: " << name << endl << "种类:" << kind << endl << "售出价: " << Purchase << endl << "剩余商品数量: " <<
amount << endl << "花销:" << money << endl << "商品的产地: " << place << endl << "生产商品牌: " << brand << endl << endl << endl;
}
};
class ObjectsInformation : public Objects // ObjectsInformation类
{
public:
ObjectsInformation();
~ObjectsInformation();
void showMenu(int n);
void Find();
void Save();
void ModifyItem();
void RemoveItem();
void Swap(Objects*, Objects*);
void my_Sort();
void Purchase();
int ListCount();
void Display()
{
system("cls");
i = 0;
for (Objects* p = Head->Next; p != End; p = p->Next)
{
p->Show();
i++;
}
cout << "共有" << i << "个商品" << "n" << endl;
cout << "输入任意字符!继续……";
_getch();
}
void Display2()
{
system("cls");
i = 0;
for (Objects* p = Head->Next; p != End; p = p->Next)
{
p->Show2();
i++;
}
cout << "共有" << i << "个商品" << "n" << endl;
cout << "输入任意字符!继续……";
_getch();
}
void AddItem()//从键盘输入商品信息
{
system("cls");
End->InputName();
showMenu(1);
End->InputKind();
End->InputOther();
End->Next = new Objects;
End = End->Next;
cout << "添加成功!" << endl;
Save();
cout << "输入任意字符!继续……";
_getch();
}
private:
Objects* Head, * End;
int i;
ifstream in;
ofstream out;
Objects* FindItem(char* name)
{
for (Objects* p = Head; p->Next != End; p = p->Next)//匹配成功则返回上一个指针,不成功就返回空
if (!strcmp(p->Next->name, name))return p;
return NULL;
}
Objects* Findkind(char* kind)
{
for (Objects* p = Head; p->Next != End; p = p->Next)//匹配成功则返回上一个指针,不成功就返回空
if (!strcmp(p->Next->kind, kind))return p;
return NULL;
}
Objects* Findbrand(char* brand)
{
for (Objects* p = Head; p->Next != End; p = p->Next)//匹配成功则返回上一个指针,不成功就返回空
if (!strcmp(p->Next->brand, brand))return p;
return NULL;
}
};
ObjectsInformation::ObjectsInformation() //构造函数
{
Head = new Objects;
Head->Next = new Objects;
End = Head->Next;
in.open("supermarket.txt");
if (!in)
cout <<" " <ReadFile(in);
if (End->name[0] == ' ')break;
End->Next = new Objects;
End = End->Next;
}
in.close();
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << "tttt超市里面还有一些库存,可以看看" << "n" << endl;
}
}
ObjectsInformation::~ObjectsInformation() //析构函数
{
//Save();
for (Objects* temp; Head->Next != End;)
{
temp = Head->Next;
Head->Next = Head->Next->Next;
delete temp;
}
delete Head, End;
}
void ObjectsInformation::showMenu(int n)//菜单 //菜单
{
switch (n)
{
case 1:
{
cout << "▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉n"
<< " 1. 食品 2. 化妆品 3. 日用品 4. 饮料 n"
<< "▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉n" << endl;
break; }
case 2:
{
system("cls");
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆ 超 市 管 理 系 统 ☆ ☆ ☆ ☆ ☆ ☆ ☆ ☆" << endl;
cout << " " << endl; cout << " " << endl;
cout << " ************* 1.增添商品 ************* " << endl;
cout << " " << endl;
cout << " ************* 2.统计商品 ************* " << endl;
cout << " " << endl;
cout << " ************* 3.查询商品 ************* " << endl;
cout << " " << endl;
cout << " ************* 4.删除商品 ************* " << endl;
cout << " " << endl;
cout << " ************* 5.修改商品 ************* " << endl;
cout << " " << endl;
cout << " ************* 6.购买商品 ************* " << endl;
cout << " " << endl;
cout << " ************* 0.退出系统 ************* " << endl;
cout << "ttntttt 请选择:0-6" << endl;
break; }
case 3:
{
system("cls");
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " ************* 1.修改种类 ************* n ";
cout << " " << endl;
cout << " ************* 2.修改名称 ************* n";
cout << " " << endl;
cout << " ************* 3.修改进货价 ************* n";
cout << " " << endl;
cout << " ************* 4.修改售出价 ************* n";
cout << " " << endl;
cout << " ************* 5.修改剩余量 ************* n ";
cout << " " << endl;
cout << " ************* 6.修改产地 ************* n ";
cout << " " << endl;
cout << " ************* 7.修改品牌 ************* n ";
cout << " " << endl;
cout << " ************* 8.修改全部信息 ************* " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " ************* 请选择:0-8进行相关操作" << endl;
break;
}
case 4:
{
system("cls");
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << "*************************************************************************************n"
<< "t 1. 按商品价排序 2. 按库存量排序 2. 按生产品牌排序 "
<< "**************************************************************************************" << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << "ttntttt 请选择:0-3" << endl;
break;
}
case 5:
{
system("cls");
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << "*************************************************************************************n"
<< "t 1. 按商品名称查询 2. 按商品种类查询 3. 按商品厂家查询 "
<< "**************************************************************************************" << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << "ttntttt 请选择:0-3" << endl;
}
}
}
void ObjectsInformation::Find() //查找函数
{
system("cls");
char name[20], Id[10];
int x;
Objects* p = NULL;
showMenu(5);
cin >> x;
switch (x)
{
case 1: {cout << "tt请输入要查找的商品的名称:"; cin >> name;
if (p = FindItem(name))
{
p->Next->Show2();
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到该名称的商品!" << 'n' << endl;
cout << "输入任意字符!继续……";
_getch();
}
}break;
case 2: {cout << "tt请输入要查找的商品的种类:"; cin >> name;
if (p = Findkind(kind))
{
p->Next->Show2();
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到该种类的商品!" << 'n' << endl;
cout << "输入任意字符!继续……";
_getch();
}
}break;
case 3: {cout << "tt请输入要查找的厂家的名称:"; cin >> name;
if (p = Findbrand(brand))
{
p->Next->Show2();
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到该厂家的商品!" << 'n' << endl;
cout << "输入任意字符!继续……";
_getch();
}
}break;
}
}
void ObjectsInformation::ModifyItem() //修改商品信息
{
showMenu(3);
int x;
cin >> x;
switch (x)
{
case 1:
{
char Id[20];
Objects* p = NULL;
cout << "tt请输入要修改的商品的名称:"; cin >> Id;
if (p = FindItem(Id))
{
cout << "tt已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputKind();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 2:
{
char Id[20];
Objects* p = NULL;
cout << "tt请输入要修改的商品的名称:"; cin >> Id;
if (p = FindItem(Id))
{
cout << "tt已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputName();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 3:
{
char Id[20];
Objects* p = NULL;
cout << "tt请输入要修改的商品的名称:"; cin >> Id;
if (p = FindItem(Id))
{
cout << "tt已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputBuyprice();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 4:
{
char Id[20];
Objects* p = NULL;
cout << "tt请输入要修改的商品的名称:"; cin >> Id;
if (p = FindItem(Id))
{
cout << "tt已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputPurchase();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 5:
{
char Id[20];
Objects* p = NULL;
cout << "tt请输入要修改的商品的名称:"; cin >> Id;
if (p = FindItem(Id))
{
cout << "tt已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputAmount();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 6:
{
char Id[20];
Objects* p = NULL;
cout << "tt请输入要修改的商品的名称:"; cin >> Id;
if (p = FindItem(Id))
{
cout << "tt已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputPlace();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 7:
{
char Id[20];
Objects* p = NULL;
cout << "tt请输入要修改的商品的名称:"; cin >> Id;
if (p = FindItem(Id))
{
cout << "tt已找到商品的信息,请输入新的信息!" << endl;
p->Next->Inputbrand();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 8:
{
char Id[20];
Objects* p = NULL;
cout << "tt请输入要修改的商品的名称:"; cin >> Id;
if (p = FindItem(Id))
{
cout << "tt已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputAll();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
}
}
}
void ObjectsInformation::RemoveItem() //删除商品信息
{
system("cls");
char Id[20];
Objects* p = NULL, * temp = NULL;
cout << "tt请输入要删除的商品的名称:" << endl; cin >> Id;
if (p = FindItem(Id))
{
temp = p->Next;
p->Next = p->Next->Next;
delete temp;
cout << "tt删除成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "tt没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
}
void ObjectsInformation::Purchase() //购买商品
{
system("cls");
char Id[20]; int i;
Objects* p = NULL;
Objects* temp = NULL;
cout << "tt请输入要购买的商品的名称和数量:"; cin >> Id; cin >> i;
if (p = FindItem(Id))
{
if (p->Next->amount > i)
{
p->Next->amount -= i;
p->Next->amount1 += i;
p->Next->money += i * (p->Next->Purchase);
cout << "商品购买成功!" << "n";
cout << "购买商品名称: " << Id << "t" << "数量: " << i << "n";
cout << "n";
cout << "购买后商品信息: " << endl;
p->Next->Show3();
Save();
p->Next->money = 0;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "商品数量不够,不能购买" << endl;
cout << "输入任意字符!继续……";
_getch();
Purchase();
}
}
else
{
cout << "无此种商品,不能购买" << endl;
cout << "输入任意字符!继续……";
_getch();
showMenu(2);
}
}
void ObjectsInformation::Swap(Objects* p1, Objects* p2) //交换两个商品的数据
{
Objects* temp = new Objects;
strcpy_s(temp->name, p1->name);
strcpy_s(temp->kind, p1->kind);
strcpy_s(temp->Id, p1->Id);
strcpy_s(temp->place, p1->place);
temp->Purchase = p1->Purchase;
temp->buyprice = p1->buyprice;
temp->amount = p1->amount;
temp->money = p1->money;
strcpy_s(temp->brand, p1->brand);
strcpy_s(p1->name, p2->name);
strcpy_s(p1->kind, p2->kind);
strcpy_s(p1->Id, p2->Id);
strcpy_s(p1->place, p2->place);
p1->Purchase = p2->Purchase;
p1->buyprice = p2->buyprice;
p1->amount = p2->amount;
p1->money = p2->money;
strcpy_s(p1->brand, p2->brand);
strcpy_s(p2->name, temp->name);
strcpy_s(p2->kind, temp->kind);
strcpy_s(p2->Id, temp->Id);
strcpy_s(p2->place, temp->place);
p2->Purchase = temp->Purchase;
p2->buyprice = temp->buyprice;
p2->amount = temp->amount;
p2->money = temp->money;
strcpy_s(p2->brand, temp->brand);
}
int ObjectsInformation::ListCount() //统计当前链表的记录总数,返回一个整数
{
if (!Head)
return 0;
int n = 0;
for (Objects* p = Head->Next; p != End; p = p->Next)
{
n++;
}
return n;
}
void ObjectsInformation::my_Sort() //排序商品
{
showMenu(4);
int x;
cin >> x;
switch (x)
{
case 1:
{
system("cls");
cout << "tt排序中..." << endl;
cout << "n";
Objects* p = NULL, * p1 = NULL, * k = NULL;
int n = ObjectsInformation::ListCount();
if (n < 2)
return;
for (p = Head->Next; p != End; p = p->Next)
for (k = p->Next; k != End; k = k->Next)
{
if (p->buyprice > k->buyprice)
{
ObjectsInformation::Swap(p, k);
}
}
Display2();
out.open("进货价.txt");
for (Objects* q = Head->Next; q != End; q = q->Next)
out << q->name << "t" << q->kind << "t" << q->Id << "t" << q->buyprice << "t" << q->Purchase << "t" << q->amount << "t" << q->place << "t" << q->brand << "t" << q->money << 'n';
out.close();
cout << "保存信息成功" << endl;
cout << "排序完成!" << endl;
_getch();
return;
}
case 2:
{
system("cls");
cout << "tt排序中..." << endl;
cout << "n";
Objects* p = NULL, * p1 = NULL, * k = NULL;
int n = ObjectsInformation::ListCount();
if (n < 2)
return;
for (p = Head->Next; p != End; p = p->Next)
for (k = p->Next; k != End; k = k->Next)
{
if (p->amount > k->amount)
{
ObjectsInformation::Swap(p, k);
}
}
Display2();
out.open("库存量.txt");
for (Objects* q = Head->Next; q != End; q = q->Next)
out << q->name << "t" << q->kind << "t" << q->Id << "t" << q->buyprice << "t" << q->Purchase << "t" << q->amount << "t" << q->place << "t" << q->brand << "t" << q->money << 'n';
out.close();
cout << "保存信息成功" << endl;
cout << "排序完成!" << endl;
_getch();
return;
}
case 3:
{
system("cls");
cout << "tt排序中..." << endl;
cout << "n";
Objects* p = NULL, * p1 = NULL, * k = NULL;
int n = ObjectsInformation::ListCount();
if (n < 2)
return;
for (p = Head->Next; p != End; p = p->Next)
for (k = p->Next; k != End; k = k->Next)
{
if (p->money > k->money)
{
ObjectsInformation::Swap(p, k);
}
}
Display2();
out.open("生产品牌.txt");
for (Objects* q = Head->Next; q != End; q = q->Next)
out << q->name << "t" << q->kind << "t" << q->Id << "t" << q->buyprice << "t" << q->Purchase << "t" << q->amount << "t" << q->place << "t" << q->brand << "t" << q->money << 'n';
out.close();
cout << "保存信息成功" << endl;
cout << "排序完成!" << endl;
_getch();
return;
}
}
}
void ObjectsInformation::Save() //保存商品信息到文件函数
{
out.open("supermarket.txt");
for (Objects* p = Head->Next; p != End; p = p->Next)
out << p->name << "t" << p->kind << "t" << p->Id << "t" << p->buyprice << "t" << p->Purchase << "t" << p->amount << "t" << p->place << "t" << p->brand << "t" << p->money << 'n';
out.close();
cout << "保存信息成功" << endl;
}
int main()
{
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " ◢ █ █ █ █ █ █ █ █ █ █ █ █ ◣" << endl;
cout << " █ █ █ █ █ █ █ █ █ █ █ █ █ █ " << endl;
cout << " █ █ ◥ █ █ ◤ █ █ " << endl;
cout << " ◢ █ █ █ ◥ ◤ █ █ ◣" << endl;
cout << " ▊ ▎ █ █ ◣ ◢ █ ▊ ▊" << endl;
cout << " ▊ ▎ █ █ ◤ ● ● ◥ █ ▊ ▊" << endl;
cout << " ▊ █ █ █ ▊ ▊" << endl;
cout << " ◥ ▇ █ █ ▊ ▊ █ ▇ ◤ 嗨嗨嗨,欢迎光临老八秘制小超市" << endl;
cout << " █ █ ◥▆ ▄ ▄ ▄ ▄ ▆ ◤ █ ▊ ◢▇ ▇◣" << endl;
cout << " ◢ █ █◥◥▆ ▅ ▄ ▂ ▂ ▂ ▂ ▄ ▅ ▆ ◢█" << endl;
cout << " █ ╳ ╳ █ ◢◤" << endl;
cout << " ◥ █ ◣ ˙ ˙ ◢ █ ◢◤ " << endl;
cout << " ▊ ▊ █" << endl;
cout << " ▊ ▊ ◢◤" << endl;
cout << " ▊ ⊕ █ ▇ ▇ ▇ ◤ " << endl;
cout << " ◢ █ ▇ ▆ ▆ ▆ ▅ ▅ ▅ ▅ ▆ ▆ ▆ ▇ █ " << endl;
cout << " ▊ ▊ " << endl;
ObjectsInformation Grade;
cout << " 温馨提示:按任意键进入超市后台";
_getch();
int x;
bool quit = false;
while (!quit)
{
Grade.showMenu(2);
cin >> x;
switch (x)
{
case 0: {quit = true; break; }
case 1: {Grade.AddItem(); break; }
case 2: {Grade.my_Sort(); break; }
case 3: {Grade.Find(); break; }
case 4: {Grade.RemoveItem(); break; }
case 5: {Grade.ModifyItem(); break; }
case 6: {Grade.Purchase(); break; }
}
}
return 0;
}
系统内容展示:
图1 系统启始页面
图2 系统菜单页面
图3 系统添加商品页面
图4 系统购买商品页面
图5-6 系统修改商品信息页面
图7 系统删除商品信息页面
图8 系统查询商品信息页面
图9 系统统计排序商品信息页面
个人经验分享:
过这次课程设计,加强了我思考和解决问题的能力。在设计同时也是对知识的渴求和对所学知识的加强,由于课本上的知识太多,平时课间的学习并不能很好的理解和运用,所以在这次课程设计过程中,对C++语言有了更深的了解,并且对于其在软件设计的使用有了更多的认识。
本次的课程设计有成功之处也有不足的地方。
成功之处是:本超市管理系统功能齐全并且经校验无错误,而且具备一定的容错性;功能代码有对应的注释信息,以便以后维护;涉及的知识点较全面,有函数、数组、指针、类与类的继承、运算符重载、异常处理机制、文件、链表;涉及的算法有顺序查找、模糊查找、选择排序;有销售信息保存机制,可以将顾客的消费记录保存,以便以后核对和汇总;有商品的库存判断机制,当商品的库存量为空的时候,在管理员进入系统的第一时刻就会提醒他补充存货。
不足之处是:界面比较单调,由于时间关系没有设计出一个美观的界面;由于没有学习数据库和网络,本系统仅仅只能由一台电脑使用;由于知识缺乏,本系统不能连接扫描仪,以至于商品购买时,只能手动输入;其次排序功能只能对3个货品起作用,逻辑较为简单;货品较少的时候,选用直接查询所有商品即可。
以上是我对本系统优劣之处的总结。但是总的来说本系统还是比较成功的,对我们而言,知识上的收获重要,精神上的丰收更加可喜。挫折是一份财富,经历是一份拥有。这次课程设计必将成为我们人生旅途上一个非常美好的回忆!



