当前位置:网站首页 > 公文范文 > 主题教育 > 员工信息管理系统java

员工信息管理系统java

时间:2022-02-27 13:43:36 浏览次数:

云南大学2019年春季学期信息学院 电子信息工程2018级 《面向对象信息系统开发》期末检测(堂下测试)试卷A 满分100分 考试时间:150分钟 任课教师:高莲 学院:__信息学院__ 专业:__电子信息工程__ 学号:_20181060261_ 姓名:_李康,周五班,33号_ 题号 1 2 3 4 总分 得分 2019年春季学期《面向对象信息系统开发》期末检测题 一、编写一个Java应用程序,对员工信息进行管理,功能实现详述如下:
1. 请先通过Java代码创建文件Employ以表格形式存储雇员的相关信息。雇员类的相关属性说明如下:
列名 数据类型 Employ_Id int (该列为员工编号,不能重复) Employ_Name varchar Employ_Address varchar Employ_Salary float Employ_Phone varchar Employ_Department varchar (市场部、财务部、人事部、研发部、生产车间)
2. 通过Java代码向Employ文件中写入初始数据:
1 王五 山东 4500 13798987654 管理部 2 安心 贵州 1200 13799999654 生产车间 3 杨燕 四川 6300 13124569874 研发部 3、创建菜单选项 1、添加记录 2、修改记录 3、删除记录 4、搜索记录 5、退出 4、选定相关菜单后,提示用户进行相关操作 1)、添加记录 提示用户输入相关数据,并检测输入数据的有效性及是否与文件中数据重复,并将操作数据结果显示给用户进行查看;

2)、修改记录 提示用户输入要修改记录员工的Employ_Id,查找该用户是否存在,不存在提醒用户该雇员信息不存在;
存在的话,提示用户输入新的Employ_Salary(其它数据列暂时不允许修改);

3)、删除记录 提示用户输入要删除记录员工的Employ_Id,查找该用户是否存在,不存在提醒用户该雇员信息不存在;
存在的话,将该行数据删除;

4)、搜索记录 提示用户输入要删除记录员工的Employ_Name,若雇员存在,将搜索信息显示出来;
若该雇员不存在,在提示无此雇员存在;

5)、退出 运行System.exit(0);退出程序。

数据的有效性说明如下:
A、 Employ_Name只能是字符,最多不超过10个;

B、 Employ_Salary为员工工资,应该大于当地最低工资(800);

C、 Employ_Phone为员工电话号码,应该都为数字,总长度为11;

D、 Employ_Department取值为市场部、财务部、人事部、研发部、生产车间中一个。

一、实现思路:
二、源代码:
public class abnormal {public static boolean panduanId(String Id){//判断Id是否有效的方法 boolean flag=true; fileOperation temp=new fileOperation(“d:\\2.txt“); StringBuffer buffer1=temp.readFile(); int result1=buffer1.indexOf(Id+“ “,0);//判断员工是否存在但不能确定是员工号还是手机号或薪水if(result1>1)//判断获得的位置,如果不是1,则加强判断条件 result1=buffer1.indexOf(“:“+Id+“ “,0); if(result1==-1)//不存在重复 flag=false; return flag; } public static boolean panduanname(String name){//判断姓名是否有效的方法 String name1=name; boolean flag=false; Pattern p=Pattern.compile(“.*\\d+.*“); Matcher m=p.matcher(name1); if(m.matches()) flag=true; return flag;} public static boolean panduandepartment(String department){//判断部门是否有效的方法 boolean flag=false; if(department.equals(“市场部“)||“人事部“.equals(department)||“研发部“.equals(department)||“生产车间“.equals(department)||“财务部“.equals(department)) flag=true; return flag;}} public class employDo extends javax.swing.JFrame { String newId; public employDo() { initComponents(); } private void addcancelActionPerformed(java.awt.event.ActionEvent evt) { tianjiatankuang.setVisible(false); Id.setText(““);//输入框摸空 name.setText(““); place.setText(““); money.setText(““); phonenum.setText(““); department.setText(““);} private void addsureActionPerformed(java.awt.event.ActionEvent evt) { while(true){//异常处理区域 try{if(abnormal.panduanId(Id.getText())) throw new Exception(); break; } catch(Exception ex){ Id.setText(JOptionPane.showInputDialog(null, “错误!该员工已存在:\n“,null)); }} while(true){//异常处理区域 try{if(abnormal.panduanname(name.getText())) throw new Exception(); break; } catch(Exception ex){ name.setText(JOptionPane.showInputDialog(null, “错误!姓名中不可含有数字:\n“,null)); }} while(true) {try{if(Double.parseDouble(money.getText())<800) throw new Exception(); break; } catch(Exception ex){ money.setText(JOptionPane.showInputDialog(null, “错误!工资应高于或等于800元:\n“,null)); }} while(true){ try{ boolean result2=phonenum.getText().matches(“[0-9]+“); if(phonenum.getText().length()!=11||result2==false) throw new Exception(); break; } catch(Exception ex){ phonenum.setText(JOptionPane.showInputDialog(null, “错误!请输入有效的手机号:\n“,null)); }} while(true){ try{ if(abnormal.panduandepartment(department.getText())==false) throw new Exception(); break; } catch(Exception ex){ department.setText(JOptionPane.showInputDialog(null, “错误!不存在该部门:\n“,null)); }} tianjiabingxianshi();} public void tianjiabingxianshi(){//存入文件并重新显示 fileOperation temp=new fileOperation(“d:\\2.txt“); temp.writeFile(Id.getText()+“ “+name.getText()+“ “+place.getText()+“ “+money.getText()+“ “+phonenum.getText()+“ “+department.getText()); Id.setText(““);//输入框摸空 name.setText(““); place.setText(““); money.setText(““); phonenum.setText(““); department.setText(““); tianjiatankuang.setVisible(false); JOptionPane.showMessageDialog(null, “添加成功“, “提示“, JOptionPane.PLAIN_MESSAGE); look.xianshi(this.biaoge1.getModel(),this.biaoge1.getRowCount()); } private void searchsureActionPerformed(java.awt.event.ActionEvent evt) { fileOperation temp=new fileOperation(“d:\\2.txt“); StringBuffer buffer1=temp.readFile(); int result1=buffer1.indexOf(searchName.getText()+“ “,0);//判断员工是否存在 if(result1!=-1) {searchtankuang.setVisible(false); function.search(buffer1,searchName.getText()+“ “ , this.biaoge2.getRowCount(), this.biaoge2.getModel()); employinformationtankuang.setVisible(true); } else JOptionPane.showMessageDialog(null, “没有找到此员工“, “抱歉“, JOptionPane.WARNING_MESSAGE); searchName.setText(““);} private void searchcancelActionPerformed(java.awt.event.ActionEvent evt) { searchtankuang.setVisible(false); searchName.setText(““);} private void deletesureActionPerformed(java.awt.event.ActionEvent evt) { fileOperation temp=new fileOperation(“d:\\2.txt“); StringBuffer buffer1=temp.readFile(); int result1=buffer1.indexOf(deleteId.getText()+“ “,0);//如果不是第一个位置,则可能将其他数据当作员工Id if(result1!=-1) { function.delete(deleteId.getText(), buffer1); deleteId.setText(““); deletetankuang.setVisible(false); JOptionPane.showMessageDialog(null, “删除成功“, “提示“, JOptionPane.PLAIN_MESSAGE); look.xianshi(this.biaoge1.getModel(),this.biaoge1.getRowCount());} else {JOptionPane.showMessageDialog(null, “没有找到此员工“, “抱歉“, JOptionPane.WARNING_MESSAGE); } deleteId.setText(““);} private void changesureActionPerformed(java.awt.event.ActionEvent evt) { newId=changeId.getText(); fileOperation temp=new fileOperation(“d:\\2.txt“); StringBuffer buffer1=temp.readFile(); int result1=buffer1.indexOf(changeId.getText()+“ “,0);//如果不是第一个位置,则可能将其他数据当作员工Id if(result1>=1)//不是第一个位置 result1=buffer1.indexOf(“:“+changeId.getText()+“ “,0);//加强判断条件 if(result1!=-1) {changemoneytankuang.setVisible(true); }else { JOptionPane.showMessageDialog(null, “没有找到此员工“, “抱歉“, JOptionPane.WARNING_MESSAGE); changeId.setText(““);}} private void changemoneysureActionPerformed(java.awt.event.ActionEvent evt) { while(true) { try{if(Double.parseDouble(changemoney.getText())<800) throw new Exception(); break; } catch(Exception ex){ changemoney.setText(JOptionPane.showInputDialog(null, “错误!工资应高于或等于800元:\n“,null)); }} function.changemoney(changeId.getText(), changemoney.getText()); changeId.setText(““); changemoney.setText(““); changetankuang.setVisible(false); changemoneytankuang.setVisible(false); JOptionPane.showMessageDialog(null, “修改成功“, “提示“, JOptionPane.PLAIN_MESSAGE); look.xianshi(this.biaoge1.getModel(),this.biaoge1.getRowCount());} private void 警告sureActionPerformed(java.awt.event.ActionEvent evt) { fileOperation.deleteFile(“d:\\2.txt“);//删除原文件信息 getIt.deposit();//存入系统设定好的信息 System.out.println(“已成功初始化数据“); JOptionPane.showMessageDialog(null, “已成功初始化数据“, “恭喜“, JOptionPane.PLAIN_MESSAGE); look.xianshi(this.biaoge1.getModel(),this.biaoge1.getRowCount()); jinggao.setVisible(false); } public class fileOperation {private String filePath; private File file; public void setFilePath(String filePath) {this.filePath=filePath;} public String getFilePath() {return this.filePath; } fileOperation() { } fileOperation(String filePath) { this.filePath=filePath; this.file=new File(this.filePath); if(!this.file.exists()) {try { this.file.createNewFile();} catch(IOException ce) {ce.printStackTrace();}}} public StringBuffer readFile() {StringBuffer buffer=new StringBuffer(); try{ String line=br.readLine(); while(line!=null){ System.out.println(line); buffer.append(line+“:“); line=br.readLine();} br.close(); fr.close();} catch(IOException ce) {ce.printStackTrace();} return buffer; } public boolean writeFile(String str) { boolean success=true; try{ BufferedWriter bw=new BufferedWriter(fw); bw.write(str); bw.newLine(); bw.flush(); fw.close();} catch(IOException ce) {ce.printStackTrace(); success=false; return success; } return success; } public static void deleteFile(String fileName){ File file1=new File(fileName); if(file1.isFile()&&file1.exists()) {file1.delete(); System.out.println(“已经为您删除“+fileName+“处的文件“); } else System.out.println(fileName+“处无文件,将自动为您添加“);}} public class function { public static void search(StringBuffer buffer1,String character,int infor1,TableModel infor2) { //找到员工信息并显示int result1=buffer1.indexOf(character,0); DefaultTableModel tableModel2=(DefaultTableModel)infor2; for(int k=0;k<infor1;k++) tableModel2.removeRow(0); String child[]=new String[6]; int firstSpace,secondSpace,thirdSpace,forthSpace,fifthSpace; firstSpace=buffer1.indexOf(“ “,result1); secondSpace=buffer1.indexOf(“ “,firstSpace+1); thirdSpace=buffer1.indexOf(“ “,secondSpace+1); forthSpace=buffer1.indexOf(“ “,thirdSpace+1); fifthSpace=buffer1.indexOf(“:“,forthSpace+1); child[0]=buffer1.substring(result1-3,result1-1); child[1]=buffer1.substring(result1,firstSpace); child[2]=buffer1.substring(firstSpace+1,secondSpace); child[3]=buffer1.substring(secondSpace+1,thirdSpace); child[4]=buffer1.substring(thirdSpace+1,forthSpace); child[5]=buffer1.substring(forthSpace+1,fifthSpace); tableModel2.addRow(child); } public static void delete(String Id,StringBuffer buffer1){//根据获得的位置进行删除 int result1=buffer1.indexOf(Id+“ “,0); if(result1>=1)//不是第一个位置 result1=buffer1.indexOf(“:“+Id+“ “,0);//加强判断条件 String letter2,letter3; int duan=buffer1.indexOf(“ “,result1);//将位置后移避免永远都搜到自身 int newduan=buffer1.indexOf(“:“,duan);//获得“:”的位置 if(0!=result1)//不是第一个位置的数 { letter2=buffer1.substring(0,result1); letter3=buffer1.substring(newduan,buffer1.length()-1); } else { letter2=buffer1.substring(0,result1); letter3=buffer1.substring(newduan+1,buffer1.length()-1); } fileOperation temp5=new fileOperation(“d:\\2.txt“);//实例化一个对象 fileOperation.deleteFile(“d:\\2.txt“);//删除原数据 temp5.writeFile(letter2+letter3); } public static void changemoney(String Id,String money){ fileOperation temp=new fileOperation(“d:\\2.txt“);//在文件里修改,思路:薪水前+新的薪水+薪水后 StringBuffer buffer1=temp.readFile();//将文件读出来 int result1=buffer1.indexOf(Id,0);//判断文件字符串中是否有显示框中的内容并获得位置下标 String letter4,letter5; int firstSpace,secondSpace,thirdSpace,forthSpace; firstSpace=buffer1.indexOf(“ “,result1);//获得空格的位置 secondSpace=buffer1.indexOf(“ “,firstSpace+1); thirdSpace=buffer1.indexOf(“ “,secondSpace+1); forthSpace=buffer1.indexOf(“ “,thirdSpace+1); letter4=buffer1.substring(0,thirdSpace); letter5=buffer1.substring(forthSpace+1,buffer1.length()-1); fileOperation.deleteFile(“d:\\2.txt“);//删除原数据 fileOperation temp5=new fileOperation(“d:\\2.txt“);//实例化一个对象 temp5.writeFile(letter4+“ “+money+“ “+letter5); }} public class getIt {public static void deposit(){ String firstFormation=“01 王五 山东 4500 13798987654 管理部“; String secondFormation=“02 安心 贵州 1200 13799999654 生产车间“; String thirdFormation=“03 杨燕 四川 6300 15709200187 研发部“; fileOperation temp=new fileOperation(“d:\\2.txt“);//实例化一个对象 temp.writeFile(firstFormation+“\n“+secondFormation+“\n“+thirdFormation);}} public class look { public static void xianshi(TableModel infor1,int infor2){ fileOperation temp=new fileOperation(“d:\\2.txt“); StringBuffer buffer=temp.readFile();//将文件读出来 int count=0,start=0,end1,k=0;//count计算行数 while(true) {end1=buffer.indexOf(“:“,start);//获得第一个冒号的位置 count++; if(end1==buffer.length()-1)//到达末尾 break; start=end1+1; } String str[]=new String[count];//创建用于存放每一段字符串的数组 start=0; DefaultTableModel tableModel1=(DefaultTableModel)infor1; while(true) {end1=buffer.indexOf(“:“,start); str[k]=buffer.substring(start,end1);//将每一行字符串给数组 if(end1==buffer.length()-1) break; start=end1+1; k++;} int count1=infor2;//获得表格行数 for(k=0;k<count1;k++) tableModel1.removeRow(0);//删除原表格数据 for(k=0;k<count;k++) {String child[]=new String[6]; int firstSpace,secondSpace,thirdSpace,forthSpace,fifthSpace; firstSpace=str[k].indexOf(“ “,0);//获得空格的位置 secondSpace=str[k].indexOf(“ “,firstSpace+1); thirdSpace=str[k].indexOf(“ “,secondSpace+1); forthSpace=str[k].indexOf(“ “,thirdSpace+1); fifthSpace=str[k].indexOf(“ “,forthSpace+1); child[0]=str[k].substring(0,firstSpace); child[1]=str[k].substring(firstSpace+1,secondSpace); child[2]=str[k].substring(secondSpace+1,thirdSpace);//将空格与空格间的数据存入数组 child[3]=str[k].substring(thirdSpace+1,forthSpace); child[4]=str[k].substring(forthSpace+1,fifthSpace); child[5]=str[k].substring(fifthSpace+1,str[k].length()); tableModel1.addRow(child);//存入表格} }} 三、类及其方法 abnormal fileOperation function getIt look panduanId(String Id) panduanname(String name) panduandepartment(String department) readFile() writeFile(String str) deleteFile(String fileName) search(StringBuffer buffer1,String character,int infor1,TableModel infor2) delete(String Id,StringBuffer buffer1) changemoney(String Id,String money) deposit() xianshi(TableModel infor1,int infor2) 四、调试过程

推荐访问:员工信息管理系统java链接数据库

猜你喜欢