Home > Backend Development > C++ > body text

How to write a simple car rental management system using C++?

WBOY
Release: 2023-11-04 12:44:07
Original
1618 people have browsed it

How to write a simple car rental management system using C++?

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:

  1. Add vehicle information: including vehicle ID, vehicle brand, and model , rent, vehicle status, etc.
  2. Query vehicle information: You can query based on vehicle ID, vehicle brand, model and other information.
  3. Rental vehicle: Set the vehicle status to Rental.
  4. Return the vehicle: Set the vehicle status to available for rent.
  5. Statistical rental: Calculate the total rental of vehicles rented within a certain period of time.
  6. Show all vehicle information: Display detailed information of all vehicles.

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.

  1. Create Car class

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;
    }
};
Copy after login
  1. Create the CarRentalSystem class

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
        }
    }
};
Copy after login
  1. Main function

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();
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template