Question

In: Computer Science

Java Code. Using Decorator pattern make class BurgerToppings: -Each topping is an extra $1 and the...

Java Code.

Using Decorator pattern make class BurgerToppings:

-Each topping is an extra $1 and the name provided in the constructor

-getTotalPrice: returns total price of burger + toppings

-getDetails: returns the burger name + name of topping with the word (extra)

Code:

public interface Burger{
    double C();
    String getDetails();
}
public class BigMac implements Burger{
    private String order;
    private double price ;

    public BigMac() {
        this.order= "Big Mac";
        this.price = 4.00;
    }

    @Override
    public double getPrice() {
        return price;
    }

    @Override
    public String getorder() {
        return name;
    }
}

Output expected:

Ordering BigMac + extra pickles+ extra cheese
Total price = 6.0

Solutions

Expert Solution

CODE :-

Burger -

BigMac -

BurgerWithToppings -

BigMacWithToppings -

Main -

OUTPUT :-

TEXT CODE :-

Burger -

public interface Burger {
   double getPrice();
   String getOrder();
}

BigMac -

public class BigMac implements Burger {

   private String order;
   private double price;

   public BigMac() {
       this.order = "Big Mac";
       this.price = 4.00;
   }

   // Method C
   @Override
   public double getPrice() {
       return price;
   }

   // Method getDetails
   @Override
   public String getOrder() {
       return order;
   }

}

BurgerWithToppings -

public abstract class BurgerWithToppings implements Burger {
  
   protected Burger burger;
  
   public BurgerWithToppings(Burger b) {
       this.burger = b;
   }

   @Override
   public double getPrice() {
       return burger.getPrice();
   }

   @Override
   public String getOrder() {
       return burger.getOrder();
   }

}

BigMacWithToppings -

public class BigMacWithToppings extends BurgerWithToppings {
  
   private String toppings[];
  
   public BigMacWithToppings(BigMac bm,String[] toppings) {
       super(bm);
       this.toppings = toppings;
   }

   @Override
   public double getPrice() {
       double tempPrice = super.getPrice();
       for(int i =0; i < this.toppings.length; i++) {
           tempPrice++;
       }
       return tempPrice;
   }

   @Override
   public String getOrder() {
       String tempName = super.getOrder();
       for(String t : this.toppings) {
           tempName += " + extra "+t;
       }
       return tempName;
   }
}

Main -

public class Main {

   public static void main(String[] args) {
      
       String toppings[] = {"pickles", "cheese"};
      
       Burger burgWidTop = new BigMacWithToppings(new BigMac(),toppings);
      
       System.out.println("Ordering "+burgWidTop.getOrder());
       System.out.println("Total Price = "+burgWidTop.getPrice());
   }

}


Related Solutions

Java Code. Using Decorator pattern make class BurgerToppings: -Each topping is an extra $1 and the...
Java Code. Using Decorator pattern make class BurgerToppings: -Each topping is an extra $1 and the name provided in the constructor -getTotalPrice: returns total price of burger + toppings -getDetails: returns the burger name + name of topping with the word (extra) Code: public interface Burger{ double C(); String getDetails(); } public class BigMac implements Burger{ private String order; private double price ; public BigMac() { this.order= "Big Mac"; this.price = 4.00; } @Override public double getPrice() { return price;...
Can you give a java code example of using a singleton pattern and factory pattern design...
Can you give a java code example of using a singleton pattern and factory pattern design together?
In Java, using the code provided for Class Candle, create a child class that meets the...
In Java, using the code provided for Class Candle, create a child class that meets the following requirements. Also compile and run and show output ------------------------------------------------------------------------ 1. The child class will be named  ScentedCandle 2. The data field for the ScentedCandle class is:    scent 3. It will also have getter and setter methods 4. You will override the parent's setHeight( ) method to set the price of a ScentedCandle object at $3 per inch (Hint:   price = height * PER_INCH) CODE...
Write a Java code to represent a 1. Date class. As date class is composed of...
Write a Java code to represent a 1. Date class. As date class is composed of three attributes, namely month, year and day; so the class contains three Data Members, and one method called displayDate() which will print these data members. Test the Date class using main class named DateDemo. Create two objects of date class. Initialize the data fields in Date class using the objects, invoke the method displyaDate(). Date month : String year: int day : int displayDate():...
Using maps in Java. Make a public class called ExampleOne that provides a single class (static)...
Using maps in Java. Make a public class called ExampleOne that provides a single class (static) method named firstOne. firstOne accepts a String array and returns a map from Strings to Integer. The map must count the number of passed Strings based on the FIRST letter. For example, with the String array {“banana”, “apples”, “blueberry”, “orange”}, the map should return {“b”:2, “a”:1, “o”:1}. Disregard empty Strings and zero counts. Retrieve first character of String as a char using charAt, or,...
USING JAVA: Complete the following class. input code where it says //TODO. public class BasicBioinformatics {...
USING JAVA: Complete the following class. input code where it says //TODO. public class BasicBioinformatics { /** * Calculates and returns the complement of a DNA sequence. In DNA sequences, 'A' and 'T' are * complements of each other, as are 'C' and 'G'. The complement is formed by taking the * complement of each symbol (e.g., the complement of "GTCA" is "CAGT"). * * @param dna a char array representing a DNA sequence of arbitrary length, * containing only...
write a java code to represent a sales class as follows: 1- The Sales class contains...
write a java code to represent a sales class as follows: 1- The Sales class contains the names of sellers (strings) and the sales/seller/day (matrix of integers). Assume the number of sellers is set dynamically by the constructor and that the sellers work 6 days/week. Example: names/days 0 1 2 3 4 5 Ali 30 5 89 71 90 9 Ahmad 15 81 51 69 78 25 Omar 85 96 7 87 41 54 The class should contain the following...
Check errors and revise/update this java code below and update with OOp class; at least make...
Check errors and revise/update this java code below and update with OOp class; at least make two classes, use java library, validate user input, format and create UML/Pseudocode and flowchart for the code. import java.util.Scanner; public class TestScore {    public static void main(String[] args) {        String firstName;        String lastName;        int numTest;        int i;        int score;        double totalScore;        double avgScore;        String grade;        Scanner input = new Scanner(System.in);        System.out.println("Enter First Name");        firstName = input.nextLine();        System.out.println("Enter Last Name");        lastName = input.nextLine();        System.out.println("How many...
Using JAVA: Create a simple numeric code class SimpleCode that takes a set of numbers and...
Using JAVA: Create a simple numeric code class SimpleCode that takes a set of numbers and turns them into words and sentences. The code should use the 0 to represent a space between words and 00 to represent a period at the end of a sentence. The 26 letters of the alphabet should be represented in reverse order. In other words, Z is 26 and A is one. In your final product, only the first letter of a sentence should...
This Code Is Supposed To Be Performed In JAVA 1.) Create an abstract class DiscountPolicy. It...
This Code Is Supposed To Be Performed In JAVA 1.) Create an abstract class DiscountPolicy. It should have a single abstract method computeDiscount that will return the discount for the purchase of a given number of a single item. The method has two parameters, count and itemCost. Create a driver class that tests this class and provide the UML. 2.) In a separate program, define DiscountPolicy as an interface instead of the abstract class. Create a driver class that tests...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT