How to write a simple car rental management system using C?
The car rental business is becoming more and more popular, which has also led to an increase in the demand for car rental management systems. This article will introduce how to use C to write a simple car rental management system.
System requirements:
We need a system that can manage rental vehicles, including the following functions:
System design:
Before entering the system, the user needs to enter the administrator's user name and password for verification. After passing the verification, the user can enter the system to operate.
First, we need to create a Car class to define the properties and methods of the vehicle.
class Car { private: int carID; string brand; string model; double rentalPrice; bool isRented; public: Car(int id, string b, string m, double price) { carID = id; brand = b; model = m; rentalPrice = price; isRented = false; } // getter and setter for carID, brand, model, rentalPrice, isRented void rentCar() { isRented = true; } void returnCar() { isRented = false; } double calculateRent(double numDays) { return rentalPrice * numDays; } };
Next, we create a CarRentalSystem class to manage the rental and return of vehicles.
class CarRentalSystem { private: vector<Car> cars; string adminUsername; string adminPassword; public: CarRentalSystem(string username, string password) { adminUsername = username; adminPassword = password; } void addCar(int id, string brand, string model, double price) { Car newCar(id, brand, model, price); cars.push_back(newCar); } void rentCar(int id) { for (int i = 0; i < cars.size(); i++) { if (cars[i].getCarID() == id) { cars[i].rentCar(); break; } } } void returnCar(int id) { for (int i = 0; i < cars.size(); i++) { if (cars[i].getCarID() == id) { cars[i].returnCar(); break; } } } double calculateTotalRent(double numDays) { double totalRent = 0.0; for (int i = 0; i < cars.size(); i++) { if (cars[i].isRented()) { double rent = cars[i].calculateRent(numDays); totalRent += rent; } } return totalRent; } void displayAllCars() { for (int i = 0; i < cars.size(); i++) { // display car information } } };
Finally, we use the CarRentalSystem class in the main function to create an instance and test various functions of the system.
int main() { string username = "admin"; string password = "password"; CarRentalSystem system(username, password); // 添加车辆信息 system.addCar(1, "Toyota", "Camry", 50.0); system.addCar(2, "Honda", "Accord", 60.0); system.addCar(3, "BMW", "X5", 100.0); // 租赁和归还车辆 system.rentCar(1); system.rentCar(3); system.returnCar(1); // 统计租金 double rent = system.calculateTotalRent(5); cout << "Total rent: $" << rent << endl; // 显示所有车辆信息 system.displayAllCars(); }
Summary:
This article introduces how to use C to write a simple car rental management system. By creating Car and CarRentalSystem classes to manage vehicle information and rental operations, we can easily implement various functions of the rental management system. Through step-by-step design and testing, we can easily extend and improve this simple system. I hope this article will help you write a car rental management system.
The above is the detailed content of How to write a simple car rental management system using C++?. For more information, please follow other related articles on the PHP Chinese website!