使用 OOP (JS) 方法创建一个计算披萨成本的程序。我对 OOP 很陌生,两个小时的讲座后我有这个任务要做
Create a program that calculates cost of a pizza, using OOP (JS) approach. I am very new to OOP , and I have this task to do after two hours lecture
I need some hints what to do next, because I'm stuck for two days.
I DO NOT NEED TO DO THE WHOLE APP FOR ME...
您可以创建不同大小和不同类型的披萨:
Sizes:
- S (+50 UAH)
- M (+75 UAH)
- L (+100 UAH)
Types:
- VEGGIE (+50 UAH)
- MARGHERITA (+60 UAH)
- PEPPERONI (+70 UAH)
Extra ingredients:
- CHEESE (+7 UAH)
- TOMATOES (+5 UAH)
- MEAT (+9 UAH)
Write a program that calculates the cost of a pizza and info about
your pizza. Use an OOP approach (hint: you need a Pizza class,
constants, methods for choosing options and calculating the required
values).
The code must be error-proof. Imagine another programmer using your
class. If it passes the wrong type of pizza, for example, or the wrong
kind of ingredient, an exception should be thrown (the error should
not be silently ignored) (hint: you need PizzaException class).
Pizza Class Description
Class members:
- properties (size and type are required):
- size: size of pizza (must be from the allowedSizes property)
- type: type of pizza (must be from the allowedTypes property)
- methods:
- addExtraIngredient(ingredient): add extra ingredient. Method must:
- 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 already exists; if there is, show error (you can add one ingredient only once)
- removeExtraIngredient(ingredient): remove extra ingredient. Method must:
- 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(): returns size of pizza
- getPrice(): returns total price
- getPizzaInfo(): returns size, type, extra ingredients and price of pizza
PizzaExeption Class Description
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.
I have this code so far:
'use strict';
function Pizza(size, type) {
this.size = size;
this.type = type;
/* Sizes, types and extra ingredients */
Pizza.SIZE_S = 50;
Pizza.SIZE_M = 75;
Pizza.SIZE_L = 100;
Pizza.TYPE_VEGGIE = 50;
Pizza.TYPE_MARGHERITA = 60;
Pizza.TYPE_PEPPERONI = 70;
Pizza.EXTRA_TOMATOES = 5;
Pizza.EXTRA_CHEESE = 7;
Pizza.EXTRA_MEAT = 9;
/* Allowed properties */
Pizza.allowedSizes = [SIZE_S, SIZE_M, SIZE_L];
Pizza.allowedTypes = [TYPE_VEGGIE, TYPE_MARGHERITA, TYPE_PEPPERONI];
Pizza.allowedExtraIngredients = [EXTRA_TOMATOES, EXTRA_CHEESE, EXTRA_MEAT];
}
function PizzaException() {}
let pizza = new Pizza(Pizza.SIZE_S, Pizza.TYPE_VEGGIE);
pizza.addExtraIngredient(Pizza.EXTRA_MEAT);
console.log(`Price: ${pizza.getPrice()} UAH`);
pizza.addExtraIngredient(Pizza.EXTRA_CHEESE);
pizza.addExtraIngredient(Pizza.EXTRA_TOMATOES);
console.log(`Price with extra ingredients: ${pizza.getPrice()} UAH`); // Price: 121 UAH
console.log(`Is pizza large: ${pizza.getSize() === Pizza.SIZE_L}`); // Is pizza large: false
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
我设法写了这段代码,但我不明白如何在总和中添加额外的成分。
'use strict';
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;
}
}
/* 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};
Pizza.allowedExtraIngredients = [Pizza.EXTRA_TOMATOES, Pizza.EXTRA_CHEESE, Pizza.EXTRA_MEAT ];
let pizza = new Pizza(Pizza.SIZE_S, Pizza.TYPE_VEGGIE);
// add extra meat
pizza.addExtraIngredient(Pizza.EXTRA_MEAT);
// check price
console.log(`Price: ${pizza.getPrice()} UAH`); //=> Price: 109 UAH
如果您的问题是如何实现方法,我可以提出以下建议。顺便说一下,我在这里使用 class
关键字而不是 function
来声明一个 class。我认为它更清楚地描述了您的代码的意图(创建 class,而不是函数)。
class Pizza {
static SIZE_S = 50
static TYPE_VEGGIE = "veggie"
constructor(size, type) {
this.size = size
this.type = type
}
getSize() {
return this.size
}
getPizzaInfo() {
return `Here is your ${this.type} pizza of size ${this.size}!`
}
}
let p = new Pizza(Pizza.SIZE_S, Pizza.TYPE_VEGGIE)
console.log(p.getPizzaInfo())
更新
以上代码转ES5使用prototype
添加方法:
function Pizza(size,type) {
this.size = size
this.type = type
}
Pizza.prototype.getSize = function() {
return this.size
}
Pizza.prototype.getPizzaInfo = function() {
return `Hey, here is your ${this.type} pizza of ${this.size} size!`
}
Pizza.SIZE_S = 50
Pizza.TYPE_VEGGIE = 50
let p = new Pizza(Pizza.SIZE_S, Pizza.TYPE_VEGGIE)
console.log(p.getPizzaInfo())
我终于完成了这个任务。非常感谢 Kokodoko 的建议。以下是答案:
'use strict';
function Pizza(size, type) {
const requiredArguments = 2;
if (arguments.length !== requiredArguments) {
throw new PizzaException(`Required two arguments, given: ${arguments.length}`)
}
if (!Pizza.allowedTypes.includes(type) || !Pizza.allowedSizes.includes(size)) {
throw new PizzaException('Invalid type');
}
const negativeIndex = -1;
let _size = size;
let extrasType = [];
let extrasPrice = [];
this.type = type;
Pizza.prototype.getSize = function () {
return _size.size;
};
Pizza.prototype.getPrice = function () {
return _size.price + this.type.price;
};
Pizza.prototype.addExtraIngredient = function (ingredient) {
if (!ingredient) {
throw new PizzaException('Invalid ingredient')
}
if (type.type === 'VEGGIE' && ingredient.extra === 'MEAT') {
throw new PizzaException('Invalid ingredient');
}
if (extrasType.includes(ingredient.extra)) {
throw new PizzaException('Duplicate ingredient');
}
extrasPrice.push(_size.price += ingredient.price);
extrasType.push(ingredient.extra);
return _size.price;
};
Pizza.prototype.removeExtraIngredient = function (ingredient) {
extrasPrice.pop(this.type.price -= ingredient.price);
const index = extrasType.indexOf(ingredient.extra);
if (index > negativeIndex) {
extrasType.splice(index, 1);
}
return this.type.price;
};
Pizza.prototype.getExtraIngredients = function () {
return extrasPrice;
};
Pizza.prototype.getPizzaInfo = function () {
return `Size: ${_size.size}, type: ${
type.type
}; extra ingredients: ${extrasType}; price: ${
_size.price + this.type.price
}UAH`;
};
}
/* 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: 'VEGGIE', 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.SIZE_S, Pizza.SIZE_M, Pizza.SIZE_L];
Pizza.allowedTypes = [Pizza.TYPE_VEGGIE, Pizza.TYPE_MARGHERITA, Pizza.TYPE_PEPPERONI];
Pizza.allowedExtraIngredients = [Pizza.EXTRA_TOMATOES, Pizza.EXTRA_CHEESE, Pizza.EXTRA_MEAT];
function PizzaException(log) {
this.log = log;
PizzaException.prototype.log = function () {
return log;
};
}
//////////////// Tests //////////////////
// // small pizza, type Margherita 110 UAH
// let pizza = new Pizza(Pizza.SIZE_S, Pizza.TYPE_MARGHERITA);
// // add extra meat 110 + 9 = 119
// pizza.addExtraIngredient(Pizza.EXTRA_MEAT);
// console.log(`Price: ${pizza.getPrice()} UAH`); //Price: 119 UAH
// // add extra cheese 119 + 7 = 126
// pizza.addExtraIngredient(Pizza.EXTRA_CHEESE);
// // add extra tomatoes 126 + 5 = 131;
// pizza.addExtraIngredient(Pizza.EXTRA_TOMATOES);
// console.log(`Price with extra ingredients: ${pizza.getPrice()} UAH`); // Price: 131 UAH
// // check pizza size
// console.log(`Is pizza large: ${pizza.getSize() === Pizza.SIZE_L}`); //Is pizza large: false
// // remove extra ingredient cheese 131 - 7 = 124
// pizza.removeExtraIngredient(Pizza.EXTRA_CHEESE);
// console.log(`Extra ingredients: ${pizza.getExtraIngredients().length}`); // Extra ingredients: 2
// console.log(pizza.getPizzaInfo()); //Size: SMALL, type: MARGHERITA; extra ingredients: MEAT,TOMATOES; price: 124UAH.
//////////// Examples of errors ///////////////////////
/////////////////////////// 1 ///////////////////////
// let pizza = new Pizza(Pizza.SIZE_S); // "Required two arguments, given: 1"
/////////////////////////// 2 ///////////////////////
// let pizza = new Pizza(Pizza.SIZE_S, Pizza.SIZE_S); // "Invalid type"
/////////////////////////// 3 ///////////////////////
// let pizza = new Pizza(Pizza.SIZE_S, Pizza.TYPE_PEPPERONI);
// pizza.addExtraIngredient(Pizza.EXTRA_MEAT);
// pizza.addExtraIngredient(Pizza.EXTRA_MEAT); // "Duplicate ingredient"
/////////////////////////// 4 ///////////////////////
// let pizza = new Pizza(Pizza.SIZE_S, Pizza.TYPE_VEGGIE);
// pizza.addExtraIngredient(Pizza.EXTRA_MEAT); // "Invalid ingredient"
/////////////////////////// 5 ///////////////////////
// let pizza = new Pizza(Pizza.SIZE_S, Pizza.TYPE_VEGGIE);
// pizza.addExtraIngredient(Pizza.EXTRA_CORN); // "Invalid ingredient"
您错过了检查 addExtraIngredient 内的 allowedIngredients 中是否存在此类成分。
Pizza.prototype.addExtraIngredient = function (ingredient) {
...
if (!Pizza.allowedExtraIngredients.includes(ingredient)) {
throw new PizzaException('Invalid ingredient');
}
...
}
这是您的任务,但使用 ES6 类 方法完成
class Pizza {
constructor(type, size) {
this.extraIngredient = []
if (arguments.length < 2) {
return new PizzaException(`Required two arguments, given: ${arguments.length}`)
}
if (!Pizza.allowedTypes.includes(type) || !Pizza.allowedSizes.includes(size)) {
return new PizzaException('Invalid type')
}
this.type = type.type
this.size = size.size
this.price = type.price + size.price
}
addExtraIngredient(ingredient) {
if (arguments.length > 1) {
return new PizzaException('You can add only one ingredient per time')
}
if (!Pizza.allowedExtraIngredients.includes(ingredient)) {
return new PizzaException('You cannot add that ingredient')
}
if (this.extraIngredient.includes(ingredient)) {
return new PizzaException('You can add one ingredient only once')
} else {
this.extraIngredient.push(ingredient)
}
}
removeExtraIngredient(ingredient) {
if (arguments.length > 1) {
return new PizzaException('You can remove only one ingredient per time')
}
if (!Pizza.allowedExtraIngredients.includes(ingredient)) {
return new PizzaException('You cannot remove that ingredient')
}
if (this.extraIngredient.includes(ingredient)) {
this.extraIngredient.splice(this.extraIngredient[ingredient], 1)
} else {
return new PizzaException('You cannot remove the ingredient that was not added')
}
}
getSize() {
return `Pizza size: ${this.size}`
}
getPrice() {
return this.extraIngredient.length
? this.extraIngredient.reduce((a, b) => a + b.price, 0) + this.price + 'UAH'
: this.price + 'UAH'
}
getPizzaInfo() {
return `${this.getSize()}, type: ${this.type}; extra ingredients: ${this.extraIngredient
.map(item => item.ingredient).join(',') || 'none'}; price: ${this.getPrice()}`
}
}
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: 'veggie', price: 50}
Pizza.TYPE_MARGHERITA = {type: 'margherita', price: 60}
Pizza.TYPE_PEPPERONI = {type: 'pepperoni', price: 70}
Pizza.EXTRA_TOMATOES = {ingredient: 'tomatoes', price: 5}
Pizza.EXTRA_CHEESE = {ingredient: 'cheese', price: 7}
Pizza.EXTRA_MEAT = {ingredient: 'meat', price: 9}
Pizza.allowedSizes = [Pizza.SIZE_S, Pizza.SIZE_M, Pizza.SIZE_L]
Pizza.allowedTypes = [Pizza.TYPE_VEGGIE, Pizza.TYPE_MARGHERITA, Pizza.TYPE_PEPPERONI]
Pizza.allowedExtraIngredients = [Pizza.EXTRA_TOMATOES, Pizza.EXTRA_CHEESE, Pizza.EXTRA_MEAT]
class PizzaException {
constructor(log) {
throw new Error(log)
}
}
I need some hints what to do next, because I'm stuck for two days.
I DO NOT NEED TO DO THE WHOLE APP FOR ME...
您可以创建不同大小和不同类型的披萨:
Sizes:
- S (+50 UAH)
- M (+75 UAH)
- L (+100 UAH)
Types:
- VEGGIE (+50 UAH)
- MARGHERITA (+60 UAH)
- PEPPERONI (+70 UAH)
Extra ingredients:
- CHEESE (+7 UAH)
- TOMATOES (+5 UAH)
- MEAT (+9 UAH)
Write a program that calculates the cost of a pizza and info about your pizza. Use an OOP approach (hint: you need a Pizza class, constants, methods for choosing options and calculating the required values).
The code must be error-proof. Imagine another programmer using your class. If it passes the wrong type of pizza, for example, or the wrong kind of ingredient, an exception should be thrown (the error should not be silently ignored) (hint: you need PizzaException class).
Pizza Class Description
Class members:
- properties (size and type are required):
- size: size of pizza (must be from the allowedSizes property)
- type: type of pizza (must be from the allowedTypes property)
- methods:
- addExtraIngredient(ingredient): add extra ingredient. Method must:
- 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 already exists; if there is, show error (you can add one ingredient only once)
- removeExtraIngredient(ingredient): remove extra ingredient. Method must:
- 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(): returns size of pizza
- getPrice(): returns total price
- getPizzaInfo(): returns size, type, extra ingredients and price of pizza
PizzaExeption Class Description
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.
I have this code so far:
'use strict';
function Pizza(size, type) {
this.size = size;
this.type = type;
/* Sizes, types and extra ingredients */
Pizza.SIZE_S = 50;
Pizza.SIZE_M = 75;
Pizza.SIZE_L = 100;
Pizza.TYPE_VEGGIE = 50;
Pizza.TYPE_MARGHERITA = 60;
Pizza.TYPE_PEPPERONI = 70;
Pizza.EXTRA_TOMATOES = 5;
Pizza.EXTRA_CHEESE = 7;
Pizza.EXTRA_MEAT = 9;
/* Allowed properties */
Pizza.allowedSizes = [SIZE_S, SIZE_M, SIZE_L];
Pizza.allowedTypes = [TYPE_VEGGIE, TYPE_MARGHERITA, TYPE_PEPPERONI];
Pizza.allowedExtraIngredients = [EXTRA_TOMATOES, EXTRA_CHEESE, EXTRA_MEAT];
}
function PizzaException() {}
let pizza = new Pizza(Pizza.SIZE_S, Pizza.TYPE_VEGGIE);
pizza.addExtraIngredient(Pizza.EXTRA_MEAT);
console.log(`Price: ${pizza.getPrice()} UAH`);
pizza.addExtraIngredient(Pizza.EXTRA_CHEESE);
pizza.addExtraIngredient(Pizza.EXTRA_TOMATOES);
console.log(`Price with extra ingredients: ${pizza.getPrice()} UAH`); // Price: 121 UAH
console.log(`Is pizza large: ${pizza.getSize() === Pizza.SIZE_L}`); // Is pizza large: false
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
我设法写了这段代码,但我不明白如何在总和中添加额外的成分。
'use strict';
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;
}
}
/* 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};
Pizza.allowedExtraIngredients = [Pizza.EXTRA_TOMATOES, Pizza.EXTRA_CHEESE, Pizza.EXTRA_MEAT ];
let pizza = new Pizza(Pizza.SIZE_S, Pizza.TYPE_VEGGIE);
// add extra meat
pizza.addExtraIngredient(Pizza.EXTRA_MEAT);
// check price
console.log(`Price: ${pizza.getPrice()} UAH`); //=> Price: 109 UAH
如果您的问题是如何实现方法,我可以提出以下建议。顺便说一下,我在这里使用 class
关键字而不是 function
来声明一个 class。我认为它更清楚地描述了您的代码的意图(创建 class,而不是函数)。
class Pizza {
static SIZE_S = 50
static TYPE_VEGGIE = "veggie"
constructor(size, type) {
this.size = size
this.type = type
}
getSize() {
return this.size
}
getPizzaInfo() {
return `Here is your ${this.type} pizza of size ${this.size}!`
}
}
let p = new Pizza(Pizza.SIZE_S, Pizza.TYPE_VEGGIE)
console.log(p.getPizzaInfo())
更新
以上代码转ES5使用prototype
添加方法:
function Pizza(size,type) {
this.size = size
this.type = type
}
Pizza.prototype.getSize = function() {
return this.size
}
Pizza.prototype.getPizzaInfo = function() {
return `Hey, here is your ${this.type} pizza of ${this.size} size!`
}
Pizza.SIZE_S = 50
Pizza.TYPE_VEGGIE = 50
let p = new Pizza(Pizza.SIZE_S, Pizza.TYPE_VEGGIE)
console.log(p.getPizzaInfo())
我终于完成了这个任务。非常感谢 Kokodoko 的建议。以下是答案:
'use strict';
function Pizza(size, type) {
const requiredArguments = 2;
if (arguments.length !== requiredArguments) {
throw new PizzaException(`Required two arguments, given: ${arguments.length}`)
}
if (!Pizza.allowedTypes.includes(type) || !Pizza.allowedSizes.includes(size)) {
throw new PizzaException('Invalid type');
}
const negativeIndex = -1;
let _size = size;
let extrasType = [];
let extrasPrice = [];
this.type = type;
Pizza.prototype.getSize = function () {
return _size.size;
};
Pizza.prototype.getPrice = function () {
return _size.price + this.type.price;
};
Pizza.prototype.addExtraIngredient = function (ingredient) {
if (!ingredient) {
throw new PizzaException('Invalid ingredient')
}
if (type.type === 'VEGGIE' && ingredient.extra === 'MEAT') {
throw new PizzaException('Invalid ingredient');
}
if (extrasType.includes(ingredient.extra)) {
throw new PizzaException('Duplicate ingredient');
}
extrasPrice.push(_size.price += ingredient.price);
extrasType.push(ingredient.extra);
return _size.price;
};
Pizza.prototype.removeExtraIngredient = function (ingredient) {
extrasPrice.pop(this.type.price -= ingredient.price);
const index = extrasType.indexOf(ingredient.extra);
if (index > negativeIndex) {
extrasType.splice(index, 1);
}
return this.type.price;
};
Pizza.prototype.getExtraIngredients = function () {
return extrasPrice;
};
Pizza.prototype.getPizzaInfo = function () {
return `Size: ${_size.size}, type: ${
type.type
}; extra ingredients: ${extrasType}; price: ${
_size.price + this.type.price
}UAH`;
};
}
/* 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: 'VEGGIE', 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.SIZE_S, Pizza.SIZE_M, Pizza.SIZE_L];
Pizza.allowedTypes = [Pizza.TYPE_VEGGIE, Pizza.TYPE_MARGHERITA, Pizza.TYPE_PEPPERONI];
Pizza.allowedExtraIngredients = [Pizza.EXTRA_TOMATOES, Pizza.EXTRA_CHEESE, Pizza.EXTRA_MEAT];
function PizzaException(log) {
this.log = log;
PizzaException.prototype.log = function () {
return log;
};
}
//////////////// Tests //////////////////
// // small pizza, type Margherita 110 UAH
// let pizza = new Pizza(Pizza.SIZE_S, Pizza.TYPE_MARGHERITA);
// // add extra meat 110 + 9 = 119
// pizza.addExtraIngredient(Pizza.EXTRA_MEAT);
// console.log(`Price: ${pizza.getPrice()} UAH`); //Price: 119 UAH
// // add extra cheese 119 + 7 = 126
// pizza.addExtraIngredient(Pizza.EXTRA_CHEESE);
// // add extra tomatoes 126 + 5 = 131;
// pizza.addExtraIngredient(Pizza.EXTRA_TOMATOES);
// console.log(`Price with extra ingredients: ${pizza.getPrice()} UAH`); // Price: 131 UAH
// // check pizza size
// console.log(`Is pizza large: ${pizza.getSize() === Pizza.SIZE_L}`); //Is pizza large: false
// // remove extra ingredient cheese 131 - 7 = 124
// pizza.removeExtraIngredient(Pizza.EXTRA_CHEESE);
// console.log(`Extra ingredients: ${pizza.getExtraIngredients().length}`); // Extra ingredients: 2
// console.log(pizza.getPizzaInfo()); //Size: SMALL, type: MARGHERITA; extra ingredients: MEAT,TOMATOES; price: 124UAH.
//////////// Examples of errors ///////////////////////
/////////////////////////// 1 ///////////////////////
// let pizza = new Pizza(Pizza.SIZE_S); // "Required two arguments, given: 1"
/////////////////////////// 2 ///////////////////////
// let pizza = new Pizza(Pizza.SIZE_S, Pizza.SIZE_S); // "Invalid type"
/////////////////////////// 3 ///////////////////////
// let pizza = new Pizza(Pizza.SIZE_S, Pizza.TYPE_PEPPERONI);
// pizza.addExtraIngredient(Pizza.EXTRA_MEAT);
// pizza.addExtraIngredient(Pizza.EXTRA_MEAT); // "Duplicate ingredient"
/////////////////////////// 4 ///////////////////////
// let pizza = new Pizza(Pizza.SIZE_S, Pizza.TYPE_VEGGIE);
// pizza.addExtraIngredient(Pizza.EXTRA_MEAT); // "Invalid ingredient"
/////////////////////////// 5 ///////////////////////
// let pizza = new Pizza(Pizza.SIZE_S, Pizza.TYPE_VEGGIE);
// pizza.addExtraIngredient(Pizza.EXTRA_CORN); // "Invalid ingredient"
您错过了检查 addExtraIngredient 内的 allowedIngredients 中是否存在此类成分。
Pizza.prototype.addExtraIngredient = function (ingredient) {
...
if (!Pizza.allowedExtraIngredients.includes(ingredient)) {
throw new PizzaException('Invalid ingredient');
}
...
}
这是您的任务,但使用 ES6 类 方法完成
class Pizza {
constructor(type, size) {
this.extraIngredient = []
if (arguments.length < 2) {
return new PizzaException(`Required two arguments, given: ${arguments.length}`)
}
if (!Pizza.allowedTypes.includes(type) || !Pizza.allowedSizes.includes(size)) {
return new PizzaException('Invalid type')
}
this.type = type.type
this.size = size.size
this.price = type.price + size.price
}
addExtraIngredient(ingredient) {
if (arguments.length > 1) {
return new PizzaException('You can add only one ingredient per time')
}
if (!Pizza.allowedExtraIngredients.includes(ingredient)) {
return new PizzaException('You cannot add that ingredient')
}
if (this.extraIngredient.includes(ingredient)) {
return new PizzaException('You can add one ingredient only once')
} else {
this.extraIngredient.push(ingredient)
}
}
removeExtraIngredient(ingredient) {
if (arguments.length > 1) {
return new PizzaException('You can remove only one ingredient per time')
}
if (!Pizza.allowedExtraIngredients.includes(ingredient)) {
return new PizzaException('You cannot remove that ingredient')
}
if (this.extraIngredient.includes(ingredient)) {
this.extraIngredient.splice(this.extraIngredient[ingredient], 1)
} else {
return new PizzaException('You cannot remove the ingredient that was not added')
}
}
getSize() {
return `Pizza size: ${this.size}`
}
getPrice() {
return this.extraIngredient.length
? this.extraIngredient.reduce((a, b) => a + b.price, 0) + this.price + 'UAH'
: this.price + 'UAH'
}
getPizzaInfo() {
return `${this.getSize()}, type: ${this.type}; extra ingredients: ${this.extraIngredient
.map(item => item.ingredient).join(',') || 'none'}; price: ${this.getPrice()}`
}
}
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: 'veggie', price: 50}
Pizza.TYPE_MARGHERITA = {type: 'margherita', price: 60}
Pizza.TYPE_PEPPERONI = {type: 'pepperoni', price: 70}
Pizza.EXTRA_TOMATOES = {ingredient: 'tomatoes', price: 5}
Pizza.EXTRA_CHEESE = {ingredient: 'cheese', price: 7}
Pizza.EXTRA_MEAT = {ingredient: 'meat', price: 9}
Pizza.allowedSizes = [Pizza.SIZE_S, Pizza.SIZE_M, Pizza.SIZE_L]
Pizza.allowedTypes = [Pizza.TYPE_VEGGIE, Pizza.TYPE_MARGHERITA, Pizza.TYPE_PEPPERONI]
Pizza.allowedExtraIngredients = [Pizza.EXTRA_TOMATOES, Pizza.EXTRA_CHEESE, Pizza.EXTRA_MEAT]
class PizzaException {
constructor(log) {
throw new Error(log)
}
}