Venciity

Restaurant Manager 100/100 without validation

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