Java Stream for "tính cán bộ có lương cao nhất và tổng lương"

Joe

Thành viên VIP
21/1/13
3,022
1,335
113
There are some bugs in your app.
1) Use Stream to calculate the Max/Min/Sum
2) Be careful with String Object and String Primitive. So far I could remember I have told you and explained to you the differences.
Java:
public class QuanLyNhanSu {
    ...
    public static void tieuDe() {
        //                                                                    BUGGs---> %,14.2f<----
        System.out.printf("%3s %-5s %-23s %-8s %-9s %-23s %-23s %-12s %-12.2s %-8s %8s %,14.2f%n", "STT", "Ma ns",
                "Ho ten", "Nam sinh", "Gioi tinh", "Phong/Xuong", "Truong phong/quan doc", "Chuc vu/Cv",
                "He so luong/So ngay cong", "Xep loai", "So cong nhan", "Luong");
    }
    ...
    public static void hienThiCongNhanNam() {
        tieuDe();
        int stt = 1;
        for (NhanSu h : list) {
          // You confuse Object String with String Primitive
            //if (h.getGioiTinh() == "Nu" && h instanceof CongNhan) {
            // CORRECT is with the equals() method
            if ("Nu".equals(h.getGioiTinh()) && h instanceof CongNhan) {
                System.out.printf("%-3d", stt++);
                h.hienThi();
            }

        }

    }

    public static void hienThiCanBoNu() {
        tieuDe();
        int stt = 1;
        for (NhanSu h : list) {
          // You confuse Object String with String Primitive
            //if (h.getGioiTinh() == "Nam" && h instanceof CanBo) {
            // CORRECT is with the equals() method
            if ("Nam".equals(h.getGioiTinh()) && h instanceof CanBo) {
                System.out.printf("%-3d", stt++);
                h.hienThi();
            }

        }

    }
    public static void canBoLuongMax() {
        tieuDe();
        //int stt = 1;
        //int maxIndex = 0;
        java.util.List<Double> luong = new java.util.ArrayList<>();
        for (Object obj : list)if (obj instanceof CanBo) luong.add(((CanBo)obj).luong());
        Double max = luong.stream().mapToDouble(v -> v).max().getAsDouble();
        System.out.println("Max Luong:"+max);
/*
        for (int i = 0; i < list.length & i < 1; i++) {
            // if (luongMax < tong(i)) {
            // maxIndex = i;
            // luongMax = tong(i);
            // }

            System.out.println("");
        }
*/
    }

    public static void canBoLuongMax() {
        tieuDe();
        java.util.List<Double> luong = new java.util.ArrayList<>();
        for (Object obj : list)if (obj instanceof CanBo) luong.add(((CanBo)obj).luong());
        Double max = luong.stream().mapToDouble(v -> v).max().getAsDouble();
        System.out.println("Max Luong:"+max);
/*
        int stt = 1;
        int maxIndex = 0;
        for (int i = 0; i < list.length & i < 1; i++) {
            // if (luongMax < tong(i)) {
            // maxIndex = i;
            // luongMax = tong(i);
            // }

            System.out.println("");
        }
*/
    }

    public static void congNhanLuongMin() {
        tieuDe();
        java.util.List<Double> luong = new java.util.ArrayList<>();
        for (Object obj : list)if (obj instanceof CanBo) luong.add(((CanBo)obj).luong());
        Double min = luong.stream().mapToDouble(v -> v).min().getAsDouble();
        System.out.println("Min Luong:"+min);
        /*
        int stt = 1;
        int minIndex = 0;

        for (int i = 0; i < list.length & i < 1; i++) {

            System.out.println("");
        }
        */
    }
    ...
    public static void sumSalaryCanBo() {
        java.util.List<Double> luong = new java.util.ArrayList<>();
        for (Object obj : list)if (obj instanceof CanBo) luong.add(((CanBo)obj).luong());
        Double max = luong.stream().mapToDouble(f -> f.doubleValue()).sum();
        System.out.println("Max Luong:"+max);
/*
        int tong;
        for (int i = 0; i < list.length; i++) {

        }
*/
    }

    public static void sumSalaryCongNhan() {
        java.util.List<Double> luong = new java.util.ArrayList<>();
        for (Object obj : list)if (obj instanceof CongNhan) luong.add(((CongNhan)obj).luong());
        Double max = luong.stream().mapToDouble(v -> v).sum();
        System.out.println("Max Luong:"+max);
/*
        int sum;
        for (int i = 0; i < list.length; i++) {

        }
*/
    }
    ...
By the way, try to learn how to post the codes. People don't have time to unRAR and to check for the codes. How to post the codes?
posting.png
 
Sửa lần cuối: