用js写了一个学生管理系统

const readline = require("readline-sync");

//老师登录数据库
let teacher = [{ id: 1, name: "yhr", password: "123" }]
// 学生信息库
let information = [
    { id: 1, name: "123", age: "20", gender: "male", score: "98" },
    { id: 2, name: "456", age: "22", gender: "female", score: "80" },
    { id: 3, name: "789", age: "20", gender: "female", score: "75" }
];

//登陆
function login(demo) {
    let error = 3;
    while (true) {
        console.log("请输入账号名:");
        let user = readline.question();
        console.log("请输入密码:");
        let pswd = readline.question();

        for (let i = 0; i < demo.length; i++) { //遍历数组
            if (demo[i].name == user && demo[i].password == pswd) {  // 判断用户输入的用户名和密码 是否正确
                console.log("登录成功");
                operation();
                return;
            }
        }
        console.log("登录失败");
        error--;
        if (error == 0) {
            console.log("你连续输入错误三次,这怕不是你的号哟。记清楚再来哟");
            return;
        } else {
            console.log("你还有" + error + "次机会");
        }
    }
}

//注册
function register(demo) {
    let error = 3;  // 判断用户输入错误次数
    while (true) {
        let state = true;  // 状态值
        if (error == 0) {
            console.log("想好再来吧!!");
            return;
        }
        console.log("请输入用户名:");
        let username = readline.question();
        for (let i = 0; i < demo.length; i++) {
            if (demo[i].name == username) {
                console.log("该用户名已经存在");
                error--;
                state = false;
            }
        }
        if (state) {
            console.log("请输入密码:");
            let name = readline.question();
            console.log("请确认密码:");
            let pswd = readline.question();
            if (name === pswd) {
                console.log("注册成功,正在跳转登录页面....");
                demo.push({ name: username, password: pswd });
                login(teacher);
                return;
            } else {
                console.log("两次密码不正确,请重新注册");
            }
        }
    }
}

// 查询方式
function query() {
    console.log("请选择查询方式: 1.查询所有  2.分类查询");
    let choose = readline.question() - 0;
    switch (choose) {
        case 1: {
            console.log("查询结果如下:");
            for (let item of information) {
                console.log(item);
            }
            break;
        }
        case 2:
            option();
            break;
    }

}

// 查询类型
function option() {
    console.log("请选择查询类型:1.姓名  2.年龄  3.性别  4.成绩")
    let choose = readline.question() - 0;
    switch (choose) {
        case 1: {
            console.log("请输入查询的姓名:")
            let inputName = readline.question();
            for (let i = 0; i < information.length; i++) {
                if (information[i].name == inputName) {
                    console.log("查询结果,信息如下");
                    console.log(information[i]);
                }
            }
            break;
        }
        case 2: {
            console.log("请输入查询的年龄:")
            let inputAge = readline.question();
            console.log("查询结果,信息如下");
            for (let i = 0; i < information.length; i++) {
                if (information[i].age == inputAge) {
                    console.log(information[i]);
                }
            }
            break;
        }
        case 3: {
            console.log("请输入查询的性别:")
            let inputGender = readline.question();
            for (let i = 0; i < information.length; i++) {
                if (information[i].gender == inputGender) {
                    console.log("查询结果,信息如下");
                    console.log(information[i]);
                }
            }
            break;
        }
        case 4: {
            console.log("请输入查询的成绩:")
            let inputScore = readline.question();
            for (let i = 0; i < information.length; i++) {
                if (information[i].score == inputScore) {
                    console.log("查询结果,信息如下");
                    console.log(information[i]);
                }
            }
            break;
        }

    }
}

// 新增学生信息
function newStudentInformation() {
    let number = 0;
    for (let i = 0; i < information.length; i++) {     //遍历数组
        if (i == information.length - 1) {
            number = information[i].id + 1;             // 如果是数组的最后一项,那么就在最后一项的 id+1
            console.log(number);
        }
    }
    console.log("请输入新增学生新增姓名:");
    let studentName = readline.question();
    console.log("请输入新增学生年龄:");
    let studentAge = readline.question();
    console.log("请输入新增学生性别");
    let studentGender = readline.question();
    console.log("请输入新增学生成绩");
    let studentScore = readline.question();
    let NewStudent = { id: number, name: studentName, age: studentAge, gender: studentGender, score: studentScore };
    information.push(NewStudent);
    console.log("新增成功,该学生信息如下:");
    console.log(NewStudent);
}

// 修改学生信息
function revise() {
    while (true) {
        console.log("请输入要修改的学生学号:");
        let studentId = readline.question();
        for (let i = 0; i < information.length; i++) {
            if (information[i].id == studentId) {
                console.log("该学生信息如下:");
                console.log(information[i]);
                console.log("请选择要修改的信息: 1.姓名 2.年龄 3.性别 4.成绩");
                let choose = readline.question() - 0;
                switch (choose) {
                    case 1: {
                        console.log("请输入新的名字:")
                        let newName = readline.question();
                        information[i].name = newName;
                        console.log(information[i]);
                        break;
                    }
                    case 2: {
                        console.log("请输入新的年龄:")
                        let newAge = readline.question();
                        information[i].age = newAge;
                        console.log(information[i]);
                        break;
                    }
                    case 3: {
                        console.log("请输入新的性别:")
                        let newGender = readline.question();
                        information[i].gender = newGender;
                        console.log(information[i]);
                        break;
                    }
                    case 4: {
                        console.log("请输入新的成绩:")
                        let newScore = readline.question();
                        information[i].score = newScore;
                        console.log(information[i]);
                        break;
                    }
                }
                return;
            }
        }
        console.log("没有找到该学号的学生,请核对后再输入");
    }
}

// 删除学生信息
function deleteInformation() {
    console.log("请输入你要删除学生的学号:");
    let number = readline.question();
    console.log("确定是否删除? 确定请输入1,取消删除请输入0");
    let chooes = readline.question() - 0;
    switch (chooes) {
        case 1: {
            for (let i = 0; i < information.length; i++) {
                if (information[i].id == number) {
                    delete information[i];
                    console.log("删除成功!");
                }
            }
            break;
        }
        case 2: return;
    }
}

// 选择操作
function operation() {
    while (true) {
        console.log("请选择操作方式: 1.查询  2.新增  3.修改 4.删除  5.退出");
        let userOperation = readline.question() - 0;
        switch (userOperation) {
            case 1:
                query();
                break;
            case 2:
                newStudentInformation();
                break;
            case 3:
                revise();
                break;
            case 4:
                deleteInformation();
                break;
            case 5:
                console.log("欢迎下次使用,再见!");
                return;
        }
    }
}

// 主流程
function menu() {
    console.log("请选择业务:1. 登录  2.注册  3.退出");
    let userChoose = readline.question() - 0;
    switch (userChoose) {
        case 1:
            login(teacher);
            break;
        case 2:
            register(teacher);
            break;
        case 3:
            console.log("欢迎下次使用!!");
            return;
    }
}

menu();

转载于:https://www.cnblogs.com/yehongrun/p/9092781.html

你可能感兴趣的:(用js写了一个学生管理系统)