Java實現(xiàn)用戶管理系統(tǒng)
基于Java的簡單的用戶管理系統(tǒng),供大家參考,具體內(nèi)容如下
此系統(tǒng)功能和方法都比較簡單
本次系統(tǒng)通過控制臺輸入商品的基本信息,加入管理員的登錄與對是否為管理員進(jìn)行操作
對于功能的實現(xiàn),分別定義了3個類
用戶基本屬性類
此類包含用戶id、賬號、密碼、年齡、角色(是否為管理員)、郵箱、辦事處、賬戶狀態(tài)
private int id;// id號 private String username;// 賬號 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; ?? ?}
完成對用戶屬性的定義
在新類中對用戶屬性進(jìn)行修飾和功能的實現(xiàn)
通過ArrayList動態(tài)數(shù)組能夠?qū)?shù)組的基本信息進(jìn)行存儲
Scanner方法來使用控制臺輸入功能,結(jié)合方法來輸入對應(yīng)的信息
static int i = 0; ?? ?String[] admin = { "admin", "admin123" }; ?? ?static ArrayList<User> list = new ArrayList<>(); ?? ?private Scanner sc = new Scanner(System.in);
用簡單的if/else語句和for循環(huán)結(jié)合來實現(xiàn)增刪改查功能
用戶的增添與密碼的修改實習(xí)了控制臺輸入與修改信息的能力
/**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查看個人信息 */ ?? ?public User findById(int id) { ?? ??? ?User us = null; ?? ??? ?for (User u : list) { ?? ??? ??? ?if (u.getId() == id) { ?? ??? ??? ??? ?us = u; ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?return us; ?? ?} ?? ?/** 4-根據(jù)ID修改賬號狀態(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("請輸入用戶名:"); ?? ??? ?if (sc.next().equals(admin[0])) { ?? ??? ??? ?System.out.println("請輸入密碼:"); ?? ??? ??? ?if (sc.next().equals(admin[1])) { ?? ??? ??? ??? ?System.out.println("登錄成功!"); ?? ??? ??? ?} else { ?? ??? ??? ??? ?System.out.println("密碼錯誤!請重新輸入!"); ?? ??? ??? ??? ?register(); ?? ??? ??? ?} ?? ??? ?} else { ?? ??? ??? ?System.out.println("用戶名錯誤!請重新輸入!"); ?? ??? ??? ?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; ?? ?}
由此就通過方法對一些必要的功能進(jìn)行了完成
再通過另一個主窗口類對其他類進(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】個人信息查看==================="); ?? ??? ?msg("======【4】賬號狀態(tài)修改(禁用0、啟用1)========"); ?? ??? ?msg("======【5】用戶登錄======================="); ?? ??? ?msg("======【6】修改用戶角色(設(shè)置取消管理員)======="); ?? ??? ?msg("======【7】用戶列表======================"); ?? ??? ?msg("======【8】刪除用戶======================"); ?? ??? ?msg("======【0】退出系統(tǒng)======================"); ?? ??? ?msg("請輸入操作指令: "); ?? ??? ?start(); ?? ?}
通過基礎(chǔ)語句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("請輸入正確的操作指令!!!"); ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?menu(); ?? ?}
此方法能夠處理經(jīng)常使用輸出窗口的代碼麻煩,直接調(diào)用此內(nèi)部方法實現(xiàn)輸出語句,更簡潔的完成輸出語句
?/** 能夠兼容輸入的屬性 */ ?? ?public void msg(Object obj) { ?? ??? ?System.out.println(obj); ?? ?}
結(jié)合上一個類中的方法完成對用戶的添加功能,后續(xù)的功能與此功能一樣,再switch語句中有輸入窗口能夠調(diào)用此類方法
/** 1-用戶添加的客戶端實現(xiàn) */ private void add() { ?? ??? ?msg("請輸入用戶信息:((按以下格式:Id/賬號/密碼/年齡/角色/郵箱/辦事處/賬戶狀態(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用戶已存在,請重新輸入"); ?? ??? ??? ?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修改其密碼,簡單的判斷語句對密碼信息確認(rèn)與修改
/** 2-根據(jù)ID修改密碼 */ private void alter() { ?? ??? ?sc = new Scanner(System.in); ?? ??? ?msg("請輸入用戶的ID:"); ?? ??? ?int id = sc.nextInt(); ?? ??? ?msg("密碼修改為:"); ?? ??? ?String passwor = sc.next(); ?? ??? ?if (um.modifypassword(id, passwor)) { ?? ??? ??? ?msg("修改成功!"); ?? ??? ?} else { ?? ??? ??? ?msg("修改失敗!"); ?? ??? ?} ?? ?}
通過ID來查看用戶的個人信息
/** 3-個人信息查看 */ ?? ?private void queryById() { ?? ??? ?sc = new Scanner(System.in); ?? ??? ?msg("請輸入需要查詢的用戶ID"); ?? ??? ?int id = sc.nextInt(); ?? ??? ?User u = um.findById(id); ?? ??? ?if (u == null) { ?? ??? ??? ?msg(id + "號不存在,請重新輸入"); ?? ??? ??? ?queryById(); ?? ??? ??? ?return; ?? ??? ?} ?? ??? ?msg("Id\t賬號\t密碼\t\t年齡\t角色\t郵箱\t\t辦事處\t賬戶狀態(tài)\t"); ?? ??? ?msg(u); ?? ?}
輸入用戶ID后對其狀態(tài)(是否禁用)進(jìn)行修改
/** 4-賬號狀態(tài)修改(禁用0、啟用1)*/ private void thestatus() { ?? ??? ?sc = new Scanner(System.in); ?? ??? ?msg("請輸入用戶ID:"); ?? ??? ?int id = sc.nextInt(); ?? ??? ?msg("賬號狀態(tài)修改(0/1):"); ?? ??? ?String status = sc.next(); ?? ??? ?if (um.modifystatus(id, status)) { ?? ??? ??? ?msg("修改成功!"); ?? ??? ?} else { ?? ??? ??? ?msg("修改失敗!"); ?? ??? ?} ?? ?}
結(jié)合之前定義的用戶信息,實現(xiàn)簡單的用戶登錄功能
/** 5-用戶登錄*/ ?? ?private void enter(){ ?? ??? ?UserModule um = new UserModule(); ?? ??? ?um.register(); ?? ?}
修改用戶角色(是否為管理員),給其權(quán)限
?/** 6-修改用戶角色*/ ?private void update() { ?? ??? ?sc = new Scanner(System.in); ?? ??? ?msg("請輸入用戶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賬號\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("請輸入用戶ID:"); ?? ??? ?int id = sc.nextInt(); ?? ??? ?if (um.delete(id)) { ?? ??? ??? ?msg("刪除成功!"); ?? ??? ?} else { ?? ??? ??? ?msg("刪除失敗!"); ?? ??? ?} ?? ?}
外加一個簡單的退出系統(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)用的方法中再實現(xiàn)所有功能
public static void main(String[] args) { ?? ??? ?TestUser tu = new TestUser(); ?? ??? ?tu.enter(); ?? ??? ?tu.menu(); ?? ?}
技術(shù)含量不高的完成了一個有用戶登錄,對用戶信息進(jìn)行增刪改查功能的系統(tǒng)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java中CountDownLatch進(jìn)行多線程同步詳解及實例代碼
這篇文章主要介紹了Java中CountDownLatch進(jìn)行多線程同步詳解及實例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03java 中設(shè)計模式(裝飾設(shè)計模式)的實例詳解
這篇文章主要介紹了java 中設(shè)計模式(裝飾設(shè)計模式)的實例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09解決springboot文件配置端口不起作用(默認(rèn)8080)
這篇文章主要介紹了解決springboot文件配置端口不起作用(默認(rèn)8080),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08HttpServletRequest對象常用功能_動力節(jié)點Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了HttpServletRequest對象常用功能的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07