Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function processRestaurantManagerCommands(commands) {
- 'use strict';
- var RestaurantEngine = (function () {
- var _restaurants, _recipes;
- Object.prototype.extends = function (parent) {
- // old browsers support
- if (!Object.create) {
- Object.prototype.create = function (proto) {
- function F() { };
- F.prototype = proto;
- return new F;
- };
- };
- this.prototype = Object.create(parent.prototype);
- this.prototype.constructor = this;
- };
- Object.prototype.validateString = function (value, varName) {
- if (typeof (value) != 'string' || !value) {
- throw new Error("The " + varName + " is required.");
- };
- }
- Object.prototype.validateNumber = function (value, varName) {
- if (value < 0) {
- throw new Error("The " + varName + " must be positive.");
- };
- }
- function initialize() {
- _restaurants = [];
- _recipes = [];
- }
- var Restaurant = (function () {
- function Restaurant(name, location) {
- this.setName(name);
- this.setLocation(location);
- this._restaurantRecipes = [];
- }
- Restaurant.prototype.getName = function () {
- return this._name;
- }
- Restaurant.prototype.setName = function (name) {
- this.validateString(name, 'name');
- this._name = name;
- }
- Restaurant.prototype.getLocation = function () {
- return this._location;
- }
- Restaurant.prototype.setLocation = function (location) {
- this.validateString(location, 'location');
- this._location = location;
- }
- Restaurant.prototype.addRecipe = function (recipe) {
- this._restaurantRecipes.push(recipe);
- }
- Restaurant.prototype.removeRecipe = function (recipe) {
- var index = this._restaurantRecipes.indexOf(recipe);
- this._restaurantRecipes.splice(index, 1);
- }
- Restaurant.prototype.printRestaurantMenu = function () {
- var drinksRecipes = new String;
- var saladRecipes = new String;
- var mainCourseRecipes = new String;
- var dessertRecipes = new String;
- var length = this._restaurantRecipes.length;
- var restaurantMenu = "***** " + this.getName() + " - " + this.getLocation() + " *****\n";
- if (length == 0) {
- restaurantMenu += "No recipes... yet\n";
- }
- else {
- this._restaurantRecipes.sort(function (a, b) {
- if (a.getName() < b.getName()) {
- return -1;
- }
- if (a.getName() > b.getName()) {
- return 1;
- }
- return 0;
- });
- for (var i = 0; i < length; i++) {
- var currentRecipe = this._restaurantRecipes[i];
- if (currentRecipe instanceof Drink) {
- drinksRecipes += currentRecipe.toString();
- }
- if (currentRecipe instanceof Salad) {
- saladRecipes += currentRecipe.toString();
- }
- if (currentRecipe instanceof MainCourse) {
- mainCourseRecipes += currentRecipe.toString();
- }
- if (currentRecipe instanceof Dessert) {
- dessertRecipes += currentRecipe.toString();
- }
- }
- if (drinksRecipes.length > 0) {
- restaurantMenu += "~~~~~ DRINKS ~~~~~\n" + drinksRecipes;
- }
- if (saladRecipes.length > 0) {
- restaurantMenu += "~~~~~ SALADS ~~~~~\n" + saladRecipes;
- }
- if (mainCourseRecipes.length > 0) {
- restaurantMenu += "~~~~~ MAIN COURSES ~~~~~\n" + mainCourseRecipes;
- }
- if (dessertRecipes.length > 0) {
- restaurantMenu += "~~~~~ DESSERTS ~~~~~\n" + dessertRecipes;
- }
- }
- return restaurantMenu;
- };
- return Restaurant;
- }())
- var Recipe = (function () {
- function Recipe(name, price, caloriesPerServing, quantity, timeToPrepare) {
- if (this.constructor === Recipe) {
- throw new Error("Can't instantiate abstract class Recipe.");
- };
- this.setName(name);
- this.setPrice(price);
- this.setCaloriesPerServing(caloriesPerServing);
- this.setQuantity(quantity);
- this.setTimeToPrepare(timeToPrepare);
- }
- Recipe.prototype.getName = function () {
- return this._name;
- }
- Recipe.prototype.setName = function (name) {
- this.validateString(name, "name");
- this._name = name;
- }
- Recipe.prototype.getPrice = function () {
- return this._price.toFixed(2);
- }
- Recipe.prototype.setPrice = function (price) {
- this.validateNumber(price, "price");
- this._price = price;
- }
- Recipe.prototype.getCaloriesPerServing = function () {
- return this._caloriesPerServing;
- }
- Recipe.prototype.setCaloriesPerServing = function (caloriesPerServing) {
- this.validateNumber(caloriesPerServing, "calories per serving");
- if (this.constructor === Drink) {
- if (caloriesPerServing > 100) {
- throw new Error("The calories must not be greater than 100");
- };
- };
- this._caloriesPerServing = caloriesPerServing;
- }
- Recipe.prototype.getQuantity = function () {
- return this._quantity;
- }
- Recipe.prototype.setQuantity = function (quantity) {
- this.validateNumber(quantity, "quantity per serving");
- this._quantity = quantity;
- }
- Recipe.prototype.getTimeToPrepare = function () {
- return this._timeToPrepare;
- }
- Recipe.prototype.setTimeToPrepare = function (timeToPrepare) {
- this.validateNumber(timeToPrepare, "time to prepare");
- if (this.constructor === Drink) {
- if (timeToPrepare > 20) {
- throw new Error("The time to prepare must not be greater than 20 minutes");
- };
- };
- this._timeToPrepare = timeToPrepare;
- }
- Recipe.prototype.toString = function () {
- var unit;
- if (this instanceof Drink) {
- unit = "ml";
- } else if (this instanceof Meal) {
- unit = "g";
- }
- return "== " + this.getName() + " == $" + this.getPrice() +
- "\nPer serving: " + this.getQuantity() + " " + unit +
- ", " + this.getCaloriesPerServing() + " kcal" +
- "\nReady in " + this.getTimeToPrepare() + " minutes";
- }
- return Recipe;
- }())
- var Drink = (function () {
- function Drink(name, price, caloriesPerServing, quantity, timeToPrepare, isCarbonated) {
- Recipe.call(this, name, price, caloriesPerServing, quantity, timeToPrepare);
- this.setIsCarbonated(isCarbonated);
- }
- Drink.extends(Recipe);
- Drink.prototype.getIsCarbonated = function () {
- return this._isCarbonated;
- }
- Drink.prototype.setIsCarbonated = function (isCarbonated) {
- this._isCarbonated = isCarbonated;
- }
- Drink.prototype.toString = function () {
- var carbonated = this.getIsCarbonated() ? "yes" : "no";
- return Recipe.prototype.toString.call(this) +
- "\nCarbonated: " + carbonated + "\n";
- }
- return Drink;
- }())
- var Meal = (function () {
- function Meal(name, price, caloriesPerServing, quantity, timeToPrepare, isVegan) {
- if (this.constructor === Meal) {
- throw new Error("Can't instantiate abstract class Meal");
- };
- Recipe.call(this, name, price, caloriesPerServing, quantity, timeToPrepare);
- this.setIsVegan(isVegan);
- }
- Meal.extends(Recipe);
- Meal.prototype.getIsVegan = function () {
- return this._isVegan;
- }
- Meal.prototype.setIsVegan = function (isVegan) {
- this._isVegan = isVegan;
- //finish code
- }
- Meal.prototype.toggleVegan = function () {
- if (this._isVegan) {
- this._isVegan = false;
- }
- else {
- this._isVegan = true;
- }
- }
- Meal.prototype.toString = function () {
- if (this.getIsVegan() || this.constructor === Salad) {
- return "[VEGAN] " + Recipe.prototype.toString.call(this);
- }
- else {
- return Recipe.prototype.toString.call(this);
- }
- }
- return Meal;
- }())
- var Dessert = (function () {
- function Dessert(name, price, calories, quantityquantity, timeToPrepare, isVegan, withSugar) {
- Meal.call(this, name, price, calories, quantityquantity, timeToPrepare, isVegan);
- this.setWithSugar(withSugar);
- }
- Dessert.extends(Meal);
- Dessert.prototype.getWithSugar = function () {
- return this._withSugar;
- };
- Dessert.prototype.setWithSugar = function (withSugar) {
- this._withSugar = typeof withSugar == 'undefined' ? true : withSugar;
- };
- Dessert.prototype.toggleSugar = function () {
- if (this._withSugar) {
- this._withSugar = false;
- } else {
- this._withSugar = true;
- }
- };
- Dessert.prototype.toString = function () {
- if (!this.getWithSugar()) {
- return "[NO SUGAR] " + Meal.prototype.toString.call(this) + "\n";
- } else {
- return Meal.prototype.toString.call(this) + "\n";
- }
- };
- return Dessert;
- }());
- var MainCourse = (function () {
- function MainCourse(name, price, caloriesPerServing, quantity, timeToPrepare, isVegan, type) {
- Meal.call(this, name, price, caloriesPerServing, quantity, timeToPrepare, isVegan);
- this.setType(type);
- }
- MainCourse.extends(Meal);
- MainCourse.prototype.getType = function () {
- return this._type;
- }
- MainCourse.prototype.setType = function (type) {
- this._type = type;
- }
- MainCourse.prototype.toString = function () {
- return Meal.prototype.toString.call(this) +
- "\nType: " + this.getType() + "\n";
- }
- return MainCourse;
- }())
- var Salad = (function () {
- function Salad(name, price, caloriesPerServing, quantity, timeToPrepare, isVegan, containsPasta) {
- Meal.call(this, name, price, caloriesPerServing, quantity, timeToPrepare, isVegan);
- this.setContainsPasta(containsPasta);
- }
- Salad.extends(Meal);
- Salad.prototype.getContainsPasta = function () {
- return this._containsPasta;
- }
- Salad.prototype.setContainsPasta = function (containsPasta) {
- this._containsPasta = containsPasta;
- }
- Salad.prototype.toString = function () {
- var pasta = this.getContainsPasta() ? "yes" : "no";
- return Meal.prototype.toString.call(this) +
- "\nContains pasta: " + pasta + "\n";
- }
- return Salad;
- }())
- var Command = (function () {
- function Command(commandLine) {
- this._params = new Array();
- this.translateCommand(commandLine);
- }
- Command.prototype.translateCommand = function (commandLine) {
- var self, paramsBeginning, name, parametersKeysAndValues;
- self = this;
- paramsBeginning = commandLine.indexOf("(");
- this._name = commandLine.substring(0, paramsBeginning);
- name = commandLine.substring(0, paramsBeginning);
- parametersKeysAndValues = commandLine
- .substring(paramsBeginning + 1, commandLine.length - 1)
- .split(";")
- .filter(function (e) { return true });
- parametersKeysAndValues.forEach(function (p) {
- var split = p
- .split("=")
- .filter(function (e) { return true; });
- self._params[split[0]] = split[1];
- });
- }
- return Command;
- }());
- function createRestaurant(name, location) {
- _restaurants[name] = new Restaurant(name, location);
- return "Restaurant " + name + " created\n";
- }
- function createDrink(name, price, calories, quantity, timeToPrepare, isCarbonated) {
- _recipes[name] = new Drink(name, price, calories, quantity, timeToPrepare, isCarbonated);
- return "Recipe " + name + " created\n";
- }
- function createSalad(name, price, calories, quantity, timeToPrepare, containsPasta) {
- _recipes[name] = new Salad(name, price, calories, quantity, timeToPrepare, containsPasta);
- return "Recipe " + name + " created\n";
- }
- function createMainCourse(name, price, calories, quantity, timeToPrepare, isVegan, type) {
- _recipes[name] = new MainCourse(name, price, calories, quantity, timeToPrepare, isVegan, type);
- return "Recipe " + name + " created\n";
- }
- function createDessert(name, price, calories, quantity, timeToPrepare, isVegan) {
- _recipes[name] = new Dessert(name, price, calories, quantity, timeToPrepare, isVegan);
- return "Recipe " + name + " created\n";
- }
- function toggleSugar(name) {
- var recipe;
- if (!_recipes.hasOwnProperty(name)) {
- throw new Error("The recipe " + name + " does not exist");
- }
- recipe = _recipes[name];
- if (recipe instanceof Dessert) {
- recipe.toggleSugar();
- return "Command ToggleSugar executed successfully. New value: " + recipe._withSugar.toString().toLowerCase() + "\n";
- } else {
- return "The command ToggleSugar is not applicable to recipe " + name + "\n";
- }
- }
- function toggleVegan(name) {
- var recipe;
- if (!_recipes.hasOwnProperty(name)) {
- throw new Error("The recipe " + name + " does not exist");
- }
- recipe = _recipes[name];
- if (recipe instanceof Meal) {
- recipe.toggleVegan();
- return "Command ToggleVegan executed successfully. New value: " +
- recipe._isVegan.toString().toLowerCase() + "\n";
- } else {
- return "The command ToggleVegan is not applicable to recipe " + name + "\n";
- }
- }
- function printRestaurantMenu(name) {
- var restaurant;
- if (!_restaurants.hasOwnProperty(name)) {
- throw new Error("The restaurant " + name + " does not exist");
- }
- restaurant = _restaurants[name];
- return restaurant.printRestaurantMenu();
- }
- function addRecipeToRestaurant(restaurantName, recipeName) {
- var restaurant, recipe;
- if (!_restaurants.hasOwnProperty(restaurantName)) {
- throw new Error("The restaurant " + restaurantName + " does not exist");
- }
- if (!_recipes.hasOwnProperty(recipeName)) {
- throw new Error("The recipe " + recipeName + " does not exist");
- }
- restaurant = _restaurants[restaurantName];
- recipe = _recipes[recipeName];
- restaurant.addRecipe(recipe);
- return "Recipe " + recipeName + " successfully added to restaurant " + restaurantName + "\n";
- }
- function removeRecipeFromRestaurant(restaurantName, recipeName) {
- var restaurant, recipe;
- if (!_recipes.hasOwnProperty(recipeName)) {
- throw new Error("The recipe " + recipeName + " does not exist");
- }
- if (!_restaurants.hasOwnProperty(restaurantName)) {
- throw new Error("The restaurant " + restaurantName + " does not exist");
- }
- restaurant = _restaurants[restaurantName];
- recipe = _recipes[recipeName];
- restaurant.removeRecipe(recipe);
- return "Recipe " + recipeName + " successfully removed from restaurant " + restaurantName + "\n";
- }
- function executeCommand(commandLine) {
- var cmd, params, result;
- cmd = new Command(commandLine);
- params = cmd._params;
- switch (cmd._name) {
- case 'CreateRestaurant':
- result = createRestaurant(params["name"], params["location"]);
- break;
- case 'CreateDrink':
- result = createDrink(params["name"], parseFloat(params["price"]), parseInt(params["calories"]),
- parseInt(params["quantity"]), params["time"], parseBoolean(params["carbonated"]));
- break;
- case 'CreateSalad':
- result = createSalad(params["name"], parseFloat(params["price"]), parseInt(params["calories"]),
- parseInt(params["quantity"]), params["time"], parseBoolean(params["pasta"]));
- break;
- case "CreateMainCourse":
- result = createMainCourse(params["name"], parseFloat(params["price"]), parseInt(params["calories"]),
- parseInt(params["quantity"]), params["time"], parseBoolean(params["vegan"]), params["type"]);
- break;
- case "CreateDessert":
- result = createDessert(params["name"], parseFloat(params["price"]), parseInt(params["calories"]),
- parseInt(params["quantity"]), params["time"], parseBoolean(params["vegan"]));
- break;
- case "ToggleSugar":
- result = toggleSugar(params["name"]);
- break;
- case "ToggleVegan":
- result = toggleVegan(params["name"]);
- break;
- case "AddRecipeToRestaurant":
- result = addRecipeToRestaurant(params["restaurant"], params["recipe"]);
- break;
- case "RemoveRecipeFromRestaurant":
- result = removeRecipeFromRestaurant(params["restaurant"], params["recipe"]);
- break;
- case "PrintRestaurantMenu":
- result = printRestaurantMenu(params["name"]);
- break;
- default:
- throw new Error('Invalid command name: ' + cmdName);
- }
- return result;
- }
- function parseBoolean(value) {
- switch (value) {
- case "yes":
- return true;
- case "no":
- return false;
- default:
- throw new Error("Invalid boolean value: " + value);
- }
- }
- return {
- initialize: initialize,
- executeCommand: executeCommand
- };
- }());
- // Process the input commands and return the results
- var results = '';
- RestaurantEngine.initialize();
- commands.forEach(function (cmd) {
- if (cmd != "") {
- try {
- var cmdResult = RestaurantEngine.executeCommand(cmd);
- results += cmdResult;
- } catch (err) {
- results += err.message + "\n";
- }
- }
- });
- return results.trim();
- }
- // ------------------------------------------------------------
- // Read the input from the console as array and process it
- // Remove all below code before submitting to the judge system!
- // ------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment