您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)Java怎么實(shí)現(xiàn)用戶管理系統(tǒng)的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
具體內(nèi)容如下
此系統(tǒng)功能和方法都比較簡(jiǎn)單
本次系統(tǒng)通過控制臺(tái)輸入商品的基本信息,加入管理員的登錄與對(duì)是否為管理員進(jìn)行操作
對(duì)于功能的實(shí)現(xiàn),分別定義了3個(gè)類
用戶基本屬性類
此類包含用戶id、賬號(hào)、密碼、年齡、角色(是否為管理員)、郵箱、辦事處、賬戶狀態(tài)
private int id;// id號(hào) private String username;// 賬號(hào) private String password;// 密碼 private int age;// 年齡 private String role;// 角色 private String email;// 郵箱 private String officeID;// 辦事處 private String status;// 賬戶狀態(tài)
通過快捷鍵方法快速生成其屬性get/set方法與構(gòu)造器
@Override public String toString() { return id + "\t" + username + "\t" + password + "\t" + age + "\t" + role + "\t" + email + "\t" + officeID + "\t" + status; } public User(int id, String username, String password, int age, String role, String email, String officeID, String status) { super(); this.id = id; this.username = username; this.password = password; this.age = age; this.role = role; this.email = email; this.officeID = officeID; this.status = status; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getOfficeID() { return officeID; } public void setOfficeID(String officeID) { this.officeID = officeID; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; }
完成對(duì)用戶屬性的定義
在新類中對(duì)用戶屬性進(jìn)行修飾和功能的實(shí)現(xiàn)
通過ArrayList動(dòng)態(tài)數(shù)組能夠?qū)?shù)組的基本信息進(jìn)行存儲(chǔ)
Scanner方法來使用控制臺(tái)輸入功能,結(jié)合方法來輸入對(duì)應(yīng)的信息
static int i = 0; String[] admin = { "admin", "admin123" }; static ArrayList<User> list = new ArrayList<>(); private Scanner sc = new Scanner(System.in);
用簡(jiǎn)單的if/else語(yǔ)句和for循環(huán)結(jié)合來實(shí)現(xiàn)增刪改查功能
用戶的增添與密碼的修改實(shí)習(xí)了控制臺(tái)輸入與修改信息的能力
/**1-用戶添加*/ public void add(User u) { list.add(u); } /** 2-密碼修改(根據(jù)Id修改密碼) */ public boolean modifypassword(int id, String password) { User user = findById(id); if (user != null) { user.setPassword(password); return true; } return false; } /** 3-根據(jù)ID查看個(gè)人信息 */ public User findById(int id) { User us = null; for (User u : list) { if (u.getId() == id) { us = u; break; } } return us; } /** 4-根據(jù)ID修改賬號(hào)狀態(tài)(禁用0、啟用1)*/ public boolean modifystatus(int id, String status) { User user = findById(id); if (user != null) { user.setStatus(status); return true; } return false; } /** 5-用戶登錄*/ public void register() { System.out.println("請(qǐng)輸入用戶名:"); if (sc.next().equals(admin[0])) { System.out.println("請(qǐng)輸入密碼:"); if (sc.next().equals(admin[1])) { System.out.println("登錄成功!"); } else { System.out.println("密碼錯(cuò)誤!請(qǐng)重新輸入!"); register(); } } else { System.out.println("用戶名錯(cuò)誤!請(qǐng)重新輸入!"); register(); } } /** 6-修改用戶角色(設(shè)置取消管理員) */ public boolean modifyrole(int id, String role) { User user = findById(id); if (user != null) { user.setRole(role); return true; } return false; } /** 7-用戶列表*/ public ArrayList<User> findAll() { return list; } /** 8-刪除用戶*/ public boolean delete(int id) { User user = findById(id); if (user != null) { return list.remove(user); } return false; }
由此就通過方法對(duì)一些必要的功能進(jìn)行了完成
再通過另一個(gè)主窗口類對(duì)其他類進(jìn)行功能的調(diào)用與界面信息的完成
創(chuàng)建main程序入口類,并且在類中完成輸入界面與指令
輸入窗口的指令界面
private UserModule um = new UserModule(); private Scanner sc = new Scanner(System.in); /** 輸入窗口的指令界面 */ public void menu() { msg("====================================="); msg("======SOFTEEM用戶管理系統(tǒng)================"); msg("======【1】用戶添加======================"); msg("======【2】密碼修改======================"); msg("======【3】個(gè)人信息查看==================="); msg("======【4】賬號(hào)狀態(tài)修改(禁用0、啟用1)========"); msg("======【5】用戶登錄======================="); msg("======【6】修改用戶角色(設(shè)置取消管理員)======="); msg("======【7】用戶列表======================"); msg("======【8】刪除用戶======================"); msg("======【0】退出系統(tǒng)======================"); msg("請(qǐng)輸入操作指令: "); start(); }
通過基礎(chǔ)語(yǔ)句switch完成程序按鍵的入口
/** 程序按鍵入口 */ private void start() { sc = new Scanner(System.in); int i = sc.nextInt(); switch (i) { case 1: add(); break; case 2: alter(); break; case 3: queryById(); break; case 4: thestatus(); break; case 5: enter(); break; case 6: update(); break; case 7: list(); break; case 8:daima delete(); break; case 0: exit(); break; default: msg("請(qǐng)輸入正確的操作指令!!!"); break; } menu(); }
此方法能夠處理經(jīng)常使用輸出窗口的代碼麻煩,直接調(diào)用此內(nèi)部方法實(shí)現(xiàn)輸出語(yǔ)句,更簡(jiǎn)潔的完成輸出語(yǔ)句
/** 能夠兼容輸入的屬性 */ public void msg(Object obj) { System.out.println(obj); }
結(jié)合上一個(gè)類中的方法完成對(duì)用戶的添加功能,后續(xù)的功能與此功能一樣,再switch語(yǔ)句中有輸入窗口能夠調(diào)用此類方法
/** 1-用戶添加的客戶端實(shí)現(xiàn) */ private void add() { msg("請(qǐng)輸入用戶信息:((按以下格式:Id/賬號(hào)/密碼/年齡/角色/郵箱/辦事處/賬戶狀態(tài)))"); sc = new Scanner(System.in); String s = sc.nextLine(); // 根據(jù)"/"截取用戶信息 String[] info = s.split("/"); if (um.findById(Integer.parseInt(info[0])) != null) { msg("該ID用戶已存在,請(qǐng)重新輸入"); add(); return; } else { User u = new User(Integer.parseInt(info[0]), info[1], info[2], Integer.parseInt(info[3]), info[4], info[5], info[6], info[7]); um.add(u); msg("添加成功!"); } }
根據(jù)用戶ID修改其密碼,簡(jiǎn)單的判斷語(yǔ)句對(duì)密碼信息確認(rèn)與修改
/** 2-根據(jù)ID修改密碼 */ private void alter() { sc = new Scanner(System.in); msg("請(qǐng)輸入用戶的ID:"); int id = sc.nextInt(); msg("密碼修改為:"); String passwor = sc.next(); if (um.modifypassword(id, passwor)) { msg("修改成功!"); } else { msg("修改失敗!"); } }
通過ID來查看用戶的個(gè)人信息
/** 3-個(gè)人信息查看 */ private void queryById() { sc = new Scanner(System.in); msg("請(qǐng)輸入需要查詢的用戶ID"); int id = sc.nextInt(); User u = um.findById(id); if (u == null) { msg(id + "號(hào)不存在,請(qǐng)重新輸入"); queryById(); return; } msg("Id\t賬號(hào)\t密碼\t\t年齡\t角色\t郵箱\t\t辦事處\t賬戶狀態(tài)\t"); msg(u); }
輸入用戶ID后對(duì)其狀態(tài)(是否禁用)進(jìn)行修改
/** 4-賬號(hào)狀態(tài)修改(禁用0、啟用1)*/ private void thestatus() { sc = new Scanner(System.in); msg("請(qǐng)輸入用戶ID:"); int id = sc.nextInt(); msg("賬號(hào)狀態(tài)修改(0/1):"); String status = sc.next(); if (um.modifystatus(id, status)) { msg("修改成功!"); } else { msg("修改失敗!"); } }
結(jié)合之前定義的用戶信息,實(shí)現(xiàn)簡(jiǎn)單的用戶登錄功能
/** 5-用戶登錄*/ private void enter(){ UserModule um = new UserModule(); um.register(); }
修改用戶角色(是否為管理員),給其權(quán)限
/** 6-修改用戶角色*/ private void update() { sc = new Scanner(System.in); msg("請(qǐng)輸入用戶ID:"); int id = sc.nextInt(); msg("角色修改(是否為管理員):"); String role = sc.next(); if (um.modifyrole(id, role)) { msg("修改成功!"); } else { msg("修改失敗!"); } }
將已存入的用戶信息列表化輸出
/** 7-用戶列表*/ private void list() { msg("Id\t賬號(hào)\t密碼\t\t年齡\t角色\t郵箱\t\t辦事處\t賬戶狀態(tài)\t"); for (User u : um.findAll()) { msg(u); } }
刪除功能,根據(jù)ID刪除存入數(shù)組的用戶信息
/** 8-根據(jù)ID刪除用戶*/ private void delete() { sc = new Scanner(System.in); msg("請(qǐng)輸入用戶ID:"); int id = sc.nextInt(); if (um.delete(id)) { msg("刪除成功!"); } else { msg("刪除失敗!"); } }
外加一個(gè)簡(jiǎn)單的退出系統(tǒng)的功能,不用鼠標(biāo)關(guān)閉Console窗口
/** 0-體統(tǒng)退出 */ private void exit() { sc = new Scanner(System.in); msg("是否確定退出?(Y/N)"); String op = sc.next(); if (op.equalsIgnoreCase("Y")) { msg("謝謝使用,再見!"); System.exit(1); } }
此類的程序執(zhí)行入口,調(diào)用系統(tǒng)用戶登錄方法和主窗口方法,調(diào)用的方法中再實(shí)現(xiàn)所有功能
public static void main(String[] args) { TestUser tu = new TestUser(); tu.enter(); tu.menu(); }
技術(shù)含量不高的完成了一個(gè)有用戶登錄,對(duì)用戶信息進(jìn)行增刪改查功能的系統(tǒng)
感謝各位的閱讀!關(guān)于“Java怎么實(shí)現(xiàn)用戶管理系統(tǒng)”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。