【源文件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();
}