Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 32 additions & 22 deletions src/main/java/JobData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<String> findAll(String field) {

// load data, if not already loaded
// Load data, if not already loaded
loadData();

ArrayList<String> values = new ArrayList<>();
Expand All @@ -46,8 +45,7 @@ public static ArrayList<String> findAll(String field) {
}

public static ArrayList<HashMap<String, String>> findAll() {

// load data, if not already loaded
// Load data, if not already loaded
loadData();

return allJobs;
Expand All @@ -56,23 +54,20 @@ public static ArrayList<HashMap<String, String>> 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<HashMap<String, String>> findByColumnAndValue(String column, String value) {

// load data, if not already loaded
// Load data, if not already loaded
loadData();

ArrayList<HashMap<String, String>> jobs = new ArrayList<>();

for (HashMap<String, String> row : allJobs) {

String aValue = row.get(column);

if (aValue.contains(value)) {
Expand All @@ -87,29 +82,45 @@ public static ArrayList<HashMap<String, String>> 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<HashMap<String, String>> findByValue(String value) {

// load data, if not already loaded
// Load data, if not already loaded
loadData();

// TODO - implement this method
return null;
ArrayList<HashMap<String, String>> jobs = new ArrayList<>();

// Iterate over all jobs
for (HashMap<String, String> job : allJobs) {
boolean found = false;

// Iterate over all columns of the job using Map.Entry
for (Map.Entry<String, String> 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);
Expand All @@ -130,13 +141,12 @@ 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) {
System.out.println("Failed to load job data");
e.printStackTrace();
}
}

}
}
29 changes: 25 additions & 4 deletions src/main/java/TechJobs.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ private static String getUserSelection(String menuHeader, HashMap<String, String
Boolean validChoice = false;
String[] choiceKeys = new String[choices.size()];

// Put the choices in an ordered structure so we can
// associate an integer with each one
// Put the choices in an ordered structure, so we can associate an integer with each one
int i = 0;
for (String choiceKey : choices.keySet()) {
choiceKeys[i] = choiceKey;
Expand Down Expand Up @@ -120,6 +119,28 @@ private static String getUserSelection(String menuHeader, HashMap<String, String
// Print a list of jobs
private static void printJobs(ArrayList<HashMap<String, String>> 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<String, String> 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<String, String> entry : job.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + ": " + value);
}
}
}
}
}


}