【源文件main.c】
#include <stdio.h>
#include <stdlib.h> // system函数所在地
#include <string.h>
#include <conio.h>
#include "db.h"
int exit_flag = 0; // 程序退出标志
void show_food()
{
int type_id;
char sql[100];
char type_name[100];
void *stmt1, *stmt2;
int food_id;
char food_name[100];
float food_price;
int i;
char order_name[100];
char place[100];
stmt1 = db_query("SELECT * FROM Categories");
db_bind_int(stmt1, 1, &type_id);
db_bind_str(stmt1, 2, type_name, sizeof(type_name));
system("cls"); // 清屏
while (db_fetch(stmt1))
{
printf("------------------类别%d %s-------------------\n", type_id, type_name);
sprintf_s(sql, sizeof(sql), "SELECT FoodID, FoodName, FoodPrice FROM Food WHERE FoodType = %d", type_id);
stmt2 = db_query(sql);
db_bind_int(stmt2, 1, &food_id);
db_bind_str(stmt2, 2, food_name, sizeof(food_name));
db_bind_float(stmt2, 3, &food_price);
for (i = 0; db_fetch(stmt2); i++)
{
if (i % 3 == 0 && i != 0)
putchar('\n');
printf("%d.%s(%.1f元)\t", food_id, food_name, food_price);
}
putchar('\n');
db_free(stmt2);
}
db_free(stmt1);
printf("请输入所选菜的编号: ");
scanf_s("%d", &food_id);
sprintf_s(sql, sizeof(sql), "SELECT * FROM Food WHERE FoodID = %d", food_id);
if (!db_has_records(sql))
{
puts("该菜不存在!");
_getch();
system("cls");
return;
}
fflush(stdin);
printf("请输入客户名:");
gets_s(order_name, sizeof(order_name));
printf("请输入用餐地点: ");
gets_s(place, sizeof(place));
stmt1 = db_prepare("INSERT INTO Orders (OrderName, FoodID, OrderPlace) VALUES (?, ?, ?)");
db_set_str(stmt1, 1, order_name);
db_set_int(stmt1, 2, &food_id);
db_set_str(stmt1, 3, place);
if (db_exec_stmt(stmt1, 1))
puts("添加订单成功");
else
puts("添加订单失败");
_getch();
system("cls");
}
void show_orders()
{
void *s1;
int order_id;
char order_name[100];
char food_name[100];
char order_place[100];
char order_time[100];
system("cls");
puts("------------------订单------------------");
s1 = db_query("SELECT OrderID, OrderName, (SELECT FoodName FROM Food WHERE FoodID = Orders.FoodID), OrderPlace, OrderTime FROM Orders ORDER BY OrderID DESC");
db_bind_int(s1, 1, &order_id);
db_bind_str(s1, 2, order_name, sizeof(order_name));
db_bind_str(s1, 3, food_name, sizeof(food_name));
db_bind_str(s1, 4, order_place, sizeof(order_place));
db_bind_str(s1, 5, order_time, sizeof(order_time));
puts("单号\t客户\t菜名\t地点\t时间");
while (db_fetch(s1))
{
printf("%4d %8s %8s %8s %s\n", order_id, order_name, food_name, order_place, order_time);
}
db_free(s1);
_getch();
system("cls");
}
void handle_order()
{
int order_id, food_id;
char sql[100];
char customer_id[100];
char comment[500];
void *stmt;
printf("请输入订单号: ");
scanf_s("%d", &order_id);
sprintf_s(sql, sizeof(sql), "SELECT FoodID FROM Orders WHERE OrderID = %d", order_id);
stmt = db_query(sql);
db_bind_int(stmt, 1, &food_id);
if (!db_fetch(stmt))
{
db_free(stmt);
puts("该订单不存在!");
_getch();
system("cls");
return;
}
db_free(stmt);
printf("请输入客户身份证号码: ");
fflush(stdin);
gets_s(customer_id, sizeof(customer_id));
printf("请输入客户评价内容: ");
gets_s(comment, sizeof(comment));
stmt = db_prepare("INSERT INTO Comments (FoodID, CustomerID, Comment) VALUES (?, ?, ?)");
db_set_int(stmt, 1, &food_id);
db_set_str(stmt, 2, customer_id);
db_set_str(stmt, 3, comment);
if (db_exec_stmt(stmt, 1))
{
sprintf_s(sql, sizeof(sql), "DELETE FROM Orders WHERE OrderID = %d", order_id);
db_exec(sql);
puts("处理订单成功");
}
else
puts("处理订单失败");
_getch();
system("cls");
}
void show_comments()
{
int comment_id, food_id;
char food_name[100];
char customer_id[100];
char comment[500];
char time[30];
void *stmt, *stmt2;
system("cls");
stmt = db_query("SELECT * FROM Comments ORDER BY CommentID DESC");
db_bind_int(stmt, 1, &comment_id);
db_bind_int(stmt, 2, &food_id);
db_bind_str(stmt, 3, customer_id, sizeof(customer_id));
db_bind_str(stmt, 4, comment, sizeof(comment));
db_bind_str(stmt, 5, time, sizeof(time));
while (db_fetch(stmt))
{
stmt2 = db_prepare("SELECT FoodName FROM Food WHERE FoodID = ?");
db_set_int(stmt2, 1, &food_id);
db_exec_stmt(stmt2, 0);
db_bind_str(stmt2, 1, food_name, sizeof(food_name));
db_fetch(stmt2);
printf("--------------------评价%d: %d.%s-----------------\n", comment_id, food_id, food_name);
printf(" 客户身份证号: %s\n", customer_id);
printf(" 评价内容: %s\n", comment);
printf(" 评价时间: %s\n", time);
puts("------------------------------------------------");
db_free(stmt2);
}
db_free(stmt);
_getch();
system("cls");
}
void show_menu()
{
int n;
puts("------------------欢迎使用点餐系统------------------");
puts("--- 1.订餐 -----");
puts("--- 2.查看订单 -----");
puts("--- 3.处理订单 -----");
puts("--- 4.查看评价 -----");
puts("--- 5.退出 -----");
puts("---------------------------------------------------");
printf("请输入: ");
scanf_s("%d", &n);
switch (n)
{
case 1:
show_food();
break;
case 2:
show_orders();
break;
case 3:
handle_order();
break;
case 4:
show_comments();
break;
case 5:
exit_flag = 1;
puts("谢谢您的使用....");
break;
default:
puts("输入错误, 请重新输入!");
}
}
void main()
{
if (db_connect("data.mdb"))
{
while (!exit_flag)
{
show_menu();
}
}
db_disconnect();
}