const readline = require('readline-sync'); let maxLength = 5; //帳號庫 let accountName = () => { let account = [{ name: '123', password: 456 }, { name: 'lisi', password: 123 }, { name: 'wangwu', password: 123 }] return account; } //信息庫 let dateBase = () => { let date = [ { id: 1, name: 'zhang', age: '18', gender: 'man', score: '98' }, { id: 2, name: 'liu', age: '19', gender: 'woman', score: '78' }, { id: 3, name: 'wang', age: '20', gender: 'woman', score: '65' }, { id: 4, name: 'wang', age: '20', gender: 'woman', score: '65' }, { id: 5, name: 'yang', age: '22', gender: 'man', score: '100' } ]; return date; } //數據格式化 let dateFrom = (arr) => { console.log('學號 \t 姓名\t 年齡\t 性別\t 成績\t ') for (let item of arr) { let { id, name, age, gender, score } = item; console.log(id + '\t', name + '\t', age + '\t', gender + '\t', score + '\t') } } // 登錄 let login = (account) => { let error = 3 while (true) { console.log('請輸入帳號:') let uesrname = readline.question(); console.log('請輸入密碼:') let password = readline.question(); for (i = 0; i < account.length; i++) { if (uesrname == account[i].name && password == account[i].password) { console.log('登錄成功'); operation(); return true; } } error--; if (error == 0) { console.log('輸入次數過多,退出程序') return true; } console.log('登陸錯誤,還有' + error + '次機會,請從新輸入') } } //註冊 let register = (account) => { while (true) { console.log('請輸入註冊帳號:') let username = readline.question(); let flag = false; for (let i = 0; i < account.length; i++) { if (account[i].name === username) { console.log('帳號已被佔用,請從新註冊~') flag = true; } } if (flag) { continue; } console.log('請輸入註冊密碼:') let getpassword = readline.question(); let table = { name: username, password: getpassword } account.push(table); console.log(`恭喜你,註冊成功!帳號爲${username},密碼爲${getpassword}。即將跳轉登陸頁面...`) login(account); break; } } //分類查詢 let classQuery = (key, date) => { console.log('請輸入查詢信息:') let queryDate = readline.question(); let reg = new RegExp(queryDate); let arr = []; let i =0; for (let item of date) { if (item[key].search(reg) != -1) { arr.push(item) i++; } } if(i==0) { console.log('無此信息') return; } dateFrom(arr); } //查詢 let query = (date) => { console.log('請選擇查詢方式: 1.查詢全部 2.分類查詢'); let option = readline.question() - 0; switch (option) { case 1: dateFrom(date); break; case 2: { console.log('請選擇查詢: 1.姓名 2.年齡 3.性別 4.成績') let queryOption = readline.question() - 0; switch (queryOption) { case 1: classQuery('name', date); break; case 2: classQuery('age', date); break; case 3: classQuery('gender', date); break; case 4: classQuery('score', date); break; default:console.log('選擇有誤,返回主菜單~');break; }break; } } } //新增 let increre = (date) => { console.log('請輸入要新增學生的姓名:') let name = readline.question(); console.log('請輸入要新增學生的年齡:') let age = readline.question(); console.log('請輸入要新增學生的性別:') let gender = readline.question(); console.log('請輸入要新增學生的成績:') let score = readline.question(); date.push({ id: ++maxLength, name, age, gender, score }) console.log(maxLength); console.log('新增成功!該學生信息以下:'); let arr = []; arr.push(date[date.length - 1]); dateFrom(arr); } //分類修改數據 let classModify = (date, i, key) => { console.log('請輸入新的數據'); let newDate = readline.question(); date[i][key] = newDate; console.log('修改爲功!該學生信息以下:'); let arr = []; arr.push(date[i]); dateFrom(arr); return; } //修改 let modify = (date) => { while (true) { console.log('請輸入要修改學生的學號:') let id = readline.question(); for (let i = 0; i < date.length; i++) { if (date[i].id == id) { console.log('當前學生信息以下:') let arr = []; arr.push(date[i]); dateFrom(arr); console.log('請選擇要修改的信息 1.姓名 2.年齡 3.性別 4.成績 5.返回 ') let option = readline.question() - 0; switch (option) { case 1: classModify(date, i, 'name'); break; case 2: classModify(date, i, 'age'); break; case 3: classModify(date, i, 'gender'); break; case 4: classModify(date, i, 'score'); break; case 5: return; } return; } } console.log('無此學生信息,請從新輸入') } } //刪除 let deletedate = (date) => { while (true) { console.log('請輸入要刪除的學號:') let id = readline.question(); for (let i = 0; i < date.length; i++) { if (date[i].id == id) { console.log('當前學生信息以下:') let arr = []; arr.push(date[i]); dateFrom(arr); console.log('確認刪除:1.是 2.否') let num = readline.question(); if (num == 1) { date.splice(i, 1) console.log('刪除成功!') return; } if (num == 2) { return; } } } console.log('無此學生信息,請從新輸入') } } //操做選項 let operation = () => { let date = dateBase(); while (true) { console.log('請選擇操做 1.查詢 2.新增 3.修改 4.刪除 5.退出 ') let option = readline.question() - 0; switch (option) { case 1: query(date); break; case 2: increre(date); break; case 3: modify(date); break; case 4: deletedate(date); break; case 5: return false; } } } //主頁 let main = () => { let account = accountName(); while (true) { console.log('請選擇 1.登錄 2.註冊 3.退出') let num = readline.question() - 0; switch (num) { case 1: let flag = false; flag = login(account); if (flag) { return; } break; case 2: register(account); break; case 3: console.log('謝謝使用!'); return; } } } main();