-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudent.js
104 lines (104 loc) · 3.71 KB
/
student.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
import inquirer from "inquirer";
let studentSystem = [];
while (true) {
let options = await inquirer.prompt([
{
type: "list",
name: "selection",
message: "What would you like to do?",
choices: ["Add_Student", "Remove_Student", "View_Students", "See_Details", "Exit"]
}
]);
if (options.selection === "Add_Student") {
let newStudentName = await inquirer.prompt({
type: "input",
name: "name",
message: "Enter the student's name: "
});
let newStudentAge = await inquirer.prompt({
type: "number",
name: "age",
message: "Enter the student's age: "
});
let newStudentId = await inquirer.prompt({
type: "number",
name: "id",
message: "Enter the student's ID: "
});
// Check for duplicate ID
let duplicateId = studentSystem.find(student => student.id === newStudentId.id);
if (duplicateId) {
console.log("This ID is already in use. Please enter a unique ID.");
}
else {
let student = {
name: newStudentName.name,
age: newStudentAge.age,
id: newStudentId.id
};
studentSystem.push(student);
}
}
else if (options.selection === "View_Students") {
if (studentSystem.length === 0) {
console.log("The list is empty");
}
else {
let studentList = studentSystem.map(student => student.name).join("\n");
console.log(studentList);
}
}
else if (options.selection === "Remove_Student") {
let removeOption = await inquirer.prompt([
{
type: "list",
name: "removelist",
message: "How do you want to remove the student?",
choices: ["Find from list", "Search by ID"]
}
]);
if (removeOption.removelist === "Find from list") {
let studentList = studentSystem.map(student => student.name);
let removeStudentByList = await inquirer.prompt([
{
type: "list",
name: "removeByList",
message: "Which student do you want to remove?",
choices: studentList
}
]);
const index = studentSystem.findIndex(student => student.name === removeStudentByList.removeByList);
if (index !== -1) {
console.log(`${studentSystem[index].name} has been removed from the list`);
studentSystem.splice(index, 1);
}
}
if (removeOption.removelist === "Search by ID") {
let removeStudentByID = await inquirer.prompt([
{
type: "input",
name: "removeByID",
message: "Enter the student's ID: "
}
]);
const index = studentSystem.findIndex(student => student.id === Number(removeStudentByID.removeByID));
if (index !== -1) {
console.log(`Student with ID ${studentSystem[index].id} has been removed from the list`);
studentSystem.splice(index, 1);
}
}
}
else if (options.selection === "See_Details") {
if (studentSystem.length === 0) {
console.log("The list is empty");
}
else {
console.log(studentSystem
.map(student => `Name: ${student.name}, Age: ${student.age}, Student ID: ${student.id}`)
.join("\n"));
}
}
if (options.selection === "Exit") {
break;
}
}