#include <iostream>
#include "main.h"
#include "sqlite.h"
using namespace std;
int main()
{
sqlite db;//创建一个数据库对象
db.initdb();
//在数据库中进行创建表
//1.先定义一个创建表的类的对象
table_creat tab;
//2.调用制表的类的成员函数
tab.table_creat_meau(db); // 创建一个菜单表
tab.table_creat_num(db); //创建一个销售量表
tab.table_creat_money(db); //统计营业额
tab.table_creat_administrator(db);//创建一张管理员表单
//创建一个 管理员对象
administrator admin;
customer custo;
//创建显示界面的类对象
interface face;
vector <string> v;
int op = 0;
int i = 0;
int ret = 0;
while(1)
{
//通过界面管理类,打印界面1
face.interface1();
cin>>op;
if(0 == op)
{
cout<<"成功退出"<<endl;
break;
}
switch(op)
{
//如果输入的值为1则进入客户端界面
case 1:
while(1)
{
face.interface3();
cin>>i;
if( 0 == i )
{
cout<<"已退出顾客界面"<<endl;
break;
}
switch(i)
{
//顾客输入1时,进行打印菜单操作
case 1:
custo.get_meau(db,v);
break;
//顾客输入2时,进行购买商品
case 2:
v.clear();
custo.buy(db,v);//执行完成之后,用户的购买信息将存储于容器v 中,将数据导入数据库中
admin.get_num(db,v);
//购买完成后需要进行返回账单和价格
cout<<"你的消费信息如下"<<endl;
for(int j = 0; j < v.size(); j++)
{
cout<<v[j].data()<<endl;4
}
//计算需要支付的费用
cout<<"你需要支付"<<db.customre_money(v)<<endl;
break;
default:
break;
}
}
break;
//进入管理员界面
case 2:
//进行登录操作
ret = admin.administrator_login(db);
if(ret < 0)
{
cout<<"登录失败"<<endl;
break;
}
cout<<"登录成功"<<endl;
while(1)
{
face.interface2();
cin>>i;
if( 0 == i)
{
cout<<"已退出当前界面"<<endl;
break;
}
switch(i)
{
case 1:
admin.get_meau(db,v);//查看菜单
break;
case 2:
admin.add_food(db);//添加菜品
break;
case 3:
admin.del_food(db);//删除菜品
break;
case 4:
admin.change_price(db);//修改价格
break;
case 5:
admin.show_num(db);//获得销量表
break;
case 6:
admin.get_money(db);//获取营业额表
break;
default:
cout<<"输入的值有误"<<endl;
break;
}
}
}
}
return 0;
}
以上时main函数主体部分
封装的对数据库类的操作:sqlite.cpp
#include "sqlite.h"
#include <stdlib.h>
#include <iomanip>
using namespace std;
int sqlite::initdb()
{
int ret = sqlite3_open("D:\\qt\\homework\\c++\\restaurant\\restaurant.db",&pdb);
if(ret != SQLITE_OK)
{
cout<<"sqlite3_open error"<<endl;
return -1;
}
cout<<"sqlite3_open success"<<endl;
return 0;
}
//在数据库中创建表
int sqlite::createtable(char *buf)
{
char *erromg = NULL;
int ret = 0;
ret = sqlite3_exec(pdb,buf,NULL,NULL,&erromg);
if( ret == SQLITE_OK)
{
cout<<"creat table success"<<endl;
return 0;
}
cout<<"creat table error"<<endl;
cout<<erromg<<endl;
return -1;
}
//在数据库中进行插入数据
int sqlite::insert_table(char *buf)
{
char *erromg = NULL;
int ret = 0;
ret = sqlite3_exec(pdb,buf,NULL,NULL,&erromg);
if(ret != SQLITE_OK)
{
cout<<"insret_table error "<<erromg<<endl;
return -1;
}
cout<<"insert_table success "<<endl;
return 0;
}
//在数据库中删除数据
int sqlite::delete_table(char *sql)
{
char *erromg;
int ret = 0;
ret = sqlite3_exec(pdb,sql,NULL,NULL,&erromg);
if(ret != SQLITE_OK)
{
cout<<"删除失败"<<erromg<<endl;
return -1;
}
cout<<"删除成功"<<endl;
return 1;
}
//在数据库中查找数据
int sqlite::find_table(char *sql)
{
char *erromg = NULL;
int ret = 0;
char **dbresult;
int i ,j;
ret = sqlite3_get_table(pdb,sql,&dbresult,&i,&j,&erromg);
if( i == 0)
{
cout<<"没查到"<<endl;
return -1;
}
cout<<"查到了"<<endl;
return 1;
}
int sqlite::update_table(char *sql)
{
char *errmog;
int ret = 0;
ret = sqlite3_exec(pdb,sql,NULL,NULL,&errmog);
if(ret != SQLITE_OK)
{
cout<<"修改失败"<<endl;
cout<<errmog<<endl;
return -1;
}
cout<<"修改成功"<<endl;
return 1;
}
int sqlite::show_table(char *sql)
{
char *erromg;
int ret = 0;
char **dbresult;
int i,j;
ret = sqlite3_get_table(pdb,sql,&dbresult,&i,&j,&erromg);
if(i == 0)
{
cout<<"该表为空"<<endl;
return -1;
}
int a,b,c;
c = j;
cout<<dbresult[0]<<" || "<<dbresult[1]<<endl;
cout<<"----------------------------------------"<<endl;
for(a = 0; a < i; a++)
{
// for(b = 0; b < j; b++)
{
cout<<left;
cout.width(18);
cout<<dbresult[c++];
cout.width(16);
cout<<"||";
cout.width(20);
cout<<left;
cout<<dbresult[c++]<<endl;
}
}
return 1;
}
void sqlite::find_change(vector<string> &v1, vector<string> &v2)
{
char *erromg;
char **dbresult;
int row,colunm;
char buf[255] = {};
int i;
for(i = 0; i < v1.size();i++)
{
sprintf(buf,"select * from food_num where food_name = '%s';",v1[i].data());
int ret = sqlite3_get_table(pdb,buf,&dbresult,&row,&colunm,&erromg);
if(ret != SQLITE_OK)
{
cout<<erromg<<endl;
}
if(row > 0)
{
cout<<dbresult[colunm]<<endl;
int a = atoi(dbresult[colunm+1]);
cout<<"数据库:"<<a<<endl;
int b = atoi(v2[i].data());
cout<<"顾客购买量"<<b<<endl;
a = a +b;
sprintf(buf,"update food_num set number = '%d' where food_name = '%s';",a,v1[i].data());
update_table(buf);
}
if(row == 0)
{
//进行直接插入
sprintf(buf,"insert into food_num values('%s','%s');",v1[i].data(),v2[i].data());
insert_table(buf);
}
}
}
int sqlite::money(char *sql)
{
int i,j,k;
char *erromg;
char **dbresult;
int row,colum;
char buf[255];
int ret = sqlite3_get_table(pdb,sql,&dbresult,&row,&colum,&erromg);
int sum = 0;
if(row == 0)
{
cout<<"查询为空"<<endl;
return -1;
}
vector <string> v1;
vector <string> v2;
v1.clear();
v2.clear();
int c= colum;
cout<<row<<endl;
cout<<colum<<endl;
//将销量表中的数据存储在容器v1,v2中,v1存储菜品名字,v2存储销量数
for( i = 0; i < row; i++)
{
v1.push_back(dbresult[c++]);
v2.push_back(dbresult[c++]);
cout<<v1[i].data()<<endl;
}
//通过销量表中的v1容器进行查询food_meau数据库,查询单价进行计算
for( i = 0; i < v1.size(); i++)
{
cout<<v1[i].data()<<endl;
sprintf(buf,"select * from food_meau where food_name = '%s';",v1[i].data());
int ret = sqlite3_get_table(pdb,buf,&dbresult,&row,&colum,&erromg);
if(row == 0)
{
cout<<"没找到"<<endl;
return -1;
}
cout<<"找到了单价"<<endl;
//找到了该菜品后拿到该菜品的单价
k = atoi(dbresult[colum+1]);
//然后计算总价
sum += k * atoi(v2[i].data());
}
return sum;
}
int sqlite::customre_money(vector<string> &v)
{
int i,j,k;
char *erromg;
char **dbresult;
int row,colum;
char buf[255];
int sum = 0;
vector <string> v1;
vector <string> v2;
v1.clear();
v2.clear();
for( i = 0; i < v.size(); i++)
{
if(i%2 == 0)
v1.push_back(v[i].data());
else
v2.push_back(v[i].data());
//cout<<v1[i].data()<<endl;
}
//通过销量表中的v1容器进行查询food_meau数据库,查询单价进行计算
for( i = 0; i < v1.size(); i++)
{
cout<<v1[i].data()<<endl;
sprintf(buf,"select * from food_meau where food_name = '%s';",v1[i].data());
int ret = sqlite3_get_table(pdb,buf,&dbresult,&row,&colum,&erromg);
if(row == 0)
{
cout<<"没找到"<<endl;
return -1;
}
cout<<"找到了单价"<<endl;
//找到了该菜品后拿到该菜品的单价
k = atoi(dbresult[colum+1]);
//然后计算总价
sum += k * atoi(v2[i].data());
}
return sum;
}
下面是管理员类的操作:administrtor.cpp
#include "administrator.h"
int administrator::administrator_login(sqlite &db) //进行管理员的登录
{
cout<<"请输入你的账号"<<endl;
cin>>name;
cout<<"请输入你的密码"<<endl;
cin>>pwd;
char buf[255] = {};
sprintf(buf,"select * from administrator where number = '%s' and name = '%s';",name,pwd);
int ret =db.find_table(buf);
if(ret < 0)
{
return -1;
}
return 1;
}
void administrator::add_food(sqlite &db)
{
cout<<"请输入你要添加的菜品"<<endl;
cin>>name;
cout<<"请输入你添加菜品的价格"<<endl;
cin>>pwd;
char buf[255] = {};
sprintf(buf,"insert into food_meau values('%s','%s');",name,pwd);
db.insert_table(buf);
}
void administrator::del_food(sqlite &db)
{
cout<<"请输入你要删除的菜品名"<<endl;
cin>>name;
char buf[255] = {};
sprintf(buf,"delete from food_meau where food_name = '%s';",name);
db.delete_table(buf);
}
void administrator::change_price(sqlite &db) //修改菜品的价格
{
cout<<"请输入你要修改价格的菜品名字"<<endl;
cin>>name;
char buf[255] = {};
//想要修改要先进行查询,判断菜品是否存在
sprintf(buf,"select * from food_meau where food_name = '%s';",name);
int ret = db.find_table(buf);
if(ret < 0)
{
cout<<"该菜品不存在,修改价格失败"<<endl;
return ;
}
cout<<"请输入你要修改的价格"<<endl;
float num = 0;
cin>>num;
sprintf(buf,"update food_meau set food_price = '%f' where food_name = '%s';",num,name);
db.update_table(buf);
}
void administrator::get_meau(sqlite &db, vector<string> &v) //查看菜单
{
char buf[255] = "select * from food_meau;";
db.show_table(buf);
}
void administrator::get_money(sqlite &db)
{
//想要获得营业总额,必须从销量表中获得每个菜品的名字和数量,再由菜单中获得菜品的单价最后算出数据
char buf[255] = "select * from food_num;";
int ret = db.money(buf);
//获得销售金额总额后进行入库操作,及插入数据操作
sprintf(buf,"insert into sell_money values('%d');",ret);
db.insert_table(buf);
}
void administrator::show_num(sqlite &db)
{
char buf[255] = "select * from food_num;";
db.show_table(buf);
}
void administrator::get_num(sqlite &db, vector<string> &v)//将用户的账单信息进行,入库
{
int i = 0;
int ret = 0;
int erromg;
int row,colum;
char **dbresult;
char buf[255] = {};
vector <string> v1;
vector <string> v2;
cout<<v.size()<<endl;
for(i = 0; i < v.size(); i++)
{
//将客户提供的账单信息中的菜品名 放入v1容器中
//将客户提供的菜品的数量信息存放入 v2容器中
if( 0 == i%2)
{
v1.push_back(v[i]);
}
if( 1 == i%2 )
{
v2.push_back(v[i]);
}
}
//将数据分好容器后,对数据库进行插入/更改 操作
//在更改前先进行检测一下该商品在数据库中存在吗
db.find_change(v1,v2);
}
下面是对顾客类的具体操作:customer.cpp
#include "customer.h"
#include "sqlite.h"
void customer::get_meau(sqlite &db, vector<string> &v) //查看菜单
{
char buf[255] = "select * from food_meau;";
db.show_table(buf);
//cout<<"正常"<<endl;
}
void customer::buy(sqlite &db, vector<string> &v)//将用户的选择加入到容器中
{
string number;
while(1)
{
cout<<"请输入你要购买的菜品"<<endl;
cin>>name;
v.push_back(name);
cout<<"请输入你要购买的数量"<<endl;
cin>>number;
v.push_back(number);
int op = 0;
flag1:
cout<<"是否还需要添加菜品 <1>添加 <2>不添加 "<<endl;
cin>>op;
if( 1 == op )
{
continue;
}
else if( 2 == op )
{
break;
}
else
{
cout<<"输入错误,请重新输入";
goto flag1;
}
}
}
下面是对数据库创建表的操作:table_creat.cpp
#include "table_creat.h"
#include "sqlite.h"
void table_creat::table_creat_meau(sqlite &db)
{
//创建的表的语句,第一个只能创建,如果存在则会失败,且食物的名字如果重复不能检查出错误,第二句为更健全的创建表语句
//char table_buf[255] = "creat table food_meau(food_name text not null,food_price float not null);" ;//创建一菜单的名字和价格表
char buf[255] = "create table if not exists food_meau(food_name text unique not null,food_price float not null);";
db.createtable(buf);
}
void table_creat::table_creat_num(sqlite &db)
{
char buf[255] = "create table if not exists food_num(food_name text unique not null,number integer not null);";
db.createtable(buf);
}
void table_creat::table_creat_money(sqlite &db)
{
char buf[255] = "create table if not exists sell_money(moeny float not null);";
db.createtable(buf);
}
void table_creat::table_creat_administrator(sqlite &db)
{
char buf[255] = "create table if not exists administrator(number text unique not null,name text not null);";
db.createtable(buf);
}
下面是对服务器界面和客户端界面的实现:interface.cpp
#include "interface.h"
void interface::interface1() //选择身份界面
{
cout<<"欢迎使用智能点餐系统"<<endl;
cout<<"****************************************************"<<endl;
cout<<" 1 --- 进入顾客界面"<<endl;
cout<<" 2 --- 进入管理员界面"<<endl;
cout<<" 0 --- 退出当前界面"<<endl;
cout<<"****************************************************"<<endl;
}
void interface::interface2() //管理员界面
{
cout<<"你好 管理员"<<endl;
cout<<"****************************************************"<<endl;
cout<<" 1 ----- 菜单查看"<<endl;
cout<<" 2 ----- 添加菜品"<<endl;
cout<<" 3 ----- 删除菜品"<<endl;
cout<<" 4 ----- 修改价格"<<endl;
cout<<" 5 ----- 销量报表"<<endl;
cout<<" 6 ----- 营业总额"<<endl;
cout<<" 0 ----- 退出系统"<<endl;
cout<<"****************************************************"<<endl;
}
void interface::interface3() //顾客界面
{
cout<<"欢迎光临"<<endl;
cout<<"****************************************************"<<endl;
cout<<" 1 ----- 查看菜单"<<endl;
cout<<" 2 ----- 选菜购买"<<endl;
cout<<" 0 ----- 退出系统"<<endl;
cout<<"****************************************************"<<endl;
}
下面是在类中的定义main.h
#ifndef MAIN_H
#define MAIN_H
#include "administrator.h"
#include "customer.h"
#include "sqlite.h"
#include "sqlite3.h"
#include "table_creat.h"
#include "interface.h"
#include <vector>
#endif // MAIN_H
sqlite.h:
#ifndef SQLITE_H
#define SQLITE_H
#include <iostream>
#include "sqlite3.h"
#include <vector>
#include <stdlib.h>
using namespace std;
class sqlite
{
public:
int initdb();//初始化数据库
int createtable(char *sql); //创建数据库表
int insert_table(char *sql); //往表中插入数据
int delete_table(char *sql); //删除表中的数据
int update_table(char *sql); //更改表中的数据
int find_table(char *sql); //在表中查找数据
int show_table(char *sql);//显示表中的数据
void find_change(vector <string> &v1, vector <string> &v2);
int money(char *sql);
int customre_money( vector <string> &v);
private:
sqlite3 *pdb;
};
#endif // SQLITE_H
table_creat.h:
#ifndef TABLE_CREAT_H
#define TABLE_CREAT_H
#include <iostream>
#include "sqlite.h"
class table_creat
{
public:
void table_creat_meau(sqlite &db);//在指定数据库中创建表的函数(菜单表)
void table_creat_num(sqlite &db); //创建一张销量表(统计销售量)
void table_creat_money(sqlite &db); //创建一张营业额表,负责统计收入的营业额
void table_creat_administrator(sqlite &db); //创建一个管理员信息表
};
#endif // TABLE_CREAT_H
administrator.h:
#ifndef ADMINISTRATOR_H
#define ADMINISTRATOR_H
#include <iostream>
#include "sqlite.h"
#include "sqlite3.h"
#include "table_creat.h"
#include <vector>
using namespace std;
class administrator
{
public:
int administrator_login(sqlite &db);//管理员登录
void add_food(sqlite &db); //往菜单表中添加数据
void del_food(sqlite &db); //从菜单中删除数据
void change_price(sqlite &db);//修改某道菜的价格
void get_meau(sqlite &db, vector <string> &v ); //打印菜单
void get_num(sqlite &db, vector <string> &v); //制作销量表
void show_num(sqlite &db);
void get_money(sqlite &db); // 打印销售额表
private:
char name[255];
char pwd[255];
};
#endif // ADMINISTRATOR_H
customer.h:
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include <iostream>
using namespace std;
#include "sqlite.h"
#include <vector>
class customer
{
public:
void get_meau(sqlite &db, vector <string> &v);//在顾客界面获取菜单
void buy(sqlite &db, vector <string> &v); //顾客购买菜品
private:
string name;
};
#endif // CUSTOMER_H
interface.h:
#ifndef INTERFACE_H
#define INTERFACE_H
#include <iostream>
using namespace std;
class interface
{
public:
void interface1();//登录界面,显示顾客或者管理员入口
void interface2();//管理员界面
void interface3();//客户界面
};
#endif // INTERFACE_H
本文含有隐藏内容,请 开通VIP 后查看