Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This are my cashier and customer java classes. I need help putting try /catch bl

ID: 3823989 • Letter: T

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

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<String> sandwichIngredients = currentSandwich.getIngredients();

       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 + "]";

   }

}

Explanation / Answer

Hi, You have not posted Sandwitch class, So i can not test.

But I have added required Exception in methods.

############

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) throws InsufficientMoneyException {

       if(cash < total)

           throw new InsufficientMoneyException("You do not have sufficient money");

         

       cash -= total;

       return true;

   }

   @Override

   public String toString() {

       return "Customer [name=" + name + ", cash=" + cash + ", desiredSandwiches=" + Arrays.toString(desiredSandwiches)

       + ", desiredIngredients=" + Arrays.toString(desiredIngredients) + "]";

   }

}

class InsufficientMoneyException extends Exception {

  

   public InsufficientMoneyException(String msg) {

       super(msg);

   }

}

#################

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<String> sandwichIngredients = currentSandwich.getIngredients();

       for (String sandwichIngredient : sandwichIngredients) {

           for (Ingredient ingredient : Ingredient.values()) {

               if (ingredient.getName().equalsIgnoreCase(sandwichIngredient)) {

                   total += ingredient.getPrice();

                   break;

               }

           }

       }

       try{

           currentCustomer.givePayment(total);

           cash += total;

       }catch (InsufficientMoneyException e) {

           System.out.println(e.getMessage());

       }

   }

   @Override

   public String toString() {

       return "Cashier [name=" + name + ", currentSandwich=" + currentSandwich + ", currentCustomer=" + currentCustomer

               + ", tillCash=" + cash + "]";

   }

}