-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui_consola.js
147 lines (105 loc) · 3.87 KB
/
ui_consola.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const scanner = require("readline-sync");
const Validator = require("./validator.js")
const Service =require("./service.js");
const chalk = require("chalk");
class Ui_consola
{
validator;
srv;
constructor(validator,service)
{
this.validator = validator;
this.srv = service;
console.log(typeof srv)
}
print_cmd()
{
console.log(chalk.yellow("exit->0"))
console.log(chalk.yellow("search a stock->1"))
console.log(chalk.yellow("Filter stocks->2"))
console.log(chalk.yellow("Add a stock->3"))
console.log(chalk.yellow("show stocks->4"));
}
print_stocks()
{
let stocks = this.srv.print_data()
stocks.forEach( (stock)=>{
console.log(chalk.blue(stock.name + " stock costs: " + stock.price+"$"));
})
}
search_stock()
{
// throw Error("nu mere")
let stock_name= scanner.question(chalk.green("Enter Stock name: "))
let price =this.srv.search_stock(stock_name);
console.log(chalk.blue(stock_name + " stock costs: " + price+"$"));
}
filter()
{
let price = scanner.question(chalk.green("enter Stock price for filter: "))
this.validator.validate_float(price);
console.log(chalk.yellow("CMD FOR ORDER:1->Ascending order"));
console.log(chalk.yellow(" 0)->Descending order"));
let order = scanner.question(chalk.green("enter order: "));
//console.log(order);
if(order !="1" && order !="0")
throw Error("Order must be 0 or 1")
let stocks = this.srv.filter_stocks(price,order)
console.log(chalk.yellow("stocks after order:"))
stocks.forEach( (stock)=>{
console.log(chalk.blue(stock.name + " stocks costs: " + stock.price+"$"));
})
}
add_stock()
{
let name = scanner.question(chalk.green("Add Stock name: "))
this.validator.validate_uppercase(name);
let price = scanner.question(chalk.green("Add price for Stock: "))
this.validator.validate_float(price);
this.srv.add_stock(name,price);
console.log(chalk.blue("stock added"));
}
// nu putem sa rulam cu un while pentru ca avem async functions in node si pusca
// trenuie sa fac functia recursiva pentru a putea folosti readline (exista un npm readline-sync care imi poate rezolva problema dar am decis sa nu il folosesc)
run()
{
let cmd
this.print_cmd();
let run=1
while(run){
let cmd = scanner.question("Enter command: ")
try{
switch (cmd){
case("0"):
console.log("closing program");
console.log("\n")
run=0;
break;
case("1"):
this.search_stock();
console.log("\n")
break;
case("2"):
this.filter();
console.log("\n")
break;
case("3"):
this.add_stock();
console.log("\n")
break;
case("4"):
this.print_stocks();
console.log("\n");
break;
default:
console.log(chalk.red("invalid comand... try again\n"))
}
}catch(err)
{
console.log(chalk.red(err.message + "\n"))
}
}
console.log("end");
}
}
module.exports = Ui_consola;