Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve() {
- let robot = {
- protein: 0,
- carbohydrates: 0,
- fat: 0,
- flavour: 0,
- prepareCoke: function (quantity) {
- if (this.carbohydrates < 10 * Number(quantity)) {
- return 'Error: not enough carbohydrates in stock';
- }
- if (this.flavour < 20 * Number(quantity)) {
- return `Error: not enough flavour in stock`;
- }
- this.carbohydrates -= 10 * Number(quantity);
- this.flavour -= 20 * Number(quantity);
- return "Success";
- },
- prepareApple: function (quantity) {
- if (this.carbohydrates < Number(quantity)) {
- return `Error: not enough carbohydrates in stock`;
- }
- if (this.flavour < 2 * Number(quantity)) {
- return `Error: not enough flavour in stock`;
- }
- this.carbohydrates -= Number(quantity);
- this.flavour -= 2 * Number(quantity);
- return "Success";
- },
- prepareBurger: function (quantity) {
- if (this.carbohydrates < 5 * Number(quantity)) {
- return `Error: not enough carbohydrates in stock`;
- }
- if (this.fat < 7 * Number(quantity)) {
- return `Error: not enough fat in stock`;
- }
- if (this.flavour < 3 * Number(quantity)) {
- return `Error: not enough flavour in stock`;
- }
- this.carbohydrates -= 5 * Number(quantity);
- this.fat -= 7 * Number(quantity);
- this.flavour -= 3 * Number(quantity);
- return "Success";
- },
- prepareOmelet: function (quantity) {
- if (this.protein < 5 * Number(quantity)) {
- return `Error: not enough protein in stock`;
- }
- if (this.fat < Number(quantity)) {
- return `Error: not enough fat in stock`;
- }
- if (this.flavour < Number(quantity)) {
- return `Error: not enough flavour in stock`;
- }
- this.protein -= 5 * Number(quantity);
- this.fat -= Number(quantity);
- this.flavour -= Number(quantity);
- return "Success";
- },
- prepareCheverme: function (quantity) {
- if (this.protein < 10 * Number(quantity)) {
- return `Error: not enough protein in stock`;
- }
- if (this.carbohydrates < 10 * Number(quantity)) {
- return `Error: not enough carbohydrates in stock`;
- }
- if (this.fat < 10 * Number(quantity)) {
- return `Error: not enough fat in stock`;
- }
- if (this.flavour < 10 * Number(quantity)) {
- return `Error: not enough flavour in stock`;
- }
- this.protein -= 10 * Number(quantity);
- this.carbohydrates -= 10 * Number(quantity);
- this.fat -= 10 * Number(quantity);
- this.flavour -= 10 * Number(quantity);
- return "Success";
- }
- };
- return function (instruction) {
- let commandArgs = instruction.split(' ');
- switch (commandArgs[0]) {
- case "restock":
- restock(commandArgs[1], commandArgs[2]);
- break;
- case "prepare":
- prepare(commandArgs[1], commandArgs[2]);
- break;
- case "report":
- report();
- break;
- }
- function restock(ingredient, quantity) {
- if (robot.hasOwnProperty(ingredient)) {
- robot[ingredient] += Number(quantity);
- return "Success";
- }
- }
- function prepare(item, quantity) {
- switch (item) {
- case "coke":
- robot.prepareCoke(quantity);
- break;
- case "apple":
- robot.prepareApple(quantity);
- break;
- case "burger":
- robot.prepareBurger(quantity);
- break;
- case "cheverme":
- robot.prepareCheverme(quantity);
- break;
- case "omelet":
- robot.prepareOmelet(quantity);
- break;
- }
- }
- function report() {
- return `protein=${robot.protein} carbohydrate=${robot.carbohydrates} fat=${robot.fat} flavour=${robot.flavour}`;
- }
- }
- }
- let manager = solve();
- manager('restock flavour 50');
- manager("report");
- manager("prepare coke 4");
- manager("report");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement