Commit the (untested) implementation of all assignments in part two.

master
DjBushido 2014-01-22 14:56:19 -05:00
parent ab678065fb
commit 57dfd938fa
3 changed files with 45 additions and 3 deletions

View File

@ -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<Map.Entry<String, Integer>> vehicleCountList = new ArrayList<Map.Entry<String, Integer>>(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;

View File

@ -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;
}
}

View File

@ -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<Entry<String, Integer>> {
@Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
if (o1.getValue() > o2.getValue())
return 1;
else if (o1.getValue() < o2.getValue())
return -1;
else
return 0;
}
}