Project Completion
Source Code below
CoffeeShop.java
/*
* This is the code for our CoffeeShop class. It will contain
* the major functions outlined in our Project Idea (Phase One) and use the
* techniques described in our Project Plan (Phase Two)
*/
import java.util.Scanner; // for reading user input
import java.util.ArrayList; // for dynamic lists (new technique)
public class CoffeeShop {
public static void main(String[] Args) {
// Variables we use in main declared below
ArrayList allInteractions = new ArrayList();
String userinput = new String(); // this is the user’s input to continue/not continue with the program
Scanner scanner = new Scanner(System.in);
// This is the list of valid inputs that the user can request for
String[] Menu = {
“coffee”,
“soda”,
“chips”,
“bagel”,
“water”,
“apple”,
“doughnut”,
“egg sandwich”,
“muffin”,
};
String[] Sizes = {
“small”,
“medium”,
“large”
};
String[] Places = {
“house”,
“school”,
“classroom”,
“car”,
“beach”,
“park”
};
System.out.println(“Hello and welcome to the coffee shop!”);
do {
OrderFunction(scanner, allInteractions, Menu, Sizes, Places);
// Force user to enter in something that conforms to Y/N here.
userinput = forceYesOrNoInput(scanner, “Would you like to go back to ordering? “);
} while(userinput.equals(“Y”));
System.out.println(“———————“);
System.out.println(“———————“);
System.out.println(“Summarizing all successfully delivered and completed actions today.”);
for(Interaction interaction : allInteractions) {
System.out.println(“——-“);
System.out.println(interaction);
}
System.out.println(“Have a nice day!”);
}
/**
* This is the OrderFunction, which will take in a bunch of orders from the user, then deal with preparations
* and deliveries after.
* @param scanner
* @param allInteractions
*/
public static void OrderFunction(
Scanner scanner,
ArrayList allInteractions,
String[] Menu,
String[] Sizes,
String[] Places
) {
String userinput = new String();
ArrayList currentInteractions = new ArrayList();
// print instructions on how to order.
System.out.println(“———————“);
System.out.println(“Before you order, please make sure that you pick an item from the below menu when prompted: “);
for(String item : Menu) {
System.out.println(item);
}
System.out.println(“And pick the corresponding size as needed: “);
for(String size : Sizes) {
System.out.println(size);
}
System.out.println(“———————“);
do {
Interaction interaction = new Interaction();
System.out.print(“Please select an item from the menu: “);
userinput = scanner.nextLine();
System.out.print(“Please select a size: “);
userinput = userinput + “, ” + scanner.nextLine();
// save the user input
interaction.setOrderDetails(userinput);
currentInteractions.add(interaction);
// Now force user to indicate whether or not to keep giving orders
userinput = forceYesOrNoInput(scanner, “Would you like to keep placing orders? “);
} while(userinput.equals(“Y”));
System.out.println(“———————“);
System.out.println(“We can deliver only to the following places: “);
for(String place : Places) {
System.out.println(place);
}
System.out.println(“———————“);
// Now handle all interactions
for(Interaction interaction : currentInteractions) {
// Send off to preparation
if(PrepareFunction(interaction, Menu, Sizes)) {
// Send off to delivery – have user provide delivery
System.out.println(“Order: ” + interaction.getOrderDetails() + ” was prepared. Now allowing for delivery specification”);
System.out.print(“Please enter delivery details: “);
userinput = scanner.nextLine();
interaction.setDeliveryDetails(userinput);
if(DeliveryFunction(interaction, Places)) {
// We successfully handled this order!
allInteractions.add(interaction);
}
}
}
// Now we’re done!
}
/**
* This is a helper function we’ll use to enforce a “Y/N” input from our user.
* @param scanner
* @return
*/
public static String forceYesOrNoInput(Scanner scanner, String genericQuestion) {
String userinput = new String();
do {
System.out.print(genericQuestion + ” Please enter Y/N to answer: “);
userinput = scanner.nextLine();
} while(!userinput.equals(“N”) && !userinput.equals(“Y”));
return userinput;
}
/**
* This is our prepare function. It will mark the OrderPrepared attribute
* to true if the order appears to be and tack on a message
* @param interaction
* @return
*/
public static boolean PrepareFunction(Interaction interaction, String[] Menu, String[] Sizes) {
// First, test if the interaction is ok – it should be an item from the menu and have a size
// in Sizes
// We know that the interaction is split by a comma and space, so we’ll break it up and parse that way
String[] orderAsArray = interaction.getOrderDetails().split(“, “);
if(orderAsArray.length != 2) {
System.out.println(“Didn’t detect , format!”);
interaction.setOrderPrepared(false);
return false;
}
// We did detect the format, so let’s make sure orderAsArray[0] == a menu item and
// orderAsArray[1] == a size
boolean foundItem = false, foundSize = false;
for(String item: Menu) {
if(item.equals(orderAsArray[0].toLowerCase())) {
foundItem = true;
break;
}
}
for(String size: Sizes) {
if(size.equals(orderAsArray[1].toLowerCase())) {
foundSize = true;
break;
}
}
if(foundItem && foundSize) {
interaction.setOrderPrepared(true);
} else {
System.out.printf(“Either %s was not a menu item or %s is not a sizen”, orderAsArray[0], orderAsArray[1]);
interaction.setOrderPrepared(false);
}
return interaction.getOrderPrepared();
}
/**
* This is our delivery function – it makes sure that the place specified is reachable
* @param interaction
* @param Places
* @return
*/
public static boolean DeliveryFunction(Interaction interaction, String[] Places) {
for(String place : Places) {
if(interaction.getDeliveryDetails().toLowerCase().equals(place)) {
interaction.setOrderDelivered(true);
}
}
if(!interaction.getOrderDelivered()) {
System.out.printf(“The specified place: %s is not valid!n”, interaction.getDeliveryDetails());
interaction.setOrderDelivered(false);
}
return interaction.getOrderDelivered();
}
}
Interaction.java
/*
* This is our custom class for abstracting orderDetails/deliveryDetails from the user.
*/
public class Interaction {
// Under the hood orderDetails/deliveryDetails are stored as strings
private String orderDetails;
private String deliveryDetails;
// Flags to mark if the order has been prepared and if its been delivered.
private boolean orderPrepared;
private boolean orderDelivered;
/**
* Basic constructor for our Interaction class to set private attributes
* to defined default values
*/
Interaction() {
orderDetails = new String();
deliveryDetails = new String();
orderPrepared = false;
orderDelivered = false;
}
/**
* Basic setter for our orderDetails
* @param s
*/
public void setOrderDetails(String s) {
orderDetails = s;
}
/**
* Basic setter for the deliveryDetails
* @param s
*/
public void setDeliveryDetails(String s) {
deliveryDetails = s;
}
/**
* Basic setter for orderPrepared
* @param b
*/
public void setOrderPrepared(boolean b) {
orderPrepared = b;
}
/**
* Basic setter for orderDelivered
* @param b
*/
public void setOrderDelivered(boolean b) {
orderDelivered = b;
}
/**
* Basic getter for the orderDetails
* @return
*/
public String getOrderDetails() {
return orderDetails;
}
/**
* Basic getter for the deliveryDetails
* @return
*/
public String getDeliveryDetails() {
return deliveryDetails;
}
/**
* Basic getter for orderPrepared
* @return
*/
public boolean getOrderPrepared() {
return orderPrepared;
}
/**
* Basic getter for orderDelivered
* @return
*/
public boolean getOrderDelivered() {
return orderDelivered;
}
/**
* Define what printing an interaction object would look like, as a string
* @return
*/
public String toString() {
return “orderDetails: ” + orderDetails + “, isPrepared: ” + orderPrepared + “n” +
“deliveryDetails: ” + deliveryDetails + “, isDelivered: ” + orderDelivered;
}
}
Outputs Are Provided as Separate Screenshots.
The post Project Completion Source Code below CoffeeShop.java /* * This is the code appeared first on PapersSpot.