Java實(shí)現(xiàn)員工信息管理系統(tǒng)
在Java SE中,對(duì)IO流與集合的操作在應(yīng)用中比較重要。接下來(lái),我以一個(gè)小型項(xiàng)目的形式,演示IO流、集合等知識(shí)點(diǎn)在實(shí)踐中的運(yùn)用。
該項(xiàng)目名稱為“員工信息管理系統(tǒng)”(或“員工收錄系統(tǒng)”),主要是通過(guò)輸入員工的id、姓名信息,實(shí)現(xiàn)簡(jiǎn)單的增刪改查功能。
該項(xiàng)目主要在DOS窗口的控制臺(tái)或者Eclipse的控制臺(tái)上進(jìn)行操作。操作界面如下:

該項(xiàng)目的文件結(jié)構(gòu)如下:

Step 1:
入口類SystemMain的代碼為:
package empsystem;
import java.util.Scanner;
/**
* 主界面
* 一個(gè)Scanner錄入對(duì)象
* 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("無(wú)此功能");
break;
}
showWelcome();
}
public static void main(String[] args) {
new SystemMain();
}
}Step 2:
寫(xiě)文件路徑FilePath接口。
package empsystem;
public interface FilePath {
public static final String PATH_NAME="emp.em";
}Step 3:
寫(xiě)員工類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:
接下來(lái)是增,查,改,刪的類,分別是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;
/**
* 一個(gè)輸入器對(duì)象Scanner
* 文件
* 集合對(duì)象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)部問(wèn)題,無(wú)法操作");
return;
}
}
public boolean checkNum(String idStr){
//檢測(cè)輸入格式
if(idStr==null || idStr.equals("")){
System.out.println("非法輸入,重來(lái)");
return false;
}
char[] cs=idStr.toCharArray();
for(int i=0;i<cs.length;i++){
if(cs[i]<'0' || cs[i]>'9'){
System.out.println("輸入非法,重來(lái)");
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("請(qǐng)問(wèn)是否繼續(xù)錄入?Y/N");
String choice=sc.nextLine();
if("Y".equalsIgnoreCase(choice)){
add();
}else if("N".equalsIgnoreCase(choice)){
//保存到文件
saveToFile();
return;
}else{
System.out.println("無(wú)此命令,請(qǐng)重新選擇!");
askGoOn();
}
}
public void saveToFile(){
try {
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(ems);
oos.close();
//測(cè)試打印查看
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("請(qǐng)輸入用戶ID:");
//返回整數(shù)
int id=getRightNum();
for(int i=0;i<ems.size();i++){
if(id==ems.get(i).getId()){
System.out.println("id已存在,請(qǐng)重新輸入");
add();
}
}
System.out.println("請(qǐng)輸入員工姓名:");
String name=sc.nextLine();
Employ em=new Employ(id,name);
ems.add(em);
//詢問(wèn)是否繼續(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;
/**
* 一個(gè)輸入器對(duì)象Scanner
* 文件
* 集合對(duì)象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)部問(wèn)題,無(wú)法操作");
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ù)文件不存在,無(wú)法查看");
return;
}
}
public boolean checkNum(String idStr){
//檢測(cè)輸入格式
if(idStr==null || idStr.equals("")){
System.out.println("非法輸入,重來(lái)");
return false;
}
char[] cs=idStr.toCharArray();
for(int i=0;i<cs.length;i++){
if(cs[i]<'0' || cs[i]>'9'){
System.out.println("輸入非法,重來(lái)");
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,查看單個(gè)員工輸入N");
String choice=sc.nextLine();
if("Y".equalsIgnoreCase(choice)){
System.out.println(ems);
return;
}else if("N".equalsIgnoreCase(choice)){
System.out.println("請(qǐng)輸入要查詢的員ID:");
int id=getRightNum();
if(SearchID.searchId(id)!=null){
System.out.println("您查找的員工信息為:\n"+SearchID.searchId(id));
return;
}else{
System.out.println("無(wú)此用戶");
return;
}
}else{
System.out.println("無(wú)此命令,請(qǐng)重新選擇!");
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;
/**
* 一個(gè)輸入器對(duì)象Scanner
* 文件
* 集合對(duì)象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)部問(wèn)題,無(wú)法操作");
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ù)文件不存在,無(wú)法查看");
return;
}
}
public boolean checkNum(String idStr){
//檢測(cè)輸入格式
if(idStr==null || idStr.equals("")){
System.out.println("非法輸入,重來(lái)");
return false;
}
char[] cs=idStr.toCharArray();
for(int i=0;i<cs.length;i++){
if(cs[i]<'0' || cs[i]>'9'){
System.out.println("輸入非法,重來(lái)");
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();
//測(cè)試打印查看
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("請(qǐng)輸入要修改的用戶ID:");
int id=getRightNum();
if(SearchID.searchId(id)!=null){
System.out.println("修改前用戶的姓名為:"+SearchID.searchId(id).getName());
System.out.println("請(qǐng)輸入修改后的姓名:");
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("無(wú)此用戶");
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;
/**
* 一個(gè)輸入器對(duì)象Scanner
* 文件
* 集合對(duì)象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)部問(wèn)題,無(wú)法操作");
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ù)文件不存在,無(wú)法查看");
return;
}
}
public boolean checkNum(String idStr){
//檢測(cè)輸入格式
if(idStr==null || idStr.equals("")){
System.out.println("非法輸入,重來(lái)");
return false;
}
char[] cs=idStr.toCharArray();
for(int i=0;i<cs.length;i++){
if(cs[i]<'0' || cs[i]>'9'){
System.out.println("輸入非法,重來(lái)");
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("刪除成功");
//測(cè)試打印查看
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("請(qǐng)輸入要?jiǎng)h除的員工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("無(wú)此用戶");
return;
}
}
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)PDF在線預(yù)覽功能(四種方式)
這篇文章主要介紹了Java實(shí)現(xiàn)PDF在線預(yù)覽功能的四種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Java實(shí)現(xiàn)中文字符串與unicode互轉(zhuǎn)工具類
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)中文字符串與unicode互轉(zhuǎn)的工具類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
java Iterator接口和LIstIterator接口分析
這篇文章主要介紹了java Iterator接口和LIstIterator接口分析的相關(guān)資料,需要的朋友可以參考下2017-05-05
Java實(shí)現(xiàn)五子棋游戲單機(jī)版(1.0)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)五子棋游戲單機(jī)版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Spring Boot如何動(dòng)態(tài)創(chuàng)建Bean示例代碼
這篇文章主要給大家介紹了關(guān)于Spring Boot如何動(dòng)態(tài)創(chuàng)建Bean的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09
java數(shù)據(jù)結(jié)構(gòu)基礎(chǔ):線性表
這篇文章主要介紹了Java的數(shù)據(jù)解構(gòu)基礎(chǔ),希望對(duì)廣大的程序愛(ài)好者有所幫助,同時(shí)祝大家有一個(gè)好成績(jī),需要的朋友可以參考下,希望能給你帶來(lái)幫助2021-07-07
JavaWeb實(shí)現(xiàn)文件上傳與下載實(shí)例詳解
在Web應(yīng)用程序開(kāi)發(fā)中,文件上傳與下載功能是非常常用的功能,下面通過(guò)本文給大家介紹JavaWeb實(shí)現(xiàn)文件上傳與下載實(shí)例詳解,對(duì)javaweb文件上傳下載相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-02-02

