1. import java.util.HashMap;
  2. import java.util.Map;
  3. public class RecordIntegrityChecker {
  4. /**
  5. * Verifies the integrity of a diagnostic record by checking for required fields and data consistency.
  6. *
  7. * @param record A map representing the diagnostic record. Keys are field names, values are field values.
  8. * @return True if the record is valid, false otherwise.
  9. */
  10. public static boolean verifyRecord(Map<String, Object> record) {
  11. // Check if the record is null or empty.
  12. if (record == null || record.isEmpty()) {
  13. System.err.println("Error: Record is null or empty.");
  14. return false;
  15. }
  16. // Define required fields.
  17. Map<String, String> requiredFields = new HashMap<>();
  18. requiredFields.put("patientId", "required");
  19. requiredFields.put("timestamp", "required");
  20. requiredFields.put("deviceId", "required");
  21. requiredFields.put("temperature", "required");
  22. // Check for missing required fields.
  23. for (String field : requiredFields.keySet()) {
  24. if (!record.containsKey(field)) {
  25. System.err.println("Error: Missing required field: " + field);
  26. return false;
  27. }
  28. }
  29. // Validate data types and ranges.
  30. Object patientIdObj = record.get("patientId");
  31. Object timestampObj = record.get("timestamp");
  32. Object deviceIdObj = record.get("deviceId");
  33. Object temperatureObj = record.get("temperature");
  34. if (!(patientIdObj instanceof String) || patientIdObj.toString().trim().isEmpty()) {
  35. System.err.println("Error: Invalid patientId format.");
  36. return false;
  37. }
  38. if (!(timestampObj instanceof String) || timestampObj.toString().trim().isEmpty()) {
  39. System.err.println("Error: Invalid timestamp format.");
  40. return false;
  41. }
  42. if (!(deviceIdObj instanceof String) || deviceIdObj.toString().trim().isEmpty()) {
  43. System.err.println("Error: Invalid deviceId format.");
  44. return false;
  45. }
  46. if (!(temperatureObj instanceof Double)) {
  47. System.err.println("Error: Invalid temperature format.");
  48. return false;
  49. }
  50. double temperature = (Double) temperatureObj;
  51. if (temperature < -50 || temperature > 50) {
  52. System.err.println("Error: Temperature out of range.");
  53. return false;
  54. }
  55. // Additional checks can be added here, such as:
  56. // - Checking for valid device IDs.
  57. // - Verifying that the timestamp is in the correct format.
  58. // - Validating the temperature range.
  59. // If all checks pass, the record is considered valid.
  60. return true;
  61. }
  62. public static void main(String[] args) {
  63. // Example Usage
  64. Map<String, Object> validRecord = new HashMap<>();
  65. validRecord.put("patientId", "12345");
  66. validRecord.put("timestamp", "2023-10-27T10:00:00");
  67. validRecord.put("deviceId", "sensor001");
  68. validRecord.put("temperature", 37.5);
  69. Map<String, Object> invalidRecordMissingField = new HashMap<>();
  70. invalidRecordMissingField.put("timestamp", "2023-10-27T10:00:00");
  71. invalidRecordMissingField.put("deviceId", "sensor001");
  72. invalidRecordMissingField.put("temperature", 37.5);
  73. Map<String, Object> invalidRecordInvalidType = new HashMap<>();
  74. invalidRecordInvalidType.put("patientId", "12345");
  75. invalidRecordInvalidType.put("timestamp", "2023-10-27T10:00:00");
  76. invalidRecordInvalidType.put("deviceId", "sensor001");
  77. invalidRecordInvalidType.put("temperature", "abc");
  78. System.out.println("Valid record: " + verifyRecord(validRecord));
  79. System.out.println("Invalid record (missing field): " + verifyRecord(invalidRecordMissingField));
  80. System.out.println("Invalid record (invalid type): " + verifyRecord(invalidRecordInvalidType));
  81. }

Add your comment