-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcar_system.cpp
executable file
·186 lines (162 loc) · 6.25 KB
/
car_system.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <bits/stdc++.h>
#include <mariadb/conncpp.hpp>
#include "readme.hpp"
// Register client function
void registerFunc(std::unique_ptr<sql::Connection> &conn,
std::string name,
std::string surname,
std::string address,
std::string cpf,
std::string phone) { //All column names in the table "clients"
try{
// Create a PreparedStatement
std::unique_ptr<sql::PreparedStatement> stmnt(conn->prepareStatement("INSERT INTO clients (name, surname, address, cpf, phone) VALUES (?, ?, ?, ?, ?)"));
// Bind values to SQL statement
stmnt->setString(1, name);
stmnt->setString(2, surname);
stmnt->setString(3, address);
stmnt->setString(4, cpf);
stmnt->setString(5, phone);
// Execute query
stmnt->executeQuery();
}
catch(sql::SQLException& e){
std::cerr << "Error registering new client: " << e.what() << std::endl;
}
}
// Make an order function
void makeOrder(std::unique_ptr<sql::Connection> &conn,
std::string brand,
std::string model,
std::string kilometers,
std::string years,
std::string name,
std::string surname,
std::string cpf) { //All columns needed in the "cars" and "clients" tables, to filter it and make an order
try {
std::unique_ptr<sql::PreparedStatement> stmnt(conn->prepareStatement("INSERT INTO orders (id_car, id_client) VALUES ((SELECT id_car FROM cars WHERE brand = ? AND model = ? AND kilometers = ? AND year = ?), (SELECT id_client FROM clients WHERE name = ? AND surname = ? AND cpf = ?))"));
// Bind values to SQL statement
stmnt->setString(1, brand);
stmnt->setString(2, model);
stmnt->setString(3, kilometers);
stmnt->setString(4, years);
stmnt->setString(5, name);
stmnt->setString(6, surname);
stmnt->setString(7, cpf);
// Execute query
stmnt->executeQuery();
}
catch(sql::SQLException& e) {
std::cerr << "Error making an order: " << e.what() << std::endl;
}
}
// Register a new car
void addCar(std::unique_ptr<sql::Connection> &conn,
std::string brand,
std::string model,
std::string kilometers,
std::string year,
std::string price) { //All column names in the table "cars"
try{
// Create a PreparedStatement
std::unique_ptr<sql::PreparedStatement> stmnt(conn->prepareStatement("INSERT INTO cars (brand, model, kilometers, year, price) VALUES (?, ?, ?, ?, ?)"));
// Bind values to SQL statement
stmnt->setString(1, brand);
stmnt->setString(2, model);
stmnt->setString(3, kilometers);
stmnt->setString(4, year);
stmnt->setString(5, price);
// Execute query
stmnt->executeQuery();
}
catch(sql::SQLException& e){
std::cerr << "Error adding a new car: " << e.what() << std::endl;
}
}
// Print inventory function
void printInventory(std::unique_ptr<sql::Connection> &conn) {
try{
// Create Statement
std::unique_ptr<sql::Statement> stmnt(conn->createStatement());
// Execute query
sql::ResultSet *res = stmnt->executeQuery("SELECT * FROM cars");
// Loop through and print results
while (res->next()) {
std::cout << "id = " << res->getInt(1);
std::cout << ", brand = " << res->getString(2);
std::cout << ", model = " << res->getBoolean(3);
std::cout << ", kilometers = " << res->getFloat(4);
std::cout << ", year = " << res->getInt(5);
std::cout << ", price = " << res->getFloat(6) << "\n";
}
}
catch(sql::SQLException& e) {
std::cerr << "Error printing inventory: " << e.what() << std::endl;
}
}
// Print orders function
void printOrders(std::unique_ptr<sql::Connection> &conn) {
try{
// Create Statement
std::unique_ptr<sql::Statement> stmnt(conn->createStatement());
// Execute query
sql::ResultSet *res = stmnt->executeQuery("SELECT * FROM orders");
// Loop through and print results
while (res->next()) {
std::cout << "id_order = " << res->getInt(1);
std::cout << ", id_car = " << res->getInt(2);
std::cout << ", id_client = " << res->getInt(3) << "\n";
}
}
catch(sql::SQLException& e) {
std::cerr << "Error printing inventory: " << e.what() << std::endl;
}
}
// Main Function
int main(int argc, char const *argv[]) {
if (argc==1){
readtxt();
}
else{
try {
// Instantiate Driver
sql::Driver* driver = sql::mariadb::get_driver_instance();
// Configure Connection
sql::SQLString url("jdbc:mariadb://localhost:3306/car_system");
sql::Properties properties({{"user", "admin"}, {"password", "admin"}});
// Establish Connection
std::unique_ptr<sql::Connection> conn(driver->connect(url, properties));
if (!strcmp(argv[1],"Register")) {
if (argc != 7) {
std::cout << "Invalid arguments";
return 1;
}
registerFunc(conn, argv[2], argv[3], argv[4], argv[5], argv[6]);
}
if(!strcmp(argv[1],"makeOrder")) {
if (argc != 9){
std::cout << "Invalid arguments";
return 1;
}
makeOrder(conn, argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8]);
}
if (!strcmp(argv[1],"addCar")) {
if (argc != 7) {
std::cout << "Invalid arguments";
return 1;
}
addCar(conn, argv[2], argv[3], argv[4], argv[5], argv[6]);
}
if(!strcmp(argv[1], "printInventory")){
printInventory(conn);
}
if(!strcmp(argv[1], "printOrders")){
printOrders(conn);
}
}
catch(const std::exception& e){
std::cerr << e.what() << '\n';
}
}
return 0;
}