Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Bread.java package sandwich; /** * @author * Bread.java is to store and provide bread information related to breadType, calories per slice */ public class Bread

image text in transcribed

Bread.java

package sandwich;

/**

* @author

* Bread.java is to store and provide bread information related to breadType, calories per slice

*/

public class Bread {

private String breadType;

private double caloriesPerSclice;

public static final String SLOGAN = "The Way A Sandwich Should Be.";

/**

* @param breadType

* @param caloriesPerSclice

* Constructor with parameters initializes the variables breadType and caloriesPerSlice

*/

public Bread(String breadType, double caloriesPerSclice) {

this.breadType = breadType;

this.caloriesPerSclice = caloriesPerSclice;

}

/**

* @returns breadType

*/

public String getBreadType() {

return breadType;

}

/**

* @return caloriesPerSclice

*/

public double getCaloriesPerSclice() {

return caloriesPerSclice;

}

}

TestBread.java

package sandwich;

/**

* @author

* TestBread.java contains main method which uses Bread class to create

* Bread objects and display information to the user

*

*/

public class TestBread {

public static void main(String[] args) {

//Create Array of Bread objects of size 3

Bread[] breads=new Bread[3];

//Create each Bread object and set to array

Bread bread1=new Bread("rye", 94);

breads[0]=bread1;

Bread bread2=new Bread("Bun",80);

breads[1]=bread2;

Bread bread3=new Bread("Toat",90);

breads[2]=bread3;

//iterate through loop and print all the bread objects

for (Bread bread : breads) {

System.out.println(Bread.SLOGAN+" Bread Type: "+bread.getBreadType()+" Calories Per Slice: "+bread.getCaloriesPerSclice());

}

}

}

Sample Output:

The Way A Sandwich Should Be. Bread Type: rye Calories Per Slice: 94.0 The Way A Sandwich Should Be. Bread Type: Bun Calories Per Slice: 80.0 The Way A Sandwich Should Be. Bread Type: Toat Calories Per Slice: 90.0

SandwichFilling.java

package sandwich;

/**

* @author SandwichFilling.java contaings the details of sandwich filling and

* calories in a serving, object can be created using parameterized

* construtor which accepts String for fillingType and double for

* Calories in a serving as parameters

*

*/

public class SandwichFilling {

private String fillingType;

private double caloriesInServing;

/**

* @param fillingType

* @param caloriesInServing

* @Description Parameterized constructor used to create object by accepting

* Sting and double values as parameters

*/

public SandwichFilling(String fillingType, double caloriesInServing) {

this.fillingType = fillingType;

this.caloriesInServing = caloriesInServing;

}

/**

* @return

*/

public String getFillingType() {

return fillingType;

}

/**

* @return

*/

public double getCaloriesInServing() {

return caloriesInServing;

}

}

TestSandwichFilling.java

package sandwich;

public class TestSandwichFilling {

public static void main(String[] args) {

// Create Array of SandwichFilling objects of size 3

SandwichFilling[] fillings = new SandwichFilling[3];

// Create each SandwichFilling object and set to array

SandwichFilling filling1 = new SandwichFilling("egg salad", 100);

fillings[0] = filling1;

SandwichFilling filling2 = new SandwichFilling("Sausage", 95);

fillings[1] = filling2;

SandwichFilling filling3 = new SandwichFilling("Tuna & Cucumber", 110);

fillings[2] = filling3;

// iterate through loop and print all the bread objects

for (SandwichFilling filling : fillings) {

System.out.println("Filling Type: " + filling.getFillingType() + " Calories In a Serving: "

+ filling.getCaloriesInServing());

}

}

}

Sample Output:

Filling Type: egg salad Calories In a Serving: 100.0 Filling Type: Sausage Calories In a Serving: 95.0 Filling Type: Tuna & Cucumber Calories In a Serving: 110.0

Sandwich.java

package sandwich;

/**

* @author

* @Description Sandwich.java contains Bread and Sandwich filling objects which

* are used to get details of Bread and Sandwich filling related

* data.

*

*/

public class Sandwich {

private Bread bread;

private SandwichFilling filling;

public Sandwich(Bread bread, SandwichFilling filling) {

this.bread = bread;

this.filling = filling;

}

public Bread getBread() {

return bread;

}

public SandwichFilling getFilling() {

return filling;

}

}

TestSandwich.java

package sandwich;

/**

* @author

* @Description TestSandwich.java has main method which uses Bread,Sandwich,

* SandwichFilling classes to create objects and display the

* information related to sandwich

*/

public class TestSandwich {

public static void main(String[] args) {

//Create Bread and SandwichFilling objects and set to Sandwich object

Bread bread1 = new Bread("Rye", 94);

SandwichFilling filling1 = new SandwichFilling("Egg salad", 100);

Sandwich sandwich1 = new Sandwich(bread1, filling1);

Bread bread2 = new Bread("Bun", 80);

SandwichFilling filling2 = new SandwichFilling("Sausage", 95);

Sandwich sandwich2 = new Sandwich(bread2, filling2);

Bread bread3 = new Bread("Toast", 90);

SandwichFilling filling3 = new SandwichFilling("Tuna & Cucumber", 110);

Sandwich sandwich3 = new Sandwich(bread3, filling3);

//create array of sandwiches to store the sandwiches data

Sandwich[] sandwiches = new Sandwich[3];

sandwiches[0] = sandwich1;

sandwiches[1] = sandwich2;

sandwiches[2] = sandwich3;

// iterate through loop and print all the Sandwich objects data

System.out.printf("%-15s %-20s %-15s ", "Bread Type", "Filling Type", "Total Calories");

System.out.println("----------------------------------------------------");

for (Sandwich sandwich : sandwiches) {

double totalCalories = (sandwich.getBread().getCaloriesPerSclice() * 2)

+ sandwich.getFilling().getCaloriesInServing();

System.out.printf("%-15s %-20s %-15s ", sandwich.getBread().getBreadType(),

sandwich.getFilling().getFillingType(), totalCalories);

}

}

}

Sample Output:

Bread Type Filling Type Total Calories ---------------------------------------------------- Rye Egg salad 288.0 Bun Sausage 255.0 Toast Tuna & Cucumber 290.0

Order.java

package sandwich;

/**

* @author

* @Description Order.java contains information regarding customer name sandwich

* ordered and number of Sandwiches ordered

*

*/

public class Order {

private String customerName;

private Sandwich sandwich;

private int noOfSandwiches;

/**

* @param customerName

* @param sandwich

* @param noOfSandwiches

*/

public Order(String customerName, Sandwich sandwich, int noOfSandwiches) {

this.customerName = customerName;

this.sandwich = sandwich;

this.noOfSandwiches = noOfSandwiches;

}

/**

* @return customer name

*/

public String getCustomerName() {

return customerName;

}

/**

* @return sandwich ordered

*/

public Sandwich getSandwich() {

return sandwich;

}

/**

* @return number of sandwiches ordered

*/

public int getNoOfSandwiches() {

return noOfSandwiches;

}

}

TestOrder.java

package sandwich;

import java.util.Scanner;

public class TestOrder {

public static void main(String[] args) {

//Scanner object to read the input from the console

Scanner scanner = new Scanner(System.in);

//Prompts user to enter customer name

System.out.println("Enter Customer Name: ");

String customerName = scanner.nextLine();

//Prompts user to enter number of orders

System.out.println("Number of sandwiches: ");

int noOfSandwiches=scanner.nextInt();

Bread bread1 = new Bread("Rye", 94);

SandwichFilling filling1 = new SandwichFilling("Egg salad", 100);

Sandwich sandwich1 = new Sandwich(bread1, filling1);

Bread bread2 = new Bread("Bun", 80);

SandwichFilling filling2 = new SandwichFilling("Sausage", 95);

Sandwich sandwich2 = new Sandwich(bread2, filling2);

Bread bread3 = new Bread("Toast", 90);

SandwichFilling filling3 = new SandwichFilling("Tuna & Cucumber", 110);

Sandwich sandwich3 = new Sandwich(bread3, filling3);

Bread bread4 = new Bread("Brown Bread", 60);

SandwichFilling filling4 = new SandwichFilling("Turkey Salad", 180);

Sandwich sandwich4 = new Sandwich(bread4, filling4);

// create array of sandwiches to store the sandwiches data

Sandwich[] sandwiches = new Sandwich[4];

sandwiches[0] = sandwich1;

sandwiches[1] = sandwich2;

sandwiches[2] = sandwich3;

sandwiches[3] = sandwich4;

//Create order object to set sandwich type ordered and number of orders

Order order1=new Order(customerName, sandwich1,2);

// iterate through loop and print all the Sandwich objects data

System.out.println("Customer Name: "+customerName);

System.out.printf("%-15s %-20s %-15s ", "Bread Type", "Filling Type", "Total Calories");

System.out.println("----------------------------------------------------");

for (int i=0;i

double totalCalories = (order1.getSandwich().getBread().getCaloriesPerSclice() * 2)

+ order1.getSandwich().getFilling().getCaloriesInServing();

System.out.printf("%-15s %-20s %-15s ", order1.getSandwich().getBread().getBreadType(),

order1.getSandwich().getFilling().getFillingType(), totalCalories);

}

scanner.close();

}

}

Part 1. Using assignment 4, define a Custom Exception Class that prints a message if the user orders a bread or filling that is not offered by the sandwich shop. Next make a change to the Sandwich Ordering program by adding a try... catch block to check the input, provide a message that states the problem and allows the user to enter the correct bread or filling

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

AWS Certified Database Study Guide Specialty DBS-C01 Exam

Authors: Matheus Arrais, Rene Martinez Bravet, Leonardo Ciccone, Angie Nobre Cocharero, Erika Kurauchi, Hugo Rozestraten

1st Edition

1119778956, 978-1119778950

More Books

Students also viewed these Databases questions