Hi all. I am learning JS, and got the following task. I can do it by functions, but it is hard for me to code it by OOP (I am learning OOP now).
If somebody could just give me some hints about structure of the code, and how to implement method **addExtraIngredient(ingredient)
Use constructor function.
Create pizza of different sizes and different types:
Sizes:
– S (+50 UAH)– M (+75 UAH)– L (+100 UAH)
Types:– VEGGIE (+50 UAH)– MARGHERITA (+60 UAH)– PEPPERONI (+70 UAH)
You can add extra ingredients to the pizza:
Extra ingredients:– CHEESE (+7 UAH)– TOMATOES (+5 UAH)– MEAT (+9 UAH)
The program should calculate the cost of a pizza and info about your pizza.
The code must be error-proof. If you pass the wrong type of pizza, for example,
or the wrong kind of ingredient, an exception should be thrown (use **PizzaException
**Class members:
–
properties (size and type are required):
• _size_allowedSizes
• _type_allowedTypes
methods:
• _addExtraIngredient(ingredient)
Accept only one parameter, otherwise show an error
Check if such an ingredient exists in _allowedIngredients
show an error
Check if such an ingredient already exists; if there is, show error (you can add
one ingredient only once)
• _removeExtraIngredient(ingredient)
Accept only one parameter, otherwise show an error
Check if such an ingredient exists in allowedIngredients, if does not exist,
show an error
Check if such an ingredient has already been added, if it not added, show an
error, otherwise remove ingredient
• _getSize()
• _getPrice()
• _getPizzaInfo()
Provides information about an error while working with a Pizza. Details are stored in the log
property.
Class members:
– properties:
• log: information about an error while working with a Pizza.
What I managed to code is below:
“`
‘use strict’;
/**
* Class
* @constructor
* @param size – size of pizza
* @param type – type of pizza
* @throws {PizzaException} – in case of improper use
*/
function Pizza(size, type) {
let totalPrice = 0;
this.size = size;
this.type = type;
this.getPrice = function () {
// this.totalPrice = size.price + type.price + totalPrice ;
return size.price + type.price + totalPrice;
}
this.addExtraIngredient = function (ingredient) {
this.totalPrice = totalPrice + ingredient.price;
return this.totalPrice;
}
// this.getSize = function () {
// return size.size;
// }
// this.getPizzaInfo = function () {
// const report = `
// The size of pizza: ${size.size}, type: ${type.type}, extra ingredients: ${true}, total price: ${this.getPrice()}
// `;
// return report;
// }
}
/* Sizes, types and extra ingredients */
Pizza.SIZE_S = {size: ‘small’, price: 50};
Pizza.SIZE_M = {size: ‘medium’, price: 75};
Pizza.SIZE_L = {size: ‘large’, price: 100};
Pizza.TYPE_VEGGIE = {type: ‘veggy’, price: 50};
Pizza.TYPE_MARGHERITA = {type: ‘margherita’, price: 60};
Pizza.TYPE_PEPPERONI = {type: ‘pepperoni’, price: 70};
Pizza.EXTRA_TOMATOES = {extra: ‘tomatoes’, price: 5};
Pizza.EXTRA_CHEESE = {extra: ‘cheese’, price: 7};
Pizza.EXTRA_MEAT = {extra: ‘meat’, price: 9};
/* Allowed properties */
Pizza.allowedSizes = ;
Pizza.allowedTypes = ;
Pizza.allowedExtraIngredients = [Pizza.EXTRA_TOMATOES, Pizza.EXTRA_CHEESE, Pizza.EXTRA_MEAT ];
/**
* Provides information about an error while working with a pizza.
* details are stored in the log property.
* @constructor
*/
function PizzaException() {}
/* It should work */
// // small pizza, type: veggie
let pizza = new Pizza(Pizza.SIZE_S, Pizza.TYPE_VEGGIE);
// // add extra meat
pizza.addExtraIngredient(Pizza.EXTRA_MEAT);
console.log(pizza.totalPrice);
// // check price
console.log(`Price: ${pizza.getPrice()} UAH`); //=> Price: 109 UAH
// // add extra corn
// pizza.addExtraIngredient(Pizza.EXTRA_CHEESE);
// // add extra corn
// pizza.addExtraIngredient(Pizza.EXTRA_TOMATOES);
// // check price
// console.log(`Price with extra ingredients: ${pizza.getPrice()} UAH`); // Price: 121 UAH
// // check pizza size
// console.log(`Is pizza large: ${pizza.getSize() === Pizza.SIZE_L}`); //=> Is pizza large: false
// // remove extra ingredient
// pizza.removeExtraIngredient(Pizza.EXTRA_CHEESE);
// console.log(`Extra ingredients: ${pizza.getExtraIngredients().length}`); //=> Extra ingredients: 2
// console.log(pizza.getPizzaInfo()); //=> Size: SMALL, type: VEGGIE; extra ingredients: MEAT,TOMATOES; price: 114UAH.
// examples of errors
// let pizza = new Pizza(Pizza.SIZE_S); // => Required two arguments, given: 1
// let pizza = new Pizza(Pizza.SIZE_S, Pizza.SIZE_S); // => Invalid type
// let pizza = new Pizza(Pizza.SIZE_S, Pizza.TYPE_VEGGIE);
// pizza.addExtraIngredient(Pizza.EXTRA_MEAT);
// pizza.addExtraIngredient(Pizza.EXTRA_MEAT); // => Duplicate ingredient
// let pizza = new Pizza(Pizza.SIZE_S, Pizza.TYPE_VEGGIE);
// pizza.addExtraIngredient(Pizza.EXTRA_MEAT); // => Invalid ingredient