//===============================================================
public class Pojo {
private String name;private String idUser;
private String sex;
private String mobNum;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIdUser() {
return idUser;
}
public void setIdUser(String idUser) {
this.idUser = idUser;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getMobNum() {
return mobNum;
}
public void setMobNum(String mobNum) {
this.mobNum = mobNum;
}
@Override
public String toString() {
return "Pojo [name=" + name + ", idUser=" + idUser + ", sex=" + sex + ", mobNum=" + mobNum + "]";
}
}
//=========================================================================
import java.util.ArrayList;
public class ShowService {
ArrayList<Pojo> datList = new ArrayList<Pojo>();
public static Pojo upData;
public ArrayList<Pojo> getDatList() {
return datList;
}
public void setDatList(ArrayList<Pojo> datList) {
this.datList = datList;
}
public void showInit() {
System.out.println("1.新增");
System.out.println("2.查询");
System.out.println("3.更改");
System.out.println("4.删除");
System.out.println("5.查询所有员工");
System.out.println("请选择操作,推出请输入bye");
}
public boolean addData(Pojo emp) {
// TODO Auto-generated method stub
return datList.add(emp);
}
public boolean delData(String rcIdUser) {
// TODO Auto-generated method stub
for (Pojo pojo : datList) {
if(rcIdUser.equals(pojo.getIdUser())) {
datList.remove(pojo);
return true;
}
}
return false;
}
public boolean findDat(String rcName) {
// TODO Auto-generated method stub
for (Pojo pojo : datList) {
if(rcName.equals(pojo.getName())){
System.out.println(pojo);
return true;
}
}
return false;
}
public boolean testKey(String string, String string2) {
// TODO Auto-generated method stub
for (Pojo pojo : datList) {
if(string.equals(pojo.getName())&&string2.equals(pojo.getIdUser())) {
upData=pojo;
datList.remove(pojo);
return true;
}
}
return false;
}
public void dataSet() {
// TODO Auto-generated method stub
System.out.println("A+姓名");
System.out.println("B+员工编号");
System.out.println("C+性别");
System.out.println("D+手机号码");
System.out.println("请选择操作,推出请输入bye");
}
public boolean dataUpdate(String str, String str2) {
// TODO Auto-generated method stub
if(str.equals("A")) {
upData.setName(str2);
return true;
}else if(str.equals("B")) {
upData.setIdUser(str2);
return true;
}else if(str.equals("C")) {
upData.setSex(str2);
return true;
}else if(str.equals("D")) {
upData.setMobNum(str2);
return true;
}
return false;
}
}
//========================================================================
import java.util.ArrayList;
import java.util.Scanner;
public class DoMain {
//初始界面
public static void main(String[] args) {
ShowService view =new ShowService();
while(true) {
view.showInit();
Scanner input =new Scanner(System.in);
String optVal=input.next();
if(optVal.equals("1")) {
System.out.println("请输入员工的信息,逗号隔开");
String data = input.next();
String[] rcData = data.split(",");
//添加信息
Pojo emp=new Pojo();
emp.setName(rcData[0]);
emp.setIdUser(rcData[1]);
emp.setSex(rcData[2]);
emp.setMobNum(rcData[3]);
boolean b = view.addData(emp);
if(b) {
System.out.println("信息添加成功");
}
else {
System.out.println("信息添加失败");
}
}else if(optVal.equals("2")) {
//按员工姓名查询
System.out.println("请输入员工姓名进行查询");
String rcName = input.next();
boolean b = view.findDat(rcName);
if(b) {
System.out.println("信息查询成功");
}
else {
System.out.println("信息查询失败");
}
}else if(optVal.equals("3")) { //更改信息
System.out.println("请输入员工姓名与编号");
String key =input.next();
String[] rcKey = key.split(",");
boolean b = view.testKey(rcKey[0],rcKey[1]);
if(b) {
System.out.println("验证通过");
view.dataSet();
String rcData=input.next();
String[] rcSetData=rcData.split("\\+");
boolean temp=view.dataUpdate(rcSetData[0],rcSetData[1]);
boolean b1=view.addData(view.upData);
if(temp&&b1==true) {
System.out.println(view.upData);
System.out.println("信息更改成功");
}
else {
System.out.println("信息更改失败");
}
}
}else if(optVal.equals("4")) { //删除信息
System.out.println("请输入要删除的员工编号(编号唯一)");
String rcIdUser=input.next();
boolean b = view.delData(rcIdUser);
if(b) {
System.out.println("信息删除成功");
}else {
System.out.println("信息删除失败");
}
}else if(optVal.equals("5")) {
//查询所有员工测试用
System.out.println("所有员工信息");
ArrayList<Pojo> allList = view.getDatList(); //获取集合
for(int i=0;i<allList.size();i++) {
System.out.println(allList.get(i));
}
}else if(optVal.equals("bye")) {
System.out.println("欢迎再次使用");
System.exit(0);
}
}
}
}