超市管理系统的重构及优化

一、程序来源

  本次的程序来源于同学之前完成的大作业,是超市物资管理系统,在对超市物资进行管理的基础上做了部分修改,增加了对于超市后续开分店等对超市的这一事物进行的管理。

二、运行环境和运行结果的截图(代码附加)

  1. 运行环境如下:

  1. 原程序的主要功能如下:

进入程序后首先进入超市管理系统的欢迎界面,效果如下:

A-主要功能

选择 1 “管理物资”后,输入之前创建的超市名称,可以进入对应超市的管理系统,选择对应的操作,按照商品的类别对超市物资进行管理;

屏幕截图 2024-03-06 172212

其中增加商品分为增加商品类别和增加商品数量两种,会在进入选择后进一步划分;

之后按照类别或者商品名称进行操作即可,以下以增加商品为例进行展示;

选择 2 “创建新店”后,可以输入超市名称以创建新店铺,创建成功后会给出提示,并且展示特效。

选择 3 “退出系统”后,则直接退出当前的管理系统。

除了以上的基本功能外,系统还有一些细节的地方,比如增加商品数量时,若是输入的商品不存在,则会给出提示;操作完毕后,会给出选择,是在当前的主菜单继续操作,还是返回之前的主菜单进行操作等等。

源代码如下:

首先是各个函数的头文件:

  1. DailyGoods.h
    复制代码
     1 #pragma once
     2 #include"Goods.h"
     3 #include<string>
     4 #include<iostream>
     5 using namespace std;
     6 
     7 // 派生类:日常用品类
     8 class DailyGoods : public Goods {
     9 public:
    10     static int count1;     //总商品样数
    11     DailyGoods(const string& n="", int q=0) : Goods(n, q) {}
    12     void ShowMe();
    13     friend  istream& operator >>(istream& is, DailyGoods& dailygoods);  // 重载输入操作符,用于直接读入商品信息
    14     friend ostream& operator<<(ostream& output, DailyGoods& dailygoods);
    15     
    16 };
    17 istream& operator >>(istream& is, DailyGoods& dailygoods);
    18 ostream& operator<<(ostream& output, DailyGoods& dailygoods);
    DailyGoods.h
    复制代码
  2. ElectricalAppliance.h
    复制代码
     1 #pragma once
     2 #include"Goods.h"
     3 #include<string>
     4 #include<iostream>
     5 using namespace std;
     6 
     7 // 派生类:家电类
     8 class ElectricalAppliance : public Goods {
     9 private:
    10     string color;    // 家电类商品颜色
    11     
    12 public:
    13     static int count3;     //总商品样数
    14     ElectricalAppliance(const string& n = "", int q = 0, const string& c = "") : Goods(n, q) { color=c; }
    15     void ShowMe() override;
    16     friend  istream& operator >>(istream& is, ElectricalAppliance& appliance);  // 重载输入操作符,用于直接读入商品信息
    17     friend ostream& operator<<(ostream& output, ElectricalAppliance& appliance); //重载输出运算符,便于向显示器输出
    18     friend void get();
    19     friend void set();
    20 };
    ElectricalAppliance.h
    复制代码
  3. Food.h
    复制代码
     1 #pragma once
     2 #include"Goods.h"
     3 #include<iostream>
     4 #include<string>
     5 using namespace std;
     6 
     7 
     8 // 派生类:食品类
     9 class Food : public Goods {
    10 private:
    11     int shelfLife;   // 食品类商品保质期
    12 public:
    13     static int count2;     //总商品样数
    14     Food(const string& n = "", int q = 0, int sl = 0) : Goods(n, q) { shelfLife = sl; }
    15     void ShowMe()override;
    16     friend  istream& operator >>(istream& is, Food& foods);  // 重载输入操作符,用于直接读入商品信息
    17     friend ostream& operator<<(ostream& output, Food& food); //重载输出运算符,便于向显示器输出
    18     friend void get();
    19     friend void set();
    20 };
    Food.h
    复制代码
  4. Goods.h
    复制代码
     1 #pragma once
     2 #include<iostream>
     3 using namespace std;
     4 
     5 // 基类:商品类
     6 class Goods {
     7 public:
     8     int quantity;     // 商品现有数量
     9     string name;      // 商品名称
    10     Goods(const string n, int q) : name(n), quantity(q) {}  //初始构造函数
    11     virtual void ShowMe() = 0;   // 纯虚函数,用于显示商品信息
    12     string getname(){return name;}
    13     friend  istream& operator >>(istream& is, Goods& goods);  // 重载输入操作符,用于直接读入商品信息
    14    // friend int Supermarket::search01(string s);
    15 };
    16 istream& operator >>(istream& is, Goods& goods);
    Goods.h
    复制代码
  5. Supermarket.h
    复制代码
     1 #include"Goods.h"
     2 #include"DailyGoods.h"
     3 #include"Food.h"
     4 #include"ElectricalAppliance.h"
     5 #include<vector>
     6 #include<iostream>
     7 #include<string>
     8 using namespace std;
     9 
    10 // 超市类
    11 class Supermarket {
    12 private:
    13     string name;
    14     vector<DailyGoods> goods1;   // 超市的日用商品库存
    15     vector<Food> goods2;
    16     vector<ElectricalAppliance> goods3;
    17 public:
    18     Supermarket(string s="");
    19     void Sale();
    20     void Search();
    21     void Add();
    22     string get_name();
    23     int search01(string s);
    24     int search02(string s);
    25     int search03(string s);
    26     friend void set();
    27     friend void get();
    28 };
    Supermarket.h
    复制代码
  6. box.h(主要是功能函数的集成)
    复制代码
     1 #include<iostream>
     2 #include<string>
     3 #include<vector>
     4 using namespace std;
     5 
     6 void add_store();
     7 void huansong();
     8 void huanying();
     9 void set();
    10 void get();
    box.h
    复制代码

接下来是各个函数的具体实现:

  1. DailyGoods.cpp
    复制代码
     1 #include"DailyGoods.h"
     2 #include<iostream>
     3 #include<vector>
     4 using namespace std;
     5 
     6 
     7 void DailyGoods::ShowMe() {
     8     cout << "商品名称:" << name << endl;
     9     cout << "商品现有数量:" << quantity << endl;
    10 }
    11 istream& operator >>(istream& is, DailyGoods& dailygoods) {
    12     char m;
    13     cout << "请输入商品名称: " << endl;
    14     is >> dailygoods.name; m=getchar();
    15     cout << "请输入商品数量: " << endl;
    16     is >> dailygoods.quantity; m=getchar();
    17     return is;
    18 }
    19 ostream& operator<<(ostream&output, DailyGoods& dailygoods) {
    20     output << "商品名称:" << dailygoods.name << endl;
    21     output << "商品现有数量:" << dailygoods.quantity << endl;
    22     return output;
    23 }
    DailyGoods.cpp
    复制代码
  2. ElectricalAppliance.cpp
    复制代码
     1 #include"ElectricalAppliance.h"
     2 #include<iostream>
     3 #include<vector>
     4 using namespace std;
     5 
     6 void ElectricalAppliance::ShowMe(){
     7     cout << "商品名称:" << name << endl;
     8     cout << "商品现有数量:" << quantity << endl;
     9     cout << "颜色:" << color << endl;
    10 }
    11 istream& operator >>(istream& is, ElectricalAppliance& appliance) {
    12     char m;
    13     cout << "请输入商品名称: " << endl;
    14     is >> appliance.name; m=getchar();
    15     cout << "请输入商品数量: " << endl;
    16     is >> appliance.quantity; m=getchar();
    17     cout << "请输入商品颜色: " << endl;
    18     is >> appliance.color; m=getchar();
    19     return is;
    20 }
    21 ostream& operator<<(ostream& output, ElectricalAppliance& appliance) {
    22     output << "商品名称:" << appliance.name << endl;
    23     output << "商品现有数量:" << appliance.quantity << endl;
    24     output << "颜色:" << appliance.color << endl;
    25     return output;
    26 }
    ElectricalAppliance.cpp
    复制代码
  3. Food.cpp
    复制代码
     1 #include<iostream>
     2 #include"Food.h"
     3 #include<vector>
     4 using namespace std;
     5 
     6 void Food::ShowMe(){
     7     cout << "商品名称:" << name << endl;
     8     cout << "商品现有数量:" << quantity << endl;
     9     cout << "保质期:" << shelfLife << "" << endl;
    10 }
    11 istream& operator >>(istream& is, Food& food) {
    12     char m;
    13     cout << "请输入商品名称: " << endl;
    14     is >> food.name; m=getchar();
    15     cout << "请输入商品数量: " << endl;
    16     is >> food.quantity;m=getchar();
    17     cout << "请输入商品保质期(单位/天): " << endl;
    18     is >> food.shelfLife;m=getchar();
    19     return is;
    20 }
    21 ostream& operator<<(ostream& output, Food& food) {
    22     output << "商品名称:" << food.name << endl;
    23     output << "商品现有数量:" << food.quantity << endl;
    24     output << "保质期:" << food.shelfLife << "" << endl;
    25     return output;
    26 }
    Food.cpp
    复制代码
  4. Goods.cpp
    复制代码
     1 #include"Goods.h"
     2 #include<iostream>
     3 #include<string>
     4 #include "Food.h"
     5 #include"Supermarket.h"
     6 
     7 using namespace std;
     8 
     9 istream& operator >>(istream &is, Goods& goods){
    10     char m;
    11     cout << "请输入商品名称: " << endl;
    12     is >> goods.name; m=getchar();
    13     cout << "请输入商品数量: " << endl;
    14     is >> goods.quantity; m=getchar();
    15     return is;
    16 }
    Goods.cpp
    复制代码
  5. Supermarket.cpp
    复制代码
      1 #include<iostream>
      2 #include <stdlib.h>
      3 #include<vector>
      4 #include"DailyGoods.h"
      5 #include"Food.h"
      6 #include"ElectricalAppliance.h"
      7 #include"Goods.h"
      8 #include"Supermarket.h"
      9 using namespace std;
     10 
     11 int DailyGoods::count1 = 0;           //日用类商品样数
     12 int Food::count2 = 0;                 //食物类商品样数
     13 int ElectricalAppliance::count3 = 0;  //家电类商品样数
     14 
     15 extern vector<DailyGoods> goods1;   // 超市的日用商品库存
     16 extern vector<Food> goods2;
     17 extern vector<ElectricalAppliance> goods3;
     18 vector<Supermarket> store;
     19 
     20 string Supermarket::get_name() {
     21     return name;
     22 }
     23 Supermarket::Supermarket(string s):name(s){
     24     DailyGoods a("模板",0);
     25     Food b("模板",0,0);
     26     ElectricalAppliance c("模板",0,"模板");
     27     goods1.push_back(a);
     28     goods2.emplace_back(b);
     29     goods3.emplace_back(c);
     30 }
     31 
     32 void Supermarket::Sale() {               // 卖出商品
     33     char m;
     34     xuanze:
     35      cout << "请选择要卖出的商品类别:" << endl;
     36      cout << "1.日用商品类" << endl;
     37      cout << "2.食物类" << endl;
     38      cout << "3.家电类" << endl;
     39      cout << "4.返回上一界面" << endl;
     40      cout << "请输入:" << endl;
     41      int x = 0; cin >> x;
     42      switch (x) {
     43      case(1):
     44      {
     45          string s = "";
     46          int amount = 0, b = 0;
     47          int flag = 1;
     48          cout << "请输入商品名称:" << '\t';
     49          cin >> s; m=getchar();
     50          b = search01(s);
     51          if (b == 0) {
     52              cout << endl << "该商品不存在,请重新确认商品类别!" << endl;
     53              system("pause");
     54              system("cls");
     55              goto xuanze;
     56          }
     57       wojiuyaozhege1:
     58          cout << "请输入卖出的商品数量:" << '\t';
     59          cin >> amount; m=getchar();
     60          if (goods1[b].quantity >= amount) {
     61              goods1[b].quantity -= amount;
     62              cout << "成功卖出商品:" << goods1[b].name << endl;
     63          }
     64          else {
     65              cout << "商品"<< goods1[b].name<<"剩余量为:"<<goods1[b].quantity << ",库存不足,无法卖出!"  << endl;
     66          }
     67          cout << "若继续买出该商品,请按“1”;若重新选择商品,请按“2”;若退出商品买出界面,请按此外任意建:" << '\t';
     68          cin >> x; m=getchar();
     69          if (x == 1) {
     70              //system("cls");
     71              goto wojiuyaozhege1;
     72          }
     73          else if (x == 2) {
     74              system("cls");
     75              goto xuanze;
     76          }
     77          else {
     78              system("cls");
     79              break;
     80          }
     81      }
     82      case(2):
     83      {
     84          string s = "";
     85          int amount = 0, b = 0;
     86          int flag = 1;
     87          cout << "请输入商品名称:" << '\t';
     88          cin >> s; m=getchar();
     89          b = search02(s);
     90          if (b == 0) {
     91              cout << endl << "该商品不存在,请重新确认商品类别!" << endl;
     92              system("pause");
     93              system("cls");
     94              goto xuanze;
     95          }
     96      wojiuyaozhege2:
     97          cout << "请输入卖出的商品数量:" << '\t';
     98          cin >> amount; m=getchar();
     99          if (goods2[b].quantity >= amount) {
    100              goods2[b].quantity -= amount;
    101              cout << "成功卖出商品:" << goods2[b].name << endl;
    102          }
    103          else {
    104              cout << "商品" << goods2[b].name << "剩余量为:" <<goods2[b].quantity << ",库存不足,无法卖出!" << endl;
    105          }
    106          cout << "若继续卖出该商品,请按“1”;若重新选择商品,请按“2”;若退出商品卖出界面,请按此外任意建:" << '\t';
    107          cin >> x; m=getchar();
    108          if (x == 1) {
    109              system("cls");
    110              goto wojiuyaozhege2;
    111          }
    112          else if (x == 2) {
    113             // system("cls");
    114              goto xuanze;
    115          }
    116          else {
    117              system("cls");
    118              break;
    119          }
    120      }
    121      case(3):
    122      {
    123          string s = "";
    124          int amount = 0, b = 0;
    125          int flag = 1;
    126          cout << "请输入商品名称:" << '\t';
    127          cin >> s; m=getchar();
    128          b = search03(s);
    129          if (b == 0) {
    130              cout << endl << "该商品不存在,请重新确认商品类别!" << endl;
    131              system("pause");
    132              system("cls");
    133              goto xuanze;
    134          }
    135      wojiuyaozhege3:
    136          cout << "请输入卖出的商品数量:" << '\t';
    137          cin >> amount; m=getchar();
    138          if (goods3[b].quantity >= amount) {
    139              goods3[b].quantity -= amount;
    140              cout << "成功卖出商品:" << goods3[b].name << endl;
    141          }
    142          else {
    143              cout << "商品" << goods3[b].name << "剩余量为:" << goods3[b].quantity << ",库存不足,无法卖出!" << endl;
    144          }
    145          cout << "若继续卖出该商品,请按“1”;若重新选择商品,请按“2”;若退出商品卖出界面,请按此外任意建:" << '\t';
    146          cin >> x; m=getchar();
    147          if (x == 1) {
    148              system("cls");
    149              goto wojiuyaozhege3;
    150          }
    151          else if (x == 2) {
    152              //system("cls");
    153              goto xuanze;
    154          }
    155          else {
    156              system("cls");
    157              break;
    158          }
    159      }
    160      case(4):
    161          system("cls");
    162          break;
    163      default:
    164          cout << "输入错误,请重新输入!";
    165      }
    166 }
    167 
    168 int Supermarket::search01(string s) {
    169     if (goods1.size() == 0) return 0;
    170      int i;
    171      for (i = 0; i < goods1.size(); i++)
    172          if (goods1[i].name == s) break;
    173      if (i < goods1.size()) return i;
    174      else return 0;
    175  }
    176 int Supermarket::search02(string s) {
    177     if (goods2.size() == 0) return 0;
    178      int i;
    179      for (i = 0; i < goods2.size(); i++)
    180          if (goods2[i].name == s) break;
    181      if (i < goods2.size()) return i;
    182      else return 0;
    183  }
    184 int Supermarket::search03(string s) {
    185     if (goods3.size() == 0) return 0;
    186      int i;
    187      for (i = 0; i < goods1.size(); i++)
    188          if (goods3[i].name == s) break;
    189      if (i < goods3.size()) return i;
    190      else return 0;
    191  }
    192 
    193 void Supermarket::Search() {                 // 查询商品情况
    194     char m;
    195 xuanze:
    196     cout << "请选择要查询的商品类别:" << endl;
    197     cout << "1.日用商品类" << endl;
    198     cout << "2.食物类" << endl;
    199     cout << "3.家电类" << endl;
    200     cout << "4.返回上一界面" << endl;
    201     cout << "请输入:" << endl;
    202     int x = 0;
    203     cin >> x; m=getchar();
    204     switch (x) {
    205     case(1):
    206         if (goods1.size() == 1) cout << "----------当前类无商品!----------" << endl;
    207         else {
    208             cout << "--------以下是超市" <<name<<"的所有日日常用品类商品--------"<< endl;
    209             cout << "当前日常用品类商品总数为:" <<goods1.size()-1<< endl;
    210             for (int i = 1; i < goods1.size(); i++)
    211             {
    212                 goods1[i].ShowMe(); cout << endl;
    213             }
    214             cout << "----------查询完毕!----------" << endl;
    215         }
    216         cout << "若继续查询,请按“1”,若结束查询,请按此外任意键:" << endl;
    217         cin >> x; m=getchar();
    218         if (x == 1) {
    219             //system("pause");
    220             system("cls");
    221             goto xuanze;
    222         }
    223         else {
    224             system("cls");
    225             break;
    226         }
    227     case(2):
    228         if (goods2.size() == 1) cout << "----------当前类无商品!----------" << endl;
    229         else {
    230             cout << "--------以下是超市" << name << "的所有食物类商品--------" << endl;
    231             cout << "当前食物类商品总数为:" << goods2.size() - 1 << endl;
    232             for (int i = 1; i < goods2.size(); i++)
    233             {
    234                 goods2[i].ShowMe(); cout << endl;
    235             }
    236             cout << "----------查询完毕!----------" << endl;
    237         }
    238         cout << "若继续查询,请按“1”,若结束查询,请按此外任意键:" << endl;
    239         cin >> x; m=getchar();
    240         if (x == 1)
    241         {
    242             //system("pause");
    243             system("cls");
    244             goto xuanze;
    245         }
    246         else {
    247             system("cls");
    248             break;
    249         }
    250     case(3):
    251         if (goods3.size() == 1) cout << "----------当前类无商品!----------" << endl;
    252         else {
    253             cout << "--------以下是超市" << name << "的所有电器类商品--------" << endl;
    254             cout << "当前电器类商品总数为:" << goods3.size() - 1 << endl;
    255             for (int i = 1; i < goods3.size(); i++)
    256             {
    257                 goods3[i].ShowMe(); cout << endl;
    258             }
    259             cout << "----------查询完毕!----------" << endl;
    260         }
    261         cout << "若继续查询,请按“1”,若结束查询,请按此外任意键:" << endl;
    262         cin >> x; m=getchar();
    263         if (x == 1) {
    264             //system("pause");
    265             system("cls");
    266             goto xuanze;
    267         }
    268         else {
    269             system("cls");
    270             break;
    271         }
    272     case(4):
    273         break;
    274     default:
    275         cout << "输入错误,请重新输入!" << endl;
    276         goto xuanze;
    277     }
    278 }
    279 void Supermarket::Add() {             // 增加商品
    280     char m;
    281     caidan1:
    282     cout << "请按需求选择: " << endl;
    283     cout << "1.增加商品" << endl;
    284     cout << "2.增加商品数量" << endl;
    285     cout << "3.返回上一界面" << endl;
    286     cout << "请输入:" << endl;
    287     int x;
    288     cin >> x; m=getchar();
    289     switch (x) {
    290     case(1):
    291         caidan2:
    292         cout << "请选择要增加的商品类别: " << endl;
    293         cout << "1.日用商品类" << endl;
    294         cout << "2.食物类" << endl;
    295         cout << "3.家电类" << endl;
    296         cout << "4.返回上一界面" << endl;
    297         cin >> x; m=getchar();
    298         switch (x) {
    299         case(1):
    300         {
    301             DailyGoods d;
    302             cin >> d;
    303             int p = search01(d.name);
    304             if (p) {
    305                 cout << "该商品已存在,不可重复添加!" << endl << "目前该商品的剩余量为";
    306                 cout<< goods1[p].quantity << endl;
    307                 cout << "若继续添加其数量,请按“1”,若停止请按此外任意键:";
    308                 int q; cin >> q; m = getchar();
    309                 if (q != 1) goto caidan2;
    310                 else {
    311                     system("cls");
    312                     goto caidan1;
    313                 }
    314             }
    315             else {
    316                 goods1.push_back(d);
    317                 DailyGoods::count1++;
    318                 cout << "添加成功!" << endl;
    319             }
    320             cout << "若继续添加,请按“1”,如果直接退出添加界面,请按此外任意键:" << endl;
    321             cin >> x; m=getchar();
    322             if (x == 1) {
    323                 system("cls");
    324                 goto caidan1;
    325             }
    326             else {
    327                 system("cls");
    328                 break;
    329             }
    330         }
    331         case(2):
    332         {
    333             Food b;
    334             cin >> b;
    335             int p = search02(b.name);
    336             if (p) {
    337                 cout << "该商品已存在,不可重复添加!" << endl << "目前该商品的剩余量为";
    338                 cout << goods2[p].quantity << endl;
    339                 cout << "若继续添加其数量,请按“1”,若停止请按此外任意键:";
    340                 int q; cin >> q; m = getchar();
    341                 if (q != 1) goto caidan2;
    342                 else {
    343                     system("cls");
    344                     goto caidan1;
    345                 }
    346             }
    347             else {
    348                 goods2.push_back(b);
    349                 Food::count2++;
    350                 cout << "添加成功!" << endl;
    351             }
    352             cout << "若继续添加,请按“1”,如果直接退出添加界面,请按此外任意键:" << endl;
    353             cin >> x; m=getchar();
    354             if (x == 1) {
    355                 system("cls");
    356                 goto caidan1;
    357             }
    358             else {
    359                 system("cls");
    360                 break;
    361             }
    362         }
    363         case(3):
    364         {
    365             ElectricalAppliance c;
    366             cin >> c;
    367             int p = search03(c.name);
    368             if (p) {
    369                 cout << "该商品已存在,不可重复添加!" << endl << "目前该商品的剩余量为";
    370                 cout << goods3[p].quantity << endl;
    371                 cout << "若继续添加其数量,请按“1”,若停止请按此外任意键:";
    372                 int q; cin >> q; m = getchar();
    373                 if (q != 1) goto caidan2;
    374                 else {
    375                     system("cls");
    376                     goto caidan1;
    377                 }
    378             }
    379             else {
    380                 goods3.emplace_back(c);
    381                 ElectricalAppliance::count3++;
    382                 cout << "添加成功!" << endl;
    383             }
    384             cout << "若继续添加,请按“1”,如果直接退出添加界面,请按此外任意键:" << endl;
    385             cin >> x; m=getchar();
    386             if (x == 1) {
    387                 system("cls");
    388                 goto caidan1;
    389             }
    390             else {
    391                 system("cls");
    392                 break;
    393             }
    394         }
    395         case(4):
    396             //system("pause");
    397             system("cls");
    398             goto caidan1;
    399         default:
    400             cout << "输入错误,请重新输入!";
    401             goto caidan2;
    402         }
    403         goto caidan1;
    404     case(2):
    405     {
    406     caidan3:
    407         cout << "请选择要增加商品的类别: " << endl;
    408         cout << "1.日用商品类" << endl;
    409         cout << "2.食物类" << endl;
    410         cout << "3.家电类" << endl;
    411         cout << "4.返回上一界面" << endl;
    412         cin >> x; m=getchar();
    413         switch (x) {
    414         case(1):
    415         {
    416             cout << "请输入要增加的商品名称: " << endl;
    417             string s;
    418             cin >> s; m=getchar();
    419             int y = search01(s);
    420             if (y == 0) {
    421                 cout << "该商品不存在,请重新选择!" << endl;
    422                 system("pause");
    423                 system("cls");
    424                 goto caidan3;
    425             }
    426             else {
    427                 cout << "商品目前的数量为: " << goods1[y].quantity << endl;
    428                 cout << "请输入增加数量" << endl;
    429                 int z = 0;
    430                 cin >> z; m=getchar();
    431                 goods1[y].quantity += z;
    432                 cout << "添加成功!" << endl;
    433             }
    434             cout << "若继续添加,请按“1”,如果直接退出添加界面,请按此外任意键:" << endl;
    435             cin >> x; m=getchar();
    436             if (x == 1) {
    437                 system("cls");
    438                 goto caidan1;
    439             }
    440             else {
    441                 system("cls");
    442                 break;
    443             }
    444         }
    445         case(2):
    446         {
    447             cout << "请输入要增加的商品名称: " << endl;
    448             string s;
    449             cin >> s; m=getchar();
    450             int y = search02(s);
    451             if (y == 0) {
    452                 cout << "该商品不存在,请重新选择!" << endl;
    453                 system("pause");
    454                 system("cls");
    455                 goto caidan3;
    456             }
    457             else {
    458                 cout << "商品目前的数量为: " << goods2[y].quantity << endl;
    459                 cout << "请输入增加数量" << endl;
    460                 int z = 0;
    461                 cin >> z; m=getchar();
    462                 goods2[y].quantity += z;
    463                 cout << "添加成功!" << endl;
    464             }
    465             cout << "若继续添加,请按“1”,如果直接退出添加界面,请按此外任意键:" << endl;
    466             cin >> x; m=getchar();
    467             if (x == 1) {
    468                 system("cls");
    469                 goto caidan1;
    470             }
    471             else {
    472                 system("cls");
    473                 break;
    474             }
    475         }
    476         case(3):
    477         {
    478             cout << "请输入要增加的商品名称: " << endl;
    479             string s;
    480             cin >> s; m=getchar();
    481             int y = search03(s);
    482             if (y == 0) {
    483                 cout << "该商品不存在,请重新选择!" << endl;
    484                 system("pause");
    485                 system("cls");
    486                 goto caidan3;
    487             }
    488             else {
    489                 cout << "商品目前的数量为: " << goods3[y].quantity << endl;
    490                 cout << "请输入增加数量" << endl;
    491                 int z = 0;
    492                 cin >> z; m=getchar();
    493                 goods3[y].quantity += z;
    494                 cout << "添加成功!" << endl;
    495             }
    496             cout << "若继续添加,请按“1”,如果直接退出添加界面,请按此外任意键:" << endl;
    497             cin >> x; m=getchar();
    498             if (x == 1) {
    499                 system("cls");
    500                 goto caidan1;
    501             }
    502             else {
    503                 system("cls");
    504                 break;
    505             }
    506         }
    507         case(4):
    508             system("cls");
    509             goto caidan1;
    510         default:
    511             cout << "输入错误,请重新输入!";
    512             goto caidan3;
    513         }
    514     }
    515     case(3):
    516         break;
    517     default:
    518         cout << "输入错误,请重新输入!";
    519         goto caidan1;
    520     }
    521 }
    Supermarket.cpp
    复制代码
  6. box.cpp(主要是功能函数的集成)
    复制代码
      1 #include<iostream>
      2 #include <stdlib.h >
      3 #include <windows.h>
      4 #include<string>
      5 #include<vector>
      6 #include<fstream>
      7 #include"Supermarket.h"
      8 #include"Food.h"
      9 #include"box.h"
     10 using namespace std;
     11 
     12 extern vector<Supermarket> store;
     13 
     14 int search_store(string s) {
     15     int i;
     16     for (i = 0; i < store.size(); i++) {
     17         if (s == store[i].name) break;
     18     }
     19     if (i < store.size()) return i;
     20     else return 0;
     21 }
     22 void huansong() {
     23     cout << "----------***感谢您的使用!***----------" << endl;
     24 }
     25 void huanying() {
     26     char m;
     27     if (store.size() == 1) {
     28         cout << "----------***欢迎来到超市经营系统***----------" << endl;
     29         cout << "请先创建您的第一个超市!" << endl;
     30         cout << "请输入您的超市名称:" << '\t';
     31         string s;
     32         cin >> s; m = getchar();
     33         Supermarket w(s);
     34         store.push_back(w);
     35         cout << "---------*创建成功*----------!" << endl;
     36         cout << "*******恭喜您拥有自己的第一个超市!*******" << endl;
     37         long time;
     38         {
     39             for (time = 0; time < 3; time++)
     40                 system("color A8");
     41             Sleep(50);
     42             for (time = 0; time < 3; time++)
     43                 system("color B8");
     44             Sleep(50);
     45             for (time = 0; time < 3; time++)
     46                 system("color C8");
     47             Sleep(50);
     48             for (time = 0; time < 3; time++)
     49                 system("color D8");
     50             Sleep(50);
     51             for (time = 0; time < 3; time++)
     52                 system("color E8");
     53             Sleep(50);
     54             for (time = 0; time < 3; time++)
     55                 system("color D8");
     56             Sleep(50);
     57             for (time = 0; time < 3; time++)
     58                 system("color E8");
     59             Sleep(50);
     60             for (time = 0; time < 3; time++)
     61                 system("color F8");
     62             Sleep(50);
     63             for (time = 0; time < 3; time++)
     64                 system("color 0F");
     65             Sleep(50);
     66         }
     67         system("pause");
     68         system("cls");
     69 
     70         goto zhucaidan;
     71     }
     72     else {
     73     zhucaidan:
     74         cout << "----------***欢迎来到超市经营系统***----------" << endl;
     75         cout << "请选择按需求选择您的操作:" << endl;
     76         cout << "1.管理物资" << endl;
     77         cout << "2.增加新店" << endl;
     78         cout << "3.退出系统" << endl;
     79         cout << "请输入:" << endl;
     80         int x;
     81         cin >> x; m = getchar();
     82         switch (x) {
     83         case(1):
     84         {
     85             cout << "请输入您要管理的超市名称:" << endl;
     86             string s;
     87             cin >> s; m = getchar();
     88             int i = search_store(s);
     89             if (i == 0) {
     90                 cout << "该商店不存在!" << endl;
     91                 system("pause");
     92                 system("cls");
     93                 goto zhucaidan;
     94             }
     95             else {
     96             caidan1:
     97                 cout << "请选择按需求选择您的操作:" << endl;
     98                 cout << "1.增加商品/商品数量" << endl;
     99                 cout << "2.查询商品" << endl;
    100                 cout << "3.卖出商品" << endl;
    101                 cout << "4.返回上一界面" << endl;
    102                 cout << "请输入:" << endl;
    103                 cin >> x; m = getchar();
    104                 system("cls");
    105                 switch (x) {
    106                 case(1):
    107                 {
    108                     store[i].Add();
    109                     cout << "如果您想继续在运行该超市,请按“1”,否则请按此外任意按键:" << '\t';
    110                     int y = 0;
    111                     cin >> y; m = getchar();
    112                     if (y == 1) {
    113                         system("cls");
    114                         goto caidan1;
    115                     }
    116                     else goto zhucaidan;
    117                 }
    118                 case(2):
    119                 {
    120                     store[i].Search();
    121                     cout << "如果您想继续在运行该超市,请按“1”,否则请按此外任意按键:" << '\t';
    122                     int y = 0;
    123                     cin >> y; m = getchar();
    124                     if (y == 1) {
    125                         system("cls");
    126                         goto caidan1;
    127                     }
    128                     else goto zhucaidan;
    129                 }
    130                 case(3):
    131                 {
    132                     store[i].Sale();
    133                     cout << "如果您想继续在运行该超市,请按“1”,否则请按此外任意按键:" << '\t';
    134                     int y = 0;
    135                     cin >> y; m = getchar();
    136                     if (y == 1) {
    137                         goto caidan1;
    138                         system("cls");
    139                     }
    140                     else goto zhucaidan;
    141                 }
    142                 case(4):
    143                     goto zhucaidan;
    144                 default:
    145                     cout << "输入错误,请重新输入!" << endl;
    146                     goto caidan1;
    147                 }
    148             }
    149         }
    150         case(2):
    151             add_store();
    152             system("pause");
    153             system("cls");
    154             goto zhucaidan;
    155         case(3):
    156             break;
    157         default:
    158             cout << "输入错误,请重新输入!" << endl;
    159             goto zhucaidan;
    160         }
    161     }
    162 }
    163 
    164 void set() {
    165     Supermarket w("模板");
    166     store.push_back(w);
    167     ifstream ifs;
    168     ifs.open("商品.text", ios::in);
    169     int n=0,a=0,b=0,c=0;
    170     ifs >> n;
    171     for (int i = 1; i < n; i++) {
    172         string y;ifs >> y;
    173         Supermarket w(y);
    174         ifs >> a ;
    175         DailyGoods::count1 = a;
    176         for (int j = 1; j < a; j++)
    177         {
    178             string s; int x;
    179             ifs >> s;
    180             ifs >>x;
    181             DailyGoods a(s, x);
    182             w.goods1.push_back(a);
    183         }
    184         ifs >>b;
    185         Food::count2 = b;
    186         for (int j = 1; j < b; j++)
    187         {
    188             string s; int x, h;
    189             ifs >>s;
    190             ifs >>x;
    191             ifs>>h;
    192             Food a(s, x, h);
    193             w.goods2.push_back(a);
    194         }
    195         ifs >> c;
    196         ElectricalAppliance::count3 = c;
    197         for (int j = 1; j < c; j++)
    198         {
    199             string s, l; int x;
    200             ifs >>s;
    201             ifs >>x;
    202             ifs >>l;
    203             ElectricalAppliance a(s, x, l);
    204             w.goods3.push_back(a);
    205         }
    206         store.push_back(w);
    207     }
    208     ifs.close();
    209 }
    210 
    211 void get() {
    212     ofstream ofs;
    213     ofs.open("商品.text", ios::out);
    214     ofs << store.size() << endl;
    215     for (int i = 1; i < store.size(); i++) {
    216         ofs << store[i].name << endl;
    217         ofs << store[i].goods1.size() << endl;
    218         for (int j = 1; j < store[i].goods1.size(); j++)
    219         {
    220             ofs << store[i].goods1[j].name<<endl;
    221             ofs << store[i].goods1[j].quantity << endl;
    222         }
    223         ofs << store[i].goods2.size() << endl;
    224         for (int j = 1; j < store[i].goods2.size(); j++)
    225         {
    226             ofs << store[i].goods2[j].name << endl;
    227             ofs << store[i].goods2[j].quantity << endl;
    228             ofs << store[i].goods2[j].shelfLife << endl;
    229         }
    230         ofs << store[i].goods3.size() << endl;
    231         for (int j = 1; j < store[i].goods3.size(); j++)
    232         {
    233             ofs << store[i].goods3[j].name << endl;
    234             ofs << store[i].goods3[j].quantity << endl;
    235             ofs << store[i].goods3[j].color << endl;
    236         }
    237     }
    238     ofs.close();
    239 }
    240 
    241 void add_store() {
    242     char m;
    243     cout << "----------***欢迎创建您的超市***-----------" << endl;
    244     cout << "请输入您的新超市名称:" << '\t';
    245     string s; 
    246     cin >> s; m=getchar();
    247     Supermarket a(s);
    248     store.push_back(a);
    249     cout << "恭喜您拥有自己的第" <<store.size()-1<<"家超市!" << endl;
    250     long time;
    251     {
    252         for (time = 0; time < 3; time++)
    253             system("color 0F");
    254         Sleep(50);
    255         for (time = 0; time < 3; time++)
    256             system("color 0A");
    257         Sleep(50);
    258         for (time = 0; time < 3; time++)
    259             system("color 0B");
    260         Sleep(50);
    261         for (time = 0; time < 3; time++)
    262             system("color 0C");
    263         Sleep(50);
    264         for (time = 0; time < 3; time++)
    265             system("color 0D");
    266         Sleep(50);
    267         for (time = 0; time < 3; time++)
    268             system("color 0E");
    269         Sleep(50);
    270         for (time = 0; time < 3; time++)
    271             system("color 0D");
    272         Sleep(50);
    273         for (time = 0; time < 3; time++)
    274             system("color 0B");
    275         Sleep(50);
    276         for (time = 0; time < 3; time++)
    277             system("color 0F");
    278         Sleep(50);
    279     }
    280 }
    box.cpp
    复制代码
  7. 超市.cpp(主函数)
    复制代码
     1 #include<iostream>
     2 #include"box.h"
     3 using namespace std;
     4 
     5 int main() {
     6     set();
     7     huanying();
     8     huansong();
     9     get();
    10 }
    超市.cpp
    复制代码

三、主要问题列表以及相应问题的改善与重构

 

既然系统提出了开新店的想法,那么在实际生活中,商家开新店必然会涉及店铺的转让、合并等操作,从实际应用的角度出发,给当前的系统增加了合并店铺、解散店铺等操作;

对于有保质期的商品,也应该增加商品的生产日期,以方便后续查看商品是否过期;

以下是主要的问题清单:

问题

问题描述

解决方案

问题一

没有对超市进行管理的功能,实际使用中设计店铺的问题会很麻烦

增加对超市进行管理的功能,增加开新店、查询店铺、合并店铺等操作

问题二

对于有保质期的物品,没有生产日期作为参考,不便于超市经营者进行管理

增加商品和查看商品时增加对保质期的记录和显示

 

四、重构的代码

修改后的代码主要是超市类的头文件和实现文件发生改变,以及对于基类Goods的.h文件和各个商品类的.h、.cpp文件、欢迎界面等进行修改,修改后的代码如下:

  1. Supermarket.h
    复制代码
     1 #include"Goods.h"
     2 #include"DailyGoods.h"
     3 #include"Food.h"
     4 #include"ElectricalAppliance.h"
     5 #include<vector>
     6 #include<iostream>
     7 #include<string>
     8 using namespace std;
     9 
    10 // 超市类
    11 class Supermarket {
    12 private:
    13     string name;
    14     vector<DailyGoods> goods1;   // 超市的日用商品库存
    15     vector<Food> goods2;
    16     vector<ElectricalAppliance> goods3;
    17 public:
    18     Supermarket(string s="");
    19     void Sale();
    20     void Search();
    21     void Add();
    22     string get_name();
    23     int search01(string s);
    24     int search02(string s);
    25     int search03(string s);
    26     friend void set();
    27     friend void get();
    28     friend int search_store(string s);
    29     friend void delete_store();
    30     friend void hb();
    31 };
    Supermarket.h
    复制代码
  2. Supermarket.cpp
    复制代码
      1 #include<iostream>
      2 #include <stdlib.h>
      3 #include<vector>
      4 #include"DailyGoods.h"
      5 #include"Food.h"
      6 #include"ElectricalAppliance.h"
      7 #include"Goods.h"
      8 #include"Supermarket.h"
      9 using namespace std;
     10 
     11 int DailyGoods::count1 = 0;           //日用类商品样数
     12 int Food::count2 = 0;                 //食物类商品样数
     13 int ElectricalAppliance::count3 = 0;  //家电类商品样数
     14 
     15 extern vector<DailyGoods> goods1;   // 超市的日用商品库存
     16 extern vector<Food> goods2;
     17 extern vector<ElectricalAppliance> goods3;
     18 vector<Supermarket> store;
     19 
     20 string Supermarket::get_name() {
     21     return name;
     22 }
     23 Supermarket::Supermarket(string s):name(s){
     24     DailyGoods a("模板",0);
     25     Food b("模板",0,0);
     26     ElectricalAppliance c("模板",0,"模板");
     27     goods1.push_back(a);
     28     goods2.emplace_back(b);
     29     goods3.emplace_back(c);
     30 }
     31 
     32 void Supermarket::Sale() {               // 卖出商品
     33     char m;
     34     xuanze:
     35      cout << "请选择要卖出的商品类别:" << endl;
     36      cout << "1.日用商品类" << endl;
     37      cout << "2.食物类" << endl;
     38      cout << "3.家电类" << endl;
     39      cout << "4.返回上一界面" << endl;
     40      cout << "请输入:" << endl;
     41      int x = 0; cin >> x;
     42      switch (x) {
     43      case(1):
     44      {
     45          string s = "";
     46          int amount = 0, b = 0;
     47          int flag = 1;
     48          cout << "请输入商品名称:" << '\t';
     49          cin >> s; m=getchar();
     50          b = search01(s);
     51          if (b == 0) {
     52              cout << endl << "该商品不存在,请重新确认商品类别!" << endl;
     53              system("pause");
     54              system("cls");
     55              goto xuanze;
     56          }
     57       wojiuyaozhege1:
     58          cout << "请输入卖出的商品数量:" << '\t';
     59          cin >> amount; m=getchar();
     60          if (goods1[b].quantity >= amount) {
     61              goods1[b].quantity -= amount;
     62              cout << "成功卖出商品:" << goods1[b].name << endl;
     63          }
     64          else {
     65              cout << "商品"<< goods1[b].name<<"剩余量为:"<<goods1[b].quantity << ",库存不足,无法卖出!"  << endl;
     66          }
     67          cout << "若继续买出该商品,请按“1”;若重新选择商品,请按“2”;若退出商品买出界面,请按此外任意建:" << '\t';
     68          cin >> x; m=getchar();
     69          if (x == 1) {
     70              //system("cls");
     71              goto wojiuyaozhege1;
     72          }
     73          else if (x == 2) {
     74              system("cls");
     75              goto xuanze;
     76          }
     77          else {
     78              system("cls");
     79              break;
     80          }
     81      }
     82      case(2):
     83      {
     84          string s = "";
     85          int amount = 0, b = 0;
     86          int flag = 1;
     87          cout << "请输入商品名称:" << '\t';
     88          cin >> s; m=getchar();
     89          b = search02(s);
     90          if (b == 0) {
     91              cout << endl << "该商品不存在,请重新确认商品类别!" << endl;
     92              system("pause");
     93              system("cls");
     94              goto xuanze;
     95          }
     96      wojiuyaozhege2:
     97          cout << "请输入卖出的商品数量:" << '\t';
     98          cin >> amount; m=getchar();
     99          if (goods2[b].quantity >= amount) {
    100              goods2[b].quantity -= amount;
    101              cout << "成功卖出商品:" << goods2[b].name << endl;
    102          }
    103          else {
    104              cout << "商品" << goods2[b].name << "剩余量为:" <<goods2[b].quantity << ",库存不足,无法卖出!" << endl;
    105          }
    106          cout << "若继续卖出该商品,请按“1”;若重新选择商品,请按“2”;若退出商品卖出界面,请按此外任意建:" << '\t';
    107          cin >> x; m=getchar();
    108          if (x == 1) {
    109              system("cls");
    110              goto wojiuyaozhege2;
    111          }
    112          else if (x == 2) {
    113             // system("cls");
    114              goto xuanze;
    115          }
    116          else {
    117              system("cls");
    118              break;
    119          }
    120      }
    121      case(3):
    122      {
    123          string s = "";
    124          int amount = 0, b = 0;
    125          int flag = 1;
    126          cout << "请输入商品名称:" << '\t';
    127          cin >> s; m=getchar();
    128          b = search03(s);
    129          if (b == 0) {
    130              cout << endl << "该商品不存在,请重新确认商品类别!" << endl;
    131              system("pause");
    132              system("cls");
    133              goto xuanze;
    134          }
    135      wojiuyaozhege3:
    136          cout << "请输入卖出的商品数量:" << '\t';
    137          cin >> amount; m=getchar();
    138          if (goods3[b].quantity >= amount) {
    139              goods3[b].quantity -= amount;
    140              cout << "成功卖出商品:" << goods3[b].name << endl;
    141          }
    142          else {
    143              cout << "商品" << goods3[b].name << "剩余量为:" << goods3[b].quantity << ",库存不足,无法卖出!" << endl;
    144          }
    145          cout << "若继续卖出该商品,请按“1”;若重新选择商品,请按“2”;若退出商品卖出界面,请按此外任意建:" << '\t';
    146          cin >> x; m=getchar();
    147          if (x == 1) {
    148              system("cls");
    149              goto wojiuyaozhege3;
    150          }
    151          else if (x == 2) {
    152              //system("cls");
    153              goto xuanze;
    154          }
    155          else {
    156              system("cls");
    157              break;
    158          }
    159      }
    160      case(4):
    161          system("cls");
    162          break;
    163      default:
    164          cout << "输入错误,请重新输入!";
    165      }
    166 }
    167 
    168 int Supermarket::search01(string s) {
    169     if (goods1.size() == 0) return 0;
    170      int i;
    171      for (i = 0; i < goods1.size(); i++)
    172          if (goods1[i].name == s) break;
    173      if (i < goods1.size()) return i;
    174      else return 0;
    175  }
    176 int Supermarket::search02(string s) {
    177     if (goods2.size() == 0) return 0;
    178      int i;
    179      for (i = 0; i < goods2.size(); i++)
    180          if (goods2[i].name == s) break;
    181      if (i < goods2.size()) return i;
    182      else return 0;
    183  }
    184 int Supermarket::search03(string s) {
    185     if (goods3.size() == 0) return 0;
    186      int i;
    187      for (i = 0; i < goods1.size(); i++)
    188          if (goods3[i].name == s) break;
    189      if (i < goods3.size()) return i;
    190      else return 0;
    191  }
    192 
    193 void Supermarket::Search() {                 // 查询商品情况
    194     char m;
    195 xuanze:
    196     cout << "请选择要查询的商品类别:" << endl;
    197     cout << "1.日用商品类" << endl;
    198     cout << "2.食物类" << endl;
    199     cout << "3.家电类" << endl;
    200     cout << "4.返回上一界面" << endl;
    201     cout << "请输入:" << endl;
    202     int x = 0;
    203     cin >> x; m=getchar();
    204     switch (x) {
    205     case(1):
    206         if (goods1.size() == 1) cout << "----------当前类无商品!----------" << endl;
    207         else {
    208             cout << "--------以下是超市" <<name<<"的所有日日常用品类商品--------"<< endl;
    209             cout << "当前日常用品类商品总数为:" <<goods1.size()-1<< endl;
    210             for (int i = 1; i < goods1.size(); i++)
    211             {
    212                 goods1[i].ShowMe(); cout << endl;
    213             }
    214             cout << "----------查询完毕!----------" << endl;
    215         }
    216         cout << "若继续查询,请按“1”,若结束查询,请按此外任意键:" << endl;
    217         cin >> x; m=getchar();
    218         if (x == 1) {
    219             //system("pause");
    220             system("cls");
    221             goto xuanze;
    222         }
    223         else {
    224             system("cls");
    225             break;
    226         }
    227     case(2):
    228         if (goods2.size() == 1) cout << "----------当前类无商品!----------" << endl;
    229         else {
    230             cout << "--------以下是超市" << name << "的所有食物类商品--------" << endl;
    231             cout << "当前食物类商品总数为:" << goods2.size() - 1 << endl;
    232             for (int i = 1; i < goods2.size(); i++)
    233             {
    234                 goods2[i].ShowMe(); cout << endl;
    235             }
    236             cout << "----------查询完毕!----------" << endl;
    237         }
    238         cout << "若继续查询,请按“1”,若结束查询,请按此外任意键:" << endl;
    239         cin >> x; m=getchar();
    240         if (x == 1)
    241         {
    242             //system("pause");
    243             system("cls");
    244             goto xuanze;
    245         }
    246         else {
    247             system("cls");
    248             break;
    249         }
    250     case(3):
    251         if (goods3.size() == 1) cout << "----------当前类无商品!----------" << endl;
    252         else {
    253             cout << "--------以下是超市" << name << "的所有电器类商品--------" << endl;
    254             cout << "当前电器类商品总数为:" << goods3.size() - 1 << endl;
    255             for (int i = 1; i < goods3.size(); i++)
    256             {
    257                 goods3[i].ShowMe(); cout << endl;
    258             }
    259             cout << "----------查询完毕!----------" << endl;
    260         }
    261         cout << "若继续查询,请按“1”,若结束查询,请按此外任意键:" << endl;
    262         cin >> x; m=getchar();
    263         if (x == 1) {
    264             //system("pause");
    265             system("cls");
    266             goto xuanze;
    267         }
    268         else {
    269             system("cls");
    270             break;
    271         }
    272     case(4):
    273         break;
    274     default:
    275         cout << "输入错误,请重新输入!" << endl;
    276         goto xuanze;
    277     }
    278 }
    279 void Supermarket::Add() {             // 增加商品
    280     char m;
    281     caidan1:
    282     cout << "请按需求选择: " << endl;
    283     cout << "1.增加商品" << endl;
    284     cout << "2.增加商品数量" << endl;
    285     cout << "3.返回上一界面" << endl;
    286     cout << "请输入:" << endl;
    287     int x;
    288     cin >> x; m=getchar();
    289     switch (x) {
    290     case(1):
    291         caidan2:
    292         cout << "请选择要增加的商品类别: " << endl;
    293         cout << "1.日用商品类" << endl;
    294         cout << "2.食物类" << endl;
    295         cout << "3.家电类" << endl;
    296         cout << "4.返回上一界面" << endl;
    297         cin >> x; m=getchar();
    298         switch (x) {
    299         case(1):
    300         {
    301             DailyGoods d;
    302             cin >> d;
    303             int p = search01(d.name);
    304             if (p) {
    305                 cout << "该商品已存在,不可重复添加!" << endl << "目前该商品的剩余量为";
    306                 cout<< goods1[p].quantity << endl;
    307                 cout << "若继续添加其数量,请按“1”,若停止请按此外任意键:";
    308                 int q; cin >> q; m = getchar();
    309                 if (q != 1) goto caidan2;
    310                 else {
    311                     system("cls");
    312                     goto caidan1;
    313                 }
    314             }
    315             else {
    316                 goods1.push_back(d);
    317                 DailyGoods::count1++;
    318                 cout << "添加成功!" << endl;
    319             }
    320             cout << "若继续添加,请按“1”,如果直接退出添加界面,请按此外任意键:" << endl;
    321             cin >> x; m=getchar();
    322             if (x == 1) {
    323                 system("cls");
    324                 goto caidan1;
    325             }
    326             else {
    327                 system("cls");
    328                 break;
    329             }
    330         }
    331         case(2):
    332         {
    333             Food b;
    334             cin >> b;
    335             int p = search02(b.name);
    336             if (p) {
    337                 cout << "该商品已存在,不可重复添加!" << endl << "目前该商品的剩余量为";
    338                 cout << goods2[p].quantity << endl;
    339                 cout << "若继续添加其数量,请按“1”,若停止请按此外任意键:";
    340                 int q; cin >> q; m = getchar();
    341                 if (q != 1) goto caidan2;
    342                 else {
    343                     system("cls");
    344                     goto caidan1;
    345                 }
    346             }
    347             else {
    348                 goods2.push_back(b);
    349                 Food::count2++;
    350                 cout << "添加成功!" << endl;
    351             }
    352             cout << "若继续添加,请按“1”,如果直接退出添加界面,请按此外任意键:" << endl;
    353             cin >> x; m=getchar();
    354             if (x == 1) {
    355                 system("cls");
    356                 goto caidan1;
    357             }
    358             else {
    359                 system("cls");
    360                 break;
    361             }
    362         }
    363         case(3):
    364         {
    365             ElectricalAppliance c;
    366             cin >> c;
    367             int p = search03(c.name);
    368             if (p) {
    369                 cout << "该商品已存在,不可重复添加!" << endl << "目前该商品的剩余量为";
    370                 cout << goods3[p].quantity << endl;
    371                 cout << "若继续添加其数量,请按“1”,若停止请按此外任意键:";
    372                 int q; cin >> q; m = getchar();
    373                 if (q != 1) goto caidan2;
    374                 else {
    375                     system("cls");
    376                     goto caidan1;
    377                 }
    378             }
    379             else {
    380                 goods3.emplace_back(c);
    381                 ElectricalAppliance::count3++;
    382                 cout << "添加成功!" << endl;
    383             }
    384             cout << "若继续添加,请按“1”,如果直接退出添加界面,请按此外任意键:" << endl;
    385             cin >> x; m=getchar();
    386             if (x == 1) {
    387                 system("cls");
    388                 goto caidan1;
    389             }
    390             else {
    391                 system("cls");
    392                 break;
    393             }
    394         }
    395         case(4):
    396             //system("pause");
    397             system("cls");
    398             goto caidan1;
    399         default:
    400             cout << "输入错误,请重新输入!";
    401             goto caidan2;
    402         }
    403         goto caidan1;
    404     case(2):
    405     {
    406     caidan3:
    407         cout << "请选择要增加商品的类别: " << endl;
    408         cout << "1.日用商品类" << endl;
    409         cout << "2.食物类" << endl;
    410         cout << "3.家电类" << endl;
    411         cout << "4.返回上一界面" << endl;
    412         cin >> x; m=getchar();
    413         switch (x) {
    414         case(1):
    415         {
    416             cout << "请输入要增加的商品名称: " << endl;
    417             string s;
    418             cin >> s; m=getchar();
    419             int y = search01(s);
    420             if (y == 0) {
    421                 cout << "该商品不存在,请重新选择!" << endl;
    422                 system("pause");
    423                 system("cls");
    424                 goto caidan3;
    425             }
    426             else {
    427                 cout << "商品目前的数量为: " << goods1[y].quantity << endl;
    428                 cout << "请输入增加数量" << endl;
    429                 int z = 0;
    430                 cin >> z; m=getchar();
    431                 goods1[y].quantity += z;
    432                 cout << "添加成功!" << endl;
    433             }
    434             cout << "若继续添加,请按“1”,如果直接退出添加界面,请按此外任意键:" << endl;
    435             cin >> x; m=getchar();
    436             if (x == 1) {
    437                 system("cls");
    438                 goto caidan1;
    439             }
    440             else {
    441                 system("cls");
    442                 break;
    443             }
    444         }
    445         case(2):
    446         {
    447             cout << "请输入要增加的商品名称: " << endl;
    448             string s;
    449             cin >> s; m=getchar();
    450             int y = search02(s);
    451             if (y == 0) {
    452                 cout << "该商品不存在,请重新选择!" << endl;
    453                 system("pause");
    454                 system("cls");
    455                 goto caidan3;
    456             }
    457             else {
    458                 cout << "商品目前的数量为: " << goods2[y].quantity << endl;
    459                 cout << "请输入增加数量" << endl;
    460                 int z = 0;
    461                 cin >> z; m=getchar();
    462                 goods2[y].quantity += z;
    463                 cout << "添加成功!" << endl;
    464             }
    465             cout << "若继续添加,请按“1”,如果直接退出添加界面,请按此外任意键:" << endl;
    466             cin >> x; m=getchar();
    467             if (x == 1) {
    468                 system("cls");
    469                 goto caidan1;
    470             }
    471             else {
    472                 system("cls");
    473                 break;
    474             }
    475         }
    476         case(3):
    477         {
    478             cout << "请输入要增加的商品名称: " << endl;
    479             string s;
    480             cin >> s; m=getchar();
    481             int y = search03(s);
    482             if (y == 0) {
    483                 cout << "该商品不存在,请重新选择!" << endl;
    484                 system("pause");
    485                 system("cls");
    486                 goto caidan3;
    487             }
    488             else {
    489                 cout << "商品目前的数量为: " << goods3[y].quantity << endl;
    490                 cout << "请输入增加数量" << endl;
    491                 int z = 0;
    492                 cin >> z; m=getchar();
    493                 goods3[y].quantity += z;
    494                 cout << "添加成功!" << endl;
    495             }
    496             cout << "若继续添加,请按“1”,如果直接退出添加界面,请按此外任意键:" << endl;
    497             cin >> x; m=getchar();
    498             if (x == 1) {
    499                 system("cls");
    500                 goto caidan1;
    501             }
    502             else {
    503                 system("cls");
    504                 break;
    505             }
    506         }
    507         case(4):
    508             system("cls");
    509             goto caidan1;
    510         default:
    511             cout << "输入错误,请重新输入!";
    512             goto caidan3;
    513         }
    514     }
    515     case(3):
    516         break;
    517     default:
    518         cout << "输入错误,请重新输入!";
    519         goto caidan1;
    520     }
    521 }
    Supermarket.cpp
    复制代码
  3. box.h
    复制代码
     1 #include<string>
     2 #include<vector>
     3 using namespace std;
     4 
     5 void add_store();
     6 void huansong();
     7 void huanying();
     8 void set();
     9 void get();
    10 
    11 void delete_store();
    12 void hb();
    box.h
    复制代码
  4. box.cpp
    复制代码
      1 #include<iostream>
      2 #include <stdlib.h >
      3 #include <windows.h>
      4 #include<string>
      5 #include<vector>
      6 #include<fstream>
      7 #include"Supermarket.h"
      8 #include"Food.h"
      9 #include"box.h"
     10 using namespace std;
     11 
     12 extern vector<Supermarket> store;
     13 
     14 int search_store(string s) {
     15     int i;
     16     for (i = 0; i < store.size(); i++) {
     17         if (s == store[i].name) break;
     18     }
     19     if (i < store.size()) return i;
     20     else return 0;
     21 }
     22 void huansong() {
     23     cout << "----------***感谢您的使用!***----------" << endl;
     24 }
     25 
     26 void set() {
     27     Supermarket w("模板");
     28     store.push_back(w);
     29     ifstream ifs;
     30     ifs.open("商品.text", ios::in);
     31     int n=0,a=0,b=0,c=0;
     32     ifs >> n;
     33     for (int i = 1; i < n; i++) {
     34         string y;ifs >> y;
     35         Supermarket w(y);
     36         ifs >> a ;
     37         DailyGoods::count1 = a;
     38         for (int j = 1; j < a; j++)
     39         {
     40             string s; string d; int x;
     41             ifs >> s;
     42             ifs >>x;
     43             ifs >> d;
     44             DailyGoods a(s, x,d);
     45             w.goods1.push_back(a);
     46         }
     47         ifs >>b;
     48         Food::count2 = b;
     49         for (int j = 1; j < b; j++)
     50         {
     51             string s; string d;  int x, h;
     52             ifs >>s;
     53             ifs >>x;
     54             ifs>>h;
     55             ifs >> d;
     56             Food a(s, x, h,d);
     57             w.goods2.push_back(a);
     58         }
     59         ifs >> c;
     60         ElectricalAppliance::count3 = c;
     61         for (int j = 1; j < c; j++)
     62         {
     63             string s, l;
     64             string d; 
     65             int x;
     66             ifs >>s;
     67             ifs >>x;
     68             ifs >>l;
     69             ifs >> d;
     70             ElectricalAppliance a(s, x, l,d);
     71             w.goods3.push_back(a);
     72         }
     73         store.push_back(w);
     74     }
     75     ifs.close();
     76 }
     77 
     78 void get() {
     79     ofstream ofs;
     80     ofs.open("商品.text", ios::out);
     81     ofs << store.size() << endl;
     82     for (int i = 1; i < store.size(); i++) {
     83         ofs << store[i].name << endl;
     84         ofs << store[i].goods1.size() << endl;
     85         for (int j = 1; j < store[i].goods1.size(); j++)
     86         {
     87             ofs << store[i].goods1[j].name<<endl;
     88             ofs << store[i].goods1[j].quantity << endl;
     89             ofs << store[i].goods1[j].date<< endl;
     90         }
     91         ofs << store[i].goods2.size() << endl;
     92         for (int j = 1; j < store[i].goods2.size(); j++)
     93         {
     94             ofs << store[i].goods2[j].name << endl;
     95             ofs << store[i].goods2[j].quantity << endl;
     96             ofs << store[i].goods2[j].shelfLife << endl;
     97             ofs << store[i].goods2[j].date << endl;
     98         }
     99         ofs << store[i].goods3.size() << endl;
    100         for (int j = 1; j < store[i].goods3.size(); j++)
    101         {
    102             ofs << store[i].goods3[j].name << endl;
    103             ofs << store[i].goods3[j].quantity << endl;
    104             ofs << store[i].goods3[j].color << endl;
    105             ofs << store[i].goods3[j].date << endl;
    106         }
    107     }
    108     ofs.close();
    109 }
    110 
    111 void add_store() {
    112     char m;
    113     cout << "----------***欢迎创建您的超市***-----------" << endl;
    114     cout << "请输入您的新超市名称:" << '\t';
    115     string s; 
    116     cin >> s; m=getchar();
    117     Supermarket a(s);
    118     store.push_back(a);
    119     cout << "恭喜您拥有自己的第" <<store.size()-1<<"家超市!" << endl;
    120     long time;
    121     {
    122         for (time = 0; time < 3; time++)
    123             system("color 0F");
    124         Sleep(50);
    125         for (time = 0; time < 3; time++)
    126             system("color 0A");
    127         Sleep(50);
    128         for (time = 0; time < 3; time++)
    129             system("color 0B");
    130         Sleep(50);
    131         for (time = 0; time < 3; time++)
    132             system("color 0C");
    133         Sleep(50);
    134         for (time = 0; time < 3; time++)
    135             system("color 0D");
    136         Sleep(50);
    137         for (time = 0; time < 3; time++)
    138             system("color 0E");
    139         Sleep(50);
    140         for (time = 0; time < 3; time++)
    141             system("color 0D");
    142         Sleep(50);
    143         for (time = 0; time < 3; time++)
    144             system("color 0B");
    145         Sleep(50);
    146         for (time = 0; time < 3; time++)
    147             system("color 0F");
    148         Sleep(50);
    149     }
    150 }
    151 
    152 void delete_store() {
    153     char m;
    154     cout << "请输入您要解散的超市名称:" << endl;
    155     string s;
    156     cin >> s; m = getchar();
    157 
    158     for (auto it = store.begin(); it != store.end();)
    159     {
    160         if (s == it->name) {
    161             it = store.erase(it);
    162         }
    163         else {
    164             ++it;
    165         }
    166     }
    167     cout << "------***解散成功!***-------" << endl;
    168 }
    169 
    170 void hb() {
    171     int flag = 1; char m;
    172     int x, y;
    173     //输入被解散的超市a
    174     while (flag) {
    175         cout << "您想要取消的超市是:";
    176         string a;
    177         cin >> a; m = getchar();
    178         x = search_store(a);
    179         if (x == 0) {
    180             cout << "您输入的超市不存在,请重新输入!" << endl;
    181             flag++;
    182             if (flag == 3) {
    183                 cout << "您已输入错误三次!" << endl;
    184                 cout << "若您选择返回上一界面,请按“1”,若选择继续输入,请按此外任意键:" << endl;
    185                 int x;
    186                 cin >> x; m = getchar();
    187                 if (x == 1) exit(6);
    188                 else flag = 1;
    189             }
    190         }
    191         else flag = 0;
    192     }
    193     flag = 1;
    194     //输入被加入的超市b
    195     while (flag) {
    196         cout << "您想要将前者合并到超市:";
    197         string b;
    198         cin >> b; m = getchar();
    199         y = search_store(b);
    200         if (y == 0) {
    201             cout << "您输入的超市不存在,请重新输入!" << endl;
    202             flag++;
    203             if (flag == 3) {
    204                 cout << "您已输入错误三次!" << endl;
    205                 cout << "若您选择返回上一界面,请按“1”,若选择继续输入,请按此外任意键:" << endl;
    206                 int x;
    207                 cin >> x; m = getchar();
    208                 if (x == 1) exit(6);
    209                 else flag = 1;
    210             }
    211         }
    212         else flag = 0;
    213     }
    214     //a有b也有的商品,只加数量
    215     for (int j = 1; j < store[x].goods1.size(); j++) {
    216         int g = store[y].search01(store[x].goods1[j].name);
    217         if (g != 0) {
    218             store[y].goods1[g].quantity += store[x].goods1[j].quantity;
    219             auto it = store[x].goods1.begin();
    220             int w = 0;
    221             while (w != g) {
    222                 it++;
    223                 w++;
    224             }
    225             store[x].goods1.erase(it);
    226         }
    227     }
    228     for (int j = 1; j < store[x].goods2.size(); j++) {
    229         int g = store[y].search02(store[x].goods2[j].name);
    230         if (g != 0) {
    231             store[y].goods2[g].quantity += store[x].goods2[j].quantity;
    232             auto it = store[x].goods2.begin();
    233             int w = 0;
    234             while (w != g) {
    235                 it++;
    236                 w++;
    237             }
    238             store[x].goods2.erase(it);
    239         }
    240     }
    241     for (int j = 1; j < store[x].goods3.size(); j++) {
    242         int g = store[y].search03(store[x].goods3[j].name);
    243         if (g != 0) {
    244             store[y].goods3[g].quantity += store[x].goods3[j].quantity;
    245             auto it = store[x].goods3.begin();
    246             int w = 0;
    247             while (w != g) {
    248                 it++;
    249                 w++;
    250             }
    251             store[x].goods3.erase(it);
    252         }
    253     }
    254     //a有b没有的整个加入
    255     for (int j = store[x].goods1.size() - 1; j > 0; j--) {
    256         store[y].goods1.push_back(store[x].goods1[j]);
    257         store[x].goods1.pop_back();
    258     }
    259     for (int j = store[x].goods2.size() - 1; j >0; j--) {
    260         store[y].goods2.push_back(store[x].goods2[j]);
    261         store[x].goods2.pop_back();
    262     }
    263     for (int j = store[x].goods3.size() - 1; j >0; j--) {
    264         store[y].goods3.push_back(store[x].goods3[j]);
    265         store[x].goods3.pop_back();
    266     }
    267     auto it = store.begin();
    268     int w = 0;
    269     while (w != x) {
    270         w++;
    271         it++;
    272     }
    273     store.erase(it);
    274     cout << "-----***合并成功!***-----" << endl;
    275 }
    276 
    277 void huanying() {
    278     char m;
    279     if (store.size() ==1) {
    280         cout << "----------***欢迎来到超市经营系统***----------" << endl;
    281         cout << "请先创建您的第一个超市!" << endl;
    282         cout << "请输入您的超市名称:" << '\t';
    283         string s;
    284         cin >> s; m=getchar();
    285         Supermarket w(s);
    286         store.push_back(w);
    287         cout << "---------*创建成功*----------!" << endl;
    288         cout << "*******恭喜您拥有自己的第一个超市!*******" << endl;
    289         long time;
    290         {
    291             for(time=0;time<3;time++)
    292                 system("color A8");
    293             Sleep(50);
    294             for (time = 0; time < 3; time++)
    295                 system("color B8");
    296             Sleep(50);
    297             for (time = 0; time < 3; time++)
    298                 system("color C8");
    299             Sleep(50);
    300             for (time = 0; time < 3; time++)
    301                 system("color D8");
    302             Sleep(50);
    303             for (time = 0; time < 3; time++)
    304                 system("color E8");
    305             Sleep(50);
    306             for (time = 0; time < 3; time++)
    307                 system("color D8");
    308             Sleep(50);
    309             for (time = 0; time < 3; time++)
    310                 system("color E8");
    311             Sleep(50);
    312             for (time = 0; time < 3; time++)
    313                 system("color F8");
    314             Sleep(50);
    315             for (time = 0; time < 3; time++)
    316                 system("color 0F");
    317             Sleep(50);
    318         }
    319         system("pause");
    320         system("cls");
    321         
    322         goto zhucaidan;
    323     }
    324     else {
    325     zhucaidan:
    326         cout << "----------***欢迎来到超市经营系统***----------" << endl;
    327         cout << "请选择按需求选择您的操作:" << endl;
    328         cout << "1.管理物资" << endl;
    329         cout << "2.增加新店" << endl;
    330         cout << "3.解散店铺" << endl;
    331         cout << "4.合并店铺" << endl;
    332         cout << "5.查看已有店铺" << endl;
    333         cout << "6.退出系统" << endl;
    334         cout << "请输入:" << endl;
    335         int x;
    336         cin >> x; m=getchar();
    337         switch (x) {
    338         case(1):
    339         {
    340             cout << "请输入您要管理的超市名称:" << endl;
    341             string s;
    342             cin >> s; m=getchar();
    343             int i = search_store(s);
    344             if (i == 0) {
    345                 cout << "该商店不存在!" << endl;
    346                 system("pause");
    347                 system("cls");
    348                 goto zhucaidan;
    349             }
    350             else {
    351                 caidan1:
    352                 cout << "请选择按需求选择您的操作:" << endl;
    353                 cout << "1.增加商品/商品数量" << endl;
    354                 cout << "2.查询商品" << endl;
    355                 cout << "3.卖出商品" << endl;
    356                 cout << "4.返回上一界面" << endl;
    357                 cout << "请输入:" << endl;
    358                 cin >> x;m=getchar();
    359                 system("cls"); 
    360                 switch (x) {
    361                 case(1):
    362                 {
    363                     store[i].Add();
    364                     cout << "如果您想继续在运行该超市,请按“1”,否则请按此外任意按键:" << '\t';
    365                     int y = 0;
    366                     cin >> y; m=getchar();
    367                     if (y == 1) {
    368                         system("cls");
    369                         goto caidan1;
    370                     }
    371                     else goto zhucaidan;
    372                 }
    373                 case(2):
    374                 {
    375                     store[i].Search();
    376                     cout << "如果您想继续在运行该超市,请按“1”,否则请按此外任意按键:" << '\t';
    377                     int y = 0;
    378                     cin >> y; m=getchar();
    379                     if (y == 1) {
    380                         system("cls");
    381                         goto caidan1;
    382                     }
    383                     else goto zhucaidan;
    384                 }
    385                 case(3):
    386                 {
    387                     store[i].Sale();
    388                     cout << "如果您想继续在运行该超市,请按“1”,否则请按此外任意按键:" << '\t';
    389                     int y = 0;
    390                     cin >> y; m=getchar();
    391                     if (y == 1) {
    392                         goto caidan1;
    393                         system("cls");
    394                     }
    395                     else goto zhucaidan;
    396                 }
    397                 case(4):
    398                     goto zhucaidan;
    399                 default:
    400                     cout << "输入错误,请重新输入!" << endl;
    401                     goto caidan1;
    402                 }
    403             }
    404             }
    405         case(2):
    406             add_store();
    407             system("pause");
    408             system("cls");
    409             goto zhucaidan;
    410         case(3):
    411             delete_store();
    412             system("pause");
    413             system("cls");
    414             goto zhucaidan;
    415         case(4):
    416             hb();
    417             system("pause");
    418             system("cls");
    419             goto zhucaidan;
    420         case(5):
    421             cout << "----------***欢迎查看您的资产***----------" << endl;
    422             for (int i = 1; i < store.size(); i++) {
    423                 cout <<""<<i<<"家超市名称为" << store[i].get_name() << endl;
    424             }
    425             system("pause");
    426             system("cls");
    427             goto zhucaidan;
    428         case(6):
    429             break;
    430         default:
    431             cout << "输入错误,请重新输入!" << endl;
    432             goto zhucaidan;
    433         }
    434     }
    435 }
    box.cpp
    复制代码
  5. DailyGoods.h
    复制代码
     1 #pragma once
     2 #include"Goods.h"
     3 #include<string>
     4 #include<iostream>
     5 using namespace std;
     6 
     7 // 派生类:日常用品类
     8 class DailyGoods : public Goods {
     9 public:
    10     static int count1;     //总商品样数
    11     DailyGoods(const string& n="", int q=0,const string& d="") : Goods(n, q,d) {}
    12     void ShowMe();
    13     friend  istream& operator >>(istream& is, DailyGoods& dailygoods);  // 重载输入操作符,用于直接读入商品信息
    14     friend ostream& operator<<(ostream& output, DailyGoods& dailygoods);
    15     
    16 };
    17 istream& operator >>(istream& is, DailyGoods& dailygoods);
    18 ostream& operator<<(ostream& output, DailyGoods& dailygoods);
    DailyGoods.h
    复制代码
  6. DailyGoods.cpp
    复制代码
     1 #include"DailyGoods.h"
     2 #include<iostream>
     3 #include<vector>
     4 using namespace std;
     5 
     6 
     7 void DailyGoods::ShowMe() {
     8     cout << "商品名称:" << name << endl;
     9     cout << "商品现有数量:" << quantity << endl;
    10     cout << "商品生产日期:" << date << endl;
    11 }
    12 istream& operator >>(istream& is, DailyGoods& dailygoods) {
    13     char m;
    14     cout << "请输入商品名称: " << endl;
    15     is >> dailygoods.name; m=getchar();
    16     cout << "请输入商品数量: " << endl;
    17     is >> dailygoods.quantity; m=getchar();
    18     cout << "请输入商品生产日期: " << endl;
    19     is >> dailygoods.date; m = getchar();
    20     return is;
    21 }
    22 ostream& operator<<(ostream&output, DailyGoods& dailygoods) {
    23     output << "商品名称:" << dailygoods.name << endl;
    24     output << "商品现有数量:" << dailygoods.quantity << endl;
    25     output << "商品生产日期:" << dailygoods.date << endl;
    26     return output;
    27 }
    DailyGoods.cpp
    复制代码
  7. ElectricalAppliance.h
    复制代码
     1 #include"ElectricalAppliance.h"
     2 #include<iostream>
     3 #include<vector>
     4 using namespace std;
     5 
     6 void ElectricalAppliance::ShowMe(){
     7     cout << "商品名称:" << name << endl;
     8     cout << "商品现有数量:" << quantity << endl;
     9     cout << "颜色:" << color << endl;
    10     cout << "商品生产日期:" << date << endl;
    11 }
    12 istream& operator >>(istream& is, ElectricalAppliance& appliance) {
    13     char m;
    14     cout << "请输入商品名称: " << endl;
    15     is >> appliance.name; m=getchar();
    16     cout << "请输入商品数量: " << endl;
    17     is >> appliance.quantity; m=getchar();
    18     cout << "请输入商品颜色: " << endl;
    19     is >> appliance.color; m=getchar();
    20     cout << "请输入商品生产日期: " << endl;
    21     is >> appliance.date; m = getchar();
    22     return is;
    23 }
    24 ostream& operator<<(ostream& output, ElectricalAppliance& appliance) {
    25     output << "商品名称:" << appliance.name << endl;
    26     output << "商品现有数量:" << appliance.quantity << endl;
    27     output << "颜色:" << appliance.color << endl;
    28     output << "生产日期:" << appliance.date<< endl;
    29     return output;
    30 }
    ElectricalAppliance.h
    复制代码
  8. ElectricalAppliance.cpp
    复制代码
     1 #include"ElectricalAppliance.h"
     2 #include<iostream>
     3 #include<vector>
     4 using namespace std;
     5 
     6 void ElectricalAppliance::ShowMe(){
     7     cout << "商品名称:" << name << endl;
     8     cout << "商品现有数量:" << quantity << endl;
     9     cout << "颜色:" << color << endl;
    10     cout << "商品生产日期:" << date << endl;
    11 }
    12 istream& operator >>(istream& is, ElectricalAppliance& appliance) {
    13     char m;
    14     cout << "请输入商品名称: " << endl;
    15     is >> appliance.name; m=getchar();
    16     cout << "请输入商品数量: " << endl;
    17     is >> appliance.quantity; m=getchar();
    18     cout << "请输入商品颜色: " << endl;
    19     is >> appliance.color; m=getchar();
    20     cout << "请输入商品生产日期: " << endl;
    21     is >> appliance.date; m = getchar();
    22     return is;
    23 }
    24 ostream& operator<<(ostream& output, ElectricalAppliance& appliance) {
    25     output << "商品名称:" << appliance.name << endl;
    26     output << "商品现有数量:" << appliance.quantity << endl;
    27     output << "颜色:" << appliance.color << endl;
    28     output << "生产日期:" << appliance.date<< endl;
    29     return output;
    30 }
    ElectricalAppliance.cpp
    复制代码
  9. Food.h
    复制代码
     1 #pragma once
     2 #include"Goods.h"
     3 #include<iostream>
     4 #include<string>
     5 using namespace std;
     6 
     7 
     8 // 派生类:食品类
     9 class Food : public Goods {
    10 private:
    11     int shelfLife;   // 食品类商品保质期
    12 public:
    13     static int count2;     //总商品样数
    14     Food(const string& n = "", int q = 0, int sl = 0,const string& d="") : Goods(n, q, d) { shelfLife = sl; }
    15     void ShowMe()override;
    16     friend  istream& operator >>(istream& is, Food& foods);  // 重载输入操作符,用于直接读入商品信息
    17     friend ostream& operator<<(ostream& output, Food& food); //重载输出运算符,便于向显示器输出
    18     friend void get();
    19     friend void set();
    20 };
    Food.h
    复制代码
  10. Food.cpp
    复制代码
     1 #include<iostream>
     2 #include"Food.h"
     3 #include<vector>
     4 using namespace std;
     5 
     6 
     7 void Food::ShowMe(){
     8     cout << "商品名称:" << name << endl;
     9     cout << "商品现有数量:" << quantity << endl;
    10     cout << "保质期:" << shelfLife << "" << endl;
    11     cout << "商品生产日期:" << date << endl;
    12 }
    13 istream& operator >>(istream& is, Food& food) {
    14     char m;
    15     cout << "请输入商品名称: " << endl;
    16     is >> food.name; m=getchar();
    17     cout << "请输入商品数量: " << endl;
    18     is >> food.quantity;m=getchar();
    19     cout << "请输入商品保质期(单位/天): " << endl;
    20     is >> food.shelfLife;m=getchar();
    21     cout << "请输入商品生产日期: " << endl;
    22     is >> food.date; m = getchar();
    23     return is;
    24 }
    25 ostream& operator<<(ostream& output, Food& food) {
    26     output << "商品名称:" << food.name << endl;
    27     output << "商品现有数量:" << food.quantity << endl;
    28     output << "保质期:" << food.shelfLife << "" << endl;
    29     output << "商品生产日期:" << food.date << endl;
    30     return output;
    31 }
    Food.cpp
    复制代码
  11. Goods.h
    复制代码
     1 #pragma once
     2 #include<iostream>
     3 using namespace std;
     4 
     5 // 基类:商品类
     6 class Goods {
     7 public:
     8     int quantity;     // 商品现有数量
     9     string name;      // 商品名称
    10     string date;     //商品生产日期
    11     Goods(const string n, int q,string d) : name(n), quantity(q) ,date(d){}  //初始构造函数
    12     virtual void ShowMe() = 0;   // 纯虚函数,用于显示商品信息
    13     string getname(){return name;}
    14     friend  istream& operator >>(istream& is, Goods& goods);  // 重载输入操作符,用于直接读入商品信息
    15    // friend int Supermarket::search01(string s);
    16 };
    17 istream& operator >>(istream& is, Goods& goods);
    Goods.h
    复制代码
  12. Goods.cpp
    复制代码
     1 #include"Goods.h"
     2 #include<iostream>
     3 #include<string>
     4 #include "Food.h"
     5 #include"Supermarket.h"
     6 
     7 using namespace std;
     8 
     9 istream& operator >>(istream &is, Goods& goods){
    10     char m;
    11     cout << "请输入商品名称: " << endl;
    12     is >> goods.name; m=getchar();
    13     cout << "请输入商品数量: " << endl;
    14     is >> goods.quantity; m=getchar();
    15     cout << "请输入商品生产日期: " << endl;
    16     is >> goods.date; m = getchar();
    17     return is;
    18 }
    Goods.cpp
    复制代码

五、重构后的代码测试

首先是对于超市管理功能的增加,可见效果图如下:

新增了解散店铺、合并店铺和查看已有店铺,具体功能实现细节如下:

  1. 解散店铺

  1. 合并店铺

  1. 查看已有店铺

接下来是关于增加商品和查看商品是可以记录和查看生产日期,具体实现如下:

(1)增加商品

 (2)查看商品

 六、总结

本次对于超市购物系统的修改和优化主要是从实际应用的角度出发,但其实还有很多可以修改的地方,比如程序运行时界面的转换(清屏函数的使用)、商品名称相同保质期不同时对于查询的要求等,这些都是可以进一步修改的地方,本次精力有限,主要是为了容器练手,目前只做了以上一些改善。

这里还发现了一个小细节,开第一家超市时的特效和后续开店的特效效果是不一样的,算是一个小惊喜。

第一次写这样长的博客,从3.3开始到3.5结束,主要很久没有写代码了,还是手生了都,留个戳做纪念。☆*: .。. o(≧▽≦)o .。.:*☆

posted on   小桥流水人家026  阅读(56)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示