In: Computer Science
Use Javascript to implement the class below to the code below.
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;
}
}
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*/