For this example project, we will expand on the Simple Vending Machine explained in Chapter 4 using what we have learned so far.
In summary, we want our new vending machine example to:
-
Support multiple products instead of a single product.
-
Provide a Command Line Interface (CLI) such that our users can interact with it.
For the purpose of this example we want our vending machine to be separated into multiple classes.
We’ll be creating 6 classes in the following sections that will model different parts of our example project:
1) A ProductCatalog
class
As we’ll now support multiple products, we’ll introduce the concept of a catalog:
The ProductCatalog
is an Interface that defines a getProducts()
method that returns the set of available products.
2) An InMemoryProductCatalog
implementation
Our ProductCatalog
is an interface as defined above, so we’ll need a concrete implementation.
For the purpose of the example we’ll use an In-Memory
version, but we’ll talk a bit about some of the benefits of doing this separation in the upcoming sections of this chapter.
3) A ProductDetail
class
This represents the types of products available in the ProductCatalog
.
Our ProductDetail
will define a product code, a price and a product name. We’ll model it as an immutable class.
For example, you can think of a filled ProductCatalog
like this, where each row is a ProductDetail
instance:
code | name | price |
---|---|---|
001 |
Potato Chips |
2.50 |
002 |
Soda |
3.00 |
003 |
Cookies |
1.50 |
004 |
Gummy Bears |
2.00 |
005 |
Apple |
2.50 |
4) The VendingMachine
class
This represents our main piece of functionality based on our requirements. We’ll base it upon the VendingMachine
class that we saw in Chapter 4 but it will have some changes in requirements and properties.
At construction time, the VendingMachine
should:
-
Receive a
ProductCatalog
that lists the different products that theVendingMachine
can sell. It can’t be changed once theVendingMachine
is created. -
The
VendingMachine
should throw anIllegalArgumentException
at construction time if:-
The
ProductCatalog
isnull
or if it returns anull
or an empty set of products. -
If any of the products provided exceeds 1000 as its price.
-
Once the VendingMachine
is created, it should support the following operations:
-
Customers can add money to the vending machine (same as in Chapter 4).
-
Customers can request change if available (same as in Chapter 4).
-
The vending machine can return the current balance (same as in Chapter 4).
-
Customers can ask the vending machine to deliver an item given the product code. The vending machine will deliver the item if there is enough balance and will deduct the money from the current balance. If there is not enough balance, the vending machine should return false and the balance should remain the same.
For the purpose of this example project, we’ll make the maximum balance 1000
instead of the 100
in chapter 4. Also, for simplicity, we’re going to assume that there is an unlimited supply of all products defined in the ProductCatalog, so there’s no need to keep track of inventory levels.
-
The
VendingMachine
should provide agetProductCatalog()
method that returns the catalog that the vending machine was created with.
5) A Command Line Interface (CLI)
We’ll create a class that is responsible of managing the input/output of our CLI. Our CLI
will receive the VendingMachine
object that we want it to interact with at construction time.
This CLI should allow a user to perform the following operations and should display the following details:
-
Add money.
-
Request change.
-
Deliver a product given a product code.
-
In the "main menu" of the CLI the "current balance" and the list of available products should always be displayed.
-
The CLI should be a separate class or set of classes from the main classes mentioned in items 1-3.
6) Our Application entry-point
Finally, we want to put everything together and create an VendingMachineApp
class that will create the required objects and put all the things together.
As such, we’ll create a VendingMachineApp
class that contains the main method that ties everything together and starts the CLI
:
. Inside the VendingMachineApp
we’ll create the InMemoryProductCatalog
with 5 dummy products.
. Afterwards instantiate the VendingMachine
passing in the catalog.
. Then create the CLI
that makes use of that VendingMachine
object.
There are multiple ways to approach this. We’ll present an approach with the purpose of covering different concepts from the previous chapters. However, if you come up with a slightly different approach that’s fine! |