Venciity

Restaurant manager - Alex Code 100/100

Nov 11th, 2014
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function processRestaurantManagerCommands(commands) {
  2.     'use strict';
  3.  
  4.     var RestaurantEngine = (function () {
  5.         var _restaurants, _recipes;
  6.  
  7.         Object.prototype.extends = function (parent) {
  8.             // old browsers support
  9.             if (!Object.create) {
  10.                 Object.prototype.create = function (proto) {
  11.                     function F() { };
  12.                     F.prototype = proto;
  13.                     return new F;
  14.                 };
  15.             };
  16.  
  17.             this.prototype = Object.create(parent.prototype);
  18.             this.prototype.constructor = this;
  19.         };
  20.  
  21.         Object.prototype.validateString = function (value, varName) {
  22.             if (typeof (value) != 'string' || !value) {
  23.                 throw new Error("The " + varName + " is required.");
  24.             };
  25.         }
  26.  
  27.         Object.prototype.validateNumber = function (value, varName) {
  28.             if (value < 0) {
  29.                 throw new Error("The " + varName + " must be positive.");
  30.             };
  31.         }
  32.  
  33.         function initialize() {
  34.             _restaurants = [];
  35.             _recipes = [];
  36.         }
  37.  
  38.         var Restaurant = (function () {
  39.             function Restaurant(name, location) {
  40.                 this.setName(name);
  41.                 this.setLocation(location);
  42.                 this._restaurantRecipes = [];
  43.             }
  44.  
  45.             Restaurant.prototype.getName = function () {
  46.                 return this._name;
  47.             }
  48.  
  49.             Restaurant.prototype.setName = function (name) {
  50.                 this.validateString(name, 'name');
  51.                 this._name = name;
  52.             }
  53.  
  54.             Restaurant.prototype.getLocation = function () {
  55.                 return this._location;
  56.             }
  57.  
  58.             Restaurant.prototype.setLocation = function (location) {
  59.                 this.validateString(location, 'location');
  60.                 this._location = location;
  61.             }
  62.  
  63.             Restaurant.prototype.addRecipe = function (recipe) {
  64.                 this._restaurantRecipes.push(recipe);
  65.             }
  66.  
  67.             Restaurant.prototype.removeRecipe = function (recipe) {
  68.                 var index = this._restaurantRecipes.indexOf(recipe);
  69.                 this._restaurantRecipes.splice(index, 1);
  70.             }
  71.  
  72.             Restaurant.prototype.printRestaurantMenu = function () {
  73.                 var drinksRecipes = new String;
  74.                 var saladRecipes = new String;
  75.                 var mainCourseRecipes = new String;
  76.                 var dessertRecipes = new String;
  77.  
  78.                 var length = this._restaurantRecipes.length;
  79.  
  80.                 var restaurantMenu = "***** " + this.getName() + " - " + this.getLocation() + " *****\n";
  81.  
  82.                 if (length == 0) {
  83.                     restaurantMenu += "No recipes... yet\n";
  84.                 }
  85.                 else {
  86.                     this._restaurantRecipes.sort(function (a, b) {
  87.                         if (a.getName() < b.getName()) {
  88.                             return -1;
  89.                         }
  90.                         if (a.getName() > b.getName()) {
  91.                             return 1;
  92.                         }
  93.                         return 0;
  94.                     });
  95.  
  96.                     for (var i = 0; i < length; i++) {
  97.                         var currentRecipe = this._restaurantRecipes[i];
  98.  
  99.                         if (currentRecipe instanceof Drink) {
  100.                             drinksRecipes += currentRecipe.toString();
  101.                         }
  102.                         if (currentRecipe instanceof Salad) {
  103.                             saladRecipes += currentRecipe.toString();
  104.                         }
  105.                         if (currentRecipe instanceof MainCourse) {
  106.                             mainCourseRecipes += currentRecipe.toString();
  107.                         }
  108.                         if (currentRecipe instanceof Dessert) {
  109.                             dessertRecipes += currentRecipe.toString();
  110.                         }
  111.                     }
  112.  
  113.                     if (drinksRecipes.length > 0) {
  114.                         restaurantMenu += "~~~~~ DRINKS ~~~~~\n" + drinksRecipes;
  115.                     }
  116.                     if (saladRecipes.length > 0) {
  117.                         restaurantMenu += "~~~~~ SALADS ~~~~~\n" + saladRecipes;
  118.                     }
  119.                     if (mainCourseRecipes.length > 0) {
  120.                         restaurantMenu += "~~~~~ MAIN COURSES ~~~~~\n" + mainCourseRecipes;
  121.                     }
  122.                     if (dessertRecipes.length > 0) {
  123.                         restaurantMenu += "~~~~~ DESSERTS ~~~~~\n" + dessertRecipes;
  124.                     }
  125.                 }
  126.  
  127.                 return restaurantMenu;
  128.             };
  129.  
  130.             return Restaurant;
  131.         }())
  132.  
  133.  
  134.         var Recipe = (function () {
  135.             function Recipe(name, price, caloriesPerServing, quantity, timeToPrepare) {
  136.                 if (this.constructor === Recipe) {
  137.                     throw new Error("Can't instantiate abstract class Recipe.");
  138.                 };
  139.                 this.setName(name);
  140.                 this.setPrice(price);
  141.                 this.setCaloriesPerServing(caloriesPerServing);
  142.                 this.setQuantity(quantity);
  143.                 this.setTimeToPrepare(timeToPrepare);
  144.             }
  145.  
  146.             Recipe.prototype.getName = function () {
  147.                 return this._name;
  148.             }
  149.  
  150.             Recipe.prototype.setName = function (name) {
  151.                 this.validateString(name, "name");
  152.                 this._name = name;
  153.             }
  154.  
  155.             Recipe.prototype.getPrice = function () {
  156.                 return this._price.toFixed(2);
  157.             }
  158.  
  159.             Recipe.prototype.setPrice = function (price) {
  160.                 this.validateNumber(price, "price");
  161.                 this._price = price;
  162.             }
  163.  
  164.             Recipe.prototype.getCaloriesPerServing = function () {
  165.                 return this._caloriesPerServing;
  166.             }
  167.  
  168.             Recipe.prototype.setCaloriesPerServing = function (caloriesPerServing) {
  169.                 this.validateNumber(caloriesPerServing, "calories per serving");
  170.                 if (this.constructor === Drink) {
  171.                     if (caloriesPerServing > 100) {
  172.                         throw new Error("The calories must not be greater than 100");
  173.                     };
  174.                 };
  175.                 this._caloriesPerServing = caloriesPerServing;
  176.             }
  177.  
  178.             Recipe.prototype.getQuantity = function () {
  179.                 return this._quantity;
  180.             }
  181.  
  182.             Recipe.prototype.setQuantity = function (quantity) {
  183.                 this.validateNumber(quantity, "quantity per serving");
  184.                 this._quantity = quantity;
  185.             }
  186.  
  187.             Recipe.prototype.getTimeToPrepare = function () {
  188.                 return this._timeToPrepare;
  189.             }
  190.  
  191.             Recipe.prototype.setTimeToPrepare = function (timeToPrepare) {
  192.                 this.validateNumber(timeToPrepare, "time to prepare");
  193.                 if (this.constructor === Drink) {
  194.                     if (timeToPrepare > 20) {
  195.                         throw new Error("The time to prepare must not be greater than 20 minutes");
  196.                     };
  197.                 };
  198.                 this._timeToPrepare = timeToPrepare;
  199.             }
  200.  
  201.             Recipe.prototype.toString = function () {
  202.                 var unit;
  203.                 if (this instanceof Drink) {
  204.                     unit = "ml";
  205.                 } else if (this instanceof Meal) {
  206.                     unit = "g";
  207.                 }
  208.  
  209.                 return "==  " + this.getName() + " == $" + this.getPrice() +
  210.                 "\nPer serving: " + this.getQuantity() + " " + unit +
  211.                 ", " + this.getCaloriesPerServing() + " kcal" +
  212.                 "\nReady in " + this.getTimeToPrepare() + " minutes";
  213.             }
  214.  
  215.             return Recipe;
  216.         }())
  217.  
  218.         var Drink = (function () {
  219.             function Drink(name, price, caloriesPerServing, quantity, timeToPrepare, isCarbonated) {
  220.                 Recipe.call(this, name, price, caloriesPerServing, quantity, timeToPrepare);
  221.                 this.setIsCarbonated(isCarbonated);
  222.             }
  223.  
  224.             Drink.extends(Recipe);
  225.  
  226.             Drink.prototype.getIsCarbonated = function () {
  227.                 return this._isCarbonated;
  228.             }
  229.  
  230.             Drink.prototype.setIsCarbonated = function (isCarbonated) {
  231.                 this._isCarbonated = isCarbonated;
  232.             }
  233.  
  234.             Drink.prototype.toString = function () {
  235.                 var carbonated = this.getIsCarbonated() ? "yes" : "no";
  236.                 return Recipe.prototype.toString.call(this) +
  237.                 "\nCarbonated: " + carbonated + "\n";
  238.             }
  239.  
  240.             return Drink;
  241.         }())
  242.  
  243.         var Meal = (function () {
  244.             function Meal(name, price, caloriesPerServing, quantity, timeToPrepare, isVegan) {
  245.                 if (this.constructor === Meal) {
  246.                     throw new Error("Can't instantiate abstract class Meal");
  247.                 };
  248.                 Recipe.call(this, name, price, caloriesPerServing, quantity, timeToPrepare);
  249.                 this.setIsVegan(isVegan);
  250.  
  251.             }
  252.  
  253.             Meal.extends(Recipe);
  254.  
  255.             Meal.prototype.getIsVegan = function () {
  256.                 return this._isVegan;
  257.             }
  258.  
  259.             Meal.prototype.setIsVegan = function (isVegan) {
  260.                 this._isVegan = isVegan;
  261.                 //finish code
  262.             }
  263.  
  264.             Meal.prototype.toggleVegan = function () {
  265.                 if (this._isVegan) {
  266.                     this._isVegan = false;
  267.                 }
  268.                 else {
  269.                     this._isVegan = true;
  270.                 }
  271.             }
  272.  
  273.             Meal.prototype.toString = function () {
  274.                 if (this.getIsVegan() || this.constructor === Salad) {
  275.                     return "[VEGAN] " + Recipe.prototype.toString.call(this);
  276.                 }
  277.                 else {
  278.                     return Recipe.prototype.toString.call(this);
  279.                 }
  280.             }
  281.  
  282.             return Meal;
  283.         }())
  284.  
  285.         var Dessert = (function () {
  286.             function Dessert(name, price, calories, quantityquantity, timeToPrepare, isVegan, withSugar) {
  287.                 Meal.call(this, name, price, calories, quantityquantity, timeToPrepare, isVegan);
  288.                 this.setWithSugar(withSugar);
  289.             }
  290.  
  291.             Dessert.extends(Meal);
  292.  
  293.             Dessert.prototype.getWithSugar = function () {
  294.                 return this._withSugar;
  295.             };
  296.  
  297.             Dessert.prototype.setWithSugar = function (withSugar) {
  298.                 this._withSugar = typeof withSugar == 'undefined' ? true : withSugar;
  299.             };
  300.  
  301.             Dessert.prototype.toggleSugar = function () {
  302.                 if (this._withSugar) {
  303.                     this._withSugar = false;
  304.                 } else {
  305.                     this._withSugar = true;
  306.                 }
  307.             };
  308.  
  309.             Dessert.prototype.toString = function () {
  310.                 if (!this.getWithSugar()) {
  311.                     return "[NO SUGAR] " + Meal.prototype.toString.call(this) + "\n";
  312.                 } else {
  313.                     return Meal.prototype.toString.call(this) + "\n";
  314.                 }
  315.             };
  316.  
  317.             return Dessert;
  318.         }());
  319.  
  320.         var MainCourse = (function () {
  321.             function MainCourse(name, price, caloriesPerServing, quantity, timeToPrepare, isVegan, type) {
  322.                 Meal.call(this, name, price, caloriesPerServing, quantity, timeToPrepare, isVegan);
  323.                 this.setType(type);
  324.             }
  325.  
  326.             MainCourse.extends(Meal);
  327.  
  328.             MainCourse.prototype.getType = function () {
  329.                 return this._type;
  330.             }
  331.  
  332.             MainCourse.prototype.setType = function (type) {
  333.                 this._type = type;
  334.             }
  335.  
  336.             MainCourse.prototype.toString = function () {
  337.                 return Meal.prototype.toString.call(this) +
  338.                 "\nType: " + this.getType() + "\n";
  339.             }
  340.  
  341.             return MainCourse;
  342.         }())
  343.  
  344.         var Salad = (function () {
  345.             function Salad(name, price, caloriesPerServing, quantity, timeToPrepare, isVegan, containsPasta) {
  346.                 Meal.call(this, name, price, caloriesPerServing, quantity, timeToPrepare, isVegan);
  347.                 this.setContainsPasta(containsPasta);
  348.             }
  349.  
  350.             Salad.extends(Meal);
  351.  
  352.             Salad.prototype.getContainsPasta = function () {
  353.                 return this._containsPasta;
  354.             }
  355.  
  356.             Salad.prototype.setContainsPasta = function (containsPasta) {
  357.                 this._containsPasta = containsPasta;
  358.             }
  359.  
  360.             Salad.prototype.toString = function () {
  361.                 var pasta = this.getContainsPasta() ? "yes" : "no";
  362.                 return Meal.prototype.toString.call(this) +
  363.                 "\nContains pasta: " + pasta + "\n";
  364.             }
  365.  
  366.             return Salad;
  367.         }())
  368.  
  369.         var Command = (function () {
  370.  
  371.             function Command(commandLine) {
  372.                 this._params = new Array();
  373.                 this.translateCommand(commandLine);
  374.             }
  375.  
  376.             Command.prototype.translateCommand = function (commandLine) {
  377.                 var self, paramsBeginning, name, parametersKeysAndValues;
  378.                 self = this;
  379.                 paramsBeginning = commandLine.indexOf("(");
  380.  
  381.                 this._name = commandLine.substring(0, paramsBeginning);
  382.                 name = commandLine.substring(0, paramsBeginning);
  383.                 parametersKeysAndValues = commandLine
  384.                     .substring(paramsBeginning + 1, commandLine.length - 1)
  385.                     .split(";")
  386.                     .filter(function (e) { return true });
  387.  
  388.                 parametersKeysAndValues.forEach(function (p) {
  389.                     var split = p
  390.                         .split("=")
  391.                         .filter(function (e) { return true; });
  392.                     self._params[split[0]] = split[1];
  393.                 });
  394.             }
  395.  
  396.             return Command;
  397.         }());
  398.  
  399.         function createRestaurant(name, location) {
  400.             _restaurants[name] = new Restaurant(name, location);
  401.             return "Restaurant " + name + " created\n";
  402.         }
  403.  
  404.         function createDrink(name, price, calories, quantity, timeToPrepare, isCarbonated) {
  405.             _recipes[name] = new Drink(name, price, calories, quantity, timeToPrepare, isCarbonated);
  406.             return "Recipe " + name + " created\n";
  407.         }
  408.  
  409.         function createSalad(name, price, calories, quantity, timeToPrepare, containsPasta) {
  410.             _recipes[name] = new Salad(name, price, calories, quantity, timeToPrepare, containsPasta);
  411.             return "Recipe " + name + " created\n";
  412.         }
  413.  
  414.         function createMainCourse(name, price, calories, quantity, timeToPrepare, isVegan, type) {
  415.             _recipes[name] = new MainCourse(name, price, calories, quantity, timeToPrepare, isVegan, type);
  416.             return "Recipe " + name + " created\n";
  417.         }
  418.  
  419.         function createDessert(name, price, calories, quantity, timeToPrepare, isVegan) {
  420.             _recipes[name] = new Dessert(name, price, calories, quantity, timeToPrepare, isVegan);
  421.             return "Recipe " + name + " created\n";
  422.         }
  423.  
  424.         function toggleSugar(name) {
  425.             var recipe;
  426.  
  427.             if (!_recipes.hasOwnProperty(name)) {
  428.                 throw new Error("The recipe " + name + " does not exist");
  429.             }
  430.             recipe = _recipes[name];
  431.  
  432.             if (recipe instanceof Dessert) {
  433.                 recipe.toggleSugar();
  434.                 return "Command ToggleSugar executed successfully. New value: " + recipe._withSugar.toString().toLowerCase() + "\n";
  435.             } else {
  436.                 return "The command ToggleSugar is not applicable to recipe " + name + "\n";
  437.             }
  438.         }
  439.  
  440.         function toggleVegan(name) {
  441.             var recipe;
  442.  
  443.             if (!_recipes.hasOwnProperty(name)) {
  444.                 throw new Error("The recipe " + name + " does not exist");
  445.             }
  446.  
  447.             recipe = _recipes[name];
  448.             if (recipe instanceof Meal) {
  449.                 recipe.toggleVegan();
  450.                 return "Command ToggleVegan executed successfully. New value: " +
  451.                     recipe._isVegan.toString().toLowerCase() + "\n";
  452.             } else {
  453.                 return "The command ToggleVegan is not applicable to recipe " + name + "\n";
  454.             }
  455.         }
  456.  
  457.         function printRestaurantMenu(name) {
  458.             var restaurant;
  459.  
  460.             if (!_restaurants.hasOwnProperty(name)) {
  461.                 throw new Error("The restaurant " + name + " does not exist");
  462.             }
  463.  
  464.             restaurant = _restaurants[name];
  465.             return restaurant.printRestaurantMenu();
  466.         }
  467.  
  468.         function addRecipeToRestaurant(restaurantName, recipeName) {
  469.             var restaurant, recipe;
  470.  
  471.             if (!_restaurants.hasOwnProperty(restaurantName)) {
  472.                 throw new Error("The restaurant " + restaurantName + " does not exist");
  473.             }
  474.             if (!_recipes.hasOwnProperty(recipeName)) {
  475.                 throw new Error("The recipe " + recipeName + " does not exist");
  476.             }
  477.  
  478.             restaurant = _restaurants[restaurantName];
  479.             recipe = _recipes[recipeName];
  480.             restaurant.addRecipe(recipe);
  481.             return "Recipe " + recipeName + " successfully added to restaurant " + restaurantName + "\n";
  482.         }
  483.  
  484.         function removeRecipeFromRestaurant(restaurantName, recipeName) {
  485.             var restaurant, recipe;
  486.  
  487.             if (!_recipes.hasOwnProperty(recipeName)) {
  488.                 throw new Error("The recipe " + recipeName + " does not exist");
  489.             }
  490.             if (!_restaurants.hasOwnProperty(restaurantName)) {
  491.                 throw new Error("The restaurant " + restaurantName + " does not exist");
  492.             }
  493.  
  494.             restaurant = _restaurants[restaurantName];
  495.             recipe = _recipes[recipeName];
  496.             restaurant.removeRecipe(recipe);
  497.             return "Recipe " + recipeName + " successfully removed from restaurant " + restaurantName + "\n";
  498.         }
  499.  
  500.         function executeCommand(commandLine) {
  501.             var cmd, params, result;
  502.             cmd = new Command(commandLine);
  503.             params = cmd._params;
  504.  
  505.             switch (cmd._name) {
  506.                 case 'CreateRestaurant':
  507.                     result = createRestaurant(params["name"], params["location"]);
  508.                     break;
  509.                 case 'CreateDrink':
  510.                     result = createDrink(params["name"], parseFloat(params["price"]), parseInt(params["calories"]),
  511.                         parseInt(params["quantity"]), params["time"], parseBoolean(params["carbonated"]));
  512.                     break;
  513.                 case 'CreateSalad':
  514.                     result = createSalad(params["name"], parseFloat(params["price"]), parseInt(params["calories"]),
  515.                         parseInt(params["quantity"]), params["time"], parseBoolean(params["pasta"]));
  516.                     break;
  517.                 case "CreateMainCourse":
  518.                     result = createMainCourse(params["name"], parseFloat(params["price"]), parseInt(params["calories"]),
  519.                         parseInt(params["quantity"]), params["time"], parseBoolean(params["vegan"]), params["type"]);
  520.                     break;
  521.                 case "CreateDessert":
  522.                     result = createDessert(params["name"], parseFloat(params["price"]), parseInt(params["calories"]),
  523.                         parseInt(params["quantity"]), params["time"], parseBoolean(params["vegan"]));
  524.                     break;
  525.                 case "ToggleSugar":
  526.                     result = toggleSugar(params["name"]);
  527.                     break;
  528.                 case "ToggleVegan":
  529.                     result = toggleVegan(params["name"]);
  530.                     break;
  531.                 case "AddRecipeToRestaurant":
  532.                     result = addRecipeToRestaurant(params["restaurant"], params["recipe"]);
  533.                     break;
  534.                 case "RemoveRecipeFromRestaurant":
  535.                     result = removeRecipeFromRestaurant(params["restaurant"], params["recipe"]);
  536.                     break;
  537.                 case "PrintRestaurantMenu":
  538.                     result = printRestaurantMenu(params["name"]);
  539.                     break;
  540.                 default:
  541.                     throw new Error('Invalid command name: ' + cmdName);
  542.             }
  543.  
  544.             return result;
  545.         }
  546.  
  547.         function parseBoolean(value) {
  548.             switch (value) {
  549.                 case "yes":
  550.                     return true;
  551.                 case "no":
  552.                     return false;
  553.                 default:
  554.                     throw new Error("Invalid boolean value: " + value);
  555.             }
  556.         }
  557.  
  558.         return {
  559.             initialize: initialize,
  560.             executeCommand: executeCommand
  561.         };
  562.     }());
  563.  
  564.  
  565.     // Process the input commands and return the results
  566.     var results = '';
  567.     RestaurantEngine.initialize();
  568.     commands.forEach(function (cmd) {
  569.         if (cmd != "") {
  570.             try {
  571.                 var cmdResult = RestaurantEngine.executeCommand(cmd);
  572.                 results += cmdResult;
  573.             } catch (err) {
  574.                 results += err.message + "\n";
  575.             }
  576.         }
  577.     });
  578.  
  579.     return results.trim();
  580. }
  581.  
  582. // ------------------------------------------------------------
  583. // Read the input from the console as array and process it
  584. // Remove all below code before submitting to the judge system!
  585. // ------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment