设计实现一个简单的汽车租赁管理系统。 要求: ① 车辆基本信息管理(车辆的添加、删除、修改); ② 租车管理功能(租车、换车、计费等); ③ 统计功能(统计出租率、出租费用)。 要求包括文件的创建读取 用C++代码实现请你写出完整的C++代码
时间: 2024-03-16 11:42:42 浏览: 95
简单的汽车租赁管理软件(C++控制台程序)
好的,以下是一个基本的C++代码实现:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <ctime>
using namespace std;
// 车辆信息类
class Car {
public:
Car(string id, string brand, string model, double price, bool rented = false) {
this->id = id;
this->brand = brand;
this->model = model;
this->price = price;
this->rented = rented;
}
string getID() { return id; }
string getBrand() { return brand; }
string getModel() { return model; }
double getPrice() { return price; }
bool getRented() { return rented; }
void setRented(bool rented) { this->rented = rented; }
private:
string id;
string brand;
string model;
double price;
bool rented;
};
// 租车信息类
class Rental {
public:
Rental(string id, string name, string phone, string carID, int days) {
this->id = id;
this->name = name;
this->phone = phone;
this->carID = carID;
this->days = days;
time_t now = time(nullptr);
tm* ltm = localtime(&now);
this->rentDate = to_string(1900 + ltm->tm_year) + "-" + to_string(1 + ltm->tm_mon) + "-" + to_string(ltm->tm_mday);
}
string getID() { return id; }
string getName() { return name; }
string getPhone() { return phone; }
string getCarID() { return carID; }
int getDays() { return days; }
string getRentDate() { return rentDate; }
void setReturnDate(string returnDate) { this->returnDate = returnDate; }
double getCost(vector<Car>& cars) {
for (Car& car : cars) {
if (car.getID() == carID) {
return days * car.getPrice();
}
}
return 0;
}
private:
string id;
string name;
string phone;
string carID;
int days;
string rentDate;
string returnDate;
};
// 车辆信息文件名
const string CAR_FILE = "cars.txt";
// 租车信息文件名
const string RENTAL_FILE = "rentals.txt";
// 车辆信息管理类
class CarManager {
public:
CarManager() {
// 读取车辆信息文件
ifstream ifs(CAR_FILE);
string id, brand, model;
double price;
bool rented;
while (ifs >> id >> brand >> model >> price >> rented) {
cars.push_back(Car(id, brand, model, price, rented));
}
ifs.close();
}
~CarManager() {
// 保存车辆信息到文件
ofstream ofs(CAR_FILE);
for (Car& car : cars) {
ofs << car.getID() << " " << car.getBrand() << " " << car.getModel() << " " << car.getPrice() << " " << car.getRented() << endl;
}
ofs.close();
}
void addCar(string id, string brand, string model, double price) {
cars.push_back(Car(id, brand, model, price));
}
void removeCar(string id) {
for (int i = 0; i < cars.size(); i++) {
if (cars[i].getID() == id) {
cars.erase(cars.begin() + i);
break;
}
}
}
void editCar(string id, string brand, string model, double price) {
for (Car& car : cars) {
if (car.getID() == id) {
car.setBrand(brand);
car.setModel(model);
car.setPrice(price);
break;
}
}
}
vector<Car>& getCars() { return cars; }
private:
vector<Car> cars;
};
// 租车信息管理类
class RentalManager {
public:
RentalManager() {
// 读取租车信息文件
ifstream ifs(RENTAL_FILE);
string id, name, phone, carID, rentDate, returnDate;
int days;
while (ifs >> id >> name >> phone >> carID >> days >> rentDate >> returnDate) {
Rental rental(id, name, phone, carID, days);
rental.setReturnDate(returnDate);
rentals.push_back(rental);
}
ifs.close();
}
~RentalManager() {
// 保存租车信息到文件
ofstream ofs(RENTAL_FILE);
for (Rental& rental : rentals) {
ofs << rental.getID() << " " << rental.getName() << " " << rental.getPhone() << " " << rental.getCarID() << " " << rental.getDays() << " " << rental.getRentDate() << " " << rental.getReturnDate() << endl;
}
ofs.close();
}
void rentCar(string name, string phone, string carID, int days) {
time_t now = time(nullptr);
tm* ltm = localtime(&now);
string id = to_string(ltm->tm_year + 1900) + to_string(1 + ltm->tm_mon) + to_string(ltm->tm_mday) + to_string(ltm->tm_hour) + to_string(ltm->tm_min) + to_string(ltm->tm_sec);
Rental rental(id, name, phone, carID, days);
rentals.push_back(rental);
for (Car& car : cars) {
if (car.getID() == carID) {
car.setRented(true);
break;
}
}
}
void returnCar(string rentalID) {
for (Rental& rental : rentals) {
if (rental.getID() == rentalID) {
rental.setReturnDate(getCurrentDate());
for (Car& car : cars) {
if (car.getID() == rental.getCarID()) {
car.setRented(false);
break;
}
}
break;
}
}
}
vector<Rental>& getRentals() { return rentals; }
double getRevenue() {
double revenue = 0;
for (Rental& rental : rentals) {
revenue += rental.getCost(cars);
}
return revenue;
}
double getRentRate() {
int rentedCount = 0;
for (Car& car : cars) {
if (car.getRented()) {
rentedCount++;
}
}
return (double)rentedCount / cars.size();
}
private:
vector<Rental> rentals;
vector<Car>& cars = CarManager().getCars();
string getCurrentDate() {
time_t now = time(nullptr);
tm* ltm = localtime(&now);
return to_string(1900 + ltm->tm_year) + "-" + to_string(1 + ltm->tm_mon) + "-" + to_string(ltm->tm_mday);
}
};
// 主函数
int main() {
CarManager carManager;
RentalManager rentalManager;
while (true) {
cout << "1. 车辆管理" << endl;
cout << "2. 租车管理" << endl;
cout << "3. 统计功能" << endl;
cout << "4. 退出" << endl;
cout << "请选择操作:";
int choice;
cin >> choice;
switch (choice) {
case 1:
cout << "1. 添加车辆" << endl;
cout << "2. 删除车辆" << endl;
cout << "3. 修改车辆" << endl;
cout << "请选择操作:";
cin >> choice;
switch (choice) {
case 1:
cout << "请输入车牌号:";
string id, brand, model;
double price;
cin >> id;
cout << "请输入品牌:";
cin >> brand;
cout << "请输入型号:";
cin >> model;
cout << "请输入租金:";
cin >> price;
carManager.addCar(id, brand, model, price);
cout << "添加成功!" << endl;
break;
case 2:
cout << "请输入要删除的车牌号:";
cin >> id;
carManager.removeCar(id);
cout << "删除成功!" << endl;
break;
case 3:
cout << "请输入要修改的车牌号:";
cin >> id;
cout << "请输入新的品牌:";
cin >> brand;
cout << "请输入新的型号:";
cin >> model;
cout << "请输入新的租金:";
cin >> price;
carManager.editCar(id, brand, model, price);
cout << "修改成功!" << endl;
break;
default:
cout << "无效的操作!" << endl;
break;
}
break;
case 2:
cout << "1. 租车" << endl;
cout << "2. 还车" << endl;
cout << "请选择操作:";
cin >> choice;
switch (choice) {
case 1:
cout << "请输入姓名:";
string name, phone, carID;
int days;
cin >> name;
cout << "请输入电话:";
cin >> phone;
cout << "请输入要租的车牌号:";
cin >> carID;
cout << "请输入租车天数:";
cin >> days;
if (!carManager.getCars().empty()) {
bool found = false;
for (Car& car : carManager.getCars()) {
if (car.getID() == carID && !car.getRented()) {
found = true;
rentalManager.rentCar(name, phone, carID, days);
cout << "租车成功!" << endl;
break;
}
}
if (!found) {
cout << "车辆不存在或已被租出!" << endl;
}
}
else {
cout << "当前没有车辆可供租用!" << endl;
}
break;
case 2:
cout << "请输入租车单号:";
string rentalID;
cin >> rentalID;
bool found = false;
for (Rental& rental : rentalManager.getRentals()) {
if (rental.getID() == rentalID) {
found = true;
rentalManager.returnCar(rentalID);
cout << "还车成功!" << endl;
break;
}
}
if (!found) {
cout << "租车单号不存在!" << endl;
}
break;
default:
cout << "无效的操作!" << endl;
break;
}
break;
case 3:
cout << "1. 统计出租率" << endl;
cout << "2. 统计出租费用" << endl;
cout << "请选择操作:";
cin >> choice;
switch (choice) {
case 1:
cout << "出租率:" << rentalManager.getRentRate() << endl;
break;
case 2:
cout << "出租费用:" << rentalManager.getRevenue() << endl;
break;
default:
cout << "无效的操作!" << endl;
break;
}
break;
case 4:
return 0;
default:
cout << "无效的操作!" << endl;
break;
}
}
return 0;
}
```
以上代码实现了一个简单的汽车租赁管理系统,包含车辆基本信息管理、租车管理、统计功能等模块。车辆信息和租车信息均以文件形式存储,程序启动时自动读取文件,退出时自动保存。您可以根据自己的需求进行扩展和修改。
阅读全文