1 const readline = require('readline-sync')//引用readline-sync 2 //用戶名,密碼 3 let user = [{ 4 username: 'yang', 5 password: 123 6 }, { 7 username: 'kai', ... ...
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();