PROGRAM IN JAVA DUE AT 12 PLEASE HELP I NEED HELP WITH THE DRIVER I NEED IT TO P
ID: 3779859 • Letter: P
Question
PROGRAM IN JAVA DUE AT 12 PLEASE HELP
I NEED HELP WITH THE DRIVER I NEED IT TO PRINT HOW MUCH WILL THE CUSTOMER HAVE AFTER THEY PURCHASE A SANDWICH
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 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 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;
}
}
import java.util.ArrayList;
public class Cashier
{
private String name;
private Sandwich currentSandwich;
private Customer currentCustomer;
private double cash;
public Cashier(String name, double cash)
{
this.name = name;
this.cash = cash;
}
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;
}
System.out.println("Total: " +total);
}
}
public class Main
{
public static void main(String[] args)
{
Cashier cashier = new Cashier("Williams", 0);
Customer customer = new Customer("Ryan", 0,
new String[] {"BLT", "Tasty"},
new String[] {"Cheese", "Guacamole"});
SandwichMaker sandwichMaker = new SandwichMaker("Daniel",
new String[] {"Cheese", "Gherkins", "Meatballs"}, cashier);
customer.orderSandwich(sandwichMaker);
System.out.println();
}
}
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
The following are the classes in which I made changes. Since Ingredient and SandwichVariety classes were not provided, I've made my own Implementation.
Customer.java
package samplepro;
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;
}
//added getcash method to get the cash available with the customer
public double getCash()
{
return this.cash;
}
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;
}
}
Cashier.java
package samplepro;
import java.util.ArrayList;
public class Cashier
{
private String name;
private Sandwich currentSandwich;
private Customer currentCustomer;
private double cash;
public Cashier(String name, double cash)
{
this.name = name;
this.cash = cash;
}
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();
//System.out.println(total);
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;
}
System.out.println("Total: " +total);
}
//added this method to get the current customer instance back to main.
public Customer getCurrentCustomer()
{
return currentCustomer;
}
}
Ingredient.java
package samplepro;
import java.util.ArrayList;
public class Ingredient {
private String name;
private double price;
private static ArrayList<Ingredient> ingredients = new ArrayList<Ingredient>();
public Ingredient(String name, double price) {
this.name = name;
this.price = price;
addValues(this);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
private static void addValues(Ingredient thisIngre)
{
ingredients.add(thisIngre);
}
public static ArrayList<Ingredient> values()
{
return ingredients;
}
}
SandwichVariety.java
package samplepro;
import java.util.ArrayList;
public class SandwichVariety {
private String name;
private double price;
private static ArrayList<SandwichVariety> varieties = new ArrayList<SandwichVariety>();
public SandwichVariety(String name, double price) {
this.name = name;
this.price = price;
addValues(this);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
private static void addValues(SandwichVariety variety)
{
varieties.add(variety);
}
public static ArrayList<SandwichVariety> values()
{
return varieties;
}
}
Main.java
package samplepro;
public class Main
{
public static void main(String[] args)
{
//setting up the sandwich shop
//cashier first
Cashier cashier = new Cashier("Williams", 0);
//secondly ingredients
Ingredient ingredient = new Ingredient("Cheese", 30);
Ingredient ingredient2 = new Ingredient("Guacamole", 25);
SandwichVariety variety = new SandwichVariety("BLT", 60);
SandwichVariety variety2 = new SandwichVariety("Tasty", 70);
Customer customer = new Customer("Ryan", 300,
new String[] {"BLT", "Tasty"},
new String[] {"Cheese", "Guacamole"});
SandwichMaker sandwichMaker = new SandwichMaker("Daniel",
new String[] {"Cheese", "Gherkins", "Meatballs"}, cashier);
customer.orderSandwich(sandwichMaker);
customer = cashier.getCurrentCustomer();
System.out.println("Current customer balance: " + customer.getCash());
}
}
OUTPUT:
Total: 90.0
Total: 100.0
Current customer balance: 110.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.