Java项目之员工收录系统 (3)

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;
}
}
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpswjy.html