Java實現(xiàn)員工信息管理系統(tǒng)
在Java SE中,對IO流與集合的操作在應(yīng)用中比較重要。接下來,我以一個小型項目的形式,演示IO流、集合等知識點在實踐中的運用。
該項目名稱為“員工信息管理系統(tǒng)”(或“員工收錄系統(tǒng)”),主要是通過輸入員工的id、姓名信息,實現(xiàn)簡單的增刪改查功能。
該項目主要在DOS窗口的控制臺或者Eclipse的控制臺上進行操作。操作界面如下:
該項目的文件結(jié)構(gòu)如下:
Step 1:
入口類SystemMain的代碼為:
package empsystem; import java.util.Scanner; /** * 主界面 * 一個Scanner錄入對象 * Employ類 * 文件路徑 * 查重SearchID * @author 李章勇 * */ public class SystemMain { private Scanner sc=new Scanner(System.in); public SystemMain() { showWelcome(); } public void showWelcome(){ System.out.println("----員工收錄系統(tǒng)"); System.out.println("1.增加員工功能"); System.out.println("2.查看員工功能"); System.out.println("3.修改員工功能"); System.out.println("4.刪除員工功能"); System.out.println("5.退出系統(tǒng)"); String choice=sc.nextLine(); switch(choice){ case "1": System.out.println("您選擇了增加用戶功能"); //Add new Add(); break; case "2": System.out.println("您選擇了查看用戶功能"); //Search new ShowEmp(); break; case "3": System.out.println("您選擇了修改用戶功能"); //Modify new Modify(); break; case "4": System.out.println("您選擇了刪除用戶功能"); //刪除用戶Delete new Delete(); break; case "5": System.out.println("您選擇了退出系統(tǒng)"); return; default: System.out.println("無此功能"); break; } showWelcome(); } public static void main(String[] args) { new SystemMain(); } }
Step 2:
寫文件路徑FilePath接口。
package empsystem; public interface FilePath { public static final String PATH_NAME="emp.em"; }
Step 3:
寫員工類Employ。
package empsystem; import java.io.Serializable; /** * id,name * @author 李章勇 * */ public class Employ implements Serializable{ private int id; private String name; public Employ() { } public Employ(int id, String name) { super(); this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Employ [id=" + id + ", name=" + name + "]\n"; } }
Step 4:
根據(jù)ID查找員工的類SearchID。
package empsystem; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.ObjectInputStream; import java.util.ArrayList; import java.util.List; /** * 根據(jù)Id查找員工 * @author 李章勇 * */ public class SearchID { private SearchID(){} public static Employ searchId(int id){ File file=new File(FilePath.PATH_NAME); if(file.exists()){ try { ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file)); try { ArrayList<Employ> ems=(ArrayList<Employ>) ois.readObject(); ois.close(); for(int i=0;i<ems.size();i++){ if(id==ems.get(i).getId()){ return ems.get(i); } } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else{ return null; } return null; } }
Step 5:
接下來是增,查,改,刪的類,分別是Add類,ShowEmp類, Modify類,Modify類。
(1)
package empsystem; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.Scanner; /** * 一個輸入器對象Scanner * 文件 * 集合對象ArrayList * @author 李章勇 * */ public class Add { private Scanner sc=new Scanner(System.in); private File file=new File(FilePath.PATH_NAME); private ArrayList<Employ> ems; public Add() { if(file.exists()){ try { ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file)); try { ems=(ArrayList<Employ>) ois.readObject(); ois.close(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else{ ems=new ArrayList<Employ>(); } if(ems!=null){ add(); }else{ System.out.println("系統(tǒng)內(nèi)部問題,無法操作"); return; } } public boolean checkNum(String idStr){ //檢測輸入格式 if(idStr==null || idStr.equals("")){ System.out.println("非法輸入,重來"); return false; } char[] cs=idStr.toCharArray(); for(int i=0;i<cs.length;i++){ if(cs[i]<'0' || cs[i]>'9'){ System.out.println("輸入非法,重來"); return false; } } return true; } private String idStr; public int getRightNum(){ idStr=sc.nextLine(); if(!checkNum(idStr)){ getRightNum(); } int id=Integer.parseInt(idStr); return id; } public void askGoOn(){ System.out.println("請問是否繼續(xù)錄入?Y/N"); String choice=sc.nextLine(); if("Y".equalsIgnoreCase(choice)){ add(); }else if("N".equalsIgnoreCase(choice)){ //保存到文件 saveToFile(); return; }else{ System.out.println("無此命令,請重新選擇!"); askGoOn(); } } public void saveToFile(){ try { ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file)); oos.writeObject(ems); oos.close(); //測試打印查看 System.out.println("添加成功"); System.out.println(ems); return; } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void add(){ System.out.println("請輸入用戶ID:"); //返回整數(shù) int id=getRightNum(); for(int i=0;i<ems.size();i++){ if(id==ems.get(i).getId()){ System.out.println("id已存在,請重新輸入"); add(); } } System.out.println("請輸入員工姓名:"); String name=sc.nextLine(); Employ em=new Employ(id,name); ems.add(em); //詢問是否繼續(xù)錄入 askGoOn(); } }
(2)
package empsystem; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.ObjectInputStream; import java.util.ArrayList; import java.util.Scanner; /** * 一個輸入器對象Scanner * 文件 * 集合對象ArrayList * @author 李章勇 * */ public class ShowEmp { private Scanner sc=new Scanner(System.in); private File file=new File(FilePath.PATH_NAME); private ArrayList<Employ> ems; public ShowEmp() { if(file.exists()){ try { ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file)); try { ems=(ArrayList<Employ>) ois.readObject(); ois.close(); if(ems!=null){ show(); }else{ System.out.println("系統(tǒng)內(nèi)部問題,無法操作"); return; } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else{ System.out.println("數(shù)據(jù)文件不存在,無法查看"); return; } } public boolean checkNum(String idStr){ //檢測輸入格式 if(idStr==null || idStr.equals("")){ System.out.println("非法輸入,重來"); return false; } char[] cs=idStr.toCharArray(); for(int i=0;i<cs.length;i++){ if(cs[i]<'0' || cs[i]>'9'){ System.out.println("輸入非法,重來"); return false; } } return true; } private String idStr; public int getRightNum(){ idStr=sc.nextLine(); if(!checkNum(idStr)){ getRightNum(); } int id=Integer.parseInt(idStr); return id; } public void show(){ System.out.println("查看全部員工輸入Y,查看單個員工輸入N"); String choice=sc.nextLine(); if("Y".equalsIgnoreCase(choice)){ System.out.println(ems); return; }else if("N".equalsIgnoreCase(choice)){ System.out.println("請輸入要查詢的員ID:"); int id=getRightNum(); if(SearchID.searchId(id)!=null){ System.out.println("您查找的員工信息為:\n"+SearchID.searchId(id)); return; }else{ System.out.println("無此用戶"); return; } }else{ System.out.println("無此命令,請重新選擇!"); show(); } } }
(3)
package empsystem; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.Scanner; /** * 一個輸入器對象Scanner * 文件 * 集合對象ArrayList * @author 李章勇 * */ public class Modify { private Scanner sc=new Scanner(System.in); private File file=new File(FilePath.PATH_NAME); private ArrayList<Employ> ems; public Modify() { if(file.exists()){ try { ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file)); try { ems=(ArrayList<Employ>) ois.readObject(); ois.close(); if(ems!=null){ modify(); }else{ System.out.println("系統(tǒng)內(nèi)部問題,無法操作"); return; } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else{ System.out.println("數(shù)據(jù)文件不存在,無法查看"); return; } } public boolean checkNum(String idStr){ //檢測輸入格式 if(idStr==null || idStr.equals("")){ System.out.println("非法輸入,重來"); return false; } char[] cs=idStr.toCharArray(); for(int i=0;i<cs.length;i++){ if(cs[i]<'0' || cs[i]>'9'){ System.out.println("輸入非法,重來"); return false; } } return true; } private String idStr; public int getRightNum(){ idStr=sc.nextLine(); if(!checkNum(idStr)){ getRightNum(); } int id=Integer.parseInt(idStr); return id; } public void saveToFile(){ try { ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file)); oos.writeObject(ems); oos.close(); //測試打印查看 System.out.println("修改成功"); System.out.println(ems); return; } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void modify(){ System.out.println("請輸入要修改的用戶ID:"); int id=getRightNum(); if(SearchID.searchId(id)!=null){ System.out.println("修改前用戶的姓名為:"+SearchID.searchId(id).getName()); System.out.println("請輸入修改后的姓名:"); String name=sc.nextLine(); for(int i=0;i<ems.size();i++){ if(id==ems.get(i).getId()){ ems.get(i).setName(name); saveToFile(); } } }else{ System.out.println("無此用戶"); return; } } }
(4)
package empsystem; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.Scanner; /** * 一個輸入器對象Scanner * 文件 * 集合對象ArrayList * @author 李章勇 * */ public class Delete { private Scanner sc=new Scanner(System.in); private File file=new File(FilePath.PATH_NAME); private ArrayList<Employ> ems; public Delete() { if(file.exists()){ try { ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file)); try { ems=(ArrayList<Employ>) ois.readObject(); ois.close(); if(ems!=null){ delete(); }else{ System.out.println("系統(tǒng)內(nèi)部問題,無法操作"); return; } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else{ System.out.println("數(shù)據(jù)文件不存在,無法查看"); return; } } public boolean checkNum(String idStr){ //檢測輸入格式 if(idStr==null || idStr.equals("")){ System.out.println("非法輸入,重來"); return false; } char[] cs=idStr.toCharArray(); for(int i=0;i<cs.length;i++){ if(cs[i]<'0' || cs[i]>'9'){ System.out.println("輸入非法,重來"); return false; } } return true; } private String idStr; public int getRightNum(){ idStr=sc.nextLine(); if(!checkNum(idStr)){ getRightNum(); } int id=Integer.parseInt(idStr); return id; } public void saveToFile(){ try { ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file)); oos.writeObject(ems); oos.close(); System.out.println("刪除成功"); //測試打印查看 System.out.println(ems); return; } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void delete(){ System.out.println("請輸入要刪除的員工ID:"); int id=getRightNum(); if(SearchID.searchId(id)!=null){ System.out.println("刪除前用戶的姓名為:"+SearchID.searchId(id).getName()); Iterator<Employ> it=ems.iterator(); while(it.hasNext()){ Employ em=it.next(); if(id==em.getId()){ it.remove(); saveToFile(); } } }else{ System.out.println("無此用戶"); return; } } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實現(xiàn)中文字符串與unicode互轉(zhuǎn)工具類
這篇文章主要為大家詳細介紹了Java實現(xiàn)中文字符串與unicode互轉(zhuǎn)的工具類,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-04-04java Iterator接口和LIstIterator接口分析
這篇文章主要介紹了java Iterator接口和LIstIterator接口分析的相關(guān)資料,需要的朋友可以參考下2017-05-05Spring Boot如何動態(tài)創(chuàng)建Bean示例代碼
這篇文章主要給大家介紹了關(guān)于Spring Boot如何動態(tài)創(chuàng)建Bean的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2017-09-09java數(shù)據(jù)結(jié)構(gòu)基礎(chǔ):線性表
這篇文章主要介紹了Java的數(shù)據(jù)解構(gòu)基礎(chǔ),希望對廣大的程序愛好者有所幫助,同時祝大家有一個好成績,需要的朋友可以參考下,希望能給你帶來幫助2021-07-07