diff --git a/src/main/java/JobData.java b/src/main/java/JobData.java index bcb4912f..1f340195 100644 --- a/src/main/java/JobData.java +++ b/src/main/java/JobData.java @@ -6,9 +6,9 @@ import java.io.IOException; import java.io.Reader; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.List; +import java.util.Map; // Import statement for Map /** * Created by LaunchCode @@ -25,11 +25,10 @@ public class JobData { * without duplicates, for a given column. * * @param field The column to retrieve values from - * @return List of all of the values of the given field + * @return List of all the values of the given field */ public static ArrayList findAll(String field) { - - // load data, if not already loaded + // Load data, if not already loaded loadData(); ArrayList values = new ArrayList<>(); @@ -46,8 +45,7 @@ public static ArrayList findAll(String field) { } public static ArrayList> findAll() { - - // load data, if not already loaded + // Load data, if not already loaded loadData(); return allJobs; @@ -56,23 +54,20 @@ public static ArrayList> findAll() { /** * Returns results of search the jobs data by key/value, using * inclusion of the search term. - * * For example, searching for employer "Enterprise" will include results * with "Enterprise Holdings, Inc". * - * @param column Column that should be searched. - * @param value Value of teh field to search for + * @param column Column that should be searched. + * @param value Value of the field to search for * @return List of all jobs matching the criteria */ public static ArrayList> findByColumnAndValue(String column, String value) { - - // load data, if not already loaded + // Load data, if not already loaded loadData(); ArrayList> jobs = new ArrayList<>(); for (HashMap row : allJobs) { - String aValue = row.get(column); if (aValue.contains(value)) { @@ -87,29 +82,45 @@ public static ArrayList> findByColumnAndValue(String col * Search all columns for the given term * * @param value The search term to look for - * @return List of all jobs with at least one field containing the value + * @return List of all jobs with at least one field containing the value */ public static ArrayList> findByValue(String value) { - - // load data, if not already loaded + // Load data, if not already loaded loadData(); - // TODO - implement this method - return null; + ArrayList> jobs = new ArrayList<>(); + + // Iterate over all jobs + for (HashMap job : allJobs) { + boolean found = false; + + // Iterate over all columns of the job using Map.Entry + for (Map.Entry entry : job.entrySet()) { + String columnValue = entry.getValue(); + if (columnValue.toLowerCase().contains(value.toLowerCase())) { + found = true; + break; // If found in any column, no need to continue checking + } + } + + if (found) { + jobs.add(job); // Add the job if the value is found in any column + } + } + + return jobs; } /** * Read in data from a CSV file and store it in a list */ private static void loadData() { - // Only load data once if (isDataLoaded) { return; } try { - // Open the CSV file and set up pull out column header info and records Reader in = new FileReader(DATA_FILE); CSVParser parser = CSVFormat.RFC4180.withFirstRecordAsHeader().parse(in); @@ -130,7 +141,7 @@ private static void loadData() { allJobs.add(newJob); } - // flag the data as loaded, so we don't do it twice + // Flag the data as loaded, so we don't do it twice isDataLoaded = true; } catch (IOException e) { @@ -138,5 +149,4 @@ private static void loadData() { e.printStackTrace(); } } - -} +} \ No newline at end of file diff --git a/src/main/java/TechJobs.java b/src/main/java/TechJobs.java index 099f4efb..a796bdde 100644 --- a/src/main/java/TechJobs.java +++ b/src/main/java/TechJobs.java @@ -77,8 +77,7 @@ private static String getUserSelection(String menuHeader, HashMap> someJobs) { - System.out.println("printJobs is not implemented yet"); + boolean firstJob = true; // Flag to track the first job + + // Check if there are any jobs to print + if (someJobs.isEmpty()) { + System.out.println("\nNo Results"); + } else { + // Iterate over each job HashMap + for (HashMap job : someJobs) { + if (!firstJob) { + System.out.println(); // Add a newline separator if not the first job + } else { + firstJob = false; + } + System.out.println("*****"); + for (Map.Entry entry : job.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + System.out.println(key + ": " + value); + } + } + } } -} + + +} \ No newline at end of file