One, system function
1. View menu details
2. Add dishes to the shopping cart
3. Remove the dish from the shopping cart
4. View my shopping cart details
5. Empty my shopping cart dishes
6. Menu administrator authentication
7. Admin option: add dishes to the menu
8. Admin option: delete dishes from the menu
Note: The data of the above operations all exist in the MySql database, and this project will use Visual Studio to access it
Second, the project involves
1. C/C++ access to MySQL database (references)
2. Visual Studio accesses the property configuration of MySQL
3. Use VS to simply add, delete, check and modify data in the database
Three, operation demonstration
1. Run the program and enter the selection main interface
2. Enter any character other than 1–9
3, enter “1” to view menu details
4, enter “2”, add dishes to the shopping cart
At this time, the shopping cart has two dishes with id=1 and one dish with id=3
5, enter “3”, remove the dish from the shopping cart
The dish with id=2 does not exist in the shopping cart, and the removal failed
The dish with id=3 exists in the shopping cart and has been successfully removed
6, enter “4” to view my shopping cart details
7, enter “5” to clear my shopping cart dishes
Query the shopping cart again and find that it has been emptied
8, enter “6”, menu administrator authentication
The password entered is wrong, the correct password is 123
The password entered is correct and the administrator login is successful
9, enter “7”, administrator options: add dishes to menu
This happens if the login to the administrator fails
On the premise of successfully logging in to the administrator, add dishes to the menu, and do not have a space bar for the ingredients
Adding the dish with id=9 again will fail, the id of the menu table is the primary key and cannot be repeated
10, enter “8”, admin options: remove dish from menu
The dish with id=10 does not exist in the menu table, and the deletion failed
There is a dish with id=9 in the menu table, and the deletion is successful
Check the menu table again, the dish with id=9 has been deleted
11, enter “9”, exit the system
Fourth, attach source code
1, MysqlMenu.h
#pragma once #define IP "127.0.0.1" #define USER "root" #define PASSWORD "123456" #define DBNAME "menumanage" #define PORT 3306 #include <mysql.h> // mysql file #include <iomanip> #include<string> #include <iostream> using namespace std; class menu { public: Menu();//Constructor, connect to the database bool m_check();//View menu bool m_into();//add to shopping cart bool m_remove(); //Remove from shopping cart bool m_empty();//Empty the shopping cart bool m_inquire();//Query shopping cart details void public_interface();//Provide a public interface to access functions in protected //bool load_config();//Load user configuration file, not implemented ~Menu(); //protected: bool m_add();//add dishes bool m_delete();//Delete dishes private: MYSQL* conn; //connection handle MYSQL_RES* res; //result set pointer MYSQL_ROW row; // row data type string ip; //The ip address of the local virtual network card string username; //database username string password; //database password string dbname; //database name unsigned short port;//port number string key = "123"; //menu administrator password bool identity = false;//whether it is a menu administrator };
2, MysqlMenu.cpp
#include "MysqlMenu.h" #include <iomanip> Menu::Menu() { ip = IP; username = USER; password = PASSWORD; dbname = DBNAME; port = PORT; conn = mysql_init(nullptr); res = nullptr; //Initialization, set encoding characters, connection operation mysql_options(conn, MYSQL_SET_CHARSET_NAME, "gbk"); if (!mysql_real_connect(conn, ip.c_str(), username.c_str(), password.c_str(), dbname.c_str(), port, nullptr, 0)) { cout << "Database connection failed:" << mysql_error(conn) << endl; return; } else { cout << "Database connection successful!" << endl; } } bool Menu::m_check() { if (mysql_query(conn, "select * from menu")) { cout << "Query menu failed:" << mysql_error(conn) << endl; return false; } cout << "Menu query successful:" << endl; res = mysql_store_result(conn); int cols = mysql_num_fields(res); // Calculate the number of columns in the result set while (row = mysql_fetch_row(res))//The number of rows is equal to the number of results {//Two loops, output rows and columns of each row for (int i = 0; i < cols; + + i) { cout << left << setw(18) << row[i];//Left alignment width is 18 } cout << endl; } mysql_free_result(res);//Release the result set mysql handle return true; } bool Menu::m_into() { int id; cout << "Please enter the id of the dish to be purchased: " << endl; cin >> id; //Check if the item exists string sql = "select * from menu where id = '" + to_string(id) + "'"; if (mysql_query(conn, sql.c_str())) { cerr << "id error: " << mysql_error(conn) << endl; return false; } res = mysql_store_result(conn); row = mysql_fetch_row(res); if (mysql_num_rows(res) <= 0) { cerr << "The dish that needs to be added to the shopping cart does not exist" << endl; mysql_free_result(res); return false; } //row cannot be output when it is invalid //cout << row[0] << endl; \t sql = "insert into ShoppingCart values((select id from menu where id='" + to_string(id) + "'),(select name from menu where id='" + to_string(id) + "'),(select price from menu where id='" + to_string(id) + "'))"; if (mysql_query(conn, sql.c_str())) { cout << mysql_error(conn) << endl; return false; } cout << "The dish has been successfully added to the shopping cart" << endl; return true; } bool Menu::m_remove() { int sc_id; cout << "Please enter the id of the dish to be removed from the shopping cart:" << endl; cin >> sc_id; char sql[256] = { 0 }; sprintf_s(sql, "select sc_id from ShoppingCart where sc_id = %d", sc_id); if (mysql_query(conn, sql)) { cout << "id error:" << mysql_error(conn) << endl; return false; } res = mysql_store_result(conn); if (mysql_num_rows(res) <= 0) { cerr << "The dish to be removed does not exist in the shopping cart" << endl; mysql_free_result(res); return false; } row = mysql_fetch_row(res); sprintf_s(sql, "delete from ShoppingCart where sc_id=%d", sc_id); if (mysql_query(conn, sql)) { cout << mysql_error(conn) << endl; return false; } cout << "Dish removed successfully" << endl; return true; } bool Menu::m_empty() { //Empty the shopping cart string sql = "truncate table ShoppingCart"; if (mysql_query(conn, sql.c_str())) { cerr << "Error emptying shopping cart: " << mysql_error(conn) << endl; return false; } cout << "Shopping cart is empty" << endl; } bool Menu::m_inquire() { if (mysql_query(conn, "select * from ShoppingCart")) { cout << "Query shopping cart failed:" << mysql_error(conn) << endl; return false; } cout << "Query the shopping cart successfully:" << endl; int num = 0;//Number of purchases res = mysql_store_result(conn); int cols = mysql_num_fields(res); // Calculate the number of columns in the result set while (row = mysql_fetch_row(res))//The number of rows is equal to the number of results {//Two loops, output rows and columns of each row num++; for (int i = 0; i < cols; + + i) { cout << left << setw(18) << row[i];//Left alignment width is 18 } cout << endl; } if (num == 0) { cout << "The purchase quantity is: 0 \t\t The total order price is: 0.00" << endl; } else { if (mysql_query(conn, "select sum(sc_price) from ShoppingCart")) { cout << "Order calculation failed:" << mysql_error(conn) << endl; return false; } res = mysql_store_result(conn); cout << "The purchase quantity is:" << num << "\t\t The total order price is:" << mysql_fetch_row(res)[0] << endl; } mysql_free_result(res);//Release the result set mysql handle return true; } void Menu::public_interface() { cout << "Please enter the menu administrator login password:" << endl; string s; cin >> s; if (s == key) { identity = true; cout << "The administrator has successfully logged in and can add and delete dishes!" << endl; } else { identity = false; cout << "Administrator login failed, cannot add or delete dishes!" << endl; } } bool Menu::m_add() { if (identity == false) { cout << "You have not passed the administrator authentication, you cannot add or delete dishes" << endl; return false; } int id; string name; double price; string compose; char sql[256]; cout << "Please enter: id, name, selling price, ingredients" << endl; cin >> id >> name >> price >> compose; sprintf_s(sql, "insert into menu(id, name, price, compose) values(%d,'%s','%lf','%s')", id, name.c_str(), price, compose .c_str()); if (mysql_query(conn, sql)) { cout << "Failed to add dishes:" << mysql_error(conn) << endl; return false; } cout << "Dish added successfully:" << mysql_error(conn) << endl; return true; } bool Menu::m_delete() { if (identity == false) { cout << "You have not passed the administrator authentication, you cannot add or delete dishes" << endl; return false; } int id; cout << "Please enter the id of the dish to be deleted:" << endl; cin >> id; char sql[256] = { 0 }; sprintf_s(sql, "select id from menu where id = %d", id); if (mysql_query(conn, sql)) { cout << "id error:" << mysql_error(conn) << endl; return false; } res = mysql_store_result(conn); if (mysql_num_rows(res) <= 0) { cerr << "The dish to be deleted does not exist in the menu" << endl; mysql_free_result(res); return false; } row = mysql_fetch_row(res); sprintf_s(sql, "delete from menu where id=%d", id); if (mysql_query(conn, sql)) { cout << mysql_error(conn) << endl; return false; } cout << "Dish deleted successfully" << endl; return true; } //Release the memory connection Menu::~Menu() { if (conn != nullptr) { mysql_close(conn); } }
3, main.cpp
#include <iostream> #include "MysqlMenu.h" using namespace std; void print() { cout << "Welcome to the self-service order!!" << endl; cout << "Please choose your operation\ " << endl; cout << "1. View menu details" << endl; cout << "2. Add dishes to shopping cart" << endl; cout << "3. Remove dishes from shopping cart" << endl; cout << "4. Check my shopping cart details" << endl; cout << "5, clear my shopping cart dishes" << endl; cout << endl; cout << "6, menu administrator authentication" << endl; cout << "7, administrator options: add dishes to the menu" << endl; cout << "8. Administrator option: delete dishes from the menu" << endl; cout << "9, exit the system" << endl; } int main() { Menu* menu = new Menu(); bool go_on = true; while(go_on) { system("cls"); print(); int number; cin >> number; switch (number) { case 1:menu->m_check(); break; //1. View menu details case 2:menu->m_into(); break; //2. Add dishes to the shopping cart case 3:menu->m_remove(); break; //3. Remove dishes from the shopping cart case 4:menu->m_inquire(); break; //4. View shopping cart details case 5:menu->m_empty(); break; //5. Empty shopping cart dishes case 6:menu->public_interface(); break; //6, menu administrator authentication case 7:menu->m_add(); break; //7, administrator option: add dishes to the menu case 8:menu->m_delete(); break; //8. Administrator option: delete dishes from the menu case 9:go_on = false, //9, exit the system cout << "Thank you for your patronage, welcome to visit next time" << endl; break; default:cout << "Incorrect input, please re-enter" << endl; } system("pause"); } delete menu; }
If there is something wrong, I hope you can correct me more and don’t hesitate to teach me. Thank you for reading and liking and encouraging!