Question

In: Computer Science

Use Javascript to implement the class below to the code below. Create a class Order which...

Use Javascript to implement the class below to the code below.

  • Create a class Order which contains one array member variable that stores Sandwich objects. Initially, this array is empty.
    • Add a function named add that adds a sandwich to the array.
    • Add a getter function named price which iterates over the array and sums up the price of each  sandwich in the array.

class Sandwich {

constructor(price) {

this.price = price;

}

toString() {

return "Sandwich", this.price;

}

}

class Burger extends Sandwich {

constructor(price, lettuce, meat) {

super(price)

this.lettuce = lettuce;

this.meat = meat;

}

toString() {

return "Burger", this.price;

}

}

class CheeseBurger extends Burger {

constructor(price, lettuce, meat, cheese) {

super(price, lettuce, meat)

this.cheese = cheese;

}

toString() {

return "CheeseBurger", this.price;

}

}

Solutions

Expert Solution

class Sandwich {

/*Sandwich class already implemented and given*/

constructor(price) {

this.price = price;

}

toString() {

return "Sandwich", this.price;

}

}

class Burger extends Sandwich {

/*Burger class extending sandwich class already implemented and given*/

constructor(price, lettuce, meat) {

super(price)

this.lettuce = lettuce;

this.meat = meat;

}

toString() {

return "Burger", this.price;

}

}

class CheeseBurger extends Burger {

/*ChesseBurger class extending burger class already implemented and given*/

constructor(price, lettuce, meat, cheese) {

super(price, lettuce, meat)

this.cheese = cheese;

}

toString() {

return "CheeseBurger", this.price;

}

}

class Order{

/* New Order Class which will manage new orders*/

constructor() {

this.orderArray = []; /* Initializing empty array*/

/* For storing new sandwich objects*/

}

add(price){ /* add method which takes price of sandwich as argument*/

var sandwich = new Sandwich(price); /*new sandwich object*/

this.orderArray.push(sandwich); /*Adding objects to orderArray*/

return;

}

price() {/*This function will return the sum of price of all sandwiches*/

let sum = 0;

for(let i=0;i<this.orderArray.length;i++){

sum += this.orderArray[i].price; /*Taking sum*/

}

return sum;

}

}

/*Demo object created to show the working of new 'Order' class*/

newOrder = new Order();

newOrder.add(10);

newOrder.add(20);

newOrder.add(30);

newOrder.add(40);

console.log(newOrder.price()); /*Getting the final sum in console*/


Related Solutions

Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to...
Write the below code to use HTML and JavaScript. 1. a) Write a JavaScript program to display the current day and time. b) Write a JavaScript program to print the contents of the current window.   c) Write a JavaScript program where the program takes a random integer between 1 to 10 d) Write a JavaScript program to calculate multiplication and division of two numbers (input from the user). e)Write a JavaScript program to create a new string from a given...
Q2. Update the given code 2 to implement the following problem: Create a class triangle Use...
Q2. Update the given code 2 to implement the following problem: Create a class triangle Use dynamic memory allocation to create 10 objects of the class triangle Set default height and base of all created objects (triangle) to 5 and 10 respectively using a constructor Ask user to change only the height of the 5th triangle Print height and base of all (10) triangles to demonstrate that you did the task correctly Deallocate the 5th object Print height and base...
Please take the code below and add some javascript to it. Please use javascript inline. Please...
Please take the code below and add some javascript to it. Please use javascript inline. Please be sure to specify within the webpage with something like 'Click here' too show what was done and how to activate it. Please post in a format that can be directly copied. Thank you in advance HINT: add some fun widgets to the page index-css.html: <!DOCTYPE html> <html lang="en"> <head> <!-- title for web page --> <title>Index-CSS Page</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1">...
Exercise 1 (a) Use javascript modify the code below, so that in addition to outputting the...
Exercise 1 (a) Use javascript modify the code below, so that in addition to outputting the selection to the web page, the selection is also placed in the browser’s local storage. Use ‘select’ as the local-storage key. The value will be the name of the category that was selected or the empty string is no selection was made. (d) Add a button called ‘retrieve’. When it is clicked the local storage is read and the prior selection is shown on...
Separate code into .cpp and .h files: // C++ program to create and implement Poly class...
Separate code into .cpp and .h files: // C++ program to create and implement Poly class representing Polynomials #include <iostream> #include <cmath> using namespace std; class Poly { private: int degree; int *coefficients; public: Poly(); Poly(int coeff, int degree); Poly(int coeff); Poly(const Poly& p); ~Poly(); void resize(int new_degree); void setCoefficient(int exp, int coeff); int getCoefficient(int exp); void display(); }; // default constructor to create a polynomial with constant 0 Poly::Poly() { // set degree to 0 degree = 0; //...
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet...
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet will be a simple class consisting of three fields: name: string representing the name of a planet, such as “Mars” madeOf: string representing the main element of the planet alienPopulation: int representing if the number of aliens living on the planet Your Planet class should have the following methods: Planet(name, madeOf, alienPopulation) // Constructor getName(): string // Returns a planet’s name getMadeOf(): String //...
use VISUAL STUDIO CODE to write this javascript program Exercise 1 (a) Create a HTML file...
use VISUAL STUDIO CODE to write this javascript program Exercise 1 (a) Create a HTML file that uses createElement and appendChild to dynamically insert three paragraphs when a button is clicked. (b) Create a HTML file that includes JavaScript that is similar to: let recs = [“my item …1”,”my item…2”, …] i.e. an array that contains several CSV item records. When the user clicks a button, each array element will be rendered as a list element. Use a HTML list....
Create a class called Height Copy the code for Height given below into the class. Remember...
Create a class called Height Copy the code for Height given below into the class. Remember – test the methods as you go Take a few minutes to understand what the class does. There are comments where you will have to be changing code. A list of changes are given Change the setFeet and setInches mutators to make sure the height and width will not be less than 0, no matter what is passed in to it Change constructor that...
Code in Java Create a stack class to store integers and implement following methods: 1) void...
Code in Java Create a stack class to store integers and implement following methods: 1) void push(int num): This method will push an integer to the top of the stack. 2) int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3) void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
This is for Javascript programming language: A. Class and Constructor Creation Book Class Create a constructor...
This is for Javascript programming language: A. Class and Constructor Creation Book Class Create a constructor function or ES6 class for a Book object. The Book object should store the following data in attributes: title and author. Library Class Create a constructor function or ES6 class for a Library object that will be used with the Book class. The Library object should be able to internally keep an array of Book objects. B. Methods to add Library Class The class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT