xuannth / MoverAlert-CMS

0 stars 0 forks source link

Compare 2 CSV file #5

Open xuannth opened 3 years ago

xuannth commented 3 years ago
package utilities;

import java.util.ArrayList;
import java.util.List;

public class CSVCompare {

    static ReadCSVData CSVFile;

    public static void main(String[] args) {

        List<CapReport> CSVFile1 = ReadCSVData.getAllCapReport("cap-report-2020-12-01-to-2020-12-31.csv");
        List<CapReport> CSVFile2 = ReadCSVData.getAllCapReport("cap-report-2020-10-06-to-2020-10-13.csv");
        List<CompareMissedLead> compareResult = new ArrayList<>();

        for(int row1 = 1; row1 < CSVFile1.size(); row1 ++) {
            for(int row2 = 1; row2 < CSVFile2.size(); row2 ++) {
                if(CSVFile1.get(row1).getSubscription() == CSVFile2.get(row2).getSubscription()) {
                    compareResult.add(compareResult.get(row1));
                    System.out.println(compareResult.get(row1).getSubID());
                }
            }
        }
    }

}
xuannth commented 3 years ago
package utilities;

public class CompareMissedLead {
    private String subID;
    private String missLead1;
    private String missLead2;
    private String result;
    public CompareMissedLead(String subID, String missLead1, String missLead2, String result) {
        this.subID = subID;
        this.missLead1 = missLead1;
        this.missLead2 = missLead2;
        this.result = result;
    }
    public String getSubID() {
        return subID;
    }
    public void setSubID(String subID) {
        this.subID = subID;
    }
    public String getMissLead1() {
        return missLead1;
    }
    public void setMissLead1(String missLead1) {
        this.missLead1 = missLead1;
    }
    public String getMissLead2() {
        return missLead2;
    }
    public void setMissLead2(String missLead2) {
        this.missLead2 = missLead2;
    }
    public String getResult() {
        return result;
    }
    public void setResult(String result) {
        this.result = result;
    }
}
Jacobvu84 commented 3 years ago
package vu.jacob.features.csvreader;

public class CompareMissedLead {

    private String subID;
    private String expected;
    private String actual;
    private boolean result;

    public CompareMissedLead(String subID, String expected, String actual) {
        this.subID = subID;
        this.expected = expected;
        this.actual = actual;

        if(expected.equals(actual))
            this.result=true;
        else
            this.result=false;

    }
    public String getSubID() {
        return subID;
    }

    public String getExpected() {
        return expected;
    }

    public String getActual() {
        return actual;
    }

    public boolean getResult() {
        return result;
    }

}
Jacobvu84 commented 3 years ago

import java.util.ArrayList;
import java.util.List;

public class CSVCompare {

    static ReadCSVData CSVFile;

    public static void main(String[] args) {

        List<CapReport> expectedCaps = ReadCSVData.getAllCapReport("cap-report-2020-12-01-to-2020-12-31.csv");
        List<CapReport> actualCaps = ReadCSVData.getAllCapReport("cap-report-2020-10-06-to-2020-10-13.csv");
        List<CompareMissedLead> compareResult = new ArrayList<>();

        for(int row1 = 1; row1 < expectedCaps.size(); row1 ++) {

            // lấy subId ở dòng thứ row1 trong file 1; bắt đầu là dòng 1
            String expectedId = expectedCaps.get(row1).getSubscription();

            for(int row2 = 1; row2 < actualCaps.size(); row2 ++) {

                // lấy subId ở dòng thứ row2 trong file 2; bắt đầu là dòng 1
                String actualId = actualCaps.get(row2).getSubscription();

                // so sánh subID ở file 1 xem có trong file 2 không ?
                if(expectedId.equals(actualId)) {
                    // Nếu tìm được SubID ở file 1 trùng với SubID ở file 2 thì lấy giá trị 

                    // MissedLead là ở file 1
                    String expectedMissedLead = expectedCaps.get(row1).getMissedLead();

                    // MissedLead là ở file 2
                    String actualMissedLead = actualCaps.get(row2).getMissedLead();

                    // Tạo một đối tượng trung gian của lớp CompareMissedLead. 
                    CompareMissedLead tmp = new CompareMissedLead(expectedId, expectedMissedLead, actualMissedLead);

                    // Add kết quả từ trung gian vào danh sách compareResult đã khai báo bên trên.

                    compareResult.add(tmp);

                    // Kết thúc vòng lặp. Tất cả kết quả true/ false đã được lưu vào list. 
                }
            }
        }

        // lấy số lượng cái subID tồn tại trong cả 2 file

        int compareSize = compareResult.size();

        // lấy kết quả ở list ra
        for (int i = 0; i < compareSize; i++) {

            if(compareResult.get(i).getResult()==true) { 
                System.out.println("Kết quả so sánh tại dòng có id : " + compareResult.get(i).getSubID() + " là " + compareResult.get(i).getResult());
            } else { 
                System.out.println("Có sự khác biệt tại dòng có id : " + compareResult.get(i).getSubID());
                System.out.println(" Mong đợi là:  " + compareResult.get(i).getExpected() +  " nhưng thực tế là: " + compareResult.get(i).getActual());
            } 

        }
    }

}