import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class RecordSearch {
public static void main(String[] args) {
String configFilePath = "config.txt"; // Path to the configuration file
String recordsFilePath = "records.txt"; // Path to the records file
String searchString = "error"; // The string to search for
try {
Config config = new Config(configFilePath);
List<Record> records = parseRecords(recordsFilePath);
List<Record> results = searchRecords(records, searchString, config);
if (results.isEmpty()) {
System.out.println("No records found containing '" + searchString + "'.");
} else {
System.out.println("Found " + results.size() + " records containing '" + searchString + "':");
for (Record result : results) {
System.out.println(result);
}
}
} catch (IOException e) {
System.err.println("Error reading files: " + e.getMessage());
} catch (ConfigException e) {
System.err.println("Error reading configuration: " + e.getMessage());
}
}
//Helper class to hold configurations
static class Config {
private Map<String, String> filters;
public Config(String filePath) throws IOException {
filters = loadFilters(filePath);
}
private Map<String, String> loadFilters(String filePath) throws IOException {
Map<String, String> filters = new HashMap<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split("=");
if (parts.length == 2) {
filters.put(parts[0].trim(), parts[1].trim());
}
}
}
return filters;
}
}
//Helper class to represent a record
static class Record {
private String id;
private String content;
public Record(String id, String content) {
this.id = id;
this.content = content;
}
@Override
public String toString() {
return "Record ID: " + id + ", Content: " + content;
}
}
// Parses records from a file
public static List<Record> parseRecords(String filePath) throws IOException {
List<Record> records = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 2) {
String id = parts[0].trim();
String content = parts[1].trim();
records.add(new Record(id, content));
}
}
}
return records;
}
// Searches records for a given string and filters
public static List<Record> searchRecords(List<Record> records, String searchString, Config config) {
List<Record> results = new ArrayList<>();
for (Record record : records) {
// Check if record content contains the search string
if (record.content.toLowerCase().contains(searchString.toLowerCase())) {
// Apply filters
if (applyFilters(record, config)) {
results.add(record);
}
}
}
return results;
}
//Applies filters to a record
private static boolean applyFilters(Record record, Config config) {
for (Map.Entry<String, String> entry : config.filters.entrySet()) {
String filterKey = entry.getKey();
String filterValue = entry.getValue();
if (record.content.toLowerCase().contains(filterValue.toLowerCase())) {
return true; //Filter applied
}
}
return false; //No filters matched
}
//Custom exception class for configuration errors
static class ConfigException extends Exception {
public ConfigException(String message) {
super(message);
}
}
}
Add your comment