1 const readline = require("readline-sync"); //输出功能 2 //登录注册数组 3 let user = [{ 4 account: "d", 5 password: 1 6 }, 7 { 8 account: "e", 9 password: 2 10 }, 11 { 12 account: "f", 13 password: 3 14 } 15 ] 16 //学生信息数组 17 let user1 = [{ 18 id: 1, 19 name: 'liu', 20 age: '18', 21 gender: 'male', 22 score: '98' 23 }, 24 { 25 id: 2, 26 name: 'chen', 27 age: '20', 28 gender: 'famale', 29 score: '100' 30 }, 31 { 32 id: 3, 33 name: 'zhang', 34 age: '17', 35 gender: 'male', 36 score: '77' 37 }, 38 { 39 id: 4, 40 name: 'li', 41 age: '22', 42 gender: 'famale', 43 score: '65' 44 }, 45 { 46 id: 5, 47 name: 'sun', 48 age: '19', 49 gender: 'male', 50 score: '80' 51 } 52 ]; 53 // //注册 54 function regsiter(user) { 55 let status = true; 56 while (status) { 57 let x = 1; 58 console.log("请输入注册账号:"); 59 let account = readline.question(); 60 for (let index = 0; index < user.length; index++) { 61 if (account == user[index].account) { 62 console.log("账号已存在"); 63 x++; 64 } 65 } 66 if (x == 1) { 67 console.log("请输入注册密码:"); 68 let password = readline.question(); 69 user.push({ 70 account, 71 password 72 }); 73 console.log("注册成功!跳转登录页面..."); 74 return; 75 } 76 } 77 } 78 //登录 79 function login(user) { 80 let status = true; //状态值 81 while (status) { 82 let x = 1; //状态值 83 console.log("请输入账号:"); 84 let account = readline.question(); 85 console.log("请输入密码:"); 86 let password = readline.question(); 87 for (let index = 0; index < user.length; index++) { 88 if (account == user[index].account && password == user[index].password) { 89 console.log("登录成功"); 90 x++; 91 return; 92 } 93 } 94 if (x == 1) { 95 console.log("登录失败"); 96 } 97 } 98 } 99 // //选择业务 100 function option() { 101 console.log("请选择业务:1.登录 2.注册 3.退出"); 102 let choice = readline.question() - 0; 103 while (isNaN(choice) || choice < 1 || choice > 3) { 104 console.log("输入有误,请重输:"); 105 choice = readline.question() - 0; 106 } 107 switch (choice) { 108 case 3: 109 console.log("谢谢使用,再见"); 110 break; 111 case 2: 112 regsiter(user); 113 login(user); 114 break; 115 case 1: 116 login(user); 117 break; 118 119 default: 120 break; 121 } 122 123 124 } 125 option(); 126 // //查询 127 function search(user1) { 128 console.log("请选择查询方式:1.查询所有 2.分类查询"); 129 let choice = readline.question() - 0; //查询方式 130 while (isNaN(choice) || choice < 1 || choice > 2) { 131 console.log("选择有误,请重输:"); 132 choice = readline.question() - 0; 133 } 134 switch (choice) { 135 case 2: //分类查询 136 console.log("请选择查询类型:1.姓名 2.年龄 3.性别 4.成绩"); 137 let choice1 = readline.question() - 0; //查询类型 138 while (isNaN(choice1) || choice1 < 1 || choice1 > 4) { 139 console.log("请重输"); 140 choice1 = readline.question() - 0; 141 } 142 console.log("请输入查询信息"); 143 let message = readline.question(); 144 for (let i = 0; i < user1.length; i++) { 145 let a = ["id", "name", "age", "gender", "score"]; //用数组拿到对象的键名 146 let b = a[choice1]; //根据用户输入找到对应的键,用数组拿出来 147 let str = user1[i][b]; //在对象中,要查询的是键对应的值,用字符串把值取出来 148 if (str.includes(message)) { 149 console.log(user1[i]); //如果字符串包含所要找的字符,那么输出这个对象 150 } 151 } 152 break; 153 case 1: //查询所有 154 for (let index = 0; index < user1.length; index++) { 155 console.log(user1[index]); 156 } 157 break; 158 default: 159 break; 160 } 161 } 162 // //新增 163 function add(user1, length) { 164 let status = true; //状态值 165 console.log("请输入新增学生姓名:"); 166 let name = readline.question(); 167 let reg1 = /[a-zA-Z]/; //使用正则表达式来确保学生姓名输入正确 168 while (!reg1.test(name)) { 169 console.log("学生姓名不得包含数字和非法字符,请重输"); 170 name = readline.question(); 171 } 172 console.log("请输入新增学生年龄:"); 173 let age = readline.question() - 0; 174 while (isNaN(age) || age < 1) { //保证输出正确年龄 175 console.log("请重输"); 176 age = readline.question() - 0; 177 } 178 console.log("请输入新增学生性别:"); 179 let gender = readline.question(); 180 console.log("请输入新增学生成绩:"); 181 let score = readline.question() - 0; 182 while (isNaN(score) || score < 0 || score > 100) { //保证输出正确成绩 183 console.log("请重输"); 184 score = readline.question() - 0; 185 } 186 console.log("新增成功! 该学生信息如下:"); 187 length++; 188 user1.push({ 189 id: length, 190 name, 191 age, 192 gender, 193 score 194 }) 195 console.log(user1[length - 1]); 196 } 197 // //修改 198 function alter(user1, length) { 199 console.log("请输入要修改的学生学号:"); 200 let number = readline.question(); //学生学号 201 while (isNaN(number) || number < 1 || number > length) { 202 console.log("输入有误:请重输"); 203 number = readline.question(); 204 } 205 console.log("当前学生信息如下:"); 206 console.log(user1[number - 1]); 207 console.log("请选择要修改的信息: 1.姓名 2.年龄 3.性别 4.成绩"); 208 let choice1 = ["姓名", "年龄", "性别", "成绩"]; //信息对应汉字 209 let choice3 = ["name", "age", "gender", "score"]; //信息对应对象里面的键名 210 let choice = readline.question() - 0; //用户输入修改的信息对应编号1,2,3,4 211 while (isNaN(choice) || choice < 1 || choice > 4) { 212 console.log("请重输:"); 213 choice = readline.question() - 0; //如果用户输入的不是数字或者输入了错误的数字,加以提示 214 } 215 console.log(`您要修改的是${choice1[choice - 1]}`); 216 console.log("确认请按1,取消请按2"); //确认要修改信息 217 let choice2 = readline.question() - 0; 218 if (choice2 == 1) { 219 console.log("请输入新的数据"); 220 let data = readline.question(); //输入新信息 221 console.log("修改成功! 该学生信息如下:"); 222 user1[number - 1][`${choice3[choice - 1]}`] = data; //修改对象里面的值,方法类似数组 223 console.log(user1[number - 1]); //修改后的信息 224 return; 225 } else { 226 return; //如果不修改,就退出 227 } 228 229 } 230 //删除 231 function del(user1, length) { 232 console.log("请输入要删除的学生学号:"); 233 let number = readline.question() - 0; 234 while (isNaN(number) || number < 1 || number > length) { 235 console.log("输入有误:请重输"); 236 number = readline.question(); 237 } 238 console.log("当前学生信息如下:"); 239 console.log(user1[number - 1]); 240 console.log("确认删除请按1,返回请按2"); 241 let choice = readline.question() - 0; 242 while (isNaN(choice) || choice < 1 || choice > 2) { 243 console.log("输入有误,请重输:"); 244 choice = readline.question() - 0; 245 } 246 if (choice == 1) { 247 user1.splice(number - 1, 1); //删除学号对应的对象 248 length--; //数组长度减少1 249 let a = ["id", "name", "age", "gender", "score"]; //用数组取对象键名 250 let b = a[0]; //用变量b来表示对象的id属性 251 for (let i = number - 1; i < user1.length; i++) { 252 user1[i][b]--; //当删除一条对象之后,保证这条对象之后的对象的id属性-1 253 console.log(user1[i]); 254 } 255 console.log("删除成功"); 256 return; 257 } else { 258 return; 259 } 260 } 261 //选择操作 262 function operation(user1) { 263 let status = true; 264 while (status) { 265 console.log("请选择操作:1.查询 2.新增 3.修改 4.删除 5.退出"); 266 let choice = readline.question() - 0; 267 while (isNaN(choice) || choice < 1 || choice > 5) { 268 console.log("请重输"); 269 choice = readline.question() - 0; 270 } 271 switch (choice) { 272 case 5: 273 console.log("谢谢使用,再见"); 274 status = false; 275 break; 276 case 4: 277 del(user1, user1.length); 278 break; 279 case 3: 280 alter(user1, user1.length); 281 break; 282 case 2: 283 add(user1, user1.length); 284 break; 285 case 1: 286 search(user1); 287 break; 288 } 289 } 290 291 } 292 293 operation(user1);