Design a Vending Machine class that can keep track of the type of sodas and numb
ID: 3634156 • Letter: D
Question
Design a Vending Machine class that can keep track of the type of sodas and number of sodas in stock. Use the following UML class diagram to design the class VendingMachine.
VendingMachine
-NUMPRODUCTS : final int = 6
-brand : String[]
-inStock : int[]
+VendingMachine( )
+getBrand(subscript : int) : String
+setBrand(subscript : int, newBrand : String): void
+getInStock(subscript : int) : int
+setInStock(subscript : int, newNumInStock : int): void
+decrementStock(subscript : int) : Boolean
+totalCans( ) : int
+displaySodas( ) : void
+searchIndexOfBrand(brandName : String) : int
Program requirements and/or constraints:
• You will need to create a file to represent this class – VendingMachine.java.
• The size of the arrays to be used will be 6, and you can use a constant NUMPRODUCTS
to express this. In your class, you declare it as:
public class VendingMachine
{
private final int NUMPRODUCTS = 6;
……
}
• The constructor of the VendingMachine class needs to initialize the content of two arrays, brand and inStock. They should have the following content:
----------------------------- Soda Type Stock
-----------------------------
1. Coke 10
2. Orange 8
3. Dew 6
4. Ice Tea 4
5. Water 2
6. Pepsi 0
Thus, the array brand contains “Coke”, “Orange”, etc., and the array inStock contains 10,
8, 6, etc. Note that the array subscript/index starts with 0.
• Write the definitions of the methods of VendingMachine: set and get methods for brand array and inStock array. The getBrand method takes a subscript/index of the array as its parameter, and returns the string at that subscript/index. The setBrand method takes a subscript/index of the array and a brand name (String) to be set, then sets that brand name to the array element of that subscript/index. The similar functionalities apply to
setInStock and getInStock methods.
• The decrementStock method takes a subscript/index as its parameter, and if the number of the soda brand in the inStock array at that subscript/index is at least one, then decrement it and return true. It should return false otherwise.
• The totalCans method computes the total number of cans by going through the inStock array, and returns it.
• The displaySodas method prints out the content of the arrays in the following format
(note that stock numbers or types can change depending on the array contents):
----------------------------- Soda Type Stock
-----------------------------
1. Coke 10
2. Orange 8
3. Dew 6
4. Ice Tea 4
5. Water 2
6. Pepsi 0
• The searchIndexOfBrand method takes a brand name (String) as its parameter and searches at which subscript/index where brand name is located in the brand array. It should return -1 if it is not found in the array.
Your assignment is to write a complete Java program which will produce the sample run shown.
You also need to create Assignment8.java file that contains a main method. Your main program will do the following: Declare and instantiate a VendingMachine object.
You will first display a menu to access the vending machine. Below are the menus and the starting number of cans for each product.
Vending Machine Main Menu
----------------------------- A. Get Soda - $ 0.65
B. Change Brand
C. Restock Soda
D. Leave This Machine
-----------------------------
Ask a user to choose one of A, B, C, or D and read in the choice, then perform their corresponding functionality.
The following is an explanation of each menu choice:
A. Get Soda : This menu choice displays the vending machine. It lists out the 6 different sodas available and how many are in stock.
----------------------------- Soda Type Stock
-----------------------------
1. Coke 10
2. Orange 8
3. Dew 6
4. Ice Tea 4
5. Water 2
6. Pepsi 0
Then ask a user to choose a soda between 1 and 6. After a soda is chosen, then you need to decrease the number of that soda brand. If there is no can left for that brand (e.g., in the above example, Pepsi), then display the message such as “No Pepsi left. Make another selection.“, and ask a user to choose a soda between 1 and 6. This needs to be repeated until a user gets a can of existing soda.
B. Change Brand: Display the vending machine again, just like the choice A. This allows the user to swap out one soda for a different brand of soda. The user will be prompted for the new soda's name and the name of the old soda to be replaced. If the user does not type in the exact name of the old soda, an error message such as “Sprite is not on the list. Make a different selection.” will display, because the program will not be able to match the names. This needs to be repeated until a user enters an existing soda brand name in the machine. Once an existing soda brand name and the new brand name are entered, then the new brand name should replace the old one. The new soda will begin with five (5) cans of soda.
C. Restock Soda: The user will type in the name of the soda to be restocked. If the user does not type in the exact name of the old soda, an error message such as “Sprite is not on the list. Make a different selection.” will display, because the program will not be able to match the names. If there is a soda name match, the user can restock the soda with 20 cans (no matter what was the previous number of cans).
D. Leave this menu: Leaves the menu and quit the program. The program should the total number of sodas as “Total number of drink: 34” before the program quits.
All user input must be error checked. If it is out of range, an appropriate error message must be generated and the user must be prompted again. String input is case sensitive while character input is case insensitive. (See the Sample Run below.)
Sample runs: User input is represented in bold.
Vending Machine Main Menu
----------------------------- A. Get Soda - $ 0.65
B. Change Brand
C. Restock Soda
D. Leave This Machine
----------------------------- Enter your menu choice (A - D): A
----------------------------- Soda Type Stock
-----------------------------
1. Coke 10
2. Orange 8
3. Dew 6
4. Ice Tea 4
5. Water 2
6. Pepsi 0
Enter your choice:
1
Vending Machine Main Menu
----------------------------- A. Get Soda - $ 0.65
B. Change Brand
C. Restock Soda
D. Leave This Machine
----------------------------- Enter your menu choice (A - D): A
----------------------------- Soda Type Stock
-----------------------------
1. Coke 9
2. Orange 8
3. Dew 6
4. Ice Tea 4
5. Water 2
6. Pepsi 0
Enter your choice:
6
No Pepsi left. Make another selection. Enter your choice:
2
Vending Machine Main Menu
-----------------------------
A. Get Soda - $ 0.65
B. Change Brand
C. Restock Soda
D. Leave This Machine
-----------------------------
Enter your menu choice (A - D):
a
----------------------------- Soda Type Stock
-----------------------------
1. Coke 9
2. Orange 7
3. Dew 6
4. Ice Tea 4
5. Water 2
6. Pepsi 0
Enter your choice:
5
Vending Machine Main Menu
----------------------------- A. Get Soda - $ 0.65
B. Change Brand
C. Restock Soda
D. Leave This Machine
----------------------------- Enter your menu choice (A - D): b
----------------------------- Soda Type Stock
-----------------------------
1. Coke 9
2. Orange 7
3. Dew 6
4. Ice Tea 4
5. Water 1
6. Pepsi 0
Enter the beverage to change:
Dew
Enter the new brand:
Sunkist
Sunkist replaces Dew and is stocked with 5 cans.
Vending Machine Main Menu
-----------------------------
A. Get Soda - $ 0.65
B. Change Brand
C. Restock Soda
D. Leave This Machine
-----------------------------
Enter your menu choice (A - D):
c
Enter the beverage name:
Water
Water is restocked with 20 cans. Vending Machine Main Menu
----------------------------- A. Get Soda - $ 0.65
B. Change Brand
C. Restock Soda
D. Leave This Machine
----------------------------- Enter your menu choice (A - D): a
----------------------------- Soda Type Stock
-----------------------------
1. Coke 9
2. Orange 7
3. Sunkist 5
4. Ice Tea 4
5. Water 20
6. Pepsi 0
Enter your choice:
4
Vending Machine Main Menu
----------------------------- A. Get Soda - $ 0.65
B. Change Brand
C. Restock Soda
D. Leave This Machine
----------------------------- Enter your menu choice (A - D): c
Enter the beverage name:
Pepsi
Pepsi is restocked with 20 cans.
Vending Machine Main Menu
----------------------------- A. Get Soda - $ 0.65
B. Change Brand
C. Restock Soda
D. Leave This Machine
----------------------------- Enter your menu choice (A - D): c
Enter the beverage name:
Dew
Dew is not on the list. Make a different selection.
Enter the beverage name:
Coke
Coke is restocked with 20 cans. Vending Machine Main Menu
----------------------------- A. Get Soda - $ 0.65
B. Change Brand
C. Restock Soda
D. Leave This Machine
----------------------------- Enter your menu choice (A - D): A
----------------------------- Soda Type Stock
-----------------------------
1. Coke 20
2. Orange 7
3. Sunkist 5
4. Ice Tea 3
5. Water 20
6. Pepsi 20
Enter your choice:
4
Vending Machine Main Menu
----------------------------- A. Get Soda - $ 0.65
B. Change Brand
C. Restock Soda
D. Leave This Machine
----------------------------- Enter your menu choice (A - D): b
----------------------------- Soda Type Stock
-----------------------------
1. Coke 20
2. Orange 7
3. Sunkist 5
4. Ice Tea 2
5. Water 20
6. Pepsi 20
Enter the beverage to change:
Dew
Dew is not on the list. Make a different selection.
Enter the beverage to change:
Coke
Enter the new brand:
Sprite
Sprite replaces Coke and is stocked with 5 cans.
Vending Machine Main Menu
-----------------------------
A. Get Soda - $ 0.65
B. Change Brand
C. Restock Soda
D. Leave This Machine
----------------------------- Enter your menu choice (A - D): a
----------------------------- Soda Type Stock
-----------------------------
1. Sprite 5
2. Orange 7
3. Sunkist 5
4. Ice Tea 2
5. Water 20
6. Pepsi 20
Enter your choice:
1
Vending Machine Main Menu
----------------------------- A. Get Soda - $ 0.65
B. Change Brand
C. Restock Soda
D. Leave This Machine
----------------------------- Enter your menu choice (A - D): d
Total number of drinks: 58
Explanation / Answer
package assignment7_main; import java.util.Scanner; import javax.swing.*; import java.text.DecimalFormat; /** * * */ public class Assignment7_main { /** * */ public static void main(String[] args) { //Create Scanner Scanner input = new Scanner(System.in); DecimalFormat money = new DecimalFormat("$0.00"); System.out.print("Soda's cost: $" + vendingMachine.getSodaCost() + " Enter payment: "); float sodaPayment = input.nextFloat(); if (sodaPayment >= .65){ vendingMachine.setSodaPayment(sodaPayment); int sodaCount = vendingMachine.getSodaCount(); System.out.print(sodaCount + " " + money.format(vendingMachine.getChange())); if (sodaCount == 0) { vendingMachine.restock(); } //added what to do if the count is 0 } else System.out.println("Invalid amount"); } } class vendingMachine{ //Create field to store soda count private static int sodaCount = 50; //Create field to store soda cost private static float sodaCost; //Create field to store soda payment public static float sodaPayment; public vendingMachine(){ sodaCount--; } public vendingMachine(float newSodaPayment){ sodaPayment = newSodaPayment; sodaCount--; } //Return soda payment public static float getSodaPayment(){ return sodaPayment; } //Set soda payment public static void setSodaPayment(float newSodaPayment){ sodaPayment = (newSodaPayment >= .65) ? newSodaPayment : 0; } //Return soda count public static int getSodaCount(){ /*for (int i = 0; i <= 50; i++){ sodaCount = i; }*/ //commented the above out sodaCount--; return sodaCount; } //Return soda cost public static float getSodaCost(){ return sodaCost = .65f; } //Return method for calculating change public static float getChange(){ float change = 0; if (sodaPayment != 0){ change = sodaPayment - sodaCost; } return change; } public static void restock(){ for (int i = 0; i <= 50; i++){ sodaCount = i; } } //added restocking }