11:45 .ll online.claflin.edu Since we are not meeting Friday, March 27, you must fill out your attendance assignment in your lecture section on Moodle. There is now a new category called Friday Attendance. Please go there and do the attendance assignment to be marked for attendance. Notes: Carefully read and adhere to coding standards – pay particular attention to the use of braces and indentation Choose meaningful identifiers for all variables (use names that make sense). Use blank lines here and there to separate logical steps in your code Add comments to document your code. Comments are not optional and must be included. Use a one-line comment before each block to describe what you are doing – don't describe the code syntax, but rather the purpose of the code. These comments should effectively describe the steps in your "algorithm." Spell things carefully, including capitalization, since Java is fussy about that Check your results! Testing your code is IMPORTANT! Write a program for a coffee vending machine. The user will need to choose what type of roast and what type of flavor. You should have at least 3 choices, but may use more. Use 2 switch statements for setting the roast (1 switch) and flavor (1 switch) and then let your user know what you made for them. Rather than having print statements within your switch statements, you'll be setting values to Strings. You declare and initialize a String variable the same way you do a primitive data type. String coffee = "cappuccino"; or initialize to an empty String like this String coffee = ""; Depending on what the user enters, you can assign a value to a String and then use your String variables in your print statement. Be creative. How can you deal with the issue of someone not entering an asked for number? My output in example B handles it but not very well. You have knowledge of if statements and while loops. If they can help you to make a better program, then use them. You still have to use 2 switch statements but make your program a good program. Remember to test all possibilities. Your output should look similar to the output on the next page, but you can be creative. Page 1 of 2 CSCI 206 Lab Date: 27 March 2020 Due: 2 April 2020 Please submit your lab on Moodle. Lab 8 – Coffee Switches Output example A: Welcome to my Coffee Machine What type of of coffee would you like? Enter 1 for dark roast, 2 for light roast, or 3 for decaffeinated: 1 What flavor would you like? Enter 1 for mocha, 2 for caramel, or 3 for no flavor: 2 Here is your dark roast with caramel! Come again soon! Output example B: Welcome to my Coffee Machine What type of of coffee would you like? Enter 1 for dark roast, 2 for light roast, or 3 for decaffeinated: 2 What flavor would you like? Enter 1 for mocha, 2 for caramel, or 3 for no flavor: 4 Sorry, cannot understand your order. Here is your order with no order given! Come again soon!
- Import the required Scanner class from java.util package for taking user inputs.
- Declare the required variables like choice, coffee type, flavour, flags for checking valid input for coffee type and flavour.
- Initially input is taken for coffee type within a while loop. Loop goes on till user enter valid value in range 1-4 ,4 is to exit the loop and default case handle the case if any choice other than 1-4 is entered again input is taken for coffee type.
- Based on valid case for coffee type the flag for valid coffee type is set to true.
- If coffee type is valid then input for coffee flavour is received from user in another while loop. Same logic is applied here as in coffee type if valid flavour is entered the flag for flavour is set to true.
- If invalid coffee type is entered then input for flavour is not taken.
- At the end the message is given if its invalid input for coffee type or flavour.
import java.util.Scanner;
public class CoffeeVendingMachine {
public static void main(String[] args) {
//required for taking user input
Scanner sc = new Scanner(System.in);
//choice value is 5 so that while loop executes first time
int choice = 5;
//declare the coffeetype anf flavor variables
String coffeeType = "",flavor = "";
//flags for checking valid coffeetype and flavor
boolean validCoffeeType = false, validFlavor = false;
System.out.println("Welcome to my Coffee Machine");
//loop will go till input is less than 4
//default case handles it
while(choice > 4) {
System.out.println("What type of Coffee would you like?");
System.out.println("Enter 1 for dark roast, 2 for light roast, or 3 for decaffeinated: ");
//take user input for coffee type
choice = sc.nextInt();
//move cursor to next line
sc.nextLine();
//set the coffeetype
switch(choice) {
case 1: //dark roast
coffeeType = "dark roast";
validCoffeeType = true;
break;
case 2: //light roast
coffeeType = "light roast";
validCoffeeType = true;
break;
case 3: //decaffeinated
coffeeType = "decaffeinated";
validCoffeeType = true;
break;
case 4: //exit the coffeetype input
//if coffeetype is not entered yet
if(coffeeType.equals(""))
coffeeType = "no order";
break;
default ://invalid input
System.out.println("Invalid input ofr coffeType");
coffeeType = "no order";
break;
}
}
//if coffee type is valid only then take flavor input
if(validCoffeeType) {
//rest choice to some higher value so that loop executes the first time
choice = 5;
//check choice for coffee flavor
while(choice > 4) {
System.out.println("\nWhat flavour would you like?");
System.out.println("Enter 1 for mocha, 2 for caramel, or 3 for flavor: ");
choice = sc.nextInt();
sc.nextLine();
//set the coffee flavor
switch(choice) {
case 1://mocha flavor
flavor = "mocha";
validFlavor = true;
break;
case 2://caramel flavor
flavor = "caramel";
validFlavor = true;
break;
case 3: //no flavor
flavor = "no flavor";
validFlavor = true;
break;
case 4: //exit the loop
if(flavor.equals(""))
flavor = "no order";
break;
default://invalid flavor
System.out.println("Invalid input for flavor");
//any random name for identifying invalid flavor
flavor = "no order";
}
}
}
//check if flavor is valid as well
if(validFlavor) {
System.out.println("\nHere is your "+coffeeType+" with "+flavor);
}else {
System.out.println("\nSorry, cannot understand your order.");
System.out.println("Here is your order with no order given!");
}
System.out.println("Come again soon!");
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images