JavaScript编写学生查询系统

时间:2024-02-24 21:36:07
  1 const readline = require(\'readline-sync\')//引用readline-sync
  2 //用户名,密码
  3 let user = [{
  4     username: \'yang\',
  5     password: 123
  6 }, {
  7     username: \'kai\',
  8     password: 456
  9 }, {
 10     username: \'ming\',
 11     password: 789
 12 }
 13 ];
 14 // 注册
 15 let fn2 = function () {
 16     console.log(\'请输入注册用户名:\');
 17     let username1 = readline.question();
 18     console.log(\'请输入注册密码:\');
 19     let password1 = readline.question() - 0;
 20     let obj = {};//创建对象
 21     obj.username = username1;
 22     obj.password = password1;
 23     user.push(obj);//添加数据
 24     console.log(\'注册成功,跳转登陆页面。。。。\')
 25     add();
 26     // liuchen();
 27     //调用登录
 28 }
 29 //登陆
 30 let add = function () {
 31     let s = 2;//错误的次数
 32     while (true) {
 33         console.log(\'请输入用户名:\');
 34         let username = readline.question();
 35         console.log(\'请输入密码:\');
 36         let password = readline.question() - 0;
 37         for (let i = 0; i < user.length; i++) {
 38             if (username == user[i].username && password == user[i].password) {//判断输的值与初始值是否相等
 39                 console.log(\'欢迎你,登陆成功\');
 40                 liuchen();//调用登陆后的主流程
 41                 return;
 42             }
 43         }
 44         //输入不正确,超过三次
 45         if (s != 0) {
 46             console.log(`密码或账户有误,你还有${s}次机会,请重新输入!`);
 47             s--;
 48 
 49         } else {
 50             console.log(\'账户锁定!\');
 51             break;
 52         }
 53     }
 54 }
 55 //登录前主流程
 56 let fn3 = function () {
 57     console.log(\'请选择以下要求:1.登陆 2.注册 3.退出\');
 58     let num = readline.question() - 0;//选择要求
 59     switch (num) {
 60         case 1:
 61             add();//调用add
 62             break;
 63         case 2:
 64             fn2();//调用注册
 65             break;
 66         case 3:
 67             console.log(\'欢迎下次光临,再见!\')
 68             return;//结束
 69     }
 70 }
 71 //查询所有
 72 let arr = [
 73     { id: 1, name: \'liu\', age: \'18\', gender: \'nan\', score: \'98\' },
 74     { id: 2, name: \'li\', age: \'20\', gender: \'nv\', score: \'80\' },
 75     { id: 3, name: \'zhang\', age: \'20\', gender: \'nv\', score: \'90\' },
 76     { id: 4, name: \'ma\', age: \'21\', gender: \'nan\', score: \'70\' }
 77 ]
 78 console.log(\'学号\t姓名\t年龄\t性别\t成绩\t\');
 79 for (let ai of arr) {//遍历出数组
 80     let str = \'\';
 81     for (let key in ai) {//遍历出键名
 82 
 83         str += ai[key] + \'\t\';
 84     }
 85     console.log(str);
 86 }
 87 let maxID = 4;//给一个固定的值
 88 //分类查询
 89 let chaxun = function () {
 90     while (true) {
 91         console.log(\'请选择查询类型: 1.姓名 2.年龄 3.性别 4.成绩 5.返回\');
 92         let num3 = readline.question() - 0;
 93         switch (num3) {
 94             case 1:
 95                 console.log(\'请输入要查询的名字:\');
 96                 fun(\'name\');
 97                 break;
 98             case 2:
 99                 console.log(\'请输入要查询的年龄:\');
100                 fun(\'age\');
101                 break;
102             case 3:
103                 console.log(\'请输入要查询的性别:\');
104                 fun(\'gender\');
105                 break;
106             case 4:
107                 console.log(\'请输入要查询的成绩:\');
108                 fun(\'score\');
109                 break;
110             case 5:
111                 return;
112         }
113     }
114 }
115 let fun = function (type) {//type键名为变量要用[]
116     let name = readline.question();
117     for (let i in arr) {//模糊查找
118         if (arr[i][type].search(name) >= 0) {
119             console.log(arr[i]);
120         }
121     }
122 //第二种方法
123     // for (let i = 0; i < arr.length; i++) {//精确查找
124     //     if (arr[i][type] == name) {
125     //         console.log(arr[i]);
126     //     }
127     // }
128 }
129 //查询
130 let chaxun1 = function () {
131     while (true) {
132         console.log(\'请选择查询方式:1.查询所有 2.分类查询\');
133         let num2 = readline.question() - 0;
134         switch (num2) {
135             case 1://查询所有
136                 for (let a of arr) {//遍历数组
137                     console.log(a);
138                 }
139                 break;
140             case 2://分类查询
141                 chaxun();
142                 break;
143         }
144         break;
145     }
146 }
147 //新增
148 let xinzhen = function () {
149     while (true) {
150         console.log(\'请输入新增学生姓名:\');
151         let name = readline.question();
152         console.log(\'请输入新增学生的年龄:\');
153         let age = readline.question();
154         console.log(\'请输入新增学生的性别:\');
155         let gender = readline.question();
156         console.log(\'请输入新增学生的成绩:\');
157         let score = readline.question();
158         let arr1 = {};//创建对象
159         arr1 = ({ id: ++maxID, name, age, gender, score });
160         arr.push(arr1);//添加数据
161         console.log(arr1)
162         break;
163     }
164 }
165 //修改
166 let xiugai = function () {
167     while (true) {
168         console.log(\'请输入要修改学生的学号:\');
169         let id = readline.question() - 0;
170         for (let i4 = 0; i4 < arr.length; i4++) {
171             if (arr[i4].id == id) {//判断输入的id名
172                 console.log(\'当前学生信息如下:\');
173                 console.log(arr[i4])
174                 console.log(\'请选择要修改的信息:1.姓名 2.年龄 3.性别 4.成绩 5.返回\')
175                 let num4 = readline.question() - 0;
176                 switch (num4) {
177                     case 1:
178                         xiugai1(i4, \'name\');
179                         break;
180                     case 2:
181                         xiugai1(i4, \'age\');
182                         break;
183                     case 3:
184                         xiugai1(i4, \'gender\');
185                         break;
186                     case 4:
187                         xiugai1(i4, \'score\');
188                         break;
189                     case 5:
190                         return;
191                 }
192             }
193         } break;
194     }
195 }
196 let xiugai1 = function (i4, xinxi) {//i4.为学生下标的属性,xinxi为要修改的键名(变量用[])
197     console.log(\'请输入新的数据:\');
198     let name = readline.question();
199     arr[i4][xinxi] = name;//输入的信息赋值给键名
200     console.log(\'修改成功!该学生信息如下:\');
201     console.log(arr[i4]);
202 }
203 //删除
204 let shanchu = function () {
205     while (true) {
206         console.log(\'请输入要删除学生的学号:\');
207         let id1 = readline.question() - 0;
208         for (let i5 = 0; i5 < arr.length; i5++) {
209             if (arr[i5].id == id1) {//判断输入的id1是否等于对象的键名
210                 console.log(\'当前学生信息如下:\');
211                 console.log(arr[i5]);
212                 console.log(\'确定删除:1.是 2.否\');
213                 let num5 = readline.question() - 0;
214                 if (num5 == 1) {
215                     console.log(\'删除成功!删除后信息如下:\');
216                     arr.splice(i5, 1);//删除数据i为下标,1为删除的个数
217                     for (let a of arr) {
218                         console.log(a);
219                     }
220                 }
221 
222             }
223         }
224         break;
225     }
226 }
227 //主控住流程
228 let liuchen = function () {
229     while (true) {
230         console.log(\'请选择一下操作:。。1.查询 2.新增 3.修改 4.删除 5.退出\');
231         let num1 = readline.question() - 0;
232         switch (num1) {
233             case 1:
234                 //查询
235                 chaxun1();
236                 
237                 break;
238             case 2:
239                 // 新增
240                 xinzhen();
241                 break;
242             case 3:
243                 //修改
244                 xiugai();
245                 break;
246             case 4:
247                 //删除
248                 shanchu();
249                 break;
250             case 5:
251                 //退出
252                 console.log(\'欢迎下次使用,再见!\');
253                 return;
254         }
255     }
256 }
257 fn3();