Salary Management System (mainly from the document read, and write to the file)

Salary Management System (mainly from the document read, and write to the file)

They are divided into three categories Employee, Emp_Manage, MainTest class

  1. Employee major implementation creates a user

    /**
     * 
     * @author LvGJ
     * 只用基础的getter和setter
     *
     */
    public class Employee {
    	String name;
    	int age;
    	double salary;
    	public Employee(){
    		
    	}
    	public Employee(String name,int age,double salary){
    		this.name = name;
    		this.age = age;
    		this.salary = salary;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public int getAge() {
    		return age;
    	}
    	public void setAge(int age) {
    		this.age = age;
    	}
    	public double getSalary() {
    		return salary;
    	}
    	public void setSalary(double salary) {
    		this.salary = salary;
    	}
    	@Override
    	public String toString(){
    		return "姓名:"+this.name+"\n年龄:"+this.age+"\n工资:"+this.salary;
    	}
    	public String toFile(){
    		return this.name+","+this.age+","+this.salary;
    	}
    }
    
  2. Emp_Manage class main achievement of the management of staff

    /**
     * 
     * @author LvGJ
     * 其中的init()是用来将文件中的数据读取到List中
     * 
     */
    public class Emp_Manage {
    	private List<Employee> ls;
    	private File ef;
    	public Emp_Manage(){
    		ef = new File("Employee.txt");
    		ls = new ArrayList<Employee>();
    		init();
    	}
    	private void init(){
    		try{
    			BufferedReader br = new BufferedReader(new FileReader(ef));
    			String tmp = "";
    			while((tmp = br.readLine())!=null){
    				String[] info = tmp.split(",");
    				ls.add(new Employee(info[0],Integer.parseInt(info[1]),Double.parseDouble(info[2])) {
    				});
    			}
    			br.close();
    		}catch(Exception e){
    			e.printStackTrace();
    		}
    	}
    	public void add(Employee e){
    		ls.add(e);
    		System.out.println("添加成功!");
    		String tmp = e.toFile();
    		try{
    			BufferedWriter bw = new BufferedWriter(new FileWriter(ef,true));
    			bw.write(tmp+"\n");
    			System.out.println(tmp);
    			bw.flush();
    			bw.close();
    		}catch(Exception ex){
    			ex.printStackTrace();
    		}
    		
    	}
    	public void deleByEmp(Employee e){
    		for(int i = 0;i<ls.size();i++){
    			if(ls.get(i).getName().equals(e.getName())){
    				ls.remove(i);
    				System.out.println("删除成功!");
    				break;
    			}
    			if(i==ls.size()-1)
    				System.out.println("无该用户!");
    		}
    	}
    	public void deleByName(String name){
    		for(int i = 0;i<ls.size();i++){
    			if(ls.get(i).getName().equals(name)){
    				ls.remove(i);
    				System.out.println("删除成功!");
    				break;
    			}
    			if(i==ls.size()-1)
    				System.out.println("无该用户!");
    		}
    	}
    	public void findEmp(String name){
    		for(Employee e:ls){
    //			System.out.println(e.getName());
    			if(e.getName().equals(name)){
    				System.out.println(e.toString());
    				return ;
    			}
    		}
    		System.out.println("查无此人!");
    	}
    	public void showAll(){
    		for(Employee e : ls){
    			System.out.println(e.toString());
    			System.out.println();
    		}
    	}
    }
    
  3. MainTest class for testing

    public class MainTest {
    	public static void main(String[] args) {
    		int op = -1;
    		Emp_Manage ema = new Emp_Manage();
    		Scanner s = new Scanner(System.in);
    		while(op!=0){
                System.out.println("薪酬管理系统");
                System.out.println("---------------------------------------");
                System.out.println("1.添加新员工");
                System.out.println("2.显示所有员工信息");
                System.out.println("3.根据工号删除该员工的信息");
                System.out.println("0.结束!");
                System.out.println("---------------------------------------");
                System.out.println("请选择序号:");
                op = s.nextInt();
                
                if(op==1){
                	System.out.println("请输入姓名:");
                	String name = s.next();
                	
                	System.out.println("请输入年龄:");
                	int age = s.nextInt();
                	
                	System.out.println("请输入工资:");
                	double salary = s.nextDouble();
                	
                	Employee tmp = new Worker(name,age,salary);
                	ema.add(tmp);
                }else if(op == 2){
                	ema.showAll();
                }else if(op == 3){
                	System.out.println("请输入要删除用户的姓名:");
                	String name = s.next();
                	ema.deleByName(name);
                }
    		}
    		s.close();
    		
    	}
    }
    

Guess you like

Origin www.cnblogs.com/lvgj/p/12582849.html