1. 程式人生 > >Java項目之員工收錄系統

Java項目之員工收錄系統

true case arch log 小型項目 接下來 lsi object mov

在Java SE中,對IO流與集合的操作在應用中比較重要。接下來,我以一個小型項目的形式,演示IO流、集合等知識點在實踐中的運用。

該項目名稱為“員工收錄系統”,在Eclipse的控制臺上進行操作。操作界面如下:

技術分享圖片

該項目的文件結構如下:

技術分享圖片

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("----員工收錄系統");
System.out.println("1.增加員工功能");
System.out.println("2.查看員工功能");
System.out.println("3.修改員工功能");
System.out.println("4.刪除員工功能");
System.out.println("5.退出系統");
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("您選擇了退出系統");
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:

根據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;

/**
* 根據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("系統內部問題,無法操作");
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("請問是否繼續錄入?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:");
//返回整數
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);

//詢問是否繼續錄入
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("系統內部問題,無法操作");
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("數據文件不存在,無法查看");
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("系統內部問題,無法操作");
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("數據文件不存在,無法查看");
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("系統內部問題,無法操作");
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("數據文件不存在,無法查看");
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;
}
}
}

Java項目之員工收錄系統