# Car Wash Design Case Study

## Car Wash Simulation Project

#### Problem Description

The current car wash machine at Squeaky Clean Car Wash requires exactly 4 minutes to complete the regular car wash (including rinse, wax, and dry). The owners are considering upgrading their equipment, though, because they are suspicious that the average wait time for the cars is long enough that they are losing business. They are considering replacing the current machine with one that would take only 3 minutes. The upgrade will be expensive, so they would like to know in advance how much difference it will make.

Construct a simulation of the car wash to determine what the average waiting time would be during a typical day (9 am to 7 pm) using the new equipment. A car arrives at the car wash approximately every four minutes (cars don't arrive exactly four minutes apart: that would be easy to program!). Since the arrival times are uneven, there is often a waiting line. This can be simulated using a queue.

Think about how you might go about simulating this problem. How could you simulate the arrivals, waiting, and washing of the cars at the car wash?

Perhaps it would be useful to think of what can happen in a given minute in the car wash (we'll assume that at most one car can arrive in any given minute, although this is not necessary). In a car wash, you can have cars arriving, cars waiting, and cars being washed. You also have cars changing from one state to another. If a car arrives and there is a car being washed, the arriving car becomes a waiting car. If there is at least one car waiting and the car wash bay becomes available, a waiting car becomes a car being washed.

Some of the questions that are still open are: How will you determine whether a car should arrive in the current minute? (In any given minute, there is a 1 in 4 chance that a car will arrive.) How will you decide whether a car is currently being washed? How will you decide when the machine is empty? How will you represent the waiting time of a car? How will you represent the total waiting time of all the cars for the day? How will you represent the total time that the car wash is open?

#### One Possible Design

First, let's look at the things in the problem description that might turn into objects in the implementation. The "things" (nouns) are:

 car wash machine Squeaky Clean Car Wash minutes car owners car wash bay average waiting time lost business simulation day arrival time waiting line (queue)

Several of these refer to the business as a whole: Squeaky Clean Car Wash and owners. The word simulation covers these, and is more relevant for our program. The possibility of lost business is the reason for the simulation, but not part of it. We are left with car wash machine and car wash bay (which seem to refer to the same thing), car, waiting line (queue), and several objects related to time: minutes, day, arrival time, and average waiting time.

Let's reconsider the problem in more detail. Our primary task is to create a simulation of the car wash for one day that calculates the average waiting time of all the cars. To do that we need to know the total waiting time for all the cars and the number of cars. To calculate the total waiting time we need the individual waiting time for each car, which we calculate from its arrival time (the current time when the car arrives) and the current time when the car leaves the waiting line and enters the car wash bay. Since the time a car spends in the wash bay is measured in minutes, we might as well measure waiting times and the time that the car wash is open (the length of the day) in minutes as well. As mentioned above, if a car arrives every four minutes, on average, then in any given minute there is a one in four chance of a car arriving.

This analysis has introduced several new "things" that did not appear explicitly in the original problem description: the total waiting time for all the cars, the number of cars, the waiting time for each individual car, and the current time. Our new list is:

 Integers: length of day current time arrival time individual waiting time total waiting time average waiting time number of cars Objects: simulation car waiting line (queue) car wash bay

Since the only reason we care about individual waiting times is to determine the total waiting time, we can avoid keeping track of them by adding them to the total waiting time as we go.

What do we know about the responsibilities of the objects listed in our table? In each minute the simulation is responsible for determining whether a car arrives, whether it has to wait on line, whether a car can leave the line, and, if so, how long it waited. To determine this it needs to know the car's arrival time and the current time. This constitutes a single step of the simulation. The simulation must run for as many minutes as the car wash is open. (Actually, the simulation should usually run longer -- what happens if there are cars waiting in line or in the wash bay when the car wash officially closes?) When done, the simulation must calculate the average waiting time.

The waiting line is easy. It is a first-in/first-out (FIFO) data structure of cars and, as mentioned in the problem description, can be modeled as a queue.

What do we need to know about cars? We care about their arrival times and their waiting times. We also care about how long a car spends being washed, but we could argue that that is really a function of the car wash bay rather than of the car since all cars entering the bay will take the same amount of time to be washed. The waiting time can be calculated by the simulation, so the only thing the car really needs to keep track of is its arrival time. In fact, we could eliminate cars altogether in this simulation, and just keep a queue of arrival times. Providing a car abstraction more closely models the problem domain, however, and might be useful in the future if the problem is extended.

Finally, what are the responsibilities of a car wash bay? It should be able to tell the simulation whether it is empty or not, and, if it is not empty, it should keep track of the time until it becomes empty.

At this point, we could restate our understanding of the behavior we plan to simulate using pseudo-code:

```for each minute of the simulation

if a car arrives
increment the number of cars that have arrived
note the arrival time of the car (store this with the car)
add the car to the queue

if the car wash bay is empty and there is a car waiting to be washed
move the car from the queue to the car wash bay
determine how long that car was waiting and add to total wait time
start washing the car (bay will be occupied for next 2 minutes)

otherwise, if there is a car already being washed
decrease the amount of time left before the bay is available
```
Question: is there a case missing here that requires any action?

(For suggestions on implementing this simulation, see the Car Wash Simulation Project).

Author: Autumn C. Spaulding autumn@max.cs.kzoo.edu