Program In Java HELP WITH MAIN import java.util.ArrayList; public class Cashier
ID: 3779291 • Letter: P
Question
Program In Java
HELP WITH MAIN
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 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);
}
}
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
import java.util.ArrayList;
enum SandwichVariety{
NONVEG("nonveg" , 10 ),
VEG("veg",5);
private final String name;
private final double price;
private SandwichVariety(String name , double price)
{
this.name = name;
this.price = price;
}
public String getName()
{
return name;
}
public double getPrice()
{
return price;
}
}
enum Ingredient{
CHICKEN("chicken" , 10 ),
CHOCOLATE("veg",5),
CHESSE("cheese",15);
private final String name;
private final double price;
private Ingredient(String name , double price)
{
this.name = name;
this.price = price;
}
public String getName()
{
return name;
}
public double getPrice()
{
return price;
}
}
public class Sandwich{
private String variety;
private ArrayList<String> ingredients;
/**
* @param variety
*/
public Sandwich(String variety) {
this.variety = variety;
}
/**
* @return the variety
*/
public String getVariety() {
return variety;
}
/**
* @param variety the variety to set
*/
public void setVariety(String variety) {
this.variety = variety;
}
/**
* @return the ingredients
*/
public ArrayList<String> getIngredients() {
return ingredients;
}
public void addIngredient(String ingredient) {
if(this.ingredients==null){
this.ingredients=new ArrayList<>();
}
this.ingredients.add(ingredient);
}
}
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 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 SandwichShopDemo {
public static void main(String[] args) {
String[]desiredSandwiches={"veg"};
String[]desiredIngredients={"cheese"};
Customer customer=new Customer("Jack", 100, desiredSandwiches, desiredIngredients);
String[]availableIngredients={"cheese","chicken"};
Cashier cashier=new Cashier("Bob", 10000);
SandwichMaker maker=new SandwichMaker("Peter", availableIngredients, cashier);
customer.orderSandwich(maker);
maker.makeSandwich(customer, "veg");
maker.passSandwich(customer);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.