Vending Machine (Java)

From LiteratePrograms

Jump to: navigation, search

This is a Java program which simulates the behaviour of a vending machine that issues drinks and food to customers.

  • The price of all of the items that are in a given machine is the same.
  • Users pass values to the program which represents users inserting coins to purchase an item.
  • The vending machine keeps note of the amount of money it has received since it was initiated and the balance of a customer's coins.
  • Customers can have a refund of their money.

This is a good program for learning the basics of Java, as it introduces the ideas of fields, local variables, constructors, accessor and mutator methods and conditional statements.

Contents

Defining the Class

When naming a class it is important to have a realistic name so it is easy to identify what the class does (to yourself and others who may be viewing the program). It is not a good idea to give the class a name such as MyClass as it tells us nothing about a class and its behaviour. This class is called VendingMachine, as it identifies the behaviour described above.

<<VendingMachine.java>>=
public class VendingMachine
{
   class body
}

Declare the Vending Machine Fields

We first define the fields that our class will use to do its work. Fields essentially store data for each object to use. As the values of these fields are going to vary over time they are known as variables. The three variables that VendingMachine is going to be using are:

<<Declare the vending machine fields>>=
private int itemPrice;

which stores the price of a can of fizzy drink;

<<Declare the vending machine fields>>=
private int currentBalance;

which stores the amount of money in pence that the customer has entered before ordering the can of fizzy drink; and

<<Declare the vending machine fields>>=
private int totalCollected;

which stores a record of the amount of money in pence that the machine has received since it has been initialized.

Notice that the each of these fields are private. This means that they can only be used in this class and are not visible to other classes. They are also of type int, or integer, which means they can store single whole number values, which is handy because we are dealing with values that represent coins which of course have whole number values.

Creating new vending machines - the Constructor

After successfully defining the variables that will be used in the class we must use them to simulate the behaviour of a vending machine. Firstly we must set up the variables used by the class. This is also known as initialization and is performed in the constructor of our VendingMachine class.

The initialisation of the currentBalance and totalCollected fields is straightforward as we simply wish to give them a sensible starting value. Because a newly installed vending machine will initially contain no money, a suitable initial value in in this case is zero:

<<Initialise fields>>=
currentBalance = 0;
totalCollected = 0;

The itemPrice field, however, is not so simple. Because we don't know in advance the price that a given organization will want to charge for an item from its vending machine, we must wait until a machine has been "installed" before we can set the initial value of the itemPrice field. It is also possible that we may wish to create more than one machine that issues a different item at a different price. This means that we can't initialize the value of itemPrice to a given set value. We have to defer this decision until we install the machine. In terms of our simulation, this means that we want to set the price when we create a new vending machine object:

<<<a name="chunk def:test vending machine" href="#chunk use:test vending