/*
*程序的版权和版本声明部分:
*Copyright(c)2014,烟台大学计算机学院学生
*All rights reserved.
*文件名称:
*作者:田成琳
*完成日期:2014 年 5 月 18 日
*版本号:v1.0
*对任务及求解方法的描述部分:
*输入描述: -
*问题描述:商品信息管理系统
*程序输出:商品信息
*问题分析:
*算法设计:
*/
#include<iostream>
#include<cstdlib>
#include<iomanip>
#include<string>
using namespace std;
void menu(); //菜单
void findMenu(); //查商品界面
void showProduct(int);//显示商品信息
int size=0; //记录当前商品数量
class Product
{
public:
void addProduct(); //增加商品
void findProductName(string); //名称查找商品
void findProductNumber(string);//编号查找商品
void findProductKind(string);//品牌查找商品
void findProductKindNumber(string);//型号查找商品
void display(); //显示所有商品信息
string getName()
{
return name;
}
string getNumber()
{
return number;
}
string getKind()
{
return kind;
}
string getKindNumber()
{
return kindnumber;
}
double getPrice()
{
return price;
}
private:
string name,kind,number,kindnumber; //名称,品牌,编号,型号
double price; //价格
};
Product b[20];
int main()
{
int choose;
do
{
menu();
cin>>choose;
if(choose==1)
b[size].addProduct();
else if(choose==2)
findMenu();
else if(choose==3)
b[size].display();
else if(choose==0)
exit(0);
else
menu();
}
while(choose!=0);
return 0;
}
void Product::addProduct()
{
char yrn;
cout<<"商品编号:";
cin>>b[size].number;
cout<<"商品名称:";
cin>>b[size].name;
cout<<"品牌:";
cin>>b[size].kind;
cout<<"型号:";
cin>>b[size].kindnumber;
cout<<"价格:";
cin>>b[size].price;
cout<<"增加成功!"<<endl;
showProduct(size);
size+=1;
cout<<"是否继续添加?是(Y/y)否(N/n)";
cin>>yrn;
if(yrn=='y'||yrn=='Y')
Product::addProduct();
}
void findProductNumber(string Bnumber)
{
bool flag=false;
int i;
for(i=0; i<=size; ++i)
{
if(b[i].getNumber()==Bnumber)
{
flag=true;
break;
}
}
if(flag)
showProduct(i);
else
cout<<"查无此商品!";
findMenu();
}
void findProductName(string Bname)
{
bool flag=false;
int i;
for(i=0; i<=size; ++i)
{
if(b[i].getName()==Bname)
{
flag=true;
break;
}
}
if(flag)
showProduct(i);
else
cout<<"查无此商品!";
findMenu();
}
void findProductKind(string Bkind)
{
bool flag=false;
int i;
for(i=0; i<=size; ++i)
{
if(b[i].getKind()==Bkind)
{
flag=true;
break;
}
}
if(flag)
showProduct(i);
else
cout<<"查无此商品!";
findMenu();
}
void findProductKindnumber(string Bkindnumber)
{
bool flag=false;
int i;
for(i=0; i<=size; ++i)
{
if(b[i].getKindNumber()==Bkindnumber)
{
flag=true;
break;
}
}
if(flag)
showProduct(i);
else
cout<<"查无此商品!";
findMenu();
}
void Product::display()
{
for(int i=0; i<size; ++i)
showProduct(i);
findMenu();
}
void showProduct(int n)
{
cout<<"-------------------------------------------------------"<<endl;
cout<<"商品编号 商品名称 品牌 型号 价格"<<endl;
cout<<b[n].getNumber()<<setw(20)<<b[n].getName();
cout<<setw(8)<<b[n].getKind()<<setw(16)<<b[n].getKindNumber();
cout<<setw(6)<<b[n].getPrice()<<endl;
cout<<"-------------------------------------------------------"<<endl;
}
void findMenu()
{
bool flag=true;
int choose;
string name,number,kindnumber,kind;
while(flag)
{
cout<<"-----------------------"<<endl;
cout<<"--- 1 根据商品编号---"<<endl;
cout<<"--- 2 根据商品名称---"<<endl;
cout<<"--- 3 根据图书品牌---"<<endl;
cout<<"--- 4 根据商品型号-----"<<endl;
cout<<"--- 5 返回主菜单-----"<<endl;
cin>>choose;
switch(choose)
{
case 1:
cout<<"请输入编号:";
cin>>number;
findProductNumber(number);
break;
case 2:
cout<<"请输入名称:";
cin>>name;
findProductName(name);
break;
case 3:
cout<<"请输入品牌:";
cin>>kind;
findProductKind(kind);
break;
case 4:
cout<<"请输入型号:";
cin>>kindnumber;
findProductKind(kindnumber);
break;
case 5:
flag=false;
break;
}
}
}
void menu()
{
cout<<"------------------------"<<endl;
cout<<"----- 商品管理系统----- "<<endl;
cout<<"------------------------"<<endl<<endl;
cout<<"------------------------"<<endl;
cout<<"---- 1 增加商品 ----"<<endl;
cout<<"---- 2 查找商品 ----"<<endl;
cout<<"---- 3 显示全部 ----"<<endl;
cout<<"---- 0 退出系统 ----"<<endl;
cout<<"请选择业务:";
}
运行结果:
心得体会:增、删、改、查,啊~~