1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6. public class MaintenanceTaskHandler {
  7. private static Map<String, Record> records = new HashMap<>();
  8. private static Scanner scanner = new Scanner(System.in);
  9. public static void main(String[] args) {
  10. // Load records (replace with your data loading logic)
  11. loadRecords();
  12. while (true) {
  13. displayMenu();
  14. String choice = scanner.nextLine();
  15. switch (choice) {
  16. case "1":
  17. addRecord();
  18. break;
  19. case "2":
  20. viewRecords();
  21. break;
  22. case "3":
  23. markRecordFailed();
  24. break;
  25. case "4":
  26. reportFailures();
  27. break;
  28. case "5":
  29. System.out.println("Exiting...");
  30. scanner.close();
  31. return;
  32. default:
  33. System.out.println("Invalid choice. Please try again.");
  34. }
  35. }
  36. }
  37. private static void loadRecords() {
  38. // Simulate loading records from a file or database
  39. records.put("record1", new Record("Record 1", "Some data"));
  40. records.put("record2", new Record("Record 2", "More data"));
  41. }
  42. private static void addRecord() {
  43. System.out.print("Enter record ID: ");
  44. String id = scanner.nextLine();
  45. if (!records.containsKey(id)) {
  46. System.out.print("Enter record name: ");
  47. String name = scanner.nextLine();
  48. System.out.print("Enter record data: ");
  49. String data = scanner.nextLine();
  50. records.put(id, new Record(name, data));
  51. System.out.println("Record added successfully.");
  52. } else {
  53. System.out.println("Record ID already exists.");
  54. }
  55. }
  56. private static void viewRecords() {
  57. if (records.isEmpty()) {
  58. System.out.println("No records found.");
  59. return;
  60. }
  61. System.out.println("Records:");
  62. for (Map.Entry<String, Record> entry : records.entrySet()) {
  63. System.out.println("ID: " + entry.getKey() + ", Name: " + entry.getValue().getName());
  64. }
  65. }
  66. private static void markRecordFailed() {
  67. System.out.print("Enter record ID to mark as failed: ");
  68. String id = scanner.nextLine();
  69. if (records.containsKey(id)) {
  70. records.get(id).setFailed(true);
  71. System.out.println("Record marked as failed.");
  72. } else {
  73. System.out.println("Record not found.");
  74. }
  75. }
  76. private static void reportFailures() {
  77. System.out.println("Failed Records:");
  78. boolean hasFailures = false;
  79. for (Map.Entry<String, Record> entry : records.entrySet()) {
  80. if (entry.getValue().isFailed()) {
  81. System.out.println("ID: " + entry.getKey() + ", Name: " + entry.getValue().getName());
  82. hasFailures = true;
  83. }
  84. }
  85. if (!hasFailures) {
  86. System.out.println("No failed records.");
  87. }
  88. }
  89. private static class Record {
  90. private String name;
  91. private String data;
  92. private boolean failed;
  93. public Record(String name, String data) {
  94. this.name = name;
  95. this.data = data;
  96. this.failed = false;
  97. }
  98. public String getName() {
  99. return name;
  100. }
  101. public String getData() {
  102. return data;
  103. }
  104. public boolean isFailed() {
  105. return failed;
  106. }
  107. public void setFailed(boolean failed) {
  108. this.failed = failed;
  109. }
  110. }
  111. }

Add your comment