From 57dfd938fac25bd41538655dfc4ed572a42bbdee Mon Sep 17 00:00:00 2001 From: DjBushido Date: Wed, 22 Jan 2014 14:56:19 -0500 Subject: [PATCH] Commit the (untested) implementation of all assignments in part two. --- .../edu/uncc/itcs4180/PartTwo/PartTwo.java | 23 ++++++++++++++++--- .../edu/uncc/itcs4180/PartTwo/Vehicle.java | 6 +++++ .../PartTwo/VehicleEntryComparator.java | 19 +++++++++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 HW1/src/edu/uncc/itcs4180/PartTwo/VehicleEntryComparator.java diff --git a/HW1/src/edu/uncc/itcs4180/PartTwo/PartTwo.java b/HW1/src/edu/uncc/itcs4180/PartTwo/PartTwo.java index ce80d2f..0cea9cc 100644 --- a/HW1/src/edu/uncc/itcs4180/PartTwo/PartTwo.java +++ b/HW1/src/edu/uncc/itcs4180/PartTwo/PartTwo.java @@ -7,6 +7,7 @@ import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -84,17 +85,33 @@ public class PartTwo { "------------------------------"); // Now we need to sort our counting, and get the top 10 results. + // The way this is done is to convert the Map to a Set natively, then the Set to a List + // This way, we preserve all Key-Value pairs during the sort. + // Finally, Collections.sort() goes in ascending order, so reverse that instead of taking + // the elements off the end of the list. + List> vehicleCountList = new ArrayList>(vehicleCount.entrySet()); + Collections.sort(vehicleCountList, Collections.reverseOrder(new VehicleEntryComparator())); + + System.out.println("--------------------------------------------------" + + "\nTop 10 Models:" + + "--------------------------------------------------"); + // Go to the top 10 results, or full list, whichever comes first + for (int i = 0; i < vehicleCountList.size() && i < 10; i++) + System.out.println(i + "-" + vehicleCountList.get(i).getKey() + ":" + + vehicleCountList.get(i).getValue()); + } private static Vehicle readVehicle(String line) { String[] elements = line.split(","); try { + // Note that there is an extra column in the data - "Car/Truck/Both" in col. 4 return new Vehicle(Integer.parseInt(elements[0]), // Model Year elements[1], // Manufacturer Name elements[2], // Model Name - Integer.parseInt(elements[3]), // Horsepower - Integer.parseInt(elements[4]), // No. Cylinders - Integer.parseInt(elements[5])); // No. Gears + Integer.parseInt(elements[4]), // Horsepower + Integer.parseInt(elements[5]), // No. Cylinders + Integer.parseInt(elements[6])); // No. Gears } catch (IndexOutOfBoundsException e) { System.err.println("Improperly formatted CSV file."); return null; diff --git a/HW1/src/edu/uncc/itcs4180/PartTwo/Vehicle.java b/HW1/src/edu/uncc/itcs4180/PartTwo/Vehicle.java index b79c812..c43a3ed 100644 --- a/HW1/src/edu/uncc/itcs4180/PartTwo/Vehicle.java +++ b/HW1/src/edu/uncc/itcs4180/PartTwo/Vehicle.java @@ -67,4 +67,10 @@ public class Vehicle { return this.modelYear + this.manufacturerName + this.modelName + this.horsePower + this.noCylinders + this.noGears; } + + public String toString() { + return "Model Year: " + this.modelYear + " Manufacturer Name: " + this.manufacturerName + + " Model Name: " + this.modelName + " Horsepower: " + this.horsePower + + " No. Cylinders: " + this.noCylinders + " No. Gears: " + this.noGears; + } } diff --git a/HW1/src/edu/uncc/itcs4180/PartTwo/VehicleEntryComparator.java b/HW1/src/edu/uncc/itcs4180/PartTwo/VehicleEntryComparator.java new file mode 100644 index 0000000..a75c243 --- /dev/null +++ b/HW1/src/edu/uncc/itcs4180/PartTwo/VehicleEntryComparator.java @@ -0,0 +1,19 @@ +package edu.uncc.itcs4180.PartTwo; + +import java.util.Comparator; +import java.util.Map.Entry; + +// Create a comparator that can be used to easily sort our vehicle map +class VehicleEntryComparator implements Comparator> { + + @Override + public int compare(Entry o1, Entry o2) { + if (o1.getValue() > o2.getValue()) + return 1; + else if (o1.getValue() < o2.getValue()) + return -1; + else + return 0; + } + +} \ No newline at end of file