import { Menu } from './offering.js';//we are importing all the classes from offerings.js import { Cart } from './OrderCart.js';//we are importing Cart classe from offerings.js import input from 'readline-sync';//importing readline-sync library to read input from console. let noodle = new Menu.Noodle();//creating new class object let rice = new Menu.Rice();//creating new class object let drinks = new Menu.Drinks();//creating new class object let cart = new Cart();//creating new class object let str = "\nWelcome to NiceMeal Restaurant!\n" let str1 = "1.MENU\n 2.CART\n 0.QUIT" let str2 = " \t11-> Noodles\n \t12-> Rice\n \t13-> Drinks\n \t9-> Cancel Selection" let str3 = "\nEnter the CODE-\n" //above all string is for printing on console //\t for tab //\n for enter console.log(str, str3, str1);//printing to console let loop_variable = 1; //because m=1 here while loop will run infinite times unless m becomes 0 or false while (loop_variable) { let option = input.question("\nEnter Your Option->> "); //taking input if (option != '9' && option != "2" && option != '0') //not to print if user press 9,0,2 console.log(str3) //printing str3 // line 29 //switch is for runnig operation when user click any option if (option=="1") { console.log(str2); //printing str2 }else if (option=="2"){ cartfun() //calling cartfun function console.log(str, str1); //printing }else if (option=="0"){ loop_variable = 0; //making m=0 to exit the application console.log("\nBye !!! Thank You Come Again...:-)\n") }else if (option=="9"){ console.log(str, str3, str1); }else if (option=="11"){ list(noodle, 111); //calling the function list }else if (option== "12"){ list(rice, 121); //calling the function list }else if (option=="13"){ list(drinks, 131); //calling the function list }else if (option=="111"){ sublist(noodle.getOne(), 1111) //calling the function sublist }else if (option=="112"){ sublist(noodle.getTwo(), 1121) //calling the function sublist }else if (option=="113"){ sublist(noodle.getThree(), 1131) //calling the function sublist }else if (option=="121"){ sublist(rice.getOne(), 1211) //calling the function sublist }else if (option=="122"){ sublist(rice.getTwo(),1221) }else if (option=="131"){ sublist(drinks.getOne(), 1311) //calling the function sublist }else if (option=="132"){ sublist(drinks.getTwo(),1321) }else if (option=="133"){ sublist(drinks.getThree(), 1331) //calling the function sublist }else if (option=="1111" || option=="1112" || option=="1121" || option=="1122" || option=="1131" || option=="1132" || option=="1211" || option=="1212" || option=="1221" || option=="1222" || option=="123" || option=="1112" || option=="1112" || option=="1112" || option=="1112" || option=="1112" || option=="124" || option=="1311" || option=="1312" || option=="1313" || option=="132" || option=="1321" || option=="1322" || option=="1331" || option=="1332" || option=="135" || option=="134"){ process.stdout.moveCursor(0, -2) // move cursor up two line on the console process.stdout.clearLine(1) // from cursor to end delete everything //we did this to delete line 29 see above checkout(option); //now we are checking out i.e adding to cart for above all press CODE }else{ console.log("\nerror in code. Please enter right code...\n") } } //we have made a function list which will all the Items from the offering.js file //it will take the class object - objt of which we want the iteams. exampple we want all items of drinks function list(objt, i) { let list = objt.getItems() //calling function of objt. which we have defined in offering.js for (let index in list) { console.log("\t\t" + i++, "->>", list[index]) //we are printing the list to console so that user can chose } console.log("\t\t" + 9 + "->> " + "Cancel Selection"); //9 for exit the selection. } //we have created the function sublist which will display the sublist of Items user choses //note- not all iteams have sublist therefore we will call this function of speial iteam which have sublist //this function take the sublist we want to display and it starting index CODE function sublist(arrt, i) { for (let counter = 0; counter < arrt.length;) { console.log("\t\t\t" + i++, "->> " + arrt[counter], "(" + arrt[counter + 1] + "$" + ")"); //displaying the sublist counter = counter + 2; //increasing the k so the we choose only sublist not its price as it is array also contain price } console.log("\t\t\t" + 9 + "->> " + "Cancel Selection"); //9 for exit the selection. } //This funtion is made to insert iteams into the cart //this will take the CODE and try to understan which iteam user has selected and the insert into cart in ordercart.js function checkout(op) { let option_string = op.split(""); //split the CODE example if it is 1212 it will bacome array containing 1,2,1,2 let objtemp; let temparr = []; let price = 0; if (option_string[1] == '1') { objtemp = noodle; //if second charater of CODE is 1 it means iteam is in noodle temparr.push("Noodles "); //in temp array we will push all the name of items and subitems to display } if (option_string[1] == '2') { objtemp = rice; //if second charater of CODE is 2 it means iteam is in rice temparr.push("Rice "); } if (option_string[1] == '3') { objtemp = drinks; //if second charater of CODE is 3 it means iteam is in drinks temparr.push("Drinks "); } let listofobj = objtemp.getItems(); //get list of all items // down if 3 part of CODE is 1 it means it is 1 element of list of items and so on... let parameter = option_string[2] == '1' ? 'one' : option_string[2] == '2' ? 'two' : option_string[2] == '3' ? 'three' : option_string[2] == '4' ? 'four' : option_string[2] == '5' ? 'five' : ""; temparr.push(listofobj[parameter]); //this if is for check if item also has sublist or not if yes the fech the name of sublist if (option_string.length == 4) { try { let listofsubitems = option_string[2] == '1' ? objtemp.getOne() : option_string[2] == '2' ? objtemp.getTwo() : option_string[2] == '3' ? objtemp.getThree() : ""; if (option_string[3] == '1') { temparr.push(listofsubitems[0]); price = listofsubitems[1] //getting the price of sublist } else { temparr.push(listofsubitems[2]); price = listofsubitems[3] //getting the price of sublist } } catch (e) { let p1 = objtemp.getPrice(); //getting the object containg the price of items price = p1[parameter]; //getting the price of sublist } } if (price == 0) { let price_value = objtemp.getPrice(); //this will fetch price of iteam if they dont have sublist price = price_value[parameter]; } let q = parseInt(input.question("Enter QTY or 0 to Cancel->> ")); //getting the qty of iteam selected if (q != 0) { cart.setCart(q, temparr.join(" + "), price); //inserting into the cart all info qty,name,price console.log("\nAdded to Cart Thank You !!! ") //displaying the message } console.log(str3); console.log(str2); } //this function will display the cart and do operation on the cart function cartfun() { let cartarray = cart.getCart(); //getting the list of items in the cart let loop_cart = 1; // when w=0 exit cart let flag = 1; //to not alow the user to resend the same order while (loop_cart) { display(cartarray); //this function display the cart on console console.log("\nEnter cart options->\n") console.log("\t\t1.Send Order\n\t\t2.Remove Items\n\t\t0.Back to main menu\n"); let cartoption = input.question("Your Choice ->> "); //getting cart operatio option from user switch (cartoption) { case '1': if (flag == 1) //this will check if user have already send the order or not { if (cartarray.length == 0) { console.log("\nYour Cart is Empty!!!"); break; } let r= discount() if(r==1){ console.log("\nOrder Sent !!! Thank You !!! Please Come Again !!!"); flag = 0; } } else { console.log("\n\t\tWe have Already sent you order!!! enjoy... :-)") } break; case '2': removeitem() //if user select 2 then we remove the iteam flag = 1; break; case '0': loop_cart = 0; break; //exit from the cart default: console.log("\nerror in code. Please enter right code...\n") } } } //this function will accept the cart and display the cart function display(cartbox) { let count = 1; console.log("\t\t===========================================================================\n") for (let items of cartbox) { console.log("\t\t\t" + count + "->> " + "QTY: " + items.qty, " " + items.choice, "(" + "$" + items.price + ")");//print count++; } console.log("\n\t\t===========================================================================") console.log("\t\t\tTotal: "+ "$"+ cart.getTotal() ); console.log("\t\t===========================================================================") } //this function will remove the items from the cart function removeitem() { let index = input.question(" Which Item do you want to remove? please specify index ->> "); //get index of item user want to delete cart.removeCart(index - 1); //remove the item. this function is in ordercart.js } //this function will check if user have coupon and subtract the amount from total function discount() { var coupon = [123, 345, 876, 987, 334] let coup = input.question("Do you have Discount Coupon? press 0 for NO and 1 for YES->>>>> ") let find; if (coup == '1') { let couponNumber = input.question("please enter your coupon number->>>>> ") couponNumber = parseInt(couponNumber); find = coupon.find(function (element) { //find coupon genuine return element == couponNumber; }); if (find) //check if user has entered right coupon {console.log("Success your coupon has been applied !!!\nyou receive a $4 dicount !!!") cart.discount(4); //call function in cart to subtract from amount return 1; } else { console.log("Sorry wrong coupon !"); return 0; } } }
Write, Run & Share Javascript code online using OneCompiler's JS online compiler for free. It's one of the robust, feature-rich online compilers for Javascript language. Getting started with the OneCompiler's Javascript editor is easy and fast. The editor shows sample boilerplate code when you choose language as Javascript and start coding.
Javascript(JS) is a object-oriented programming language which adhere to ECMA Script Standards. Javascript is required to design the behaviour of the web pages.
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
rl.on('line', function(line){
console.log("Hello, " + line);
});
Keyword | Description | Scope |
---|---|---|
var | Var is used to declare variables(old way of declaring variables) | Function or global scope |
let | let is also used to declare variables(new way) | Global or block Scope |
const | const is used to declare const values. Once the value is assigned, it can not be modified | Global or block Scope |
let greetings = `Hello ${name}`
const msg = `
hello
world!
`
An array is a collection of items or values.
let arrayName = [value1, value2,..etc];
// or
let arrayName = new Array("value1","value2",..etc);
let mobiles = ["iPhone", "Samsung", "Pixel"];
// accessing an array
console.log(mobiles[0]);
// changing an array element
mobiles[3] = "Nokia";
Arrow Functions helps developers to write code in concise way, it’s introduced in ES6.
Arrow functions can be written in multiple ways. Below are couple of ways to use arrow function but it can be written in many other ways as well.
() => expression
const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const squaresOfEvenNumbers = numbers.filter(ele => ele % 2 == 0)
.map(ele => ele ** 2);
console.log(squaresOfEvenNumbers);
let [firstName, lastName] = ['Foo', 'Bar']
let {firstName, lastName} = {
firstName: 'Foo',
lastName: 'Bar'
}
const {
title,
firstName,
lastName,
...rest
} = record;
//Object spread
const post = {
...options,
type: "new"
}
//array spread
const users = [
...adminUsers,
...normalUsers
]
function greetings({ name = 'Foo' } = {}) { //Defaulting name to Foo
console.log(`Hello ${name}!`);
}
greet() // Hello Foo
greet({ name: 'Bar' }) // Hi Bar
IF is used to execute a block of code based on a condition.
if(condition){
// code
}
Else part is used to execute the block of code when the condition fails.
if(condition){
// code
} else {
// code
}
Switch is used to replace nested If-Else statements.
switch(condition){
case 'value1' :
//code
[break;]
case 'value2' :
//code
[break;]
.......
default :
//code
[break;]
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
ES6 introduced classes along with OOPS concepts in JS. Class is similar to a function which you can think like kind of template which will get called when ever you initialize class.
class className {
constructor() { ... } //Mandatory Class method
method1() { ... }
method2() { ... }
...
}
class Mobile {
constructor(model) {
this.name = model;
}
}
mbl = new Mobile("iPhone");