You are writing code for a souvenir vending machine that produces a wax figure o
ID: 3590110 • Letter: Y
Question
You are writing code for a souvenir vending machine that produces a wax figure of Mickey Mouse is a selection of colors with the purchaser's first name on the base. You must allow the user to enter their first name and choose what color they want from a numbered menu. Their name and color choice are added to an ArrayList of Strings so that the first element is a name, the second a color, the third a name etc. Once their order is placed the screen needs to return to the name prompt for the next customer. A manager can enter"orders123" for the name and screen will display all the orders. A manager can also enter "quit123" for the name in order to shut down the program. If "orders123" is entered, the screen will display the contents of the entire ArrayList as follows Name: Fred Color: red Name: Pebbles Color: purple The program will need main, getName, getChoice, addOrder, and displayOrders methods. The only global variable will be a Scanner object. An ArrayList of Strings will be created in main and passed to methods as needed. main continues looping until the manager chooses to exit. getName has no parameters and returns a String name. It checks to make sure the name is not blank before returning .getChoice has no parameters and returns a String color choice. The method continues prompting until the input matches one of the numbers in the menu addOrder is a void method that has parameters for the name, the color, and the ArrayList of orders. It simply adds the name and color to the ArrayList. displayOrders is a void method that has a parameter for the ArrayList of orders. It loops through the entire ArrayList and outputs the orders as shown above Create the entire project in NetBeans. Pay special attention to the names of the methods, the parameters, and the return datatypes required. Do not deviate from these. Do not rely on global variables, except for the Scanner object. Make sure the whole thing works as described in the first paragraph. (Note: Method comments are not required for the exam.)Explanation / Answer
Given below is the code with output. Please don't forget to rate the answer if it helped. Thank you.
To indent code in eclipse , select code by pressing Ctrl+A and then press Ctrl+i
import java.util.ArrayList;
import java.util.Scanner;
public class VendingMachine {
private static Scanner keybd = new Scanner(System.in);
private static String getName()
{
String name = null;
while(name == null || name.trim().equals(""))
{
System.out.print(" Enter name: ");
name = keybd.next();
}
return name;
}
private static String getChoice()
{
String[] colors = {"red", "blue", "green", "yellow", "purple", "pink", "brown"};
int choice = -1;
while(choice < 1 || choice > colors.length)
{
for(int i = 0; i < colors.length; i++)
{
System.out.println((i+1) + ". " + colors[i]);
}
System.out.print("Which color ? [1-" + colors.length + "]: ");
choice = keybd.nextInt();
}
return colors[choice-1];
}
private static void addOrder(String name, String color, ArrayList<String> orders)
{
orders.add(name);
orders.add(color);
}
private static void displayOrders(ArrayList<String> orders)
{
int i = 0;
while(i < orders.size())
{
System.out.println("Name: " + orders.get(i));
i++;
System.out.println(" Color: " + orders.get(i));
i++;
}
System.out.println("========================");
}
public static void main(String[] args) {
ArrayList<String> orders = new ArrayList<String>();
String name, color;
while(true)
{
name = getName();
if(name.equals("orders123"))
displayOrders(orders);
else if(name.equals("quit123"))
break;
else
{
color = getChoice();
addOrder(name, color, orders);
}
}
}
}
output
Enter name: Fred
1. red
2. blue
3. green
4. yellow
5. purple
6. pink
7. brown
Which color ? [1-7]: 1
Enter name: Pebbles
1. red
2. blue
3. green
4. yellow
5. purple
6. pink
7. brown
Which color ? [1-7]: 5
Enter name: orders123
Name: Fred
Color: red
Name: Pebbles
Color: purple
========================
Enter name: quit123
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.