Vending Machine Program

 A task I was made to do was to create a vending machine using everything that we have learnt so far with C# in Visual Studio. This is how it went:

The code above starts the simulation of  a vending machine and it provides the user with a list of items that they can purchase. In this case, they can purchase products number 1 to 4 as they have a value set to Snickers, Bounty, Twix and KitKat. When the user selects one of the above the program will then read it. I had no problem with creating this section of the code and had ran into no errors when I was building the program.

When you keep on going down to the next part of the program we have created if and else statements for the program to decide which number was selected and when it has found the correct one it will print out the statement that is under that line of code and the price of the item. Furthermore, if they didn't select a number between 1 and 4 then they will get and error printed and that they have to reselect the number. However I had problems whilst coding this part of the simulation. For the else if statements I had just named them if statements and finished the program with an else statement which created a bug with the code leading it to print every statement. However I fixed this issue by only keeping 1 if statement and changing the rest to else if and the last one to an else statement without a condition as it wouldn't run.

I also had a bonus Objective which was to create a money variable to make the items cost money and to see if the user had enough money to purchase any of the products.

I had to provide a variable for each of the products price and a variable for their total money:

The prices I have changed to whole numbers to set them up as an integer to make the program simpler and easier to read. The variables are then added and used within each if statement and made a separate task to see if they have enough money within the program.

This part of the code asks if the amount of money they have is greater than £1 and if that is true then it will remove the price of the product out of their total amount of money that they have and print out a line to tell them that the money was accepted. However, if they didn't have enough then the machine will print that you do not have enough money to purchase this product. Furthermore, the program is a starter program to get me into C# and that it is not that optimized and that most of the sections repeat themselves and it makes the code cluttered. To sum this up, this task was not too difficult and not too easy; I completed the first half of the task with a lack of help and the bonus objective was created with help as I didn't know how to create it.

The program can be found here: Vending Machine Code














Comments

Popular Posts