最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)員工信息管理系統(tǒng)

 更新時(shí)間:2022年02月28日 10:42:01   作者:奔跑在夢(mèng)想的道路上  
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)員工信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在Java SE中,對(duì)IO流與集合的操作在應(yīng)用中比較重要。接下來,我以一個(gè)小型項(xiàng)目的形式,演示IO流、集合等知識(shí)點(diǎn)在實(shí)踐中的運(yùn)用。

該項(xiàng)目名稱為“員工信息管理系統(tǒng)”(或“員工收錄系統(tǒng)”),主要是通過輸入員工的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("無此功能");
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;
/**
* 一個(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)部問題,無法操作");
return;
}
}
public boolean checkNum(String idStr){
//檢測(cè)輸入格式
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("請(qǐng)問是否繼續(xù)錄入?Y/N");
String choice=sc.nextLine();
if("Y".equalsIgnoreCase(choice)){
add();
}else if("N".equalsIgnoreCase(choice)){
//保存到文件
saveToFile();
return;
}else{
System.out.println("無此命令,請(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);
//詢問是否繼續(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)部問題,無法操作");
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){
//檢測(cè)輸入格式
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,查看單個(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("無此用戶");
return;
}
}else{
System.out.println("無此命令,請(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)部問題,無法操作");
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){
//檢測(cè)輸入格式
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();
//測(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("無此用戶");
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)部問題,無法操作");
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){
//檢測(cè)輸入格式
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("刪除成功");
//測(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("無此用戶");
return;
}
}
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • springmvc+spring+mybatis實(shí)現(xiàn)用戶登錄功能(下)

    springmvc+spring+mybatis實(shí)現(xiàn)用戶登錄功能(下)

    這篇文章主要為大家詳細(xì)介紹了springmvc+spring+mybatis實(shí)現(xiàn)用戶登錄功能的第二篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Spring Boot示例分析講解自動(dòng)化裝配機(jī)制核心注解

    Spring Boot示例分析講解自動(dòng)化裝配機(jī)制核心注解

    這篇文章主要分析了Spring Boot 自動(dòng)化裝配機(jī)制核心注解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-07-07
  • java實(shí)現(xiàn)簡(jiǎn)單圖片上傳下載功能

    java實(shí)現(xiàn)簡(jiǎn)單圖片上傳下載功能

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單圖片上傳下載功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • 關(guān)于Mybatis使用collection分頁問題

    關(guān)于Mybatis使用collection分頁問題

    項(xiàng)目中mybatis分頁的場(chǎng)景是非常高頻的,當(dāng)使用ResultMap并配置collection做分頁的時(shí)候,我們可能會(huì)遇到獲取當(dāng)前頁的數(shù)據(jù)少于每頁大小的數(shù)據(jù)問題。接下來通過本文給大家介紹Mybatis使用collection分頁問題,感興趣的朋友一起看看吧
    2021-11-11
  • Kubernetes k8s集群之包管理器Helm方式

    Kubernetes k8s集群之包管理器Helm方式

    這篇文章主要介紹了Kubernetes k8s集群之包管理器Helm方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Prometheus pushgateway的使用詳解

    Prometheus pushgateway的使用詳解

    為了防止 pushgateway 重啟或意外掛掉,導(dǎo)致數(shù)據(jù)丟失,我們可以通過 -persistence.file 和 -persistence.interval 參數(shù)將數(shù)據(jù)持久化下來,接下來通過本文給大家介紹下Prometheus pushgateway的使用,感興趣的朋友一起看看吧
    2021-11-11
  • java理論基礎(chǔ)函數(shù)式接口特點(diǎn)示例解析

    java理論基礎(chǔ)函數(shù)式接口特點(diǎn)示例解析

    這篇文章主要為大家介紹了java理論基礎(chǔ)函數(shù)式接口特點(diǎn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-03-03
  • Java多線程開發(fā)工具之CompletableFuture的應(yīng)用詳解

    Java多線程開發(fā)工具之CompletableFuture的應(yīng)用詳解

    做Java編程,難免會(huì)遇到多線程的開發(fā),但是JDK8這個(gè)CompletableFuture類很多開發(fā)者目前還沒聽說過,但是這個(gè)類實(shí)在是太好用了,本文就來聊聊它的應(yīng)用吧
    2023-03-03
  • 在mybatis 中使用if else 進(jìn)行判斷的操作

    在mybatis 中使用if else 進(jìn)行判斷的操作

    這篇文章主要介紹了在mybatis 中使用if else 進(jìn)行判斷的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • 如何在logback日志配置里獲取服務(wù)器ip和端口

    如何在logback日志配置里獲取服務(wù)器ip和端口

    這篇文章主要介紹了如何在logback日志配置里獲取服務(wù)器ip和端口的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08

最新評(píng)論

凤城市| 秦安县| 阿克苏市| 屏南县| 山阳县| 巫溪县| 聂拉木县| 平度市| 江油市| 罗江县| 淮安市| 景谷| 田东县| 叶城县| 收藏| 德阳市| 上高县| 仁布县| 翁源县| 南通市| 灵武市| 淅川县| 莎车县| 手机| 铜山县| 琼海市| 崇州市| 耿马| 邵阳市| 万州区| 武平县| 光山县| 元朗区| 高要市| 高陵县| 白山市| 蛟河市| 阿拉善右旗| 山阴县| 富锦市| 五原县|