Question
This are my cashier and customer java classes. I need help putting try /catch blocks so the customer cannot spend more money then they have
This are my cashier and customer java classes. I need help putting try /catch blocks so the customer cannot spend more money then they have
import java.util.ArrayList;
import java.util.Arrays;
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;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the cash
*/
public double getCash() {
return cash;
}
/**
* @param cash the cash to set
*/
public void setCash(double cash) {
this.cash = cash;
}
/**
* @return the desiredSandwiches
*/
public String[] getDesiredSandwiches() {
return desiredSandwiches;
}
/**
* @param desiredSandwiches the desiredSandwiches to set
*/
public void setDesiredSandwiches(String[] desiredSandwiches) {
this.desiredSandwiches = desiredSandwiches;
}
/**
* @return the desiredIngredients
*/
public String[] getDesiredIngredients() {
return desiredIngredients;
}
/**
* @param desiredIngredients the desiredIngredients to set
*/
public void setDesiredIngredients(String[] desiredIngredients) {
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;
if (cash >= total) {
cash -= total;
answer = true;
}
return answer;
}
@Override
public String toString() {
return "Customer [name=" + name + ", cash=" + cash + ", desiredSandwiches=" + Arrays.toString(desiredSandwiches)
+ ", desiredIngredients=" + Arrays.toString(desiredIngredients) + "]";
}
}
-------------------------------------------------------------
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;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the currentSandwich
*/
public Sandwich getCurrentSandwich() {
return currentSandwich;
}
/**
* @param currentSandwich the currentSandwich to set
*/
public void setCurrentSandwich(Sandwich currentSandwich) {
this.currentSandwich = currentSandwich;
}
/**
* @return the currentCustomer
*/
public Customer getCurrentCustomer() {
return currentCustomer;
}
/**
* @param currentCustomer the currentCustomer to set
*/
public void setCurrentCustomer(Customer currentCustomer) {
this.currentCustomer = currentCustomer;
}
/**
* @return the cash
*/
public double getCash() {
return cash;
}
/**
* @param cash the cash to set
*/
public void setCash(double cash) {
this.cash = cash;
}
public void ringUpSandwich(Customer customer, Sandwich sandwich) {
currentSandwich = sandwich;
currentCustomer = customer;
computeTotal();
}
public void computeTotal() {
double total = 0;
for (SandwichType type : SandwichType.values()) {
if (currentSandwich.getType().equalsIgnoreCase(type.getName())) {
total = type.getPrice();
break;
}
}
ArrayList
for (String sandwichIngredient : sandwichIngredients) {
for (Ingredient ingredient : Ingredient.values()) {
if (ingredient.getName().equalsIgnoreCase(sandwichIngredient)) {
total += ingredient.getPrice();
break;
}
}
}
if (currentCustomer.givePayment(total)) {
cash += total;
}
}
@Override
public String toString() {
return "Cashier [name=" + name + ", currentSandwich=" + currentSandwich + ", currentCustomer=" + currentCustomer
+ ", tillCash=" + cash + "]";
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started