# Chapter 15 - Worked Exercises 1

## Exercise 1 - Restaurant Queuing System

In this exercise you will create a restaurant queuing system. This system will allow you to add new customers (with their phone numbers to the back of a queue). When a space in your restaurant is made available, you would use this system to get the name and phone number of the next customer in the queue.&#x20;

You are to use the `std::deque` container for this application.&#x20;

{% embed url="<https://en.cppreference.com/w/cpp/container/deque>" %}

### Part 0 - Customer

A customer should have a name and a phone number, and have a method `sendMessage()` that just prints:&#x20;

"Sending a message to <`name`>"&#x20;

In a real application you could imagine this method actually sending a message to the customer, letting them know that they should return to the restaurant as a seat is now available for them.

### Part 1 - Queuing System

You are provided with the following class representing the queuing system:

```cpp
class QueuingSystem {   
private:
    std::deque<Customer*> queue;
    
public:
    void addToQueue(Customer* customer) {
        // To be implemented 
    }
    
    Customer* getNextCustomer() {
        // To be implemented 
    };
};
```

Implement the `addToQueue` function that takes a pointer to a customer as a parameter, and adds the customer to the back of the queue. Every time a new customer is added, the queuing system should print the expected wait time for the customer; this is 10 minutes multiplied by the number of other customers in the queue.&#x20;

Implement the `getNextCustomer` function, which returns a pointer to the next customer to be served, and removes the customer from the queue. The function should return a `nullptr` if the queue is empty.

{% hint style="info" %}
Member functions of `deque` that I expect you to use:

* push\_back
* pop\_front / front
* size / empty
  {% endhint %}

### Part 2 - Creating a Driver Program

Using the `QueuingSystem` class you have made in Part 1, can you create an actual restaurant booking application?&#x20;

As an example (you are free to implement this however you wish):

```
Restaurant queuing application:
[1] - Add a new customer
[2] - Get the next customer 
[3] - Exit
Enter a command: 1
What is the name of the customer? Alice
What is the customer's phone number? 012345
Customer is added to the queue! Expected wait time: 10 minutes
Enter a command: 1
What is the name of the customer? Bob
What is the customer's phone number? 98765
Customer is added to the queue! Expected wait time: 20 minutes
Enter a command: 2 
Next customer to serve is: Bob (tel: 98765)
Enter a command: 3
Goodbye!
```

## Exercise 2 - Grocery Store Inventory System&#x20;

In this exercise you will create an inventory system for a grocery store. This system will allow you to add new products to the grocery store's inventory, and later query the inventory for the price of a particular product.&#x20;

You are to use the `std::map` or `std::unordered_map` data structure to help you with this application. (The member functions for both classes are nearly identical)

{% embed url="<https://en.cppreference.com/w/cpp/container/map>" %}

### Part 0 - Inventory System Class

You are given the following class for the inventory system:

```cpp
class InventorySystem {
private:
    std::unordered_map<std::string, double price> pricesByProduct;
public:
    bool addProduct(std::string product_name, double product_price) {
        // To be implemented 
    }
    
    void setProduct(std::string product_name, double product_price) {
        // To be implemented
    }
    
    void showAllProducts() {
        // To be implemented 
    }
    
    double getPrice(std::string product_name) {
        // To be implemented 
    }
};
```

The `pricesByProduct` member variable, as the name implies, stores the prices of all the products, keyed by their product names; for example `pricesByProduct["eggs"]` would return the price for eggs.&#x20;

### Part 1 - Methods&#x20;

Implement the `addProduct` function, which adds a new product to the `pricesByProduct` container and returns true only if the product with the same name doesn't already exist in it. If a product with the same name already exists, the function does nothing and returns false.&#x20;

Implement the `setProduct` function, which adds a new product to the `pricesByProduct` container. If the product with the same name already exists, it should update it to the value that was just provided, and print a statement: `The price of 'eggs' was updated to 1.40`.

Implement the `getPrice` function, which simply returns the price of a product given a product name. If no matching product name was found, the function should return 0.&#x20;

Implement the `showAllProducts` function, which prints out the prices of all the products in the `pricesByProduct` container:

```
eggs: $1.40
milk: $1.80
cheese: $2.20
flour: $2.00
```

{% hint style="info" %}
Member functions of `map` or `unordered_map` that I expect you to use:

* insert&#x20;
* insert\_or\_assign
* size&#x20;
* contains / find
  {% endhint %}

### Part 2 - Creating a Driver Program&#x20;

Similar to Exercise 1, could you use the `InventorySystem` class that you created in Part 1 to build an inventory management program?&#x20;

As an example (you are free to implement this however you wish):

```
[1] - Add a new product
[2] - Update a new product
[3] - Get the price of a product
[4] - Exit 
Enter a command: 1
What is the name of the product? eggs
What is the price of 'eggs'? 1.40
Product has been added.
Enter a command: 3
What is the name of the product? eggs
The price of 'eggs' is $1.40.
Enter a command: 4
Goodbye!
```

## Exercise 3 - Wildlife Cataloguing System

A group of researchers are set out to identify how many unique species of animals are habituating a certain preservation area; and you are to create a system to keep track of this. &#x20;

The researchers are only interested in cataloguing the **unique** number of species, and not the frequency of which they've seen them. Due to the sheer number of animal sightings, its hard to keep track of which animals they have or have not seen, and so they will be entering the name of every species they encounter into your program.

You are to use the `std::set` or `std::unordered_set` container to help you with this task.&#x20;

{% embed url="<https://en.cppreference.com/w/cpp/container/set>" %}

### Part 0 - Catalogue System Class

```cpp
class CatalogueSystem {
private: 
    std::unordered_set<std::string> species;
    std::string lastSighted;
public:
    bool addRecord(std::string species_name) {
        // To be implemented 
    }
    
    void removeLast() {
        // To be implemented
    }
    
    void getReport() {
        // To be implemented
    }
};
```

The `species` container is a set that contains a collection of unique strings each corresponding to a species of animal that was spotted during the researchers' excursion.&#x20;

The `lastSighted` member variable contains the name of the species that was last spotted.

### Part 1 - Method

Implement the `addRecord` method that takes in a string parameter corresponding to the name of the species that was spotted. The function adds `species_name` to the `species` container if it doesn't exist within in, and returns true. The function returns false if the species was already previously spotted.&#x20;

The researchers may occasionally mistake a sighting at first glance, and so they need a function to be able to remove the last sighting that was added on the system. Implement the `removeLast` method that does this.&#x20;

At the end of the excursion, the researchers would like to get a report on all the species of animals they have spotted. Implement the `getReport()` function that would provide this:

```
There were 20 unique species spotted:
- Kangaroo
- Koala
- Elephant
...
```

{% hint style="info" %}
Member functions of the `unordered_set` or `set` class that I expect you to use:

* insert
* erase
* size
* contains / find&#x20;
  {% endhint %}

### Part 2 - Creating a Driver Program&#x20;

As with the previous exercises, use the `CatalogueSystem` class to implement a fully featured cataloguing system.&#x20;
