import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class MaintenanceTaskHandler {
private static Map<String, Record> records = new HashMap<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
// Load records (replace with your data loading logic)
loadRecords();
while (true) {
displayMenu();
String choice = scanner.nextLine();
switch (choice) {
case "1":
addRecord();
break;
case "2":
viewRecords();
break;
case "3":
markRecordFailed();
break;
case "4":
reportFailures();
break;
case "5":
System.out.println("Exiting...");
scanner.close();
return;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
private static void loadRecords() {
// Simulate loading records from a file or database
records.put("record1", new Record("Record 1", "Some data"));
records.put("record2", new Record("Record 2", "More data"));
}
private static void addRecord() {
System.out.print("Enter record ID: ");
String id = scanner.nextLine();
if (!records.containsKey(id)) {
System.out.print("Enter record name: ");
String name = scanner.nextLine();
System.out.print("Enter record data: ");
String data = scanner.nextLine();
records.put(id, new Record(name, data));
System.out.println("Record added successfully.");
} else {
System.out.println("Record ID already exists.");
}
}
private static void viewRecords() {
if (records.isEmpty()) {
System.out.println("No records found.");
return;
}
System.out.println("Records:");
for (Map.Entry<String, Record> entry : records.entrySet()) {
System.out.println("ID: " + entry.getKey() + ", Name: " + entry.getValue().getName());
}
}
private static void markRecordFailed() {
System.out.print("Enter record ID to mark as failed: ");
String id = scanner.nextLine();
if (records.containsKey(id)) {
records.get(id).setFailed(true);
System.out.println("Record marked as failed.");
} else {
System.out.println("Record not found.");
}
}
private static void reportFailures() {
System.out.println("Failed Records:");
boolean hasFailures = false;
for (Map.Entry<String, Record> entry : records.entrySet()) {
if (entry.getValue().isFailed()) {
System.out.println("ID: " + entry.getKey() + ", Name: " + entry.getValue().getName());
hasFailures = true;
}
}
if (!hasFailures) {
System.out.println("No failed records.");
}
}
private static class Record {
private String name;
private String data;
private boolean failed;
public Record(String name, String data) {
this.name = name;
this.data = data;
this.failed = false;
}
public String getName() {
return name;
}
public String getData() {
return data;
}
public boolean isFailed() {
return failed;
}
public void setFailed(boolean failed) {
this.failed = failed;
}
}
}
Add your comment