Creating a Vending Machine Program In Java
- f15eagle314
- Jan 14, 2022
- 3 min read
One of my first Java projects was to create a Vending Machine in Java. This was a project that was assigned to me by my Gr.11 Comp Sci teacher, and while he told us to make a simple Vending Machine, I wanted to make it as intricate as possible. One of the requirements for this assignment was to render a picture of a vending machine using print statements. Mine came out as this:

Yes that is a right, I made a vending machine that sells books. I'm a book worm, what did you expect?
Anyways, following the graphic, I was required to create a program that would simulate how a vending machine worked. Simply put, all I needed to do was ask the user for an input, ask the user to input an amount of money, and the output their change. I felt that this was a bit boring and since I had a lot of time to do it, I decided to add more steps to my vending machine such as asking the user to confirm their purchase, allow the user to buy multiple copies of an item with one purchase, and I even made the program print a receipt for the user.
First off I had the user confirming their purchase. I did this through one giant if statement that had multiple if statements buried in it. When the user made a selection, they would be prompted to confirm their selection, if they made a mistake, they would be prompted to restart the program. Here is an excerpt of two of the items located in a giant if statement. Inside the if statement you can see a smaller if statement asking the user to confirm their purchase. This if statement continued for of total of nine items. This took approximately 180 lines of code.

The following part of my program would deal with the check out of the users item. The user would be prompted to input how many items of their chosen selection they would like to buy. The number of items the user would like to buy would then be multiplied by the unit price of the book they wish to buy and that would be presented as the final price. The user would then be asked to input how much money they owe. If the user did not input enough money, then they would be told that they have not paid enough and would be prompted to restart the program. If the user inputted enough money then the transaction would occur and the user would be presented with their change.

Following the transaction, the user would be asked if they would like to receive a receipt. If the user replies with yes, then the program would output a receipt to the user the same way the vending machine was drawn: through print statements. Once the receipt was presented to the user, they would receive a thank you and the program would end. Here is a picture of what the entire program looks like once it has been run and used in the compiler:

As one of my first projects I definitely can say that there was a lot that I could improve in this program. But overall I am satisfied with how it turned out and am proud of it for my first project. I hope you enjoyed shopping at Pete's Book Nook!
Comments