c++课设代码
#include <iostream>
#include <fstream>
#include <string> //头文件引入
#include <stdio.h>
#include <conio.h>
using namespace std;
class Objects //大类
{
public: //公共权限
Objects()
{
}
char name[20];//名字
char kind[20];//种类
char place[10];//生产地
char brand[10];//品牌
float stockprice; //成本价;
float purchaseprice; //购买价;
int stockamount; //存货数量;
int sellamount; //售货数量;
int money; //花费;
Objects* Next;
void InputAll() //打印 名称 种类 进货价 售出价 存货数量 产地 品牌
{
InputName();
InputKind();
InputOther();
}
void InputKind() //打印种类
{
cout << "\t\t请选择种类:"; cin >> kind;
}
void InputName() //打印名称
{
cout << "\t\t请输入商品的名称:"; cin >> name;
}
void InputOther() //打印 进货价 售出价 存货数量 产地 品牌
{
cout << "\t\t请输入进货价:"; cin >> stockprice;
cout << "\t\t请输入售出价:"; cin >> purchaseprice;
cout << "\t\t请输入存货数量:"; cin >> stockamount;
cout << "\t\t请输入商品的产地:"; cin >> place;
cout << "\t\t请输入生产商品牌:"; cin >> brand;
money = 0;
}
void Inputstockprice() //打印进货价
{
cout << "\t\t请输入进货价:"; cin >> stockprice;
}
void Inputpurchaseprice() //打印售出价
{
cout << "\t\t请输入售出价:"; cin >> purchaseprice;
}
void Inputstockamount() //打印商品数量
{
cout << "\t\t请输入剩余商品数量:"; cin >> stockamount;
}
void InputPlace() //打印商品产地
{
cout << "\t\t请输入商品产地:"; cin >> place;
}
void Inputbrand() //打印品牌
{
cout << "\t\t请输入生产商品牌:"; cin >> brand;
}
void ReadFile(istream& in) //将东西存入文件
{
in >> name >> kind >> stockprice >> purchaseprice >> stockamount >> place >> brand;
}
void Show2() //查询和排序某一个商品时打印内容
{
cout << "商品名: " << name << endl << "种类:" << kind << endl << "进货价: " << stockprice << endl << "售出价: " << purchaseprice << endl << "剩余商品数量: " <<
stockamount << endl << "商品的产地: " << place << endl << "生产商品牌: " << brand << endl << endl << endl;
}
void Show3() //购买某一个商品后打印内容
{
cout << "商品名: " << name << endl << "种类:" << kind << endl << "售出价: " << purchaseprice << endl << "剩余商品数量: " << stockamount << endl << "生产商品牌: " <<
brand << endl << "商品的产地: " << place << endl << endl << endl << "共计花费:" << money << 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->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 << " " << endl <<
" " << endl <<
" " << endl <<
"\t\t\t\t超市现在没有任何库存了,赶紧进货吧" << endl;
else
{
while (!in.eof())
{
End->ReadFile(in);
if (End->name[0] == '\0')break;
End->Next = new Objects;
End = End->Next;
}
in.close();
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << "\t\t\t\t超市里面还有一些库存,可以看看" << "\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 << "\t\t\n\t\t\t\t 请选择: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. 按库存量排序 3. 按生产品牌排序 "
<< "**************************************************************************************" << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << "\t\t\n\t\t\t\t 请选择: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 << "\t\t\n\t\t\t\t 请选择: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 << "\t\t请输入要查找的商品的名称:"; cin >> name;
if (p = FindItem(name))
{
p->Next->Show2();
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "\t\t没有找到该名称的商品!" << '\n' << endl;
cout << "输入任意字符!继续……";
_getch();
}
}break;
case 2: {cout << "\t\t请输入要查找的商品的种类:"; cin >> name;
if (p = Findkind(kind))
{
p->Next->Show2();
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "\t\t没有找到该种类的商品!" << '\n' << endl;
cout << "输入任意字符!继续……";
_getch();
}
}break;
case 3: {cout << "\t\t请输入要查找的厂家的名称:"; cin >> name;
if (p = Findbrand(brand))
{
p->Next->Show2();
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "\t\t没有找到该厂家的商品!" << '\n' << endl;
cout << "输入任意字符!继续……";
_getch();
}
}break;
}
}
void ObjectsInformation::ModifyItem() //修改商品信息
{
showMenu(3);
int x;
cin >> x;
switch (x)
{
case 1:
{
char name[20];
Objects* p = NULL;
cout << "\t\t请输入要修改的商品的名称:"; cin >> name;
if (p = FindItem(name))
{
cout << "\t\t已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputKind();
Save();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "\t\t没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 2:
{
char name[20];
Objects* p = NULL;
cout << "\t\t请输入要修改的商品的名称:"; cin >> name;
if (p = FindItem(name))
{
cout << "\t\t已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputName();
Save();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "\t\t没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 3:
{
char name[20];
Objects* p = NULL;
cout << "\t\t请输入要修改的商品的名称:"; cin >> name;
if (p = FindItem(name))
{
cout << "\t\t已找到商品的信息,请输入新的信息!" << endl;
p->Next->Inputstockprice();
Save();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "\t\t没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 4:
{
char name[20];
Objects* p = NULL;
cout << "\t\t请输入要修改的商品的名称:"; cin >> name;
if (p = FindItem(name))
{
cout << "\t\t已找到商品的信息,请输入新的信息!" << endl;
p->Next->Inputpurchaseprice();
Save();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "\t\t没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 5:
{
char name[20];
Objects* p = NULL;
cout << "\t\t请输入要修改的商品的名称:"; cin >> name;
if (p = FindItem(name))
{
cout << "\t\t已找到商品的信息,请输入新的信息!" << endl;
p->Next->Inputstockamount();
Save();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "\t\t没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 6:
{
char name[20];
Objects* p = NULL;
cout << "\t\t请输入要修改的商品的名称:"; cin >> name;
if (p = FindItem(name))
{
cout << "\t\t已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputPlace();
Save();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "\t\t没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 7:
{
char name[20];
Objects* p = NULL;
cout << "\t\t请输入要修改的商品的名称:"; cin >> name;
if (p = FindItem(name))
{
cout << "\t\t已找到商品的信息,请输入新的信息!" << endl;
p->Next->Inputbrand();
Save();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "\t\t没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 8:
{
char name[20];
Objects* p = NULL;
cout << "\t\t请输入要修改的商品的名称:"; cin >> name;
if (p = FindItem(name))
{
cout << "\t\t已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputAll();
Save();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "\t\t没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
}
}
}
void ObjectsInformation::RemoveItem() //删除商品信息
{
system("cls");
char name[20];
Objects* p = NULL, * temp = NULL;
cout << "\t\t请输入要删除的商品的名称:" << endl; cin >> name;
if (p = FindItem(name))
{
temp = p->Next;
p->Next = p->Next->Next;
delete temp;
cout << "\t\t删除成功!" << endl;
Save();
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "\t\t没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
}
void ObjectsInformation::purchase() //购买商品
{
system("cls");
char name[20]; int i;
Objects* p = NULL;
Objects* temp = NULL;
cout << "\t\t请输入要购买的商品的名称和数量:"; cin >> name; cin >> i;
if (p = FindItem(name))
{
if (p->Next->stockamount > i)
{
p->Next->stockamount -= i;
p->Next->money += i * (p->Next->purchaseprice);
cout << "商品购买成功!" << "\n";
cout << "购买商品名称: " << name << "\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) //交换两个商品的数据 name >> kind >> stockprice >> purchaseprice >> stockamount >> place >> brand ;
{
Objects* temp = new Objects;
strcpy_s(temp->name, p1->name);
strcpy_s(temp->kind, p1->kind);
temp->stockprice = p1->stockprice;
temp->purchaseprice = p1->purchaseprice;
temp->stockamount = p1->stockamount;
strcpy_s(temp->place, p1->place);
strcpy_s(temp->brand, p1->brand);
strcpy_s(p1->name, p2->name);
strcpy_s(p1->kind, p2->kind);
strcpy_s(p1->place, p2->place);
p1->purchaseprice = p2->purchaseprice;
p1->stockprice = p2->stockprice;
p1->stockamount = p2->stockamount;
strcpy_s(p1->brand, p2->brand);
strcpy_s(p2->name, temp->name);
strcpy_s(p2->kind, temp->kind);
strcpy_s(p2->place, temp->place);
p2->purchaseprice = temp->purchaseprice;
p2->stockprice = temp->stockprice;
p2->stockamount = temp->stockamount;
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 << "\t\t排序中..." << 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->stockprice > k->stockprice)
{
ObjectsInformation::Swap(p, k);
}
}
Display();
out.open("supermarket.txt");
for (Objects* q = Head->Next; q != End; q = q->Next)
out << q->name << "\t" << q->kind << "\t" << q->stockprice << "\t" << q->purchaseprice << "\t" << q->stockamount << "\t" << q->place << "\t" << q->brand << "\t" << '\n';
out.close();
cout << "保存信息成功" << endl;
cout << "排序完成!" << endl;
_getch();
return;
}
case 2:
{
system("cls");
cout << "\t\t排序中..." << 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->stockamount > k->stockamount)
{
ObjectsInformation::Swap(p, k);
}
}
Display();
out.open("supermarket.txt");
for (Objects* q = Head->Next; q != End; q = q->Next)
out << q->name << "\t" << q->kind << "\t" << q->stockprice << "\t" << q->purchaseprice << "\t" << q->stockamount << "\t" << q->place << "\t" << q->brand << "\t" << '\n';
out.close();
cout << "保存信息成功" << endl;
cout << "排序完成!" << endl;
_getch();
return;
}
case 3:
{
system("cls");
cout << "\t\t排序中..." << 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);
}
}
Display();
out.open("supermarket.txt");
for (Objects* q = Head->Next; q != End; q = q->Next)
out << q->name << "\t" << q->kind << "\t" << q->stockprice << "\t" << q->purchaseprice << "\t" << q->stockamount << "\t" << q->place << "\t" << q->brand << "\t" << '\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->stockprice << "\t" << p->purchaseprice << "\t" << p->stockamount << "\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; }//购买商品
case 7: {Grade.Save(); break; }//保存信息
}
}
return 0;
}
#include <fstream>
#include <string> //头文件引入
#include <stdio.h>
#include <conio.h>
using namespace std;
class Objects //大类
{
public: //公共权限
Objects()
{
}
char name[20];//名字
char kind[20];//种类
char place[10];//生产地
char brand[10];//品牌
float stockprice; //成本价;
float purchaseprice; //购买价;
int stockamount; //存货数量;
int sellamount; //售货数量;
int money; //花费;
Objects* Next;
void InputAll() //打印 名称 种类 进货价 售出价 存货数量 产地 品牌
{
InputName();
InputKind();
InputOther();
}
void InputKind() //打印种类
{
cout << "\t\t请选择种类:"; cin >> kind;
}
void InputName() //打印名称
{
cout << "\t\t请输入商品的名称:"; cin >> name;
}
void InputOther() //打印 进货价 售出价 存货数量 产地 品牌
{
cout << "\t\t请输入进货价:"; cin >> stockprice;
cout << "\t\t请输入售出价:"; cin >> purchaseprice;
cout << "\t\t请输入存货数量:"; cin >> stockamount;
cout << "\t\t请输入商品的产地:"; cin >> place;
cout << "\t\t请输入生产商品牌:"; cin >> brand;
money = 0;
}
void Inputstockprice() //打印进货价
{
cout << "\t\t请输入进货价:"; cin >> stockprice;
}
void Inputpurchaseprice() //打印售出价
{
cout << "\t\t请输入售出价:"; cin >> purchaseprice;
}
void Inputstockamount() //打印商品数量
{
cout << "\t\t请输入剩余商品数量:"; cin >> stockamount;
}
void InputPlace() //打印商品产地
{
cout << "\t\t请输入商品产地:"; cin >> place;
}
void Inputbrand() //打印品牌
{
cout << "\t\t请输入生产商品牌:"; cin >> brand;
}
void ReadFile(istream& in) //将东西存入文件
{
in >> name >> kind >> stockprice >> purchaseprice >> stockamount >> place >> brand;
}
void Show2() //查询和排序某一个商品时打印内容
{
cout << "商品名: " << name << endl << "种类:" << kind << endl << "进货价: " << stockprice << endl << "售出价: " << purchaseprice << endl << "剩余商品数量: " <<
stockamount << endl << "商品的产地: " << place << endl << "生产商品牌: " << brand << endl << endl << endl;
}
void Show3() //购买某一个商品后打印内容
{
cout << "商品名: " << name << endl << "种类:" << kind << endl << "售出价: " << purchaseprice << endl << "剩余商品数量: " << stockamount << endl << "生产商品牌: " <<
brand << endl << "商品的产地: " << place << endl << endl << endl << "共计花费:" << money << 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->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 << " " << endl <<
" " << endl <<
" " << endl <<
"\t\t\t\t超市现在没有任何库存了,赶紧进货吧" << endl;
else
{
while (!in.eof())
{
End->ReadFile(in);
if (End->name[0] == '\0')break;
End->Next = new Objects;
End = End->Next;
}
in.close();
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << "\t\t\t\t超市里面还有一些库存,可以看看" << "\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 << "\t\t\n\t\t\t\t 请选择: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. 按库存量排序 3. 按生产品牌排序 "
<< "**************************************************************************************" << endl;
cout << " " << endl;
cout << " " << endl;
cout << " " << endl;
cout << "\t\t\n\t\t\t\t 请选择: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 << "\t\t\n\t\t\t\t 请选择: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 << "\t\t请输入要查找的商品的名称:"; cin >> name;
if (p = FindItem(name))
{
p->Next->Show2();
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "\t\t没有找到该名称的商品!" << '\n' << endl;
cout << "输入任意字符!继续……";
_getch();
}
}break;
case 2: {cout << "\t\t请输入要查找的商品的种类:"; cin >> name;
if (p = Findkind(kind))
{
p->Next->Show2();
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "\t\t没有找到该种类的商品!" << '\n' << endl;
cout << "输入任意字符!继续……";
_getch();
}
}break;
case 3: {cout << "\t\t请输入要查找的厂家的名称:"; cin >> name;
if (p = Findbrand(brand))
{
p->Next->Show2();
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "\t\t没有找到该厂家的商品!" << '\n' << endl;
cout << "输入任意字符!继续……";
_getch();
}
}break;
}
}
void ObjectsInformation::ModifyItem() //修改商品信息
{
showMenu(3);
int x;
cin >> x;
switch (x)
{
case 1:
{
char name[20];
Objects* p = NULL;
cout << "\t\t请输入要修改的商品的名称:"; cin >> name;
if (p = FindItem(name))
{
cout << "\t\t已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputKind();
Save();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "\t\t没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 2:
{
char name[20];
Objects* p = NULL;
cout << "\t\t请输入要修改的商品的名称:"; cin >> name;
if (p = FindItem(name))
{
cout << "\t\t已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputName();
Save();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "\t\t没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 3:
{
char name[20];
Objects* p = NULL;
cout << "\t\t请输入要修改的商品的名称:"; cin >> name;
if (p = FindItem(name))
{
cout << "\t\t已找到商品的信息,请输入新的信息!" << endl;
p->Next->Inputstockprice();
Save();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "\t\t没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 4:
{
char name[20];
Objects* p = NULL;
cout << "\t\t请输入要修改的商品的名称:"; cin >> name;
if (p = FindItem(name))
{
cout << "\t\t已找到商品的信息,请输入新的信息!" << endl;
p->Next->Inputpurchaseprice();
Save();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "\t\t没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 5:
{
char name[20];
Objects* p = NULL;
cout << "\t\t请输入要修改的商品的名称:"; cin >> name;
if (p = FindItem(name))
{
cout << "\t\t已找到商品的信息,请输入新的信息!" << endl;
p->Next->Inputstockamount();
Save();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "\t\t没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 6:
{
char name[20];
Objects* p = NULL;
cout << "\t\t请输入要修改的商品的名称:"; cin >> name;
if (p = FindItem(name))
{
cout << "\t\t已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputPlace();
Save();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "\t\t没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 7:
{
char name[20];
Objects* p = NULL;
cout << "\t\t请输入要修改的商品的名称:"; cin >> name;
if (p = FindItem(name))
{
cout << "\t\t已找到商品的信息,请输入新的信息!" << endl;
p->Next->Inputbrand();
Save();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "\t\t没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
break;
}
case 8:
{
char name[20];
Objects* p = NULL;
cout << "\t\t请输入要修改的商品的名称:"; cin >> name;
if (p = FindItem(name))
{
cout << "\t\t已找到商品的信息,请输入新的信息!" << endl;
p->Next->InputAll();
Save();
cout << "修改成功!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "\t\t没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
}
}
}
void ObjectsInformation::RemoveItem() //删除商品信息
{
system("cls");
char name[20];
Objects* p = NULL, * temp = NULL;
cout << "\t\t请输入要删除的商品的名称:" << endl; cin >> name;
if (p = FindItem(name))
{
temp = p->Next;
p->Next = p->Next->Next;
delete temp;
cout << "\t\t删除成功!" << endl;
Save();
cout << "输入任意字符!继续……";
_getch();
}
else
{
cout << "\t\t没有找到您需要的商品!" << endl;
cout << "输入任意字符!继续……";
_getch();
}
}
void ObjectsInformation::purchase() //购买商品
{
system("cls");
char name[20]; int i;
Objects* p = NULL;
Objects* temp = NULL;
cout << "\t\t请输入要购买的商品的名称和数量:"; cin >> name; cin >> i;
if (p = FindItem(name))
{
if (p->Next->stockamount > i)
{
p->Next->stockamount -= i;
p->Next->money += i * (p->Next->purchaseprice);
cout << "商品购买成功!" << "\n";
cout << "购买商品名称: " << name << "\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) //交换两个商品的数据 name >> kind >> stockprice >> purchaseprice >> stockamount >> place >> brand ;
{
Objects* temp = new Objects;
strcpy_s(temp->name, p1->name);
strcpy_s(temp->kind, p1->kind);
temp->stockprice = p1->stockprice;
temp->purchaseprice = p1->purchaseprice;
temp->stockamount = p1->stockamount;
strcpy_s(temp->place, p1->place);
strcpy_s(temp->brand, p1->brand);
strcpy_s(p1->name, p2->name);
strcpy_s(p1->kind, p2->kind);
strcpy_s(p1->place, p2->place);
p1->purchaseprice = p2->purchaseprice;
p1->stockprice = p2->stockprice;
p1->stockamount = p2->stockamount;
strcpy_s(p1->brand, p2->brand);
strcpy_s(p2->name, temp->name);
strcpy_s(p2->kind, temp->kind);
strcpy_s(p2->place, temp->place);
p2->purchaseprice = temp->purchaseprice;
p2->stockprice = temp->stockprice;
p2->stockamount = temp->stockamount;
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 << "\t\t排序中..." << 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->stockprice > k->stockprice)
{
ObjectsInformation::Swap(p, k);
}
}
Display();
out.open("supermarket.txt");
for (Objects* q = Head->Next; q != End; q = q->Next)
out << q->name << "\t" << q->kind << "\t" << q->stockprice << "\t" << q->purchaseprice << "\t" << q->stockamount << "\t" << q->place << "\t" << q->brand << "\t" << '\n';
out.close();
cout << "保存信息成功" << endl;
cout << "排序完成!" << endl;
_getch();
return;
}
case 2:
{
system("cls");
cout << "\t\t排序中..." << 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->stockamount > k->stockamount)
{
ObjectsInformation::Swap(p, k);
}
}
Display();
out.open("supermarket.txt");
for (Objects* q = Head->Next; q != End; q = q->Next)
out << q->name << "\t" << q->kind << "\t" << q->stockprice << "\t" << q->purchaseprice << "\t" << q->stockamount << "\t" << q->place << "\t" << q->brand << "\t" << '\n';
out.close();
cout << "保存信息成功" << endl;
cout << "排序完成!" << endl;
_getch();
return;
}
case 3:
{
system("cls");
cout << "\t\t排序中..." << 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);
}
}
Display();
out.open("supermarket.txt");
for (Objects* q = Head->Next; q != End; q = q->Next)
out << q->name << "\t" << q->kind << "\t" << q->stockprice << "\t" << q->purchaseprice << "\t" << q->stockamount << "\t" << q->place << "\t" << q->brand << "\t" << '\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->stockprice << "\t" << p->purchaseprice << "\t" << p->stockamount << "\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; }//购买商品
case 7: {Grade.Save(); break; }//保存信息
}
}
return 0;
}
posted on 2023-02-20 19:20 shenhshihao 阅读(28) 评论(0) 编辑 收藏 举报