package design.controller.userinput.menus; import design.controller.userinput.Menu; import design.controller.userinput.MenuOption; import design.persistence.JSONHandler; import design.persistence.XMLHandler; import java.io.File; import java.io.IOException; import java.util.List; import java.util.Map; import java.util.Scanner; import design.model.DataHandler; public class ImportExportMenu extends Menu { private static final Map handlers = Map.of( "xml", new XMLHandler(), "json", new JSONHandler() ); @Override public String getTitle() { return "import export menu"; } @Override public List getMenuOptions() { List opts = new java.util.ArrayList<>(); opts.add(new MenuOption("return to main menu", () -> new MainMenu().present())); opts.add(new MenuOption("import league...", () -> promptForPath(true, false))); opts.add(new MenuOption("export league...", () -> promptForPath(false, false))); opts.add(new MenuOption("import personal profile...", () -> promptForPath(true, true))); opts.add(new MenuOption("export personal profile...", () -> promptForPath(false, true))); return opts; } private void promptForPath(boolean isImporting, boolean isPersonalProfile) { System.out.println("Enter file path: "); Scanner sc = new Scanner(System.in); String filePath = sc.nextLine(); File file = new File(filePath); String ext = getExtension(filePath); DataHandler handler = handlers.get(ext); if (handler == null) { System.out.println("Unsupported file type: " + ext); return; } try { if (isImporting) { if (isPersonalProfile) handler.importPersonalData(file); else handler.importLeagueData(file); } else { if (isPersonalProfile) handler.exportPersonalData(file); else handler.exportLeagueData(file); } } catch (IOException e) { System.err.println("I/O error: " + e.getMessage()); } new ImportExportMenu().present(); } private static String getExtension(String fileName) { int i = fileName.lastIndexOf('.'); return (i >= 0) ? fileName.substring(i + 1).toLowerCase() : ""; } }