Program In Java NEED HELP GETTING PROGRAM TO RUN import java.util.ArrayList; pub
ID: 3779283 • Letter: P
Question
Program In Java
NEED HELP GETTING PROGRAM TO RUN
import java.util.ArrayList;
public class Cashier
{
private String name;
private Sandwich currentSandwich;
private Customer currentCustomer;
private double cash;
public Cashier(String name, double tillCash)
{
this.name = name;
this.cash = tillCash;
}
public void ringUpSandwich(Customer customer, Sandwich sandwich)
{
currentSandwich = sandwich;
currentCustomer = customer;
computeTotal();
}
public void computeTotal()
{
double total = 0;
// Check each sandwich variety for a match to currentSandwich
for (SandwichVariety variety : SandwichVariety.values())
{
if (currentSandwich.getVariety().equalsIgnoreCase(variety.getName()))
{
total = variety.getPrice();
break;
}
}
ArrayList<String> sandwichIngredients = currentSandwich.getIngredients();
// Check each ingredient for a match to sandwichIngredient
for (String sandwichIngredient : sandwichIngredients)
{
for (Ingredient ingredient : Ingredient.values())
{
if (ingredient.getName().equalsIgnoreCase(sandwichIngredient))
{
total += ingredient.getPrice();
break;
}
}
}
// Check if the customer can afford to pay
if (currentCustomer.givePayment(total))
{
cash += total;
}
}
}
public class Customer
{
private String name;
private double cash;
private String[] desiredSandwiches;
private String[] desiredIngredients;
public Customer(String name, double cash, String[] desiredSandwiches, String[] desiredIngredients)
{
this.name = name;
this.cash = cash;
this.desiredSandwiches = desiredSandwiches;
this.desiredIngredients = desiredIngredients;
}
public void orderSandwich(SandwichMaker sandwichMaker)
{
for (String sandwich : desiredSandwiches)
{
sandwichMaker.makeSandwich(this, sandwich);
}
}
public boolean chooseIngredient(String ingredient)
{
boolean answer = false;
for (String desiredIngredient : desiredIngredients)
{
if (desiredIngredient.equalsIgnoreCase(ingredient))
{
answer = true;
}
break;
}
return answer;
}
public boolean givePayment(double total)
{
boolean answer = false;
// Check if customer has enough cash to pay
if (cash >= total)
{
cash -= total;
answer = true;
}
return answer;
}
}
public enum Ingredient
{
// the string is the name of the ingredient and the number is the price of it
GHERKIN ("Gherkins", 0.99),
MEATBALL ("Meatballs", 1.99),
CHEESE ("Cheese", 2.99),
GUAC ("Guacamole", 3.99);
private final String ingredient;
private final double price;
private Ingredient (String ingredient, double price) {
this.ingredient = ingredient;
this.price = price;
}
public String getName() {
return ingredient;
}
public double getPrice() {
return price;
}
}
import java.util.ArrayList;
public class Sandwich {
private String variety;
private ArrayList<String> ingredients; // list to hold all the of ingredients placed on the sandwich
public Sandwich(String variety) {
this.variety = variety;
ingredients = new ArrayList<String>(); // no need to initialise with any values because all ingredients are added via addIngredient()
}
public void addIngredient(String ingredient) {
ingredients.add(ingredient); // add the specified ingredient to the list
}
public String getVariety() {
return variety;
}
public ArrayList<String> getIngredients() {
return ingredients;
}
}
public class SandwichMaker
{
private String name; // sandwich maker's name
private Sandwich currentSandwich; // what the sandwich maker is currently working on
private String[] availableIngredients; // the ingredients at the sandwich maker's disposal
private Cashier cashier; // assume the same cashier is going to be on shift for the entire time the sandwich maker works
public SandwichMaker(String name, String[] availableIngredients, Cashier cashier) {
this.name = name;
this.availableIngredients = availableIngredients;
this.cashier = cashier;
}
public void makeSandwich(Customer customer, String sandwichVariety) {
currentSandwich = new Sandwich(sandwichVariety); // begin making a sandwich of the variety specified by the customer
// for each available ingredient, ask the customer if they want it
for (String ingredient : availableIngredients) {
promptIngredient(customer, ingredient);
}
passSandwich(customer);
}
public void promptIngredient(Customer customer, String ingredient) {
// if the customer wants the specified ingredient, then add it to the sandwich
if (customer.chooseIngredient(ingredient)) {
currentSandwich.addIngredient(ingredient);
}
}
public void passSandwich(Customer customer) {
// give the sandwich to the cashier to ring up
cashier.ringUpSandwich(customer, currentSandwich);
}
}
public enum SandwichVariety
{
// the string is the name of the variety and the number is the price of the sandwich
HOME ("Homewrecker", 7.99),
YUMMY ("Yummy", 8.99),
ROAD ("Roadkill", 9.99),
OPTIMUS ("Optimus Prime", 6.99);
private final String variety;
private final double price;
private SandwichVariety (String variety, double price)
{
this.variety = variety;
this.price = price;
}
public String getName()
{
return variety;
}
public double getPrice()
{
return price;
}
}
public class Main
{
public static void main(String[] args) {
Cashier cashier = new Cashier("Myrtle", 10000.0);
Customer customer = new Customer("Gerald", 500.0, new String[] {"Optimus Prime", "Roadkill"}, new String[] {"Cheese", "Guacamole"});
SandwichMaker sandwichMaker = new SandwichMaker("Brad", new String[] {"Cheese", "Gherkins", "Meatballs"}, cashier);
customer.orderSandwich(sandwichMaker);
}
}
Create classes modeling the purchase of sandwich sandwiches at a typical sandwich shop. Use three objects: Cashier, Customer, SandwichMaker. (You can add more classes if you want but these three should be present in the project)Explanation / Answer
To understand any Java program start debugging the program from the main method .
Therefore class Main :-
1. There are three objects instantiated for Cashier class , Customer class and Sandwichmaker class .
Cashier class takes two arguments as "Myrtle" ----> casheir name and 10000.0 ---> cashier money
Customer class takes four arguments as customer name , cash with customer , array of Sandwiches , array of ingredients .
Sandwichmaker class takes three arguments as Sandwich Maker name , Array of available ingredients and cashier object .
In program there are two enum class defined as Ingredient and Sandwich Variety .
enum Ingredient :- name of ingredient and price of each ingredient .
enum Sandwich Variety :- name of Sandwich and price of Sandwich
To execute the program , run the main class as java application and execution starts .as
customer. orderSandwich(sandwichMaker) ---> sandwichmaker.makeSandwich (for each sandwich that customer demands ) there is a check to check for the desired Ingredient and the available ingredient and then sandwich is passed via passSandwich(customer) -----> the function ringupSandwich is called for each sandwich ---> computeTotal is called from ringupSandwich and total amount of each sandwich is calculated .
To display the total amount(price) of each sandwich made , add "System.out.println(total)" in Cashier class
output for total would be as :
9.98
12.98
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.