1、系统应具备的功能:
(1)进货员对商品基本信息进行输入、删除、修改和查询
(2)销售员对商品的基本信息进行查询和统计
(3)并可以对库存信息进行查询和修改
(4)顾客购买商品
2、要求:
(1)界面划分清晰,功能全面
(2)操作方便。
(3)数据结构选择合理,算法正确。
存储结构
1、 用户存储采用sqlite数据库,提高数据库的安全性、便利性。
2、 对商品的一系列属性用结构体表示,对商品名建立哈希表,借用STL里的MAP类。
3、 商品的属性均保存在数据库中。
算法设计
1、 商品管理系统最主要的是对产品名进行查询,获取其属性值。我们可以用二分法(O(lgn))对进行排序的结构体变量进行查询,或者如本程序以商品名为主键建立哈希表,实现常数查找(O(1))。
2、 由于用户均保存在sqlite数据库中,我们直接调用sqlite数据库API实现查询操作。
3、 3.、调用win32API进行对系统的封装美化。
概要设计
详细设计
#include <iostream> #include <algorithm> #include <string> #include <fstream> #include <cstdio> #include <cstring> #include <cstdlib> #include <conio.h> #include <iomanip> #include <map> #include <set> #include “sqlite3.h” #include <windows.h> using namespace std; #pragma comment(lib, “sqlite3.lib”) typedef struct Good { doubleprice; //价格 stringdate; //生产日期 intnum; //产品数量 }Good; //物品属性 map<string, Good> good_map; string password, good_name; bool flag; double sum; void LoadFile(); //文件载入 void Delete(string); //删除 void Insert(); //添加 void Query(); //查询 void Modify(); //修改 int Show(); //展示 void Print(); //输出 void PutFile(); //输入文件 void Choice(); //选择进货员或者销货员 void Sell(); //销售 void SetSize(short, short); //窗口属性设置 int sqlite3_exec_callback(void *data, intn_columns, char **col_values, char **col_names) //sqlite回调函数 { password= col_values[0]; /* for(int I = 0; I < n_columns; i++) { cout<< col_values[i] << endl; } */ return0; } int main() { SetConsoleTitle(“商品管理系统”); //设置窗口名 SetSize(40,20); //设置窗口大小 Choice(); system(“pause”); return0; } void SetSize(short width, short height) { intret = 0; COORDcd; SMALL_RECTsrctWindow; HANDLEhWin = GetStdHandle(STD_OUTPUT_HANDLE); if(!hWin) return; //确定适当的窗口尺寸 srctWindow.Left= 0; srctWindow.Right= width – 1; srctWindow.Top= 0; srctWindow.Bottom= height – 1; SetConsoleWindowInfo(hWin,1, &srctWindow); //确定适当的BUF尺寸 cd.X= width; cd.Y= height; SetConsoleScreenBufferSize(hWin,cd); } void Choice() { constchar Logo[8][40] = { “ “, “ @ @ @ @ @ @ @ @ @ @ “, //系统Logo “ @ @@ @ @ @ @ @ “, “ @ @ @ @ @ @ @”, “ @ @ @ @ @ @ @ @ @ @ “, “ @ @ @ @ @ @ @ @ @ “, “ @ @@ @ @ @ @ “, “ @ @ @ @ @ @ @ “ }; HANDLEhandle; handle= GetStdHandle(STD_OUTPUT_HANDLE); intn; charch; boolflag; stringinput, sql, name; sqlite3*db = NULL; char*err_msg = NULL; if(SQLITE_OK!= sqlite3_open(“user.db”, &db)) //sqlite数据库的操作函数 { cout<< “Can’t open the database” << endl; return; } SetConsoleTextAttribute(handle,FOREGROUND_RED | FOREGROUND_INTENSITY); intI; for(I = 0; I < 8; i++) { cout<< Logo[i] << endl; } cout<< endl; SetConsoleTextAttribute(handle,FOREGROUND_GREEN | FOREGROUND_INTENSITY); cout<< “ 1、进货员 2、销售员” << endl; cin>> n; if(n== 1) { system(“cls”); cout<< “请输入密码:”; while((ch= getch()) != ‘ ’) //利用getch将密码用*代替 { putchar(‘*’); input.insert(input.end(),ch); } sql= “select password from user where username = ”admin””; sqlite3_exec(db,sql.c_str(), &sqlite3_exec_callback, 0, &err_msg); system(“cls”); if(input== password) { flag= false; //循环终止条件 LoadFile(); while(!flag) { switch(Show()) { case1 : Insert(); break; case2 : cout << “输入要删除的产品”<< endl; cin >> name; Delete(name); PutFile(); break; case3 : Query(); break; case4 : Modify(); PutFile(); break; case5 : Print(); break; default:flag = true; } } } else { cout<< “密码错误” << endl; return; } } elseif(n == 2) { system(“cls”); cout<< “请输入密码:”; while((ch= getch()) != ‘ ’) { putchar(‘*’); input.insert(input.end(),ch); } sql= “select password from user where username = ”guest””; sqlite3_exec(db,sql.c_str(), &sqlite3_exec_callback, 0, &err_msg); system(“cls”); if(input== password) { flag= false; //循环终止条件 LoadFile(); while(!flag) { SetConsoleTextAttribute(handle,FOREGROUND_RED | FOREGROUND_INTENSITY); cout<< “1、查询 2、修改 3、销售 4、统计” << endl; cin>> n; SetConsoleTextAttribute(handle,FOREGROUND_GREEN | FOREGROUND_INTENSITY); switch(n) { case1 : Query(); break; case2 : Modify(); PutFile(); break; case3 : Sell(); PutFile(); break; case4 : cout << “当天销售额:” <<sum << “元” << endl;break; default:flag = true; } } } else { cout<< “密码错误” << endl; return; } } else { return; } sqlite3_close(db); } int Show() { HANDLEhandle; handle= GetStdHandle(STD_OUTPUT_HANDLE); intn; SetConsoleTextAttribute(handle,FOREGROUND_RED | FOREGROUND_INTENSITY); printf(“ 商品管理系统 ”); SetConsoleTextAttribute(handle,FOREGROUND_GREEN | FOREGROUND_INTENSITY); cout<< “***************+++++****************” << endl; cout<< “1、添加 2、删除 3、查询 4、修改 “ << endl; cout<< “ 5、输出 “ <<endl; cout<< “***************+++++****************” << endl; cin>> n; returnn; } void LoadFile() { stringname, date; doubleprice; intnum; ifstreamfin(“data.db”); while(fin>> name >> price >> date >> num) { good_map[name].price= price; good_map[name].date= date; good_map[name].num= num; } } void Delete(string name) { map<string,Good>::iterator it; it= good_map.find(name); good_map.erase(it); PutFile(); } void Insert() { ofstreamfout(“data.db”, ios::app); stringname, date; doubleprice; intnum; cout<< “输入增加的产品名、价格、生产日期、数量”<< endl; cin>> name >> price >> date >> num; good_map[name].price= price; good_map[name].date= date; good_map[name].num= num; fout<< name << “ “ << price << “ “ << date << ““ << num << endl; } void Query() { stringname; cout<< “输入要查询的产品” << endl; cin>> name; good_name= name; map<string,Good>::iterator it; it= good_map.find(name); if(it!= good_map.end()) { cout<< “产品名” << “ ”<< “价格” << “ ”<< “生产日期” << “ ”<< “数量” << endl; cout<< name << “ ” << it->second.price << “ ” <<it->second.date << “ ” << it->second.num << endl; flag= true; } else { flag= false; cout<< “没有此产品” << endl; } } void Sell() { intn; if(flag) { cout<< “销售几件:”; cin>> n; sum+= good_map[good_name].price * n; good_map[good_name].num-= n; if(good_map[good_name].num== 0) { Delete(good_name); } } } void Modify() { intn; stringname, date; doubleprice; intnum; cout<< “输入要修改都产品名:”; cin>> name; cout<< “0、修改价格 1、修改生产日期 2、修改数量 3、都修改” << endl; cin>> n; if(n == 0) { cout<< “输入价格:”; cin>> price; good_map[name].price= price; } elseif (n == 1) { cout<< “输入生产日期:”; cin>> date; good_map[name].date= date; } elseif(n == 2) { cout<< “输入数量:”; cin>> num; good_map[name].num= num; } elseif(n == 3) { cout<< “价格、生产日期、数量:”; cin>> price >> date >> num; good_map[name].price= price; good_map[name].date= date; good_map[name].num= num; } else { return; } } void Print() { map<string,Good>::iterator it; cout<< “产品名” << “ ”<< “价格” << “ ”<< “生产日期” << “ ”<< “数量” << endl; for(it= good_map.begin(); it != good_map.end(); it++) { cout<< it->first << “ ” << it->second.price << “ ”<< it->second.date << “ ” << it->second.num <<endl; } } void PutFile() { ofstreamfout(“data.db”); map<string,Good>::iterator it; for(it= good_map.begin(); it != good_map.end(); it++) { fout<< it->first << “ “ << it->second.price << “ “<< it->second.date << “ “ << it->second.num <<endl; } }
程序测试