-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPricingApp.js
107 lines (87 loc) · 2.85 KB
/
PricingApp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//Product class
//Product data
//1. enter ideal price of the products [enter product no. ]
//2. show product information
// ask for different prices for a product
// user will tell a productId for which he wants to add the price
// we will seek that product from the list of products
// add the price against that product
// addPriceForProduct(2, 20.00); // "productId not available"; if available then add the price to that product
// Products.FindById(prodId)[0].enterIdealPrice(2, 20.00)
function Product(name, id) {
this.productName = name;
this.productId = id;
this.productPrice = []; //array to hold prices entered by different users.
this.enterIdealPrice = function(price) {
this.productPrice.push(
price
); /*a method of product class to push the values to productPrice array.
not sure if this is correct? */
};
}
const products = [];
main();
function main() {
const iPhone = addNewProduct("iPhone", 1);
const android = addNewProduct("Samsung Galaxy", 2);
products.push(iPhone);
products.push(android);
listProducts(); //list products
productSelection(); //user gets a prompt to select a product to which the ideal price has to be added
}
function listProducts() {
products.forEach(item =>
console.log(
"Product Id: " + item.productId + "\t Product name: " + item.productName
)
);
}
function findById(productId) {
// return products.filter(p => p.productId == productId); //filter returns an array so changing it to use find method instead.
return products.find(p => p.productId == productId);
}
function addNewProduct(productName, productId) {
const product = new Product(productName, productId);
return product;
}
function readConsole(){
const readline = require("readline");
const ask = readline.createInterface({
input: process.stdin,
output: process.stdout
});
return ask;
}
function productSelection() {
var ask = readConsole();
var selectedProduct = -1;
var selectAProduct = function(selectedProduct) {
if (selectedProduct == 0) {
ask.close();
return;
}
var enterIdealPrice = function(userIdealPrice) {
//search for the selected product
const prod = findById(selectedProduct);
if (prod == null) {
console.log("Product not found.");
} else {
prod.enterIdealPrice(userIdealPrice);
console.log(prod.productPrice);
console.log("Product Ideal price added successfully.");
}
recursiveAsyncReadLine();
}
ask.question(
"Please enter ideal price for the selected product: ",
userIdealPrice => enterIdealPrice(userIdealPrice)
);
}
var recursiveAsyncReadLine = function() {
ask.question(
"Please select a product by entering Product Id: ",
selectedProduct => selectAProduct(selectedProduct)
);
};
recursiveAsyncReadLine();
}