From ab678065fb154fcb3b97a89bbce0355bbcfa4274 Mon Sep 17 00:00:00 2001 From: DjBushido Date: Wed, 22 Jan 2014 13:49:24 -0500 Subject: [PATCH] Add code to count unique vehicles --- .../edu/uncc/itcs4180/PartTwo/PartTwo.java | 53 +++++++++++++++++-- .../edu/uncc/itcs4180/PartTwo/Vehicle.java | 4 ++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/HW1/src/edu/uncc/itcs4180/PartTwo/PartTwo.java b/HW1/src/edu/uncc/itcs4180/PartTwo/PartTwo.java index 636077b..ce80d2f 100644 --- a/HW1/src/edu/uncc/itcs4180/PartTwo/PartTwo.java +++ b/HW1/src/edu/uncc/itcs4180/PartTwo/PartTwo.java @@ -1,12 +1,17 @@ package edu.uncc.itcs4180.PartTwo; -import java.io.BufferedInputStream; import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; +import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Set; /* Assignment: Homework 1 * File name: PartOne.java @@ -21,6 +26,7 @@ public class PartTwo { // Set up reading the actual data String filename = "raw_data.csv"; + String outputFilename = "distinct_vehicles.csv"; List vehicles; // Read in all of our data @@ -32,6 +38,7 @@ public class PartTwo { while ((line = reader.readLine()) != null) vehicles.add(readVehicle(line)); + reader.close(); } catch (FileNotFoundException e) { System.out.println("Could not open file: " + filename); return; @@ -40,7 +47,43 @@ public class PartTwo { return; } - // If we've made it here, we have the final list of all our vehicles + // If we've made it here, we have the final list of all our vehicles, + // but we need to find just the unique ones. + Set uniqueVehicles = new HashSet(vehicles); + + // Write the unique vehicles to another CSV file + try { + BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilename)); + + for (Vehicle v : uniqueVehicles) + writer.write(v.toCSV() + "\n"); + + writer.close(); + } catch (IOException e) { + System.err.println("Error writing unique vehicles to CSV"); + // Don't return here, we can still complete the rest of the program + } + + // Count the vehicles by modelYear and manufacturerName + Map vehicleCount = new HashMap(); + for (Vehicle v : uniqueVehicles) + if (vehicleCount.containsKey(vehicleMapString(v))) + vehicleCount.put(vehicleMapString(v), vehicleCount.get(vehicleMapString(v)) + 1); + + // Print out the results of our counting + // Convert to Set for iteration - http://stackoverflow.com/questions/1066589/java-iterate-through-hashmap + int counter = 1; + for (Map.Entry v : vehicleCount.entrySet()) { + System.out.println(counter + "-" + v.getKey() + ":" + v.getValue()); + counter++; + } + System.out.println("--------------------------------------------------" + + "------------------------------"); + System.out.println("Total: " + (counter - 1)); + System.out.println("--------------------------------------------------" + + "------------------------------"); + + // Now we need to sort our counting, and get the top 10 results. } private static Vehicle readVehicle(String line) { @@ -57,5 +100,9 @@ public class PartTwo { return null; } } - + + private static String vehicleMapString(Vehicle v) { + return v.getModelName() + " - " + v.getManufacturerName(); + } + } diff --git a/HW1/src/edu/uncc/itcs4180/PartTwo/Vehicle.java b/HW1/src/edu/uncc/itcs4180/PartTwo/Vehicle.java index ade80d8..b79c812 100644 --- a/HW1/src/edu/uncc/itcs4180/PartTwo/Vehicle.java +++ b/HW1/src/edu/uncc/itcs4180/PartTwo/Vehicle.java @@ -63,4 +63,8 @@ public class Vehicle { this.noGears = noGears; } + public String toCSV() { + return this.modelYear + this.manufacturerName + this.modelName + + this.horsePower + this.noCylinders + this.noGears; + } }