EmployeeFunction&Test

Màu nền
Font chữ
Font size
Chiều cao dòng

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

package Test;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.Scanner;

import java.util.StringTokenizer;

/**

 *

 * @author LongKaKa

 */

public class EmployeeFunction {

     List<Employee> listEmp;

    public EmployeeFunction() {

        listEmp = new ArrayList<Employee>();

    }

    public void inputEmployeeInfo() {

        Employee objEmp = new Employee();

        Scanner input = new Scanner(System.in);

        System.out.println("Nhap ten : ");

        objEmp.setTen(input.nextLine());

        System.out.println("Nhap tuoi : ");

        objEmp.setTuoi(input.nextInt());

        System.out.println("Nhap dia chi : ");

        input.nextLine();

        objEmp.setDiachi(input.nextLine());

        System.out.println("Nhap luong : ");

        objEmp.setLuong(input.nextDouble());

        listEmp.add(objEmp);

    }

    public void writeDataToFile() {

        FileWriter fwriter = null;

        BufferedWriter bwriter = null;

        try {

            for (int i=0; i<listEmp.size(); i++) {

                fwriter = new FileWriter("Empoyee.txt",true);

                bwriter = new BufferedWriter(fwriter);

                bwriter.write(listEmp.get(i).getTen());

                bwriter.write("\t");

                bwriter.write(String.valueOf(listEmp.get(i).getTuoi()));

                bwriter.write("\t");

                bwriter.write(listEmp.get(i).getDiachi());

                bwriter.write("\t");

                bwriter.write(String.valueOf(listEmp.get(i).getLuong()));

                bwriter.write("\t");

                bwriter.newLine();

            }

            bwriter.close();

            fwriter.close();

        } catch (FileNotFoundException e) {

            System.out.println("ERROR : File not found !");

        } catch (IOException e){

            System.out.println("ERROR : Write data to file !");

        } finally {

            try {

                fwriter.close();

                bwriter.close();

            } catch (IOException e) {

                System.out.println("ERROR : Cannot close the file !");

            }

        }

    }

    public void readDataFromFile() {

        try {

            FileReader freader = new FileReader("Empoyee.txt");

            BufferedReader breader = new BufferedReader(freader);

            String record;

            while ((record=breader.readLine()) != null) {

                StringTokenizer token = new StringTokenizer(record);

                while (token.hasMoreTokens()) {

                    System.out.print(token.nextToken("\t") + "\t\t");

                }

                System.out.println("");

            }

            breader.close();

            freader.close();

        } catch (FileNotFoundException e) {

            System.out.println("ERROR : File not found !");

        } catch (IOException e) {

            System.out.println("ERROR : Read from file !");

        } finally {

        }

    }

    public void sortByLuong() {

        FileReader freader = null;

        BufferedReader breader = null;

        List<Employee> newList = new ArrayList<Employee>();

        try {

            freader = new FileReader("Empoyee.dat");

            breader = new BufferedReader(freader);

            String record;

            while ((record=breader.readLine()) != null) {

                StringTokenizer token = new StringTokenizer(record);

                Employee objEmp = new Employee();

                while (token.hasMoreTokens()) {

                    objEmp.setTen(token.nextToken("\t"));

                    objEmp.setTuoi(Integer.parseInt(token.nextToken("\t")));

                    objEmp.setDiachi(token.nextToken("\t"));

                    objEmp.setLuong(Double.parseDouble(token.nextToken("\t")));

                }

                newList.add(objEmp);

            }

        } catch (FileNotFoundException e) {

            System.out.println("ERROR : File not found !");

        } catch (IOException e) {

            System.out.println("ERROR : Read from file !");

        } finally {

            try {

                breader.close();

                freader.close();

            } catch (IOException e) {

                System.out.println("ERROR : Cannot close the file !");

            }

        }

        //Sap xep

//        for (int i=0; i<newList.size(); i++) {

//            for (int j=i+1; j<newList.size()-1; j++) {

//                if (newList.get(i).getLuong() < newList.get(j).getLuong()) {

//                    Employee objTemp = new Employee();

//                    objTemp = newList.get(i);

//                    newList.set(i, newList.get(j));

//                    newList.set(j, objTemp);

//                }

//            }

//        }

//

//        //Hien thi sau khi sap xep

//        System.out.println("Ten \t\t Tuoi\t\t Dia chi\t\t Luong");

//        for (int i=0; i<newList.size(); i++) {

//            System.out.print(newList.get(i).getTen() + "\t\t");

//            System.out.print(newList.get(i).getTuoi() + "\t\t");

//            System.out.print(newList.get(i).getDiachi() + "\t\t");

//            System.out.print(newList.get(i).getLuong() + "

");

//        }

        Collections.sort(newList,new EmployeeComparator());

        for(Employee emp : newList){

        System.out.println(emp);

        }

    }

}

 

//Test

/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */

package Test;

import java.util.Scanner;

/**

 *

 * @author LongKaKa

 */

public class Main {

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        // TODO code application logic here

         EmployeeFunction a = new EmployeeFunction();

        for(;;)

        {

            System.out.println("1.Nhap thông tin về nhân viên.

2.Ghi vao file.

3.Doc file.

4.Sap xep.

5.Exit.");

            System.out.println("Nhap lua chon :");

            Scanner abc = new Scanner(System.in);

            int choice = abc.nextInt();

            switch (choice)

            {

                case 1:

                    a.inputEmployeeInfo();

                    break;

                case 2:

                    a.writeDataToFile();

                    break;

                case 3:

                    a.readDataFromFile();

                    break;

                case 4:

                    a.sortByLuong();

                    break;

            }

            if(choice == 5)break;

        }

    }

  }

Bạn đang đọc truyện trên: Truyen2U.Pro