Interactive Meal Kiosk¶
In this lab, we'll take meal orders from the keyboard in the terminal then display the contents of the order with its price.
Step 1: Create a Scanner Instance¶
In the MealKioskConsole
's main()
method, create an instance of Scanner
:
Scanner scanner = new Scanner(System.in);
Importing Scanner
The Scanner
class is located in the java.util.Scanner
package.
Step 2: Display a Prompt and Get Input¶
To display a prompt, use System.out.println()
, for example:
System.out.println("What toppings do you want on your Burger? none, cheese, bacon, avocado");`
To get a line of text from the user, use the nextLine()
method that's on the scanner
object. E.g.:
String input = scanner.nextLine();
Step 3: Convert Input into Choices¶
-
Take the input string with the toppings they wanted and convert it into a
Burger
instance with the toppings added, if any.-
The user can type multiple options, e.g., any of the following would be allowed:
none cheese bacon, avocado
-
You do not need to support multiple toppings of the same type, i.e.,
cheese, cheese
will only add a single cheese topping and not two. -
You should let them type their toppings in any order, e.g.,
bacon, cheese
is the same ascheese, bacon
.Substring Search
The
String
class has acontains()
method that might be helpful. E.g.:if (input.contains("avocado")) { // it has the string avocado }
It's also useful to convert the input to lowercase to make it easier to find, so
String
has atoLowerCase()
method. E.g.:String input = scanner.nextLine(); input = input.toLowerCase();
-
-
Ask for the Drink size (regular or large) and create a
Drink
instance of the appropriate size.
At this point you will have a Burger
and a Drink
that you can add to the MealOrder
-
Instantiate a
MealOrder
and add the burger and drink using a new method,addItem
, which method would look like:public void addItem(MenuItem menuItem) { items.add(menuItem); }
-
Call the
display()
method on theMealOrder
instance that you just created. -
To make the
display()
useful, you'll need to add atoString()
method to theToppings
class. A quick way to do it is:@Override public String toString() { return "Toppings{" + "toppings=" + toppings + '}'; }
[Optional] Step 4: Use While Loop¶
Surround your code with a while
loop so you continue to ask for orders until the user exits.
while
Documentation for the while loop (and other loops) can be found here: https://books.trinket.io/thinkjava/chapter7.html
Main Method For Reference¶
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Drink size? (regular, large)");
String size = scanner.nextLine().toLowerCase();
}