基于C++抽象封装集成多态开发的物品销售系统V1.0
前言
这个程序是我很久以前写的一个物品销售系统,主要用到抽象 封装 继承多态去做的。定义了一个父类,物品类
三个子类继承了父类,食物类 ,图书类和衣物类。父类是大多都是纯虚函数子类进行实现。
代码部分
class Commodity
{
public:
virtual float getPrice() = 0;
virtual void setCount(const int num) = 0;
virtual void setName(const std::string str) = 0;
virtual void setDescribe(const std::string str) = 0;
virtual void setId(const std::string str)=0;
virtual void setPrice(const float p) = 0;
virtual int getCount() = 0;
virtual std::string getId() = 0;
virtual std::string getName() = 0;
virtual std::string getDescribe() = 0;
virtual int getType() = 0;
protected:
float price;
std::string id;
std::string name;
std::string describe;
int count;
};
class Book :public Commodity
{
public:
Book(const std::string book_id, const std::string book_name, const std::string book_describe, const float book_price, const int book_count);
~Book();
float getPrice(){ return price; };
void setCount(const int num){ count = num; };
void setName(const std::string str){ name = str; };
void setDescribe(const std::string str){ describe = str; };
void setId(const std::string str){ id = str; };
void setPrice(const float p){ price = p; };
int getCount(){ return count; };
std::string getId(){ return id; };
std::string getName(){ return name; };
std::string getDescribe(){ return describe; };
int getType(){ return type; };
private:
int type;
};
Book::Book(const std::string book_id, const std::string book_name, const std::string book_describe, const float book_price, const int book_count)
{
id = book_id;
name = book_name;
describe = book_describe;
price = book_price;
count = book_count;
type = 1;
}
Book::~Book()
{
class Food:public Commodity
{
public:
Food(const std::string food_id, const std::string food_name, const std::string food_describe, const float food_price, const int food_count);
~Food();
float getPrice(){ return price; };
void setCount(const int num){ count = num; };
void setName(const std::string str){ name = str; };
void setDescribe(const std::string str){ describe = str; };
void setId(const std::string str){ id = str; };
void setPrice(const float p){ price = p; };
int getCount(){ return count; };
std::string getId(){ return id; };
std::string getName(){ return name; };
std::string getDescribe(){ return describe; };
int getType(){ return type; };
private:
int type;
};
Food::Food(std::string food_id, std::string food_name, std::string food_describe, float food_price, int food_count)
{
id = food_id;
name = food_name;
describe = food_describe;
price = food_price;
count = food_count;
type = 3;
}
Food::~Food()
{
}
class Clothes :public Commodity
{
public:
Clothes(const std::string clothes_id, const std::string clothes_name, const std::string clothes_describe, const float clothes_price, const int clothes_count);
~Clothes();
float getPrice(){ return price; };
void setCount(const int num){ count = num; };
void setName(const std::string str){ name = str; };
void setDescribe(const std::string str){ describe = str; };
void setId(const std::string str){ id = str; };
void setPrice(const float p){ price = p; };
int getCount(){ return count; };
std::string getId(){ return id; };
std::string getName(){ return name; };
std::string getDescribe(){ return describe; };
int getType(){ return type; };
private:
int type;
};
Clothes::Clothes(const std::string clothes_id, const std::string clothes_name, const std::string clothes_describe, const float clothes_price, const int clothes_count)
{
id = clothes_id;
name = clothes_name;
describe = clothes_describe;
price = clothes_price;
count = clothes_count;
type = 2;
}
Clothes::~Clothes()
{
}
class User
{
public:
virtual int getUserType()=0;
virtual void MainMenu() = 0;
virtual ~User(){};
protected:
int type;
std::string id;
std::string pwd;
float money;
};
class StoreUser :public User
{
public:
StoreUser(const std::string name = "", const std::string pwd = "", const int type = 1, const float money = 0);
~StoreUser();
int getUserType(){ return type; };
void MainMenu();
private:
void updatePwd(const std::string p);
void PrintfCommodity();
void PrintfMoney();
void InvsetMoney(const int money);
void SelectCommodity();
void DisplayMenu();
private:
bool isLogout;
};
StoreUser::StoreUser(const std::string n, const std::string p, const int t, const float m)
{
id = n;
pwd = p;
type = t;
money = m;
}
StoreUser::~StoreUser()
{
}
void StoreUser::DisplayMenu()
{
std::cout << "--------------------------------------------" << std::endl;
std::cout << " 1:浏览商品 2:查找商品 3:查看余额 4:余额充值" << std::endl;
std::cout << " 5:修改密码 C:清空屏幕 O:退出登录 " << std::endl;
std::cout << "--------------------------------------------" << std::endl;
}
void StoreUser::MainMenu()
{
char ch = 'A';
isLogout = false;
DisplayMenu();
while (!isLogout)
{
fflush(NULL);
std::cout << "请根据菜单提示选择需要进行的操作:";
std::cin >> ch;
switch (ch)
{
case '1':
PrintfCommodity();
break;
case '2':
SelectCommodity();
break;
case '3':
PrintfMoney();
break;
case 'C':
system("cls");
DisplayMenu();
break;
case 'O':
isLogout=true;
break;
case '4':
{
int i_money;
std::cout << "请输入充值的金额(>=1):";
std::cin >> i_money;
if (i_money <= 1)
{
std::cout << "金额有误" << std::endl;
break;
}
InvsetMoney(i_money);
}break;
case '5':
{
std::string pass;
std::cout << "请输入新密码:";
std::cin >> pass;
if (pass == "")
{
std::cout << "新密码有误" << std::endl;
break;
}
updatePwd(pass);
}break;
default:
break;
}
}
}
void StoreUser::PrintfMoney()
{
std::cout << "账户余额:" << money << std::endl;
}
void StoreUser::updatePwd(const std::string p)
{
if (CConfig::Instance()->UpdatePassword(id, p) == 1)
{
std::cout << "账户密码修改成功!" << std::endl;
}
else{ std::cout << "账户密码修改失败!" << std::endl; }
}
void StoreUser::InvsetMoney(const int money)
{
this->money = this->money + money;
if (CConfig::Instance()->UpdateMoney(id, this->money) == 1)
{
std::cout << "充值成功,充值金额:" << money << std::endl;
}
else
{
std::cout << "充值失败" << std::endl;
}
}
void StoreUser::SelectCommodity()
{
int chooies;
std::cout << "请选择搜索类型(1:商品名 2:商家编号 3:商品类型):";
std::cin >> chooies;
switch (chooies)
{
case 1:
{
std::string name;
std::cout << "请输入需要查找的商品名:";
std::cin >> name;
std::map<std::string, std::map<std::string, Commodity*>> map_shop = CConfig::Instance()->getShopMsgForName(name);
if (map_shop.size() <= 0)
{
std::cout << "搜索不到符合条件的商品!" << std::endl;
return;
}
std::cout << "商家编号" << std::setw(10) << "货品编号" << std::setw(14) << "货品名称" << std::setw(10) << "货品单价" << std::setw(10) << "货品总数" << std::setw(10) << "是否打折" << std::setw(10) << "折后价格" << std::setw(16) << "货品介绍" << std::endl;
std::map<std::string, std::map<int, float>> map_discount = CConfig::Instance()->getDisCount();
std::map<std::string, std::map<std::string, Commodity*>>::iterator itor = map_shop.begin();
std::cout << std::setprecision(6);
while (itor != map_shop.end())
{
std::map<std::string, Commodity*>::iterator itor1 = itor->second.begin();
if (map_discount.find(itor->first) == map_discount.end())
{
while (itor1 != itor->second.end())
{
std::cout << itor->first << std::setw(10) << itor1->second->getId() << std::setw(14) << itor1->second->getName() << std::setw(10) << itor1->second->getPrice() << std::setw(10) << itor1->second->getCount() << std::setw(10) << "无折扣" << std::setw(10) << "\t\t" << itor1->second->getDescribe() << std::endl;
itor1++;
}
}
else
{
while (itor1 != itor->second.end())
{
if (map_discount[itor->first].find(itor1->second->getType()) == map_discount[itor->first].end())
{
std::cout << itor->first << std::setw(10) << itor1->second->getId() << std::setw(14) << itor1->second->getName() << std::setw(10) << itor1->second->getPrice() << std::setw(10) << itor1->second->getCount() << std::setw(10) << "无折扣" << std::setw(10) << "\t\t" << itor1->second->getDescribe() << std::endl;
}
else
{
std::cout << itor->first << std::setw(10) << itor1->second->getId() << std::setw(14) << itor1->second->getName() << std::setw(10) << itor1->second->getPrice() << std::setw(10) << itor1->second->getCount() << std::setw(8) << map_discount[itor->first][itor1->second->getType()] * 10 << "折" << std::setw(10) << map_discount[itor->first][itor1->second->getType()] * itor1->second->getPrice() << "\t\t" << itor1->second->getDescribe() << std::endl;
}
itor1++;
}
}
itor++;
}
}break;
case 2:
{
std::string user_id;
std::cout << "请输入需要查找的商家名:";
std::cin >> user_id;
std::map<std::string, Commodity*> map_shop = CConfig::Instance()->getShopMsgForComId(user_id);
if (map_shop.size() <= 0)
{
std::cout << "搜索不到符合条件的商品!" << std::endl;
return;
}
std::map<int, float> map_discount = CConfig::Instance()->getDisCount(user_id);
if (map_discount.size() <= 0)
{
std::cout << "货品编号" << std::setw(14) << "货品名称" << std::setw(14) << "货品单价" << std::setw(14) << "货品总数" << std::setw(14) << "货品介绍" << std::endl;
std::map<std::string, Commodity*>::iterator itor = map_shop.begin();
while (itor != map_shop.end())
{
std::cout << itor->first << std::setw(14) << itor->second->getName() << std::setw(14) << itor->second->getPrice() << std::setw(14) << itor->second->getCount() << "\t\t" << itor->second->getDescribe() << std::endl;
itor++;
}
}
else
{
std::cout << "货品编号" << std::setw(14) << "货品名称" << std::setw(10) << "货品单价" << std::setw(10) << "货品总数" << std::setw(10) << "是否打折" << std::setw(10) << "折后价格" << std::setw(16) << "货品介绍" << std::endl;
std::map<std::string, Commodity*>::iterator itor = map_shop.begin();
while (itor != map_shop.end())
{
if (map_discount.find(itor->second->getType()) == map_discount.end())
{
std::cout << itor->first << std::setw(14) << itor->second->getName() << std::setw(10) << itor->second->getPrice() << std::setw(10) << itor->second->getCount() << std::setw(10) << "无折扣" << std::setw(10) << "\t\t" << itor->second->getDescribe() << std::endl;
}
else
{
std::cout << itor->first << std::setw(14) << itor->second->getName() << std::setw(10) << itor->second->getPrice() << std::setw(10) << itor->second->getCount() << std::setw(8) << map_discount[itor->second->getType()] * 10 << "折" << std::setw(10) << map_discount[itor->second->getType()] * itor->second->getPrice() << "\t\t" << itor->second->getDescribe() << std::endl;
}
itor++;
}
}
}break;
case 3:
{
std::string stype;
std::cout << "请输入需要查找的商品类型(图书、衣物、食物):";
std::cin >> stype;
int type = 1;
if (stype == "图书")
{
type = 1;
}
else if (stype == "图书")
{
type = 2;
}
else if (stype == "图书")
{
type = 3;
}
else
{
std::cout << "选择的商品类型有误" << std::endl;
break;
}
std::map<std::string, std::map<std::string, Commodity*>> map_shop = CConfig::Instance()->getShopMsgForType(type);
if (map_shop.size() <= 0)
{
std::cout << "搜索不到符合条件的商品!" << std::endl;
return;
}
std::cout << "商家编号" << std::setw(10) << "货品编号" << std::setw(14) << "货品名称" << std::setw(10) << "货品单价" << std::setw(10) << "货品总数" << std::setw(10) << "是否打折" << std::setw(10) << "折后价格" << std::setw(16) << "货品介绍" << std::endl;
std::map<std::string, std::map<int, float>> map_discount = CConfig::Instance()->getDisCount();
std::map<std::string, std::map<std::string, Commodity*>>::iterator itor = map_shop.begin();
std::cout << std::setprecision(6);
while (itor != map_shop.end())
{
std::map<std::string, Commodity*>::iterator itor1 = itor->second.begin();
if (map_discount.find(itor->first) == map_discount.end())
{
while (itor1 != itor->second.end())
{
std::cout << itor->first << std::setw(10) << itor1->second->getId() << std::setw(14) << itor1->second->getName() << std::setw(10) << itor1->second->getPrice() << std::setw(10) << itor1->second->getCount() << std::setw(10) << "无折扣" << std::setw(10) << "\t\t" << itor1->second->getDescribe() << std::endl;
itor1++;
}
}
else
{
while (itor1 != itor->second.end())
{
if (map_discount[itor->first].find(itor1->second->getType()) == map_discount[itor->first].end())
{
std::cout << itor->first << std::setw(10) << itor1->second->getId() << std::setw(14) << itor1->second->getName() << std::setw(10) << itor1->second->getPrice() << std::setw(10) << itor1->second->getCount() << std::setw(10) << "无折扣" << std::setw(10) << "\t\t" << itor1->second->getDescribe() << std::endl;
}
else
{
std::cout << itor->first << std::setw(10) << itor1->second->getId() << std::setw(14) << itor1->second->getName() << std::setw(10) << itor1->second->getPrice() << std::setw(10) << itor1->second->getCount() << std::setw(8) << map_discount[itor->first][itor1->second->getType()] * 10 << "折" << std::setw(10) << map_discount[itor->first][itor1->second->getType()] * itor1->second->getPrice() << "\t\t" << itor1->second->getDescribe() << std::endl;
}
itor1++;
}
}
itor++;
}
}break;
default:
std::cout << "筛选条件有误!" << std::endl;
break;
}
}
void StoreUser::PrintfCommodity()
{
std::map<std::string, std::map<std::string, Commodity*>> map_shop = CConfig::Instance()->getShopMsg();
std::cout << "商家编号" << std::setw(10) << "货品编号" << std::setw(14) << "货品名称" << std::setw(10) << "货品单价" << std::setw(10) << "货品总数" << std::setw(10) << "是否打折" << std::setw(10) << "折后价格" << std::setw(16) << "货品介绍" << std::endl;
std::map<std::string, std::map<int, float>> map_discount = CConfig::Instance()->getDisCount();
std::map<std::string, std::map<std::string, Commodity*>>::iterator itor = map_shop.begin();
std::cout << std::setprecision(6);
while (itor != map_shop.end())
{
std::map<std::string, Commodity*>::iterator itor1 = itor->second.begin();
if (map_discount.find(itor->first) == map_discount.end())
{
while (itor1 != itor->second.end())
{
std::cout << itor->first << std::setw(10) << itor1->second->getId() << std::setw(14) << itor1->second->getName() << std::setw(10) << itor1->second->getPrice() << std::setw(10) << itor1->second->getCount() << std::setw(10) << "无折扣" << std::setw(10) << "\t\t" << itor1->second->getDescribe() << std::endl;
itor1++;
}
}
else
{
while (itor1 != itor->second.end())
{
if (map_discount[itor->first].find(itor1->second->getType()) == map_discount[itor->first].end())
{
std::cout << itor->first << std::setw(10) << itor1->second->getId() << std::setw(14) << itor1->second->getName() << std::setw(10) << itor1->second->getPrice() << std::setw(10) << itor1->second->getCount() << std::setw(10) << "无折扣" << std::setw(10) << "\t\t" << itor1->second->getDescribe() << std::endl;
}
else
{
std::cout << itor->first << std::setw(10) << itor1->second->getId() << std::setw(14) << itor1->second->getName() << std::setw(10) << itor1->second->getPrice() << std::setw(10) << itor1->second->getCount() << std::setw(8) << map_discount[itor->first][itor1->second->getType()] * 10 << "折" << std::setw(10) << map_discount[itor->first][itor1->second->getType()] * itor1->second->getPrice() << "\t\t" << itor1->second->getDescribe() << std::endl;
}
itor1++;
}
}
itor++;
}
}
class ConsumerUser:public User
{
public:
ConsumerUser(const std::string name = "", const std::string pwd = "", const int type = 2, const float money = 0);
~ConsumerUser();
int getUserType(){ return type; };
void MainMenu();
private:
void updatePwd(const std::string p);
int insertCommodity();
void PrintfCommodity();
void PrintfMoney();
void InvsetMoney(const int money);
void SelectCommodity();
void DisplayMenu();
void UpdateCommodity();
private:
bool isLogout;
};
ConsumerUser::ConsumerUser(const std::string n, const std::string p, const int t, const float m)
{
id = n;
pwd = p;
type = t;
money = m;
}
void ConsumerUser::DisplayMenu()
{
std::cout << "--------------------------------------------" << std::endl;
std::cout << " 1:上架商品 2:浏览商品 3:查找商品 4:修改商品" << std::endl;
std::cout << " 5:查看余额 6:余额充值 7:修改密码 8:查看折扣" << std::endl;
std::cout << " 9:删除折扣 I:设置折扣 D:下架商品 C:清空屏幕" << std::endl;
std::cout << " O:退出登录 " << std::endl;
std::cout << "--------------------------------------------" << std::endl;
}
ConsumerUser::~ConsumerUser()
{
std::cout << "test" << std::endl;
}
void ConsumerUser::MainMenu()
{
char ch='A';
isLogout = false;
DisplayMenu();
while (!isLogout)
{
fflush(NULL);
std::cout << "请根据菜单提示选择需要进行的操作:";
std::cin>>ch;
switch (ch)
{
case '1':
insertCommodity();
break;
case '2':
PrintfCommodity();
break;
case '3':
SelectCommodity();
break;
case '4':
UpdateCommodity();
break;
case '5':
PrintfMoney();
break;
case '6':
{
int i_money;
std::cout << "请输入充值的金额(>=1):";
std::cin >> i_money;
if (i_money <= 1)
{
std::cout << "金额有误" << std::endl;
break;
}
InvsetMoney(i_money);
}break;
case '7':
{
std::string pass;
std::cout << "请输入新密码:";
std::cin >> pass;
if (pass == "")
{
std::cout << "新密码有误" << std::endl;
break;
}
updatePwd(pass);
}break;
case '8':
{
std::map<int, float> map_discount = CConfig::Instance()->getDisCount(this->id);
if (map_discount.size() <= 0)
{
std::cout << "无折扣信息" << std::endl;
}
else
{
std::map<int, float>::iterator itor = map_discount.begin();
while (itor != map_discount.end())
{
switch (itor->first)
{
case 1:
std::cout << "图书:"; break;
case 2:
std::cout << "衣物:"; break;
case 3:
std::cout << "食物:"; break;
default:
break;
}
std::cout << std::setprecision(3);
std::cout << itor->second * 10 << "折" << std::endl;
itor++;
}
}
}break;
case '9':
{
std::string strdis;
std::cout << "请输入需要删除折扣的商品类别:";
std::cin >> strdis;
int type = 1;
if (strdis == "图书")
{
type = 1;
}
else if (strdis == "图书")
{
type = 2;
}
else if (strdis == "图书")
{
type = 3;
}
else
{
std::cout << "选择的商品类型有误" << std::endl;
break;
}
if (CConfig::Instance()->deleteDiscount(this->id, type) == 1)
{
std::cout << "删除成功" << std::endl;
}
else
{
std::cout << "删除失败" << std::endl;
}
}break;
case'I':
{
std::string strdis;
std::cout << "请输入需要打折的商品类别:";
std::cin >> strdis;
int type = 1;
if (strdis == "图书")
{
type = 1;
}
else if (strdis == "图书")
{
type = 2;
}
else if (strdis == "图书")
{
type = 3;
}
else
{
std::cout << "选择的商品类型有误" << std::endl;
break;
}
std::cout << "请输入折扣(0~10):";
float dis;
std::cin >> dis;
if (dis >= 10 || dis <= 0)
{
std::cout << "折扣数有误" << std::endl;
break;
}
if (CConfig::Instance()->setDisCount(this->id, type, dis) == 1)
{
std::cout << "设置成功" << std::endl;
}
else
{
std::cout << "设置失败" << std::endl;
}
}
case 'D':
{
std::string strid;
std::cout << "请输入需要下架的商品编号:";
std::cin >> strid;
if (CConfig::Instance()->DeleteCommodity(this->id, strid) == 1)
{
std::cout << "下架成功" << std::endl;
}
else
{
std::cout << "下架失败" << std::endl;
}
}break;
case 'C':
system("cls");
DisplayMenu();
break;
case 'O':
isLogout = true;
default:
break;
}
}
}
int ConsumerUser::insertCommodity()
{
int commoditytype;
std::string id, name, describe;
int count;
float price;
std::cout << "请选择新上架的货物种类(1-图书,2-衣物,3-食物):";
scanf("%d", &commoditytype);
switch (commoditytype)
{
case 1:
{
std::cout << "请依次输入新上架的图书编号、名称、介绍、总数、单价:";
std::cin >> id >> name >> describe >> count >> price;
if (CConfig::Instance()->FindCommodity(this->id,id))
{
std::cout << "当前编号货物已存在您的货架!" << std::endl;
return 0;
}
if (count <= 0)
{
std::cout << "货物总数不可为小于0的数值" << std::endl;
return 0;
}
if (price <= 0)
{
std::cout << "货物单价不可为小于0的数值" << std::endl;
return 0;
}
if (CConfig::Instance()->AddNewCommodity(this->id, id, name, price, count, describe,1) == 1)
{
std::cout << "上架成功!" << std::endl;
}
else
{
std::cout << "上架失败!" << std::endl;
}
}
break;
case 2:
{
std::cout << "请依次输入新上架的衣物编号、名称、介绍、总数、单价:";
std::cin >> id >> name >> describe >> count >> price;
if (CConfig::Instance()->FindCommodity(this->id, id))
{
std::cout << "当前编号货物已存在您的货架!" << std::endl;
return 0;
}
if (count <= 0)
{
std::cout << "货物总数不可为小于0的数值" << std::endl;
return 0;
}
if (price <= 0)
{
std::cout << "货物单价不可为小于0的数值" << std::endl;
return 0;
}
if (CConfig::Instance()->AddNewCommodity(this->id, id, name, price, count, describe,2) == 1)
{
std::cout << "上架成功!" << std::endl;
}
else
{
std::cout << "上架失败!" << std::endl;
}
}
break;
case 3:
{
std::cout << "请依次输入新上架的食物编号、名称、介绍、总数、单价:";
std::cin >> id >> name >> describe >> count >> price;
if (CConfig::Instance()->FindCommodity(this->id, id))
{
std::cout << "当前编号货物已存在您的货架!" << std::endl;
return 0;
}
if (count <= 0)
{
std::cout << "货物总数不可为小于0的数值" << std::endl;
return 0;
}
if (price <= 0)
{
std::cout << "货物单价不可为小于0的数值" << std::endl;
return 0;
}
if (CConfig::Instance()->AddNewCommodity(this->id, id, name, price, count, describe,3) == 1)
{
std::cout << "上架成功!" << std::endl;
}
else
{
std::cout << "上架失败!" << std::endl;
}
}
break;
default:
{
std::cout << "没有可上架的货物种类!" << std::endl;
return 0;
}
}
return 1;
}
void ConsumerUser::PrintfCommodity()
{
std::map<std::string, std::map<std::string, Commodity*>> map_shop = CConfig::Instance()->getShopMsg();
std::cout << "商家编号" << std::setw(10) << "货品编号" << std::setw(14) << "货品名称" << std::setw(10) << "货品单价" << std::setw(10) << "货品总数" << std::setw(10) << "是否打折" << std::setw(10) << "折后价格" << std::setw(16) << "货品介绍" << std::endl;
std::map<std::string, std::map<int, float>> map_discount = CConfig::Instance()->getDisCount();
std::map<std::string, std::map<std::string, Commodity*>>::iterator itor = map_shop.begin();
std::cout << std::setprecision(6);
while (itor != map_shop.end())
{
std::map<std::string, Commodity*>::iterator itor1 = itor->second.begin();
if (map_discount.find(itor->first) == map_discount.end())
{
while (itor1 != itor->second.end())
{
std::cout << itor->first << std::setw(10) << itor1->second->getId() << std::setw(14) << itor1->second->getName() << std::setw(10) << itor1->second->getPrice() << std::setw(10) << itor1->second->getCount() << std::setw(10) << "无折扣" << std::setw(10) << "\t\t" << itor1->second->getDescribe() << std::endl;
itor1++;
}
}
else
{
while (itor1 != itor->second.end())
{
if (map_discount[itor->first].find(itor1->second->getType()) == map_discount[itor->first].end())
{
std::cout << itor->first << std::setw(10) << itor1->second->getId() << std::setw(14) << itor1->second->getName() << std::setw(10) << itor1->second->getPrice() << std::setw(10) << itor1->second->getCount() << std::setw(10) << "无折扣" << std::setw(10) << "\t\t" << itor1->second->getDescribe() << std::endl;
}
else
{
std::cout << itor->first << std::setw(10) << itor1->second->getId() << std::setw(14) << itor1->second->getName() << std::setw(10) << itor1->second->getPrice() << std::setw(10) << itor1->second->getCount() << std::setw(8)<< map_discount[itor->first][itor1->second->getType()] * 10 << "折" << std::setw(10) << map_discount[itor->first][itor1->second->getType()] * itor1->second->getPrice() << "\t\t" << itor1->second->getDescribe() << std::endl;
}
itor1++;
}
}
itor++;
}
}
void ConsumerUser::PrintfMoney()
{
std::cout << "账户余额:" << money << std::endl;
}
void ConsumerUser::updatePwd(const std::string p)
{
if (CConfig::Instance()->UpdatePassword(id, p) == 1)
{
std::cout << "账户密码修改成功!"<< std::endl;
}
else{ std::cout << "账户密码修改失败!" << std::endl; }
}
void ConsumerUser::InvsetMoney(const int money)
{
this->money = this->money + money;
if (CConfig::Instance()->UpdateMoney(id, this->money) == 1)
{
std::cout << "充值成功,充值金额:" << money << std::endl;
}
else
{
std::cout << "充值失败"<< std::endl;
}
}
void ConsumerUser::SelectCommodity()
{
int chooies;
std::cout << "请选择搜索类型(1:商品名 2:商家编号 3:商品类型):";
std::cin >> chooies;
switch (chooies)
{
case 1:
{
std::string name;
std::cout << "请输入需要查找的商品名:";
std::cin >> name;
std::map<std::string, std::map<std::string, Commodity*>> map_shop = CConfig::Instance()->getShopMsgForName(name);
if (map_shop.size() <= 0)
{
std::cout << "搜索不到符合条件的商品!" << std::endl;
return;
}
std::cout << "商家编号" << std::setw(10) << "货品编号" << std::setw(14) << "货品名称" << std::setw(10) << "货品单价" << std::setw(10) << "货品总数" << std::setw(10) << "是否打折" << std::setw(10) << "折后价格" << std::setw(16) << "货品介绍" << std::endl;
std::map<std::string, std::map<int, float>> map_discount = CConfig::Instance()->getDisCount();
std::map<std::string, std::map<std::string, Commodity*>>::iterator itor = map_shop.begin();
std::cout << std::setprecision(6);
while (itor != map_shop.end())
{
std::map<std::string, Commodity*>::iterator itor1 = itor->second.begin();
if (map_discount.find(itor->first) == map_discount.end())
{
while (itor1 != itor->second.end())
{
std::cout << itor->first << std::setw(10) << itor1->second->getId() << std::setw(14) << itor1->second->getName() << std::setw(10) << itor1->second->getPrice() << std::setw(10) << itor1->second->getCount() << std::setw(10) << "无折扣" << std::setw(10) << "\t\t" << itor1->second->getDescribe() << std::endl;
itor1++;
}
}
else
{
while (itor1 != itor->second.end())
{
if (map_discount[itor->first].find(itor1->second->getType()) == map_discount[itor->first].end())
{
std::cout << itor->first << std::setw(10) << itor1->second->getId() << std::setw(14) << itor1->second->getName() << std::setw(10) << itor1->second->getPrice() << std::setw(10) << itor1->second->getCount() << std::setw(10) << "无折扣" << std::setw(10) << "\t\t" << itor1->second->getDescribe() << std::endl;
}
else
{
std::cout << itor->first << std::setw(10) << itor1->second->getId() << std::setw(14) << itor1->second->getName() << std::setw(10) << itor1->second->getPrice() << std::setw(10) << itor1->second->getCount() << std::setw(8) << map_discount[itor->first][itor1->second->getType()] * 10 << "折" << std::setw(10) << map_discount[itor->first][itor1->second->getType()] * itor1->second->getPrice() << "\t\t" << itor1->second->getDescribe() << std::endl;
}
itor1++;
}
}
itor++;
}
}break;
case 2:
{
std::string user_id;
std::cout << "请输入需要查找的商家名:";
std::cin >> user_id;
std::map<std::string, Commodity*> map_shop = CConfig::Instance()->getShopMsgForComId(user_id);
if (map_shop.size() <= 0)
{
std::cout << "搜索不到符合条件的商品!" << std::endl;
return;
}
std::map<int, float> map_discount = CConfig::Instance()->getDisCount(user_id);
if (map_discount.size() <= 0)
{
std::cout << "货品编号" << std::setw(14) << "货品名称" << std::setw(14) << "货品单价" << std::setw(14) << "货品总数" << std::setw(14) << "货品介绍" << std::endl;
std::map<std::string, Commodity*>::iterator itor = map_shop.begin();
while (itor != map_shop.end())
{
std::cout << itor->first << std::setw(14) << itor->second->getName() << std::setw(14) << itor->second->getPrice() << std::setw(14) << itor->second->getCount() << "\t\t" << itor->second->getDescribe() << std::endl;
itor++;
}
}
else
{
std::cout << "货品编号" << std::setw(14) << "货品名称" << std::setw(10) << "货品单价" << std::setw(10) << "货品总数" << std::setw(10) << "是否打折" << std::setw(10) << "折后价格" << std::setw(16) << "货品介绍" << std::endl;
std::map<std::string, Commodity*>::iterator itor = map_shop.begin();
while (itor != map_shop.end())
{
if (map_discount.find(itor->second->getType()) == map_discount.end())
{
std::cout << itor->first << std::setw(14) << itor->second->getName() << std::setw(10) << itor->second->getPrice() << std::setw(10) << itor->second->getCount() << std::setw(10) << "无折扣" << std::setw(10)<<"\t\t" << itor->second->getDescribe() << std::endl;
}
else
{
std::cout << itor->first << std::setw(14) << itor->second->getName() << std::setw(10) << itor->second->getPrice() << std::setw(10) << itor->second->getCount() << std::setw(8) << map_discount[itor->second->getType()] * 10 << "折" << std::setw(10) << map_discount[itor->second->getType()]*itor->second->getPrice() << "\t\t" << itor->second->getDescribe() << std::endl;
}
itor++;
}
}
}break;
case 3:
{
std::string stype;
std::cout << "请输入需要查找的商品类型(图书、衣物、食物):";
std::cin >> stype;
int type = 1;
if (stype == "图书")
{
type = 1;
}
else if (stype == "图书")
{
type = 2;
}
else if (stype == "图书")
{
type = 3;
}
else
{
std::cout << "选择的商品类型有误" << std::endl;
break;
}
std::map<std::string, std::map<std::string, Commodity*>> map_shop = CConfig::Instance()->getShopMsgForType(type);
if (map_shop.size() <= 0)
{
std::cout << "搜索不到符合条件的商品!" << std::endl;
return;
}
std::cout << "商家编号" << std::setw(10) << "货品编号" << std::setw(14) << "货品名称" << std::setw(10) << "货品单价" << std::setw(10) << "货品总数" << std::setw(10) << "是否打折" << std::setw(10) << "折后价格" << std::setw(16) << "货品介绍" << std::endl;
std::map<std::string, std::map<int, float>> map_discount = CConfig::Instance()->getDisCount();
std::map<std::string, std::map<std::string, Commodity*>>::iterator itor = map_shop.begin();
std::cout << std::setprecision(6);
while (itor != map_shop.end())
{
std::map<std::string, Commodity*>::iterator itor1 = itor->second.begin();
if (map_discount.find(itor->first) == map_discount.end())
{
while (itor1 != itor->second.end())
{
std::cout << itor->first << std::setw(10) << itor1->second->getId() << std::setw(14) << itor1->second->getName() << std::setw(10) << itor1->second->getPrice() << std::setw(10) << itor1->second->getCount() << std::setw(10) << "无折扣" << std::setw(10) << "\t\t" << itor1->second->getDescribe() << std::endl;
itor1++;
}
}
else
{
while (itor1 != itor->second.end())
{
if (map_discount[itor->first].find(itor1->second->getType()) == map_discount[itor->first].end())
{
std::cout << itor->first << std::setw(10) << itor1->second->getId() << std::setw(14) << itor1->second->getName() << std::setw(10) << itor1->second->getPrice() << std::setw(10) << itor1->second->getCount() << std::setw(10) << "无折扣" << std::setw(10) << "\t\t" << itor1->second->getDescribe() << std::endl;
}
else
{
std::cout << itor->first << std::setw(10) << itor1->second->getId() << std::setw(14) << itor1->second->getName() << std::setw(10) << itor1->second->getPrice() << std::setw(10) << itor1->second->getCount() << std::setw(8) << map_discount[itor->first][itor1->second->getType()] * 10 << "折" << std::setw(10) << map_discount[itor->first][itor1->second->getType()] * itor1->second->getPrice() << "\t\t" << itor1->second->getDescribe() << std::endl;
}
itor1++;
}
}
itor++;
}
}break;
default:
std::cout << "筛选条件有误!" << std::endl;
break;
}
}
void ConsumerUser::UpdateCommodity()
{
int chooise;
std::cout << "请输入需要修改的商品属性(1:商品名 2:商品价格 3:商品总数 4:商品简介)";
std::cin >> chooise;
switch (chooise)
{
case 1:
{
std::string new_name;
std::string com_id;
std::cout << "请输入需要修改的商品编号";
std::cin >> com_id;
std::cout << "请输入新商品名:";
std::cin >> new_name;
if (CConfig::Instance()->UpdateCommodityName(id, com_id, new_name)==1)
{
std::cout << "修改成功" << std::endl;
}
else
{
std::cout << "修改失败" << std::endl;
}
}
break;
case 2:
{
float new_price;
std::string com_id;
std::cout << "请输入需要修改的商品编号";
std::cin >> com_id;
std::cout << "请输入新价格:";
std::cin >> new_price;
if (CConfig::Instance()->UpdateCommodityPrice(id, com_id, new_price) == 1)
{
std::cout << "修改成功" << std::endl;
}
else
{
std::cout << "修改失败" << std::endl;
}
}
break;
case 3:
{
int new_count;
std::string com_id;
std::cout << "请输入需要修改的商品编号";
std::cin >> com_id;
std::cout << "请输入新商品数量:";
std::cin >> new_count;
if (CConfig::Instance()->UpdateCommodityCount(id, com_id, new_count) == 1)
{
std::cout << "修改成功" << std::endl;
}
else
{
std::cout << "修改失败" << std::endl;
}
}
break;
case 4:
{
std::string new_dec;
std::string com_id;
std::cout << "请输入需要修改的商品编号";
std::cin >> com_id;
std::cout << "请输入新商品介绍:";
std::cin >> new_dec;
if (CConfig::Instance()->UpdateCommodityDec(id, com_id, new_dec) == 1)
{
std::cout << "修改成功" << std::endl;
}
else
{
std::cout << "修改失败" << std::endl;
}
}
break;
default:
break;
}
}
总结
本篇没有过多介绍关于程序的写法,主要是因为后续想出个网络版的,程序已经写好了我稍后放上连接
C++物品销售系统、多态、类、继承