1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. public class RecordSearch {
  7. public static void main(String[] args) {
  8. String configFilePath = "config.txt"; // Path to the configuration file
  9. String recordsFilePath = "records.txt"; // Path to the records file
  10. String searchString = "error"; // The string to search for
  11. try {
  12. Config config = new Config(configFilePath);
  13. List<Record> records = parseRecords(recordsFilePath);
  14. List<Record> results = searchRecords(records, searchString, config);
  15. if (results.isEmpty()) {
  16. System.out.println("No records found containing '" + searchString + "'.");
  17. } else {
  18. System.out.println("Found " + results.size() + " records containing '" + searchString + "':");
  19. for (Record result : results) {
  20. System.out.println(result);
  21. }
  22. }
  23. } catch (IOException e) {
  24. System.err.println("Error reading files: " + e.getMessage());
  25. } catch (ConfigException e) {
  26. System.err.println("Error reading configuration: " + e.getMessage());
  27. }
  28. }
  29. //Helper class to hold configurations
  30. static class Config {
  31. private Map<String, String> filters;
  32. public Config(String filePath) throws IOException {
  33. filters = loadFilters(filePath);
  34. }
  35. private Map<String, String> loadFilters(String filePath) throws IOException {
  36. Map<String, String> filters = new HashMap<>();
  37. try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
  38. String line;
  39. while ((line = reader.readLine()) != null) {
  40. String[] parts = line.split("=");
  41. if (parts.length == 2) {
  42. filters.put(parts[0].trim(), parts[1].trim());
  43. }
  44. }
  45. }
  46. return filters;
  47. }
  48. }
  49. //Helper class to represent a record
  50. static class Record {
  51. private String id;
  52. private String content;
  53. public Record(String id, String content) {
  54. this.id = id;
  55. this.content = content;
  56. }
  57. @Override
  58. public String toString() {
  59. return "Record ID: " + id + ", Content: " + content;
  60. }
  61. }
  62. // Parses records from a file
  63. public static List<Record> parseRecords(String filePath) throws IOException {
  64. List<Record> records = new ArrayList<>();
  65. try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
  66. String line;
  67. while ((line = reader.readLine()) != null) {
  68. String[] parts = line.split(",");
  69. if (parts.length == 2) {
  70. String id = parts[0].trim();
  71. String content = parts[1].trim();
  72. records.add(new Record(id, content));
  73. }
  74. }
  75. }
  76. return records;
  77. }
  78. // Searches records for a given string and filters
  79. public static List<Record> searchRecords(List<Record> records, String searchString, Config config) {
  80. List<Record> results = new ArrayList<>();
  81. for (Record record : records) {
  82. // Check if record content contains the search string
  83. if (record.content.toLowerCase().contains(searchString.toLowerCase())) {
  84. // Apply filters
  85. if (applyFilters(record, config)) {
  86. results.add(record);
  87. }
  88. }
  89. }
  90. return results;
  91. }
  92. //Applies filters to a record
  93. private static boolean applyFilters(Record record, Config config) {
  94. for (Map.Entry<String, String> entry : config.filters.entrySet()) {
  95. String filterKey = entry.getKey();
  96. String filterValue = entry.getValue();
  97. if (record.content.toLowerCase().contains(filterValue.toLowerCase())) {
  98. return true; //Filter applied
  99. }
  100. }
  101. return false; //No filters matched
  102. }
  103. //Custom exception class for configuration errors
  104. static class ConfigException extends Exception {
  105. public ConfigException(String message) {
  106. super(message);
  107. }
  108. }
  109. }

Add your comment