node.js运行简易学生信息管理系统
const readline = require('readline-sync');
//学生信息
let users = [{
id: 1,
name: 'liu',
age: '17',
gender: 'male',
score: '98'
},
{
id: 2,
name: 'dog',
age: '18',
gender: 'male',
score: '81'
},
{
id: 3,
name: 'xingxing',
age: '20',
gender: 'male',
score: '79'
},
{
id: 4,
name: 'coco',
age: '16',
gender: 'female',
score: '73'
},
{
id: 5,
name: 'along',
age: '15',
gender: 'female',
score: '66'
}
];
//账号密码
let admin = [{
username: '1',
password: '1'
}]
//登录
function islogin(admin) {
let num = 3;
while (true) {
console.log('请输入帐号:');
let username = readline.question();
console.log('请输入密码:');
let password = readline.question();
for (let i = 0; i < admin.length; i++) {
if (admin[i].username == username && admin[i].password == password) {
console.log('登录成功');
noteMenu();
return;
}
}
num--;
if (num <= 0) {
console.log('错误3次,锁卡');
return num;
} else
console.log(`登录失败,你还有${num}次机会,请重新输入`);
}
}
//注册
function isregister(admin) {
while (true) {
console.log('请输入帐号:');
let username = readline.question();
console.log('请输入密码:');
let password = readline.question();
let isOk = false;
for (let i = 0; i < admin.length; i++) {
if (admin[i].username == username) {
console.log('用户名已注册,请重新输入');
isOk = true;
break;
}
}
if (!isOk) {
admin.push({
username,
password,
});
console.log('注册成功')
menu(admin);
return;
}
}
}
//主菜单
function menu(admin) {
while (true) {
console.log('1.登录 2.注册 3.切换用户')
console.log('请选择要进行的操作:')
let option = readline.question() - 0;
switch (option) {
case 1:
let num = islogin(admin);
if (num <= 0) {
return;
}
break;
case 2:
isregister(admin);
break;
case 3:
islogin(admin);
break;
default:
console.log('请输入正确选项');
}
}
}
//操作菜单
function noteMenu() {
while (true) {
console.log('请选择操作:1.查询 2.新增 3.修改 4.删除 5.退出')
let option = readline.question() - 0;
switch (option) {
case 1:
find();
break;
case 2:
addUsers();
break;
case 3:
change();
break;
case 4:
deletSth();
break;
case 5:
return;
default:
console.log('请输入正确的选项');
}
}
}
//查询
function find() {
console.log('请输入查询方式: 1.查询所有 2.分类查询');
let option = readline.question() - 0;
switch (option) {
case 1:
findAll();
break;
case 2:
splitClass();
break;
default:
console.log('请输入正确的选项');
}
}
//查询所有
function findAll() {
for (let i = 0; i < users.length; i++) {
console.log(users[i]);
}
}
//分类查询
function splitClass() {
console.log('请选择操作:1.姓名 2.年龄 3.性别 4.分数');
let option = readline.question() - 0;
switch (option) {
case 1:
allName();
break;
case 2:
userAge();
break;
case 3:
sex();
break;
case 4:
scoreAll();
break;
default:
console.log('请输入正确的选项');
}
}
//姓名查询
function allName() {
console.log('请输入要查询的名字');
let name = readline.question();
let isOk = false;
for (let i = 0; i < users.length; i++) {
if ((users[i].name).includes(name)) {
console.log(users[i]);
isOk = true;
}
}
if (!isOk) {
console.log('没有找到该学生');
}
}
//年龄查询
function userAge() {
while (true) {
console.log('请输入要查询的年龄');
let age = readline.question();
let reg = /^\d{1,3}$/;
let isOk = false;
if (reg.test(age) == true) {
for (let i = 0; i < users.length; i++) {
if ((users[i].age).includes(age)) {
console.log(users[i]);
isOk = true;
}
}
if (!isOk) {
console.log('没有找到包含该年龄的学生');
return;
} else
return;
} else {
console.log('错误,请输入正确的年龄');
}
}
}
//性别查询
function sex() {
console.log('请选择要查询的性别: 1.男 2.女');
let sel = readline.question() - 0;
let man = /^male$/,
woman = /^female$/;
switch (sel) {
case 1:
sexsel(man);
break;
case 2:
sexsel(woman);
break;
}
}
//性别筛选
function sexsel(sex) {
for (let i = 0; i < users.length; i++) {
if (sex.test(users[i].gender)) {
console.log(users[i]);
}
}
}
//分数查询
function scoreAll() {
console.log('请输入要查询的分数');
let score = readline.question();
let isOk = false;
for (let i = 0; i < users.length; i++) {
if ((users[i].score).includes(score)) {
console.log(users[i]);
isOk = true;
}
}
if (!isOk) {
console.log('没有找到包含该成绩的学生');
}
}
//新增
function addUsers() {
while (true) {
console.log('请输入新增学生姓名:');
let name = readline.question();
console.log('请输入新增学生年龄:');
let age = readline.question();
console.log('请输入新增学生性别:');
let gender = readline.question();
console.log('请输入新增学生分数:');
let score = readline.question();
let regName = /^[a-zA-Z]+$/; //限制名字只能由字母组成
let regAge = /^\d{1,2}$/; //限制年龄只能由数字组成
let regGender = /^female|male$/; //限制性别只能由male和female组成
let regScore = /^((\d{1,2})|(1[0-4]\d)|(150))$/; //限制成绩只能0-150
if (regName.test(name)) {
if (regAge.test(age)) {
if (regGender.test(gender)) {
if (regScore.test(score)) {
users.push({
id: `${users.length+1}`,
name,
age,
gender,
score
})
console.log('添加成功,该学生信息如下:');
console.log(users.pop());
return;
} else
console.log('成绩只能在0-150,请重新输入');
} else
console.log('性别只能是male或female,请重新输入');
} else
console.log('年龄只能由数字组成,请重新输入');
} else
console.log('名字只能由字母组成,请重新输入');
}
}
//修改
function change() {
console.log('请输入要修改的学生学号:');
let stuNum = readline.question() - 0;
for (let i = 0; i < users.length; i++) {
if (users[i].id === stuNum) {
console.log('当前学生信息如下:');
console.log(users[i]);
changeAll(i);
return;
}
}
console.log('没有该学号的学生');
}
//修改主菜单
function changeAll(i) {
console.log('请选择要修改的信息:1.姓名 2.年龄 3.性别 4.成绩');
let option = readline.question() - 0;
switch (option) {
case 1:
stuName(i);
break;
case 2:
stuAge(i);
break;
case 3:
stuSex(i);
break;
case 4:
stuScore(i);
break;
default:
console.log('请输入正确的选项');
}
}
//修改姓名
function stuName(i) {
while (true) {
console.log('请输入要修改的姓名:');
let name = readline.question();
let reg = /^[a-zA-Z]+$/;
if (reg.test(name) == true) {
users[i].name = name;
console.log('修改成功!该学生信息如下:');
console.log(users[i]);
return;
} else
console.log('请输入正确的名字(字母)');
}
}
//修改年龄
function stuAge(i) {
while (true) {
console.log('请输入要修改的年龄:');
let age = readline.question();
let reg = /^\d{1,2}$/;
if (reg.test(age) == true) {
users[i].age = age;
console.log('修改成功!该学生信息如下:');
console.log(users[i]);
return;
} else
console.log('请输入正确的年龄数字(1-2位数)');
}
}
//修改性别
function stuSex(i) {
while (true) {
console.log('请输入要修改的性别:');
let gender = readline.question();
let reg = /^female|male$/;
if (reg.test(gender) == true) {
users[i].gender = gender;
console.log('修改成功!该学生信息如下:');
console.log(users[i]);
return;
} else
console.log('请输入正确的性别(male或famel)');
}
}
//修改成绩
function stuScore(i) {
while (true) {
console.log('请输入要修改的成绩:');
let score = readline.question();
let reg = /^((\d{1,2})|(1[0-4]\d)|(150))$/;
if (reg.test(score) == true) {
users[i].score = score;
console.log('修改成功!该学生信息如下:');
console.log(users[i]);
return;
} else
console.log('请输入正确的成绩(0-150)');
}
}
//删除
function deletSth() {
while (true) {
console.log('请输入要删除的学生学号:');
let stuNum = readline.question() - 0;
let reg = /^\d+$/;
if (reg.test(stuNum) == true) {
for (let i = 0; i < users.length; i++) {
if (users[i].id === stuNum) {
console.log('当前学生信息如下:');
console.log(users[i]);
deletMenu(i);
return;
}
}
console.log('没有找到该学号学生');
} else
console.log('请输入正确的学号(数字)');
}
}
//删除主菜单
function deletMenu(i) {
console.log('是否确定删除:');
console.log('1.是 2.否');
let option = readline.question() - 0;
switch (option) {
case 1:
deletTrue(i);
break;
case 2:
return;
}
}
//确定删除
function deletTrue(i) {
users.splice(i, 1);
console.log('删除成功!');
}
menu(admin);