For this assignment you are to write a program to model a (very small) car rental company. This company is so small that it only has three cars to rent. These cars are rented out to customers who drive them for a number of days and then return them. When the customer returns the car they are invoiced for the number of days that they have had it out.

When a car is not currently being rented it is kept in a garage. A car being returned to the garage (after a customer has returned it) is parked behind the cars that are currently there. Those cannot be moved out of the garage until that car has been moved out.

When a customer arrives he/she is assigned the first car in the garage. The company maintains a list of all customers who are currently renting cars.

If all of the cars are currently being rented out the customer is placed on a waiting list. The waiting list is organised so that the customer that has been waiting the longest is the first to be offered a car when one becomes available.

This assignment is broken up into several sections. You will be required to submit program code written in C++ and developed (or at least which runs) in Visual Studio 2008. You must also submit a report (as a Word document) which includes information detailed throughout these specifications.

Section A

One of the important considerations of computing is how to store data so that it is easy to access and maintain. There are many different data structures (also called abstract data types) which have been developed to store and access data in different ways.

Download the classes Queue, Stack and List. These classes have not been commented. Comment these classes yourself in some detail so that it is clear you know what their purpose is. In your report, provide class diagrams for all three of these classes. The class diagram should include the constructors and the get and set functions.

From the description of the program (in the introduction), you can see that there are three different places at which data needs to be stored:

  • The program must keep a record of the cars currently in the garage. The program should ensure that the car most recently parked in the garage is the one assigned to the next customer. A Stack object should be used to simulate the garage in which the cars are parked when they are not rented out.
  • The program must keep a record of the customers on the waiting list. The program should ensure that the customer who has been waiting the longest is assigned the next car to be returned. A Queue object should be used to simulate the waiting list.
  • The program must keep a record of the customers who are currently renting cars. A List object should be used to simulate the list of Customers who are currently renting cars.

In your report, explain why these are appropriate data structures for each of the data stores.

Dynamic Memory

This assignment will expect dynamic memory (the heap) to be used. This will require instances to be created using the operator new, and removed from memory when they are no longer required using the operator delete. This will mean that these instances must be accessed using pointers.

Consider the Stack class provided. In the constructor, three RentalVehicle instances (see Section B) are pushed onto the Stack. Examine the Stack class to see how pointers are used to manipulate the RentalVehicle instances.

This assignment will expect all instances of the following classes to be created in dynamic memory.

  • Stack, List, Queue - see Section A
  • RentalVehicle, Customer - see Section B
  • Manager - see Section C

Section B

The two major data objects for this program should model

  • The cars that the company leases
  • The customers that lease the cars

Implement two classes based on the following class diagrams. It might not be clear at this stage what some of the member functions are required to do. You can implement these as program stubs until it does become clear. See image.

In the class, RentalVehicle, there are two data members called dayOut and dayReturned. These values represent the day that the Vehicle was leased to the Customer and the day the Customer returned the vehicle respectively. The function noOfDaysBorrowed ( ) returns the number of days that the Customer had the car for, including partial days. Thus if a Customer borrows the RentalVehicle on day 2 and returns it on day 2, noOfDaysBorrowed ( ) should return 1 - not 0. If the Customer borrows the Vehicle on day 3 and returns it on day 4 then the function should return 2 - no consideration is taken into whether the Customer had the Vehicle for the full 24 hours or not.

In the class, Customer, the data member rv, represents the Vehicle that has been assigned to the Customer. This value is set to 0 when a Customer is not currently borrowing a Vehicle. This data member is used to help generate an invoice for the Customer when the Vehicle is returned. Note that the data member is a pointer to RentalVehicle.

Section C

Create another class called Manager. This class will run the whole simulation.

Write a public member function for Manager called readInFile. This function will open a text file (called 'input.txt'). This text file consists of a number of lines each represents an action that the program should implement. Each action is determined by the first character of the line, which we will call the 'directive'. A test file called 'input.txt' is provided. However, a different test file will be used to test your program when it is being marked. DO NOT tailor your program so that it is specific to 'input.txt'.

The program should read the directive and, depending upon its value, take the following actions:

Directive 'A'

If the directive is 'A', then a customer has arrived at the company and wishes to hire a car. The program should read the rest of the line which will consist of the customer's name and address (for simplicity's sake, the address consists simply of the customer's town). These two values should be used to create a new Customer instance in dynamic memory (on the heap).

If a car (RentalVehicle) is currently available it is assigned to the Customer. To do this you assign the pointer to the RentalVehicle* data member rv. The Customer pointer should be added to the RentalList. If there is no car available (in other words, if all of the cars are currently being leased) the Customer is added to the waiting list.

irective 'R'

If the directive is 'R' then the RentalVehicle is being returned. The rest of the line consists only of a Customer's name. The program should read the name and use it to find the Customer in the RentalList. You can use a simple linear search to achieve this. The Customer pointer should be first retrieved from the list and an invoice generated. The pointer should then be removed from the list.

Once this is done, the returned car is assigned to the next Customer on the waiting list. If the waiting list is empty the car is returned to the garage.

Generating an invoice

When a Customer returns the Vehicle, an invoice should be automatically generated. This invoice should be sent to an output file generated at the time. There should be a different output file for each invoice generated. The name of the invoice file should consist of:

  • The Person's name followed by
  • The Registration number of the Vehicle followed by
  • the suffix '.inv'

For example, PhilRRR555.inv might be the name of one of the generated invoices.

Each invoice should be pertinent to the actual Rental using the details of the Customer and assigned RentalVehicle. The invoice should have the following format. Text enclosed in &kt; > refers to information that must be retrieved from the relevant object. See image

Directive '1'

If the directive is '1' then the dayCount is incremented. The dayCount is a record of the passing of time. If a Customer borrows a car on day 1 and returns it on day 3 then on the invoice they are billed for three days (days 1, 2 and 3).

The class diagram for Manager is see image

The manager class should also have the following private member functions:

  • void assignCarToCustomer (Customer *cust) - this function is called when the 'A' directive is read. This function will assign a RentalVehicle to the Customer if one is available. Otherwise it will put the Customer on the waiting list.
  • void customerReturnsCar (Customer *cust) - this function is called when the 'R' directive is read. This function will return the RentalVehicle to the garage and generate an invoice charging the Customer. The Car should then be assigned to the next Customer on the waiting list. If the waiting list is empty, send the Vehicle to the garage.
  • void addPersonToWaitingList (Customer *cust) - this function is called by assignCarToCustomer.

All of these functions are used to assist the public member function - readInFile. They are not intended to be accessed by other classes. This is why they are private - not public.

Download the file test.cpp. This file contains the main function and enables the program to execute.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.