Menu

In this program, you will make a vending machine simulator using methods. The vending machine will have several methods, each of which is in charge of a specific task. The program will repeat until th…

Question

Answered step-by-step

In this program, you will make a vending machine simulator using…

In this program, you will make a vending machine simulator using methods. The vending machine will have several methods, each of which is in charge of a specific task. The program will repeat until the user terminates it by selecting the right option. (Note that these methods must be static. For example, public static void printBanner. You have to decide the number of parameters, type of each parameter, and the return value of each method based on the purpose of the method.

 

  • printBanner: This method will accept one integer argument to determine the number of rows of asterisks. The following example is when the argument is 2 (two rows above and below the text).
****************************************
****************************************
******* IST 140--Vending Machine *******
****************************************
****************************************
  • printBalance: This method will print the current balance
  • deposit: This method will take deposit and update the current available balance
  • purchase: Make two arrays of the same size in this method. One called products and the other called prices. Add five items to the products array and their corresponding prices to the prices array. The method will prompt uses for which item they want to purchase, check if the balance is sufficient, and output either insufficient balance or the item being purchased.

Make a new Java project and make sure “Make project from template” is UNCHECKED). Use a meaningful name such as M6-PA-VendingMachine. Then makee a new Java class called VendingMachine in the src folder. Add a main method to this class and then make all the required methods. Call those methods in main or in another method so that the program behaves like the sample run.

 

Sample Run:

(Note that I am only using 3 items to shorten the output. You need 5 items in your program)

****************************************
******* IST 140--Vending Machine *******
****************************************
Current balance is: $0.00

*** Main Menu ***
Please select an option:
1) Deposit
2) Purchase
3) End
2

*** Purchase Menu ***
Current balance is: $0.00
1)Chips ($1.50)
2)Soda ($2.50)
3)Snack bar ($2.00)
Which item do you want to buy? 1
!!! Insufficient balance !!!
Current balance is: $0.00

*** Main Menu ***
Please select an option:
1) Deposit
2) Purchase
3) End
1

*** Deposit Menu ***
Please enter the deposit amount: 20
Current balance is: $20.00

*** Main Menu ***
Please select an option:
1) Deposit
2) Purchase
3) End
2

*** Purchase Menu ***
Current balance is: $20.00
1)Chips ($1.50)
2)Soda ($2.50)
3)Snack bar ($2.00)
Which item do you want to buy? 3
Here is your Snack bar
Current balance is: $18.00

*** Main Menu ***
Please select an option:
1) Deposit
2) Purchase
3) End
2

*** Purchase Menu ***
Current balance is: $18.00
1)Chips ($1.50)
2)Soda ($2.50)
3)Snack bar ($2.00)
Which item do you want to buy? 2
Here is your Snack bar
Current balance is: $15.50

*** Main Menu ***
Please select an option:
1) Deposit
2) Purchase
3) End
3
You receive $15.50

 

Requirements

  • Must have 5 items in the products array
  • Must repeat until the user ends it
  • All currency values must be printed with a dollar sign and with 2 decimal places.

Thoughts

  • Do one method at a time. First, make sure that the printBanner works, then the printBalance, etc.
  • The two arrays in the purchase method is called parallel arrays. They are used to store different characteristics of data. In this case, the characters are names and prices.
  • When you design a method, you should know that you are limited to only the arguments that are provided to the method regarding to the data that is available to the method.

 

 

OR

Leave a Reply

Your email address will not be published. Required fields are marked *