-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBusManager.cpp
73 lines (61 loc) · 2.06 KB
/
BusManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// Created by 16117 on 2022/10/26.
//
#include "DatabaseManager.h"
#include "BusManager.h"
#include <cstdio>
extern char query[1500];
BusManager::BusManager() {
sprintf(query,"create table if not exists bus\n"
"(\n"
" location varchar(10) not null\n"
" primary key,\n"
" price int null,\n"
" numSeats int null,\n"
" numAvail int null\n"
");\n"
"");
MYSQL* mysql = &DatabaseManager::getInstance()->mysql;
if(mysql_query(mysql, query)) {
printf("Create Bus Failed!\n%s", mysql_error(mysql));
return ;
} else {
printf("Create Bus Successs!\n");
}
}
void BusManager::input() {
printf("请依次输入位置 价格 座位数\n");
std::cin >> location >> price >> numSeats;
}
void BusManager::insert() {
std::sprintf(query, "insert into bus values('%s',%d,%d,%d);", location.c_str(),price,numSeats,numSeats);
MYSQL* mysql = &DatabaseManager::getInstance()->mysql;
if(mysql_query(mysql, query)) {
printf("Insert Bus Failed!(%s)\n", mysql_error(mysql));
return ;
} else {
printf("Insert Bus Successs!\n");
}
}
std::string BusManager::reserve() {
printf("请选择要预定的大巴位置\n");
std::cin >> location;
show();
std::sprintf(query, "update bus set numAvail=numAvail-1 where location='%s'; ", location.c_str());
MYSQL* mysql = &DatabaseManager::getInstance()->mysql;
if(mysql_query(mysql, query)) {
printf("Reserve Bus Failed!\n%s", mysql_errno(mysql));
return "Failed";
} else {
printf("Reserve Bus Successs!\n");
return location;
}
}
void BusManager::show() {
std::sprintf(query, "select * from bus");
MYSQL *mysql = &DatabaseManager::getInstance()->mysql;
if (!mysql_query(mysql, query)) {
DatabaseManager::getInstance()->result = mysql_store_result(mysql);
DatabaseManager::getInstance()->showTable();
}
}