使用 Java 对汽车列表进行排序并按制造商排序显示它们?

Sort the list of cars and display them sort by make using Java?

我有一个问题是对 txt 文件的汽车报告中的数据进行排序。

问题是:我如何列出按品牌分类的汽车(福特、雪佛兰等)。他们只需要对 MAKE 进行排序,这样他们就可以是所有 FORD 汽车,然后是 Chevy、DODGE .. 依此类推:

这是我目前所拥有的:

这是我的源代码:


public class CustomerSale {

    // Details of reports
    private String zipCodeExtension;

    private int customerNumber;

    private String customerName;

    private int purchaseDate;

    private String make;

    private int purchasePrice;

    private int yearOfVehicle;

    private int satisfactionRating;



    // Create constructor argument

    public CustomerSale(String zipCodeExtension, int customerNumber, String customerName,
                        int purchaseDate, String make, int purchasePrice,
                        int yearOfVehicle, int satisfactionRating) {
        this.zipCodeExtension = zipCodeExtension;
        this.customerNumber = customerNumber;
        this.customerName = customerName;
        this.purchaseDate = purchaseDate;
        this.make = make;
        this.purchasePrice = purchasePrice;
        this.yearOfVehicle = yearOfVehicle;
        this.satisfactionRating = satisfactionRating;
    }


    // Create getters and setters

    public String getMake() {

        return make;

    }

    public void setMake(String make) {

        this.make = make;

    }


    public String getZipCodeExtension() {

        return zipCodeExtension;

    }

    public void setZipCodeExtension(String zipCodeExtension) {

        this.zipCodeExtension = zipCodeExtension;

    }

    public int getCustomerNumber() {

        return customerNumber;

    }

    public void setCustomerNumber(int customerNumber) {

        this.customerNumber = customerNumber;

    }

    public String getCustomerName() {

        return customerName;

    }

    public void setCustomerName(String customerName) {

        this.customerName = customerName;

    }

    public int getPurchaseDate() {

        return purchaseDate;

    }

    public void setPurchaseDate(int purchaseDate) {

        this.purchaseDate = purchaseDate;

    }

    public double getPurchasePrice() {

        return purchasePrice;

    }

    public void setPurchasePrice(int purchasePrice) {

        this.purchasePrice = purchasePrice;

    }

    public int getYearOfVehicle() {

        return yearOfVehicle;

    }

    public void setYearOfVehicle(int yearOfVehicle) {

        this.yearOfVehicle = yearOfVehicle;

    }


    public int getSatisfactionRating() {

        return satisfactionRating;

    }

    public void setSatisfactionRating(int satisfactionRating) {

        this.satisfactionRating = satisfactionRating;

    }
}



import java.io.*;
import java.text.DecimalFormat;
import java.util.*;

public class SortByMake {
    public static void main(String[] args) throws FileNotFoundException {

        File carsInfo = new File("src/custsale.txt");
        Scanner carsScanner = new Scanner(carsInfo);
        String format = "%-20s%-14s%-21s%-16s%-19s$%-17s%-14s%-20s\n";

        while (carsScanner.hasNextLine()) {
            // Read the carsScanner
            String zipCodeExtension = carsScanner.nextLine();
            int customerNumber = Integer.parseInt(carsScanner.nextLine());
            String customerName = carsScanner.nextLine();
            int purchaseDate = Integer.parseInt(carsScanner.nextLine());
            String make = carsScanner.nextLine();
            int purchasePrice = Integer.parseInt(carsScanner.nextLine());
            int yearOfVehicle = Integer.parseInt(carsScanner.nextLine());
            int satisfactionRating = Integer.parseInt(carsScanner.nextLine());
            CustomerSale custsale = new CustomerSale(zipCodeExtension, customerNumber, customerName,
                    purchaseDate, make, purchasePrice, yearOfVehicle, satisfactionRating);

                // Display the report
                DecimalFormat df = new DecimalFormat("#,#####");
                System.out.printf(format, custsale.getZipCodeExtension(), custsale.getCustomerNumber(),
                        custsale.getCustomerName(), custsale.getPurchaseDate(), custsale.getMake(),
                        df.format(custsale.getPurchasePrice()), custsale.getYearOfVehicle(),
                        custsale.getSatisfactionRating());
        }

        ArrayList<CustomerSale> carMake = new ArrayList<>();

        Collections.sort(carMake, Comparator.comparing(CustomerSale::getMake));


        final int pageSize = 30;
        int carCounter = 0;
        for(CustomerSale car : carMake){

            if (carCounter % pageSize == 0) {
                printPageHeader(carCounter / pageSize + 1);
            }

            // Increment the car counter
            carCounter++;

        }

        carsScanner.close();

    }

    private static void printPageHeader(int i) {
        System.out.println(
                "-------------------------------------------------ABC USED CARS----------------------------------------------------------------");
        System.out.println(
                "-------------------------------------------------------" + "PAGE " + i + "-----------------------------------------------------------------");
        System.out.printf("%-19s%-15s%-21s%-15s%-20s%-17s%-12s%-20s\n", "|ZIP CODE-EXT|", "|CUST No.|", "|CUSTOMER NAME|", "|DoP|",
                "|MAKE|", "|PRICE|", "|YEAR|", "|RATE|");
        System.out.println(
                "------------------------------------------------------------------------------------------------------------------------------");
    }

}





这是文本文件报告:

46410-1234
1001
ALBERT, CARL T.
08252001
FORD
0255000
1991
2
46307-1201
1003
ANDREWS, ROBERT
08262001
CHEVROLET
0700000
1958
0
46423-2311
1008
ANZIO, RAFELINO
09012001
CHEVROLET
0456050
1978
0
46424-0121
1010
ASHLEY, WILLIAM B.
09022001
PIERCE-ARROW
1240000
1932
2
46375-3110
1015
ATKINSON, MARK
08272001
STUDEBAKER
0050000
1958
1
46405-1291
1025
AVERY, ALFRED A.
08292001
HUDSON
0230000
1954
1
46301-1234
1031
BEZZMEK, JENNIFER
08272001
FORD
0455000
1995
2
46303-1201
1033
BLAKE, DONALD
08292001
FORD
0722050
1989
1
46413-2311
1041
BLONDELL, BONNIE
09042001
CHEVROLET
0356050
1988
1
46404-0121
1045
BONADIO, JAMES
09032001
PIERCE-ARROW
1150000
1935
2
46307-3110
1055
BUCKO, ONIEDA
08282001
STUDEBAKER
0245000
1964
1
46410-1291
1056
BYMANN, FREDRICK
08292001
FORD
0330000
1994
1
46342-1234
1061
CALBERT, RONALD
09052001
FORD
0355000
1993
2
46307-1211
1063
CHELSEA, MARTHA S.
08302001
DODGE BROS.
0350000
1935
0
46410-2311
1067
CLAFLIN, WAYNE R.
09012001
CHEVROLET
0456050
1990
2
46410-0121
1070
COLE, CHARLES C.
09022001
PIERCE-ARROW
1050000
1929
2
46305-3111
1075
COLEMAN, THOMAS
08272001
STUDEBAKER
0167050
1961
1
46410-1221
1076
COLWELL, RICHARD L.
08292001
HUDSON
0430000
1940
1
46414-1231
1080
COOPER, JOHNATHAN
09052001
FORD
1256000
1996
2
46307-1201
2002
COREY, SARAH D.
08272001
CHEVROLET
0650050
1994
0
46421-2311
2004
CRACKLIN, GOODMAN
09012001
CHEVROLET
1456050
1996
0
46323-0121
2011
CRAWFORD, TIMOTHY
09012001
PIERCE-ARROW
1040000
1931
2
46315-3110
2012
CURRIE, RAYMOND
08282001
STUDEBAKER
0150025
1956
1
46425-1291
2024
CYBORG, IZORE M.
09052001
HUDSON
0130000
1949
1
46410-1234
2031
DALTON, DAVID P.
08252001
FORD
0234000
1990
2
46307-1201
2043
DAVIES, RALPH O.
08262001
CHEVROLET
0333000
1989
0
46423-2311
2048
DENNICK, DONNA
09012001
CHEVROLET
0656025
1995
0
46424-0121
2050
DERBIN, DEANNA
09022001
PIERCE-ARROW
1640025
1937
2
46375-3110
2065
DONNEHUE, PHILLIP
09042001
STUDEBAKER
0054000
1966
1
46405-1291
2085
DOPPLER, RADAR O.
08302001
HUDSON
0311000
1951
1
46310-1234
2091
DUNLOP, RITA
08252001
FORD
1450045
1996
2
46303-1201
3003
DYKES, CYNTHIA
08262001
FORD
0410050
1987
1
46413-2311
3011
EATON, ESTER B.
09012001
CHEVROLET
1356050
1996
0
46404-0121
3015
EFFLEY, BAILY
09022001
PIERCE-ARROW
1446045
1933
2
46307-3110
3025
EGGERTON, AMANDA
08292001
STUDEBAKER
0335000
1965
1
46410-1291
3026
EPPLEY, DAVID
08282001
FORD
0414000
1995
1
46342-1234
3031
ERKLE, ROSA
09032001
FORD
0355020
1993
2
46307-1211
3043
FARNSWORTH, WESLEY
09052001
DODGE BROS.
1150000
1996
0
46410-2311
3047
FLANNERY, JAMES
09012001
CHEVROLET
0450050
1992
0
46410-0121
3050
FOREMAN, OTTO J.
09022001
PIERCE-ARROW
0940000
1927
2
46305-3111
3055
FOWLER, KATHLEEN
08272001
STUDEBAKER
0267050
1964
1
46410-1221
3056
FURNACE, DAVID
08292001
HUDSON
0530000
1948
1
46414-1231
3061
GALLAGHER, CLARENCE
0904200
1FORD
1006000
1992
2
46307-1201
3062
GENNERRO, TONY S.
08302001
CHEVROLET
0320050
1989
0
46421-2311
3066
GOEBEL, NANCY K.
09022001
CHEVROLET
0643050
1990
0
46323-0121
3072
GUNTHER, FREDERICK
09032001
PIERCE-ARROW
1260000
1930
2
46315-3110
3080
HAINES, MARSHALL
08292001
STUDEBAKER
02500251
959
1
46425-1291
3094
HANCOCK, JONATHON
09032001
HUDSON
0330000
1953
1
46423-2311
3098
HARTNETT, ROBERTO
09052001
CHEVROLET
0326040
1988
0
46424-0121
4005
HENNING, SONIA
09052001
PIERCE-ARROW
1305000
1928
2
46375-3110
4009
HORNSBY, ROGERS
08252001
STUDEBAKER
0167500
1962
1
46405-1291
4012
HYATT, JANET F.
08302001
HUDSON
0155000
1951
1
46301-1234
4021
IDZIOR, RAYMOND
09012001
FORD
1460000
1996
2
46303-1201
4022
JENNINGS, WILLIAM
08262001
FORD
0612040
1992
1
46410-2311
4024
JOHNSON, JACK
09032001
CHEVROLET
0256050
1985
0
46404-0121
4032
KULKA, ROBERT C.
09042001
PIERCE-ARROW
0970000
1934
2
46307-3110
4035
KURTZ, DONALD
08302001
STUDEBAKER
0345000
1966
1
46410-1291
4038
LEVANDOWSKI, JILL
08282001
DODGE BROS.
0430000
1988
1
46342-1234
4044
METZ, ARNOLD E.
09012001
FORD
1323000
1996
2
46307-1211
4046
NORRIS, CHARLES S.
09052001
CHEVROLET
0844000
1992
0
46410-2311
4047
NOWAKOWSKI, ALFRED
09012001
DODGE BROS.
0656050
1994
1
46410-0121
4053
O'BOYLE, NIEL
09032001
PIERCE-ARROW
1550000
1938
2
46305-3111
4056
O'BRIAN, PATRICK
08302001
STUDEBAKER
0347050
1962
1
46410-1221
4059
PATTERSON, LENNI R.
08292001
HUDSON
0250000
1946
1
46414-1231
4061
PERRY, SHAMUS
09052001
FORD
0895000
1994
2
46307-1201
4066
REED, ROBERT B.
08272001
CHEVROLET
0740050
1996
0
46421-2311
4067
RODRIGUEZ, ALONZO
09052001
DODGE BROS.
1050050
1995
0
46323-0121
4073
SANCHEZ, HENRY
09012001
PIERCE-ARROW
0830000
1925
2
46315-3110
4081
SWARTZ, HECTOR
08282001
STUDEBAKER
0075025
1954
1
46301-1234
4084
TORREZ, MARTIN
0827200
1FORD
0565000
1994
2
46303-1201
4090
TUTTLE, MARK
08292001
FORD
0710050
1996
1
46413-2311
4094
WARNER, JACK
09042001
CHEVROLET
0856050
1996
0
46404-0121
4115
YACKLEY, YOURTO
09042001
PIERCE-ARROW
1000000
1930
2

非常简单:

  1. 读取所有元素并将它们放入列表中:

    ArrayList<CustomerSale> sales = new ArrayList<>();
    while(...){
        sales.add(custsale);
    }
    
  2. 对元素进行排序(我正在使用流,但可能 Collections.sort 可能会占用更少的内存,因为就地,例如 Comparator.comparing(CustomerSale::getMake),谢谢 @Thomas):

    List<CustomerSale> sorted = sales.stream().sorted((f,s) -> {
        return f.getMake().compareTo(s.getMake());
    }).collect(Collectors.toList());
    
  3. 并且仅在最后打印输出:

    for(int i = 0; i < sorted.size(); i++){
        CustomerSale custsale = sorted.get(i);
        if (i % pageSize == 0) {
            printPageHeader(i / pageSize + 1);
        }
    
        DecimalFormat df = new DecimalFormat("#,#####");
        System.out.printf(format, custsale.getZipCodeExtension(), custsale.getCustomerNumber(),
                custsale.getCustomerName(), custsale.getPurchaseDate(), custsale.getMake(),
                df.format(custsale.getPurchasePrice()), custsale.getYearOfVehicle(),
                custsale.getSatisfactionRating());
    };