Home > front end >  Create a program that calculates cost of a pizza, using OOP (JS) approach. I am very new to OOP , an
Create a program that calculates cost of a pizza, using OOP (JS) approach. I am very new to OOP , an

Time:11-30

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...

You can create pizza of different sizes and different types:

  1. Sizes:

    • S ( 50 UAH)
    • M ( 75 UAH)
    • L ( 100 UAH)
  2. Types:

    • VEGGIE ( 50 UAH)
    • MARGHERITA ( 60 UAH)
    • PEPPERONI ( 70 UAH)
  3. 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):
    1. size: size of pizza (must be from the allowedSizes property)
    2. type: type of pizza (must be from the allowedTypes property)
  • methods:
    • addExtraIngredient(ingredient): add extra ingredient. Method must:
      1. Accept only one parameter, otherwise show an error
      2. Check if such an ingredient exists in allowedIngredients, if does not exist, show an error
      3. 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:
      1. Accept only one parameter, otherwise show an error
      2. Check if such an ingredient exists in allowedIngredients, if does not exist, show an error
      3. 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:
    1. 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

I managed to write this code, but I don't understand how to add extra ingredient to the total sum.

'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

CodePudding user response:

If your question is how to implement methods, I could suggest the following. By the way, I use the class keyword here instead of function to declare a class. I think it more clearly describes the intent of your code (which is creating a class, not a function).

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())

UPDATE

The above code converted to ES5 using prototype to add methods:

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())

CodePudding user response:

Finally I managed to do this task. Thanks a lot for Kokodoko advice. Below is the answer:

'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"
  • Related