java基础 集合 ArrayList 增删改除
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class demo1 { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub //创建雇员管理对象 empmanage em = new empmanage(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //做个简易的菜单 while(true) { System.out.println("请输入你要进行的操作:"); System.out.println("1:表示要添加一个雇员"); System.out.println("2:查找一个雇员"); System.out.println("3:修改一个雇员工资"); System.out.println("4:删除一个雇员"); System.out.println("5:退出"); String opertype = br.readLine(); if(opertype.equals("1")) { System.out.println("请输入编号"); String empno = br.readLine(); System.out.println("请输入名字"); String name = br.readLine(); System.out.println("请输入薪资"); float sal = Float.parseFloat(br.readLine()); emp emp = new emp(empno,name,sal); em.addemp(emp); } else if(opertype.equals("2")) { System.out.println("请输入编号"); String empno = br.readLine(); em.showinfo(empno); } else if(opertype.equals("3")) { System.out.println("请输入编号 "); String empno = br.readLine(); System.out.println("请输入新的工资"); float newsal = Float.parseFloat(br.readLine()); em.updatesal(empno, newsal); } else if(opertype.equals("4")) { System.out.println("请输入雇员号"); String empno = br.readLine(); em.delemp(empno); }else if(opertype.equals("5")) { System.exit(0); } } } } //雇员管理类 class empmanage{ private ArrayList a1 = null; //构造 public empmanage() { a1 = new ArrayList(); } //加入员工 public void addemp(emp emp){ a1.add(emp); } //显示员工的相关信息 public void showinfo(String empno) { //遍历整个ArrayList for(int i=1;i<a1.size();i++) { //去除emp对象 emp emp = (emp)a1.get(i); //比较编号 if(emp.getEmpNo().equals(empno)) { System.out.println("找到雇员信息:"); System.out.println("编号= "+emp.getEmpNo()); System.out.println("名字= "+emp.getName()); System.out.println("工资= "+emp.getSal()); } } } //修改工资 public void updatesal(String empno,float newsal) { for(int i=0;i<a1.size();i++) { emp emp = (emp)a1.get(i); if(emp.getEmpNo().equals(empno)) { //修改薪水 emp.setSal(newsal); } } } //删除雇员 public void delemp(String empno) { for(int i=0;i<a1.size();i++) { emp emp = (emp)a1.get(i); if(emp.getEmpNo().equals(empno)) { a1.remove(i); } } } } /** * @author wyl * 雇员类 */ class emp { //学号 private String empNo; private String name; private float sal; //构造函数 public emp(String empNo, String name, float sal) { super(); this.empNo = empNo; this.name = name; this.sal = sal; } public String getEmpNo() { return empNo; } public void setEmpNo(String empNo) { this.empNo = empNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getSal() { return sal; } public void setSal(float sal) { this.sal = sal; } }