js achieve student management system

let readline = require ( "readline-sync"); // introduced into the input module
// clear screen function
let clear = () => process.stdout.write(process.platform === 'win32' ? '\x1Bc' : '\x1B[2J\x1B[3J\x1B[H');
// Administrator Information
let adminInfo = [
{ "adminID": "dengfeng", "adminPWD": "123" },
{ "adminID": "liangdaye", "adminPWD": "321" }
];
// Initialize the three students in the class information using a 2-dimensional array to store
stuInfo years = [
[
{ "stuID": "1", "stuName": "bdy", "stuAge": "18", "stuGender": "male", "stuScore": "100", "className": "1班" },
{ "stuID": "2", "stuName": "luc", "stuAge": "23", "stuGender": "female", "stuScore": "89", "className": "1班" },
{ "stuID": "3", "stuName": "lc", "stuAge": "17", "stuGender": "male", "stuScore": "93", "className": "1班" },
{ "stuID": "4", "stuName": "xu", "stuAge": "19", "stuGender": "female", "stuScore": "91", "className": "1班" },
{ "stuID": "5", "stuName": "chao", "stuAge": "20", "stuGender": "male", "stuScore": "95", "className": "1班" }
],
[
{ "stuID": "1", "stuName": "wf", "stuAge": "18", "stuGender": "female", "stuScore": "85", "className": "2班" },
{ "stuID": "2", "stuName": "xtf", "stuAge": "19", "stuGender": "female", "stuScore": "91", "className": "2班" },
{ "stuID": "3", "stuName": "xj", "stuAge": "19", "stuGender": "male", "stuScore": "92", "className": "2班" },
{ "stuID": "4", "stuName": "ls", "stuAge": "21", "stuGender": "male", "stuScore": "94", "className": "2班" },
{ "stuID": "5", "stuName": "xjs", "stuAge": "22", "stuGender": "female", "stuScore": "89", "className": "2班" }
],
[
{ "stuID": "1", "stuName": "yaj", "stuAge": "22", "stuGender": "male", "stuScore": "86", "className": "3班" },
{ "stuID": "2", "stuName": "xj", "stuAge": "25", "stuGender": "female", "stuScore": "89", "className": "3班" },
{ "stuID": "3", "stuName": "xiz", "stuAge": "24", "stuGender": "female", "stuScore": "80", "className": "3班" },
{ "stuID": "4", "stuName": "yhq", "stuAge": "26", "stuGender": "female", "stuScore": "92", "className": "3班" },
{ "stuID": "5", "stuName": "npa", "stuAge": "19", "stuGender": "male", "stuScore": "96", "className": "3班" }
]
];
// administrator account password can login authentication function returns true on the match can not match the returns false
let adminLogin = function (adminID, adminPWD) {
for (let i = 0; i < adminInfo.length; i++) {
if (adminID === adminInfo[i].adminID && adminPWD === adminInfo[i].adminPWD) {
return true;
}
}
return false;
}
// Class Selection function for printing out the user-selectable class returns a string
let classSelect = function () {
let str = "";
// for loop stitching all classes, for example: 1. 2. Class 1 Class 2 Class 3 3. 4. Return
for (let i = 0; i < stuInfo.length; i++) {
if (stuInfo[i] !== null) {
str += (i + 1) + "." + "第" + (i + 1) + "班 ";
}
}
return "Please select the class:" + str + (stuInfo.length + 1) + "return.";
}
// class confirmation function for confirming the user to enter the correct class
let classCheck = function (classNo) {
for (let i = 0; i < stuInfo.length; i++) {
if (classNo === (i + 1) && stuInfo[i] !== null) {
return true;
}
}
return false;
}
// class query function receives a parameter query the user wants to print out the number of classes for all students of class information
let classQuery = function (classNo) {
clear();
the console.log ( "class information is as follows:");
console.log ( "Class Student ID Name Age Sex fraction");
let str = ""; // declare an empty string for stitching inside the class student information
for (let i = 0; i < stuInfo[classNo - 1].length; i++) {
str += stuInfo[classNo - 1][i].className + "\t" + stuInfo[classNo - 1][i].stuID + "\t" + stuInfo[classNo - 1][i].stuName + "\t" + stuInfo[classNo - 1][i].stuAge + "\t" + stuInfo[classNo - 1][i].stuGender + "\t" + stuInfo[classNo - 1][i].stuScore;
console.log (str); // good stitching line on the print line
str = ""; // empty string for the next splicing
}
}
// student receives a query function parameter student's name (string) to print out the names of all students
let stuQuery = function (stuName) {
let num = 0;
let str = ""; // declare an empty string for stitching inside the class student information
for (let i = 0; i < stuInfo.length; i++) {
for (let j = 0; j < stuInfo[i].length; j++) {
if (stuName === stuInfo[i][j].stuName) {
a ++;
if (num === 1) {
console.log ( "find the names of the student, the student specific information is as follows:");
console.log ( "Class Student ID Name Age Sex fraction");
}
str += stuInfo[i][j].className + "\t" + stuInfo[i][j].stuID + "\t" + stuInfo[i][j].stuName + "\t" + stuInfo[i][j].stuAge + "\t" + stuInfo[i][j].stuGender + "\t" + stuInfo[i][j].stuScore;
console.log(str);
str = "";
}
}
}
if (num === 0) {
console.log ( "Sorry, did not find the student");
}
}
// function takes two parameters confirm the student class number and student number to confirm the existence of the student
let stuCheck = function (classNo, stuNo) {
for (let i = 0; i < stuInfo[classNo - 1].length; i++) {
if (stuNo === stuInfo[classNo - 1][i].stuID) {
return stuInfo [classNo - 1] [i]; // return information about the student's specific
}
}
return false;
}
// modify student information function accepts two arguments student objects and modify options
let editInfo = function (singleStuInfo, editSelect) {
switch (editSelect) {
// Edit name
case 1:
{
console.log ( "Please enter the name of the student revised:");
let newStuName = readline.question("");
singleStuInfo.stuName = newStuName;
console.log ( "modified successfully, press the Enter key to return");
}
break;
// modify Age
case 2:
{
console.log ( "Please enter the age of the students revised:");
let newStuAge = readline.question("");
singleStuInfo.stuAge = newStuAge;
console.log ( "modified successfully, press the Enter key to return");
}
break;
// modify sex
case 3:
{
let editGender = true;
let str = "";
while (editGender) {
clear();
str = "";
console.log ( "the current student information is:");
console.log ( "Class Student ID Name Age Sex fraction");
str += singleStuInfo.className + "\t" + singleStuInfo.stuID + "\t" + singleStuInfo.stuName + "\t" + singleStuInfo.stuAge + "\t" + singleStuInfo.stuGender + "\t" + singleStuInfo.stuScore;
console.log(str);
console.log ( "Please select edit student information: 1. Name 2. Age 3. Gender 4. Score 5. Return");
console.log ( "Please choose the gender of students: 1. Male 2. Female");
let newStuGender = parseInt(readline.question(""));
switch (newStuGender) {
case 1:
singleStuInfo.stuGender = "male";
editGender = false;
console.log ( "modified successfully, press the Enter key to return");
break;
case 2:
singleStuInfo.stuGender = "female";
editGender = false;
console.log ( "modified successfully, press the Enter key to return");
break;
default:
console.log ( "input is incorrect, please re-enter");
console.log ( "press the Enter key to continue");
readline.question("");
}
}
}
break;
// modify scores
case 4:
{
console.log ( "Enter the student after the modified score:");
let newStuScore = readline.question("");
singleStuInfo.stuScore = newStuScore;
console.log ( "modified successfully, press the Enter key to return");
}
break;
default:
console.log ( "Select error, please re-select");
break;
}
readline.question("");
}
// add student information function accepts five parameters class number, student's name, age, sex, scores
let addStu = function (classNo, addStuName, addStuAge, addStuGender, addStuScore) {
for (let i = 0; i < stuInfo.length; i++) {
if (classNo === (i + 1)) {
let length = stuInfo[i].length;
stuInfo[i][length] = {};
stuInfo[i][length].stuID = length + 1 + "";
stuInfo[i][length].stuName = addStuName;
stuInfo[i][length].stuAge = addStuAge;
addStuGender === 1 ? stuInfo[i][length].stuGender = "male" : stuInfo[i][length].stuGender = "female";
stuInfo[i][length].stuScore = addStuScore;
stuInfo[i][length].className = classNo + "班";
console.log ( "New Student Success!");
}
}
}
// delete function takes a class argument to delete the class number
let delClass = function (classNo) {
while (true) {
clear();
console.log(classNo + "班");
let str = "";
for (let i = 0; i < stuInfo[classNo - 1].length; i++) {
str += stuInfo[classNo - 1][i].className + "\t" + stuInfo[classNo - 1][i].stuID + "\t" + stuInfo[classNo - 1][i].stuName + "\t" + stuInfo[classNo - 1][i].stuAge + "\t" + stuInfo[classNo - 1][i].stuGender + "\t" + stuInfo[classNo - 1][i].stuScore;
console.log (str); // good stitching line on the print line
str = ""; // empty string for the next splicing
}
console.log ( "Are you sure to delete this class (Y / N)?");
let isDel = readline.question("");
switch (isDel) {
case "Y":
case "y":
stuInfo[classNo - 1] = null;
console.log ( "Delete Class successfully, press Enter to return");
readline.question("");
return;
case "N":
case "n":
return;
default:
console.log ( "input is incorrect, please re-enter!");
readline.question("");
}
}
}
// delete function accepts two parameters students to be deleted class where the number of students and student objects
late delStu = function (classNo, stu, func) {
let str = "";
for (let i = 0; i < stuInfo[classNo - 1].length; i++) {
if (stu.stuID === stuInfo[classNo - 1][i].stuID) {
if (func === "del") {
while (true) {
clear();
str = "";
console.log ( "student information you want to delete is:");
console.log ( "Class Student ID Name Age Sex fraction");
str += stuInfo[classNo - 1][i].className + "\t" + stuInfo[classNo - 1][i].stuID + "\t" + stuInfo[classNo - 1][i].stuName + "\t" + stuInfo[classNo - 1][i].stuAge + "\t" + stuInfo[classNo - 1][i].stuGender + "\t" + stuInfo[classNo - 1][i].stuScore;
console.log(str);
console.log ( "whether to confirm the deletion (Y / N)?");
let isDel = readline.question("");
switch (isDel) {
case "Y":
case "y":
stuInfo[classNo - 1].splice(i, 1);
console.log ( "delete student success, press the Enter key to return");
readline.question("");
return;
case "N":
case "n":
return;
default:
console.log ( "input error, please re-enter, press the Enter key to continue");
readline.question("");
}
}
}
else {
stuInfo[classNo - 1].splice(i, 1);
}
}
}
}
// change classes function accepts three parameters of the original target number of classes students go to class number
let classTransfer = function (classNo, singleStuInfo, classNo2) {
delStu(classNo, singleStuInfo, "trans");
let length = stuInfo[classNo2 - 1].length;
if (length === 0) {
singleStuInfo.stuID = length + 1 + "";
}
else {
singleStuInfo.stuID = (parseInt(stuInfo[classNo2 - 1][length - 1].stuID) + 1) + "";
}
singleStuInfo.className = classNo2 + "班";
stuInfo[classNo2 - 1][length] = singleStuInfo;
}
let main = function () {
let loginChance = 3; // initialize the opportunity to log on 3 times
while (loginChance) // Just log is not zero chance to get into the system
{
clear();
console.log ( "Please enter your account number:");
let adminID = readline.question("");
console.log ( "Please enter your password:");
let adminPWD = readline.question("");
if (adminLogin (adminID, adminPWD)) // If the administrator authentication by entering if you
{
let useSys = true; // set using the system identification USESYS true value
while (useSys) {
clear();
console.log ( "Welcome to the Student Management System");
console.log ( "Please select the function you want to perform: 1 query 2. Modify 3. Increased 4. Delete 5. change classes 6. Exit.");
let funcSelect = parseInt(readline.question(""));
switch (funcSelect) {
// query function
case 1:
let query = true; // set the query identifying the query is true
while (query) {
clear()
console.log ( "Please select the query you want to: 1 class 2. Students query query 3. Return.");
let querySelect = parseInt(readline.question(""));
switch (querySelect) {
// class query
case 1:
{
clear();
let str = ""; // declare a temporary string for stitching tips
let classSelectStr = classSelect();
while (true) {
clear();
console.log(classSelectStr);
let classNo = parseInt(readline.question(""));
if (classCheck(classNo)) {
classQuery(classNo);
}
else {
if (classNo === stuInfo.length + 1) {
break;
}
else {
console.log ( "input is incorrect, please re-enter");
}
}
the console.log ( "Press ENTER to return");
readline.question("");
}
break;
}
// student inquiry
case 2:
{
clear();
console.log ( "Please enter your query in the student's name:");
let stuName = readline.question("");
stuQuery(stuName);
the console.log ( "Press ENTER to return");
readline.question("");
break;
}
case 3:
query = false; // set the query is false identification query
break;
default:
console.log ( "input error, please re-enter (press the Enter key to continue)!");
readline.question("");
}
}
break;
// modify the function
case 2:
{
let editChoose = true;
while (editChoose) {
clear();
let str = "";
let classSelectStr = classSelect();
console.log(classSelectStr);
let classNo = parseInt(readline.question(""));
if (classCheck(classNo)) {
clear();
console.log ( "Please enter your want to modify students' learning number:");
let stuNo = readline.question("");
let singleStuInfo = stuCheck(classNo, stuNo);
if (singleStuInfo) {
while (true) {
clear();
str = "";
console.log ( "the current student information is:");
console.log ( "Class Student ID Name Age Sex fraction");
str += singleStuInfo.className + "\t" + singleStuInfo.stuID + "\t" + singleStuInfo.stuName + "\t" + singleStuInfo.stuAge + "\t" + singleStuInfo.stuGender + "\t" + singleStuInfo.stuScore;
console.log(str);
console.log ( "Please select edit student information: 1. Name 2. Age 3. Gender 4. Score 5. Return");
let editSelect = parseInt(readline.question(""));
if (editSelect !== 5) {
editInfo(singleStuInfo, editSelect);
}
else {
editChoose = false;
break;
}
}
}
else {
console.log ( "I'm sorry not to find the corresponding student!");
the console.log ( "Press ENTER to return");
readline.question("");
break;
}
}
else {
if (classNo === stuInfo.length + 1) {
break;
}
else {
console.log ( "input is incorrect, please re-enter");
the console.log ( "Press ENTER to return");
readline.question("");
}
}
}
}
break;
// add feature
case 3:
{
let addInfo = true;
while (addInfo) {
clear();
console.log ( "Please select the content you want to add: 1. Add 2. Add class student 3. Return");
let addSelect = parseInt(readline.question(""));
switch (addSelect) {
case 1:
{
clear();
let length = stuInfo.length;
stuInfo[length] = [];
console.log ( "new class successfully press Enter to return!");
readline.question("");
}
break;
case 2:
{
clear();
let classSelectStr = classSelect();
console.log(classSelectStr);
let classNo, addStuName, addStuAge, addStuGender, addStuScore;
classNo = parseInt(readline.question(""));
if (classCheck(classNo)) {
clear();
console.log ( "Please enter the name of the student you want to add:");
addStuName = readline.question("");
clear();
console.log ( "Please enter the age of the students you want to add:");
addStuAge = readline.question("");
while (true) {
clear();
console.log ( "Please choose the gender of students: 1. Male 2. Female");
addStuGender = parseInt(readline.question(""));
if (addStuGender === 1 || addStuGender === 2) {
break;
}
else {
console.log ( "Select error, please choose again, press the Enter key to continue");
readline.question("");
}
}
clear();
console.log ( "Enter the student's scores to be added:");
addStuScore = parseInt(readline.question(""));
addStu(classNo, addStuName, addStuAge, addStuGender, addStuScore);
the console.log ( "Press ENTER to return");
readline.question("");
}
else {
if (classNo === stuInfo.length + 1) {
break;
}
else {
console.log ( "input is incorrect, please re-enter");
the console.log ( "Press ENTER to return");
readline.question("");
}
}
}
break;
case 3:
addInfo = false;
break;
default:
console.log ( "Select error, please choose again, press the Enter key to continue");
readline.question("");
}
}
}
break;
// delete function
case 4:
{
let delInfo = true;
while (delInfo) {
clear();
console.log ( "Please select the item you want to delete: 1. Delete Class 3. 2. Remove the students return");
let delSelect = parseInt(readline.question(""));
switch (delSelect) {
// Delete Class
case 1:
let delClassInfo = true;
while (delClassInfo) {
clear();
let classSelectStr = classSelect();
console.log(classSelectStr);
let classNo = parseInt(readline.question(""));
if (classCheck(classNo)) {
delClass(classNo);
delClassInfo = false;

}
else {
if (classNo === stuInfo.length + 1) {
break;
}
else {
console.log ( "input is incorrect, please re-enter");
the console.log ( "Press ENTER to return");
readline.question("");
}
}
}
break;
// remove students
case 2:
{
late delStuInfo = true;
while (delStuInfo) {
clear();
let classSelectStr = classSelect();
console.log(classSelectStr);
let classNo = parseInt(readline.question(""));
if (classCheck(classNo)) {
clear();
console.log ( "Enter the students you want to delete the Student ID:");
let stuNo = readline.question("");
let singleStuInfo = stuCheck(classNo, stuNo);
delStuInfo = false;
if (singleStuInfo) {

delStu(classNo, singleStuInfo, "del");
}
else {
console.log ( "I'm sorry not to find the corresponding student!");
the console.log ( "Press ENTER to return");
readline.question("");
}
}
else {
if (classNo === stuInfo.length + 1) {
break;
}
else {
console.log ( "input is incorrect, please re-enter");
the console.log ( "Press ENTER to return");
readline.question("");
}
}
}
}
break;
case 3:
delInfo = false;
break;
default:
console.log ( "input error, please re-enter, press the Enter key to continue");
readline.question("");
}
}
}
break;
// function to change classes
// core ideas: first remove the student, and then add the other students in the class
case 5:
{
let transfer = true;
let str = "";
while (transfer) {
clear();
let classSelectStr = classSelect();
console.log ( "(where the original class of students)" + classSelectStr);
let classNo = parseInt(readline.question(""));
if (classCheck(classNo)) {
clear();
console.log ( "Please enter your students to change classes school number:");
let stuNo = readline.question("");
let singleStuInfo = stuCheck(classNo, stuNo);
if (singleStuInfo) {
while (true) {
clear();
str = "";
let classSelectStr2 = classSelect();
console.log ( "Class Student ID Name Age Sex fraction");
str += singleStuInfo.className + "\t" + singleStuInfo.stuID + "\t" + singleStuInfo.stuName + "\t" + singleStuInfo.stuAge + "\t" + singleStuInfo.stuGender + "\t" + singleStuInfo.stuScore;
console.log(str);
console.log ( "(students should turn to the class)" + classSelectStr2);
let classNo2 = parseInt(readline.question(""));
if (classCheck(classNo2)) {
if (classNo === classNo2) {
transfer = false;
the console.log ( "same class as the original main menu and press Enter to return!");
readline.question("");
}
else {
classTransfer(classNo, singleStuInfo, classNo2);
transfer = false;
console.log ( "change classes successfully, press Enter to return to the main menu");
readline.question("");
}
break;
}
else {
if (classNo2 === stuInfo.length + 1) {
transfer = false;
break;
}
else {
console.log ( "input is incorrect, please re-enter");
the console.log ( "Press ENTER to return");
readline.question("");
}
}
}
}
else {
console.log ( "I'm sorry not to find the corresponding student!");
the console.log ( "Press ENTER to return");
readline.question("");
break;
}
}
else {
if (classNo === stuInfo.length + 1) {
break;
}
else {
console.log ( "input is incorrect, please re-enter");
the console.log ( "Press ENTER to return");
readline.question("");
}
}
}
}
break;
// exit function
case 6:
useSys = false; // set using the system identification is false USESYS
break;
default:
console.log ( "input error, please re-enter (press the Enter key to continue)!");
readline.question("");
}
}
break; // out of the outer loop
}
else {
// if the above instructions did not enter the account password in question
loginChance--; // subtract 1 from landing opportunity
if (loginChance === 0) // if the login directly out opportunities for 0 while loop
{
break;
}
else {
clear();
console.log ( `account password wrong, you left $ {loginChance} chance (press the Enter key to continue)`!);
readline.question("");
}
}
}
// based on the value loginChance to determine the user take the initiative to jump, or jump out of an opportunity to login 0
if (loginChance === 0) {
clear();
console.log ( "too many wrong, please try again later!");
}
else {
clear();
console.log ( "Thank you for your use!");
}
}
main();

Guess you like

Origin www.cnblogs.com/dbda/p/11441700.html
Recommended