c++写一个完整的图书租赁系统
时间: 2024-05-12 15:15:17 浏览: 18
为了实现一个完整的图书租赁系统,需要考虑以下几个方面:
1. 数据库设计:需要设计数据库来存储图书、用户、订单等信息。
2. 用户登录和注册:用户需要能够注册账户并登录系统。
3. 图书管理:管理员需要能够添加、删除、修改图书信息,并能够查询图书的信息。
4. 订单管理:用户可以查看订单信息,管理员可以查询、修改订单信息。
5. 支付管理:用户需要能够完成支付操作。
6. 系统安全:需要确保系统的安全性,比如防止 SQL 注入、 XSS 攻击等。
下面是一个用 C++ 实现的简单图书租赁系统:
```cpp
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
// 定义图书类
class Book {
public:
string name; // 书名
string author; // 作者
double price; // 价格
int stock; // 库存
Book(string n, string a, double p, int s) {
name = n;
author = a;
price = p;
stock = s;
}
};
// 定义用户类
class User {
public:
string username; // 用户名
string password; // 密码
double balance; // 余额
User(string u, string p, double b) {
username = u;
password = p;
balance = b;
}
};
// 定义订单类
class Order {
public:
string username; // 用户名
string bookname; // 书名
double price; // 价格
int num; // 数量
Order(string u, string b, double p, int n) {
username = u;
bookname = b;
price = p;
num = n;
}
double get_total() { // 获取订单总价
return price * num;
}
};
// 定义图书租赁系统类
class BookRentingSystem {
public:
vector<Book> books; // 存储图书信息
vector<User> users; // 存储用户信息
vector<Order> orders; // 存储订单信息
void add_book(string name, string author, double price, int stock) { // 添加图书
Book book(name, author, price, stock);
books.push_back(book);
}
void remove_book(string name) { // 删除图书
for (auto it = books.begin(); it != books.end(); it++) {
if (it->name == name) {
books.erase(it);
break;
}
}
}
void modify_book(string name, string author, double price, int stock) { // 修改图书
for (auto it = books.begin(); it != books.end(); it++) {
if (it->name == name) {
it->author = author;
it->price = price;
it->stock = stock;
break;
}
}
}
void search_book(string name) { // 查询图书
for (auto it = books.begin(); it != books.end(); it++) {
if (it->name == name) {
cout << "书名:" << it->name << endl;
cout << "作者:" << it->author << endl;
cout << "价格:" << it->price << endl;
cout << "库存:" << it->stock << endl;
break;
}
}
}
void register_user(string username, string password) { // 注册用户
User user(username, password, 0);
users.push_back(user);
}
bool login(string username, string password) { // 用户登录
for (auto it = users.begin(); it != users.end(); it++) {
if (it->username == username && it->password == password) {
return true;
}
}
return false;
}
void place_order(string username, string bookname, int num) { // 下订单
for (auto it = books.begin(); it != books.end(); it++) {
if (it->name == bookname) {
if (it->stock >= num) { // 库存充足
double price = it->price;
Order order(username, bookname, price, num);
orders.push_back(order);
it->stock -= num; // 更新库存
break;
} else { // 库存不足
cout << "库存不足!" << endl;
break;
}
}
}
}
void check_order(string username) { // 查看订单
for (auto it = orders.begin(); it != orders.end(); it++) {
if (it->username == username) {
cout << "书名:" << it->bookname << endl;
cout << "价格:" << it->price << endl;
cout << "数量:" << it->num << endl;
cout << "总价:" << it->get_total() << endl;
}
}
}
void pay(string username) { // 支付订单
double total_price = 0;
for (auto it = orders.begin(); it != orders.end(); it++) {
if (it->username == username) {
total_price += it->get_total();
}
}
for (auto it = users.begin(); it != users.end(); it++) {
if (it->username == username) {
if (it->balance >= total_price) { // 余额充足
it->balance -= total_price; // 更新余额
// 清空订单
for (auto it = orders.begin(); it != orders.end(); it++) {
if (it->username == username) {
orders.erase(it);
break;
}
}
cout << "支付成功!" << endl;
break;
} else { // 余额不足
cout << "余额不足,请充值!" << endl;
break;
}
}
}
}
};
int main() {
BookRentingSystem system;
// 添加图书
system.add_book("C++ Primer", "Stanley B. Lippman", 100, 10);
system.add_book("Effective C++", "Scott Meyers", 80, 5);
system.add_book("Head First Design Patterns", "Eric Freeman", 120, 8);
// 删除图书
system.remove_book("Effective C++");
// 修改图书
system.modify_book("C++ Primer", "Stanley B. Lippman, Josée Lajoie, Barbara E. Moo", 120, 15);
// 查询图书
system.search_book("Head First Design Patterns");
// 注册用户
system.register_user("Alice", "123456");
system.register_user("Bob", "654321");
// 用户登录
if (system.login("Alice", "123456")) {
cout << "登录成功!" << endl;
} else {
cout << "用户名或密码错误!" << endl;
}
// 下订单
system.place_order("Alice", "C++ Primer", 2);
system.place_order("Bob", "C++ Primer", 3);
system.place_order("Bob", "Head First Design Patterns", 1);
// 查看订单
system.check_order("Bob");
// 支付订单
system.pay("Bob");
return 0;
}
```
这是一个非常简单的实现,实际上还有很多功能可以添加,比如管理员登录、充值余额、查看历史订单等等。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)