-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCLI.cpp
More file actions
59 lines (55 loc) · 1.76 KB
/
CLI.cpp
File metadata and controls
59 lines (55 loc) · 1.76 KB
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
// Avigail Danesh 213372063
// Noy Dvori 211908256
#include "CLI.h"
#include <string>
#include "DefaultIO.h"
using namespace std;
void CLI::start() {
bool stop = false;
// prints the welcome message on the first try.
bool firstTime = true;
while (!stop) {
if (firstTime) {
_dio.write("printWelcome to the KNN Classifier Server. Please choose an option:\n");
firstTime = false;
} else {
// normal message.
_dio.write("printPlease choose an option:\n");
}
// prints description of every command.
for (int i = 0; i < _commands.size(); i++) {
_dio.write("print" + to_string(i + 1) + ". " + _commands[i]->description() + "\n");
}
// exit option.
_dio.write("write8. exit\n");
// client's choice.
string input = _dio.read();
// must be an integer between 1 and the number of commands.
if (!isInteger(input) || stoi(input) < 1 || (stoi(input) > _commands.size() + 1 && stoi(input) != 8)) {
// asks for input until a valid choice is entered.
do {
_dio.write("writeInvalid choice! Please try again.\n");
input = _dio.read();
} while (!isInteger(input) || stoi(input) < 1 || stoi(input) > _commands.size() + 1);
}
// executes chosen command.
int choice = stoi(input);
if (choice == 8) {
stop = true;
_dio.write("end");
} else {
_commands[choice - 1]->execute();
}
}
}
bool CLI::isInteger(const std::string &str) {
if (str.empty()) {
return false;
}
for (const char &c: str) {
if (!isdigit(c)) {
return false;
}
}
return true;
}