From 5f10dff0a624f46a36a160a7c47799b32b954b08 Mon Sep 17 00:00:00 2001 From: Christopher-Deo Date: Mon, 11 Sep 2023 19:53:18 -0500 Subject: [PATCH 1/3] print jobs function completed --- src/main/java/JobData.java | 5 ++--- src/main/java/TechJobs.java | 33 +++++++++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/main/java/JobData.java b/src/main/java/JobData.java index bcb4912f..6e65191e 100644 --- a/src/main/java/JobData.java +++ b/src/main/java/JobData.java @@ -25,7 +25,7 @@ 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) { @@ -56,7 +56,6 @@ 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". * @@ -139,4 +138,4 @@ private static void loadData() { } } -} +} \ No newline at end of file diff --git a/src/main/java/TechJobs.java b/src/main/java/TechJobs.java index 099f4efb..cf532037 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"); + // Check if there are any jobs to print + if (someJobs.isEmpty()) { + System.out.println("Search term:\n"); + System.out.println("No Results"); + return; + } + + // Iterate over each job HashMap + for (int i = 0; i < someJobs.size(); i++) { + HashMap job = someJobs.get(i); + System.out.println("\n*****"); + for (Map.Entry entry : job.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + System.out.println(key + ": " + value); + } + if (i < someJobs.size() - 1) { + System.out.println("*****\n"); + } else { + System.out.println("*****"); + } + } } -} + +} \ No newline at end of file From e3802e24bb8e9bb38d259c771f0800f19f9417a3 Mon Sep 17 00:00:00 2001 From: Christopher-Deo Date: Wed, 13 Sep 2023 18:56:43 -0500 Subject: [PATCH 2/3] completed print jobs method --- src/main/java/JobData.java | 73 ++++++------------------------------- src/main/java/TechJobs.java | 38 +++++++++---------- 2 files changed, 28 insertions(+), 83 deletions(-) diff --git a/src/main/java/JobData.java b/src/main/java/JobData.java index 6e65191e..8bd898dc 100644 --- a/src/main/java/JobData.java +++ b/src/main/java/JobData.java @@ -6,7 +6,6 @@ import java.io.IOException; import java.io.Reader; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -20,122 +19,72 @@ public class JobData { private static ArrayList> allJobs; - /** - * Fetch list of all values from loaded data, - * without duplicates, for a given column. - * - * @param field The column to retrieve values from - * @return List of all the values of the given field - */ public static ArrayList findAll(String field) { - - // load data, if not already loaded loadData(); - ArrayList values = new ArrayList<>(); - for (HashMap row : allJobs) { String aValue = row.get(field); - if (!values.contains(aValue)) { values.add(aValue); } } - return values; } public static ArrayList> findAll() { - - // load data, if not already loaded loadData(); - return allJobs; } - /** - * 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 - * @return List of all jobs matching the criteria - */ public static ArrayList> findByColumnAndValue(String column, String value) { - - // load data, if not already loaded loadData(); - ArrayList> jobs = new ArrayList<>(); - for (HashMap row : allJobs) { - String aValue = row.get(column); - if (aValue.contains(value)) { jobs.add(row); } } - return jobs; } - /** - * 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 - */ public static ArrayList> findByValue(String value) { - - // load data, if not already loaded loadData(); - - // TODO - implement this method - return null; + ArrayList> jobs = new ArrayList<>(); + for (HashMap row : allJobs) { + for (String column : row.keySet()) { + String columnValue = row.get(column); + if (columnValue.toLowerCase().contains(value.toLowerCase())) { + jobs.add(row); + break; // Break out of inner loop to avoid duplicate jobs + } + } + } + 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); List records = parser.getRecords(); Integer numberOfColumns = records.get(0).size(); String[] headers = parser.getHeaderMap().keySet().toArray(new String[numberOfColumns]); - allJobs = new ArrayList<>(); - - // Put the records into a more friendly format for (CSVRecord record : records) { HashMap newJob = new HashMap<>(); - for (String headerLabel : headers) { newJob.put(headerLabel, record.get(headerLabel)); } - allJobs.add(newJob); } - - // flag the data as loaded, so we don't do it twice isDataLoaded = true; - } catch (IOException e) { System.out.println("Failed to load job data"); e.printStackTrace(); } } - } \ No newline at end of file diff --git a/src/main/java/TechJobs.java b/src/main/java/TechJobs.java index cf532037..a796bdde 100644 --- a/src/main/java/TechJobs.java +++ b/src/main/java/TechJobs.java @@ -116,35 +116,31 @@ private static String getUserSelection(String menuHeader, HashMap> someJobs) { + boolean firstJob = true; // Flag to track the first job + // Check if there are any jobs to print if (someJobs.isEmpty()) { - System.out.println("Search term:\n"); - System.out.println("No Results"); - return; - } - - // Iterate over each job HashMap - for (int i = 0; i < someJobs.size(); i++) { - HashMap job = someJobs.get(i); - System.out.println("\n*****"); - for (Map.Entry entry : job.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - System.out.println(key + ": " + value); - } - if (i < someJobs.size() - 1) { - System.out.println("*****\n"); - } else { + 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 From 8e329c6b6e453094c44126534ef6b33f671e9624 Mon Sep 17 00:00:00 2001 From: Christopher-Deo Date: Wed, 13 Sep 2023 20:36:05 -0500 Subject: [PATCH 3/3] implemented findByValue method --- src/main/java/JobData.java | 72 +++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 5 deletions(-) diff --git a/src/main/java/JobData.java b/src/main/java/JobData.java index 8bd898dc..1f340195 100644 --- a/src/main/java/JobData.java +++ b/src/main/java/JobData.java @@ -8,6 +8,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map; // Import statement for Map /** * Created by LaunchCode @@ -19,69 +20,130 @@ public class JobData { private static ArrayList> allJobs; + /** + * Fetch list of all values from loaded data, + * without duplicates, for a given column. + * + * @param field The column to retrieve values from + * @return List of all the values of the given field + */ public static ArrayList findAll(String field) { + // Load data, if not already loaded loadData(); + ArrayList values = new ArrayList<>(); + for (HashMap row : allJobs) { String aValue = row.get(field); + if (!values.contains(aValue)) { values.add(aValue); } } + return values; } public static ArrayList> findAll() { + // Load data, if not already loaded loadData(); + return allJobs; } + /** + * 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 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 loadData(); + ArrayList> jobs = new ArrayList<>(); + for (HashMap row : allJobs) { String aValue = row.get(column); + if (aValue.contains(value)) { jobs.add(row); } } + return jobs; } + /** + * 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 + */ public static ArrayList> findByValue(String value) { + // Load data, if not already loaded loadData(); + ArrayList> jobs = new ArrayList<>(); - for (HashMap row : allJobs) { - for (String column : row.keySet()) { - String columnValue = row.get(column); + + // 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())) { - jobs.add(row); - break; // Break out of inner loop to avoid duplicate jobs + 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); List records = parser.getRecords(); Integer numberOfColumns = records.get(0).size(); String[] headers = parser.getHeaderMap().keySet().toArray(new String[numberOfColumns]); + allJobs = new ArrayList<>(); + + // Put the records into a more friendly format for (CSVRecord record : records) { HashMap newJob = new HashMap<>(); + for (String headerLabel : headers) { newJob.put(headerLabel, record.get(headerLabel)); } + allJobs.add(newJob); } + + // Flag the data as loaded, so we don't do it twice isDataLoaded = true; + } catch (IOException e) { System.out.println("Failed to load job data"); e.printStackTrace();