Source Code
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Car {
string make;
string model;
string license_plate;
bool available;
};
vector<Car> inventory;
vector<string> rental_history;
// Function to handle user login
bool login() {
string username, password;
cout << "Enter username: ";
cin >> username;
cout << "Enter password: ";
cin >> password;
// Verify username and password against a database or file
// Return true if login is successful, false otherwise
if(username=="admin" && password=="password")
return true;
else
return false;
}
// Function to update a car in the inventory
void update_car() {
string search_term;
bool car_found = false;
cout << "Enter search term (make, model, license plate): ";
cin >> search_term;
for (int i = 0; i < inventory.size(); i++) {
if (inventory[i].make == search_term || inventory[i].model == search_term || inventory[i].license_plate == search_term) {
cout << "Enter new make: ";
cin >> inventory[i].make;
cout << "Enter new model: ";
cin >> inventory[i].model;
cout << "Enter new license plate: ";
cin >> inventory[i].license_plate;
car_found = true;
cout << "Car updated." << endl;
}
}
if (!car_found) {
cout << "Car not found." << endl;
}
}
// Function to delete a car from the inventory
void delete_car() {
string search_term;
bool car_found = false;
cout << "Enter search term (make, model, license plate): ";
cin >> search_term;
for (int i = 0; i < inventory.size(); i++) {
if (inventory[i].make == search_term || inventory[i].model == search_term || inventory[i].license_plate == search_term) {
inventory.erase(inventory.begin() + i);
car_found = true;
cout << "Car deleted." << endl;
}
}
if (!car_found) {
cout << "Car not found." << endl;
}
}
// Function to rent a car
void rent_car() {
string search_term;
bool car_found = false;
string rental_info;
cout << "Enter search term (make, model, license plate): ";
cin >> search_term;
for (int i = 0; i < inventory.size(); i++) {
if (inventory[i].make == search_term || inventory[i].model == search_term || inventory[i].license_plate == search_term) {
if(inventory[i].available) {
inventory[i].available = false;
rental_info = inventory[i].make + " " + inventory[i].model + " " + inventory[i].license_plate;
rental_history.push_back(rental_info);
car_found = true;
cout << "Car rented." << endl;
} else {
cout << "Car not available" << endl;
}
}
}
if (!car_found) {
cout << "Car not found." << endl;
}
}
// Function to return a rented car
void return_car() {
string search_term;
bool car_found = false;
cout << "Enter search term (make, model, license plate): ";
cin >> search_term;
for (int i = 0; i < inventory.size(); i++) {
if (inventory[i].make == search_term || inventory[i].model == search_term || inventory[i].license_plate == search_term) {
if(!inventory[i].available) {
inventory[i].available = true;
string rental_info = inventory[i].make + " " + inventory[i].model + " " + inventory[i].license_plate;
rental_history.erase(remove(rental_history.begin(), rental_history.end(), rental_info), rental_history.end());
car_found = true;
cout << "Car returned." << endl;
} else {
cout << "Car not rented" << endl;
}
}
}
if (!car_found) {
cout << "Car not found." << endl;
}
}
// Function to view the rental history
void view_rentals() {
if (rental_history.empty()) {
cout << "No rental history found." << endl;
} else {
cout << "Rental History:" << endl;
for (int i = 0; i < rental_history.size(); i++) {
cout << rental_history[i] << endl;
}
}
}
// Main function to handle program flow
int main() {
bool logged_in = false;
int choice;
while (!logged_in) {
logged_in = login();
if (!logged_in) {
cout << "Incorrect username or password." << endl;
}
}
while (true) {
cout << "1. Add Car" << endl;
cout << "2. View Inventory" << endl;
cout << "3. Search Car" << endl;
cout << "4. Update Car" << endl;
cout << "5. Delete Car" << endl;
cout << "6. Rent Car" << endl;
cout << "7. Return Car" << endl;
cout << "8. View Rental History" << endl;
cout << "9. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
add_car();
break;
case 2:
view_inventory();
break;
case 3:
search_car();
break;
case 4:
update_car();
break;
case 5:
delete_car();
break;
case 6:
rent_car();
break;
case 7:
return_car();
break;
case 8:
view_rentals();
break;
case 9:
return 0;
default:
cout << "Invalid choice." << endl;
break;
}
}
return 0;
}
Code Explanation
A car rental system in C++ could involve the following basic functions:
- login(): This function would handle user authentication and ensure that only authorized users are able to access the system.
- add_car(): This function would allow the user to add new cars to the inventory.
- view_inventory(): This function would allow the user to view the current inventory of cars.
- search_car(): This function would allow the user to search for a specific car by make, model, or license plate.
- update_car(): This function would allow the user to update the details of a specific car in the inventory.
- delete_car(): This function would allow the user to delete a specific car from the inventory.
- rent_car(): This function would allow the user to rent a specific car and update the inventory accordingly.
- return_car(): This function would allow the user to return a rented car and update the inventory accordingly.
- view_rentals(): This function would allow the user to view the rental history.
- main(): This function would handle the overall flow of the program and call the appropriate functions based on user input.
It’s important to consider the security and the error handling of the system.
Explain Header files
#include <iostream>
– This library provides basic input/output operations in C++, such as cout
and cin
for printing to the console and reading user input, respectively.
#include <string>
– This library provides string handling functionality in C++, such as the ability to create, manipulate, and compare strings.
#include <vector>
– This library provides a dynamic array implementation in C++, called a vector. Vectors allow for dynamic resizing and easy access to elements.
#include <algorithm>
– This library provides a variety of useful algorithms, such as sorting, searching, and manipulating ranges of elements. In the code above, the remove
and erase
algorithms are used for removing elements from the rental history vector.
Including these libraries in the program provides a lot of functionality and allows the developer to write more efficient and less error-prone code.
Leave a Reply