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;...
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...
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...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ *...
Please take this c++ code and make it into java code. /* RecursionPuzzleSolver * ------------ * This program takes a puzzle board and returns true if the * board is solvable and false otherwise * * Example: board 3 6 4 1 3 4 2 5 3 0 * The goal is to reach the 0. You can move the number of spaces of your * current position in either the positive / negative direction * Solution for this game...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence;...
UML Diagram for this java code //java code import java.util.*; class Message { private String sentence; Message() { sentence=""; } Message(String text) { setSentence(text); } void setSentence(String text) { sentence=text; } String getSentence() { return sentence; } int getVowels() { int count=0; for(int i=0;i<sentence.length();i++) { char ch=sentence.charAt(i); if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' || ch=='E' || ch=='I' || ch=='O' || ch=='U') { count=count+1; } } return count; } int getConsonants() { int count=0; for(int i=0;i<sentence.length();i++)...
Using Java, define a train class and write code to demonstrate its functionality The train should...
Using Java, define a train class and write code to demonstrate its functionality The train should run between Boston and New York, stopping at different, different station along the way. Provide output to indicate the train’s current status.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT