diff --git a/src/main/java/com/unitime/feature/Editor.java b/src/main/java/com/unitime/feature/Editor.java new file mode 100644 index 0000000..f0ae8c3 --- /dev/null +++ b/src/main/java/com/unitime/feature/Editor.java @@ -0,0 +1,173 @@ +package com.unitime.feature; + +import java.util.*; +import com.unitime.UI.ResultView; +import com.unitime.algorthm.Scheduler; + +public class Editor { + + private List> schedules; + private List mandatoryList; + private List optionList; + private int goalCredit; + + private Scanner sc; + + // Constructor + public Editor(List> schedules, List mandatory, List optional, int credit, Scanner scanner) { + this.schedules = schedules; + this.mandatoryList = mandatory; + this.optionList = optional; + this.goalCredit = credit; + this.sc = scanner; + } + + // Routing: send to function depending on its input + public void route(String input, List> currentSchedules) { + if (currentSchedules != null) { + this.schedules = currentSchedules; + } + + input = input.toLowerCase().trim(); + + if (input.equals("quit")) quit(); + else if (input.equals("edit")) editList(); + else if (input.equals("next")) viewLoop(5); + else { + System.out.println("[Error] Invalid input. Going back to View Mode."); + viewLoop(0); + } + } + + // 'next': loop to show 5 more schedules + private void viewLoop(int startIndex) { + int index = startIndex; + String nextInput; + + while (true) { + ResultView rv = new ResultView(); + nextInput = rv.printBatchAndGetInput(this.schedules, index, this.sc); + + if (nextInput.equals("next")) { + if (index + 5 < schedules.size()) { + index += 5; + } else { + System.out.println("[Info] No more schedules to show."); + } + continue; + } + break; + } + + route(nextInput, this.schedules); + } + + // 'edit': edit lists and send it back to InputHandler + private void editList() { + System.out.println("[Edit Mode]"); + modifyCourse(this.mandatoryList, this.optionList); + + System.out.println("Re-calculating schedules..."); + + Scheduler scheduler = new Scheduler(); + this.schedules = scheduler.schedule(this.mandatoryList, this.optionList, this.goalCredit); + } + + // helper of 'edit' + private void modifyCourse(List mandatory, List optional) { + + InputHandler ih = new InputHandler(); + + while (true) { + System.out.println("\n------------------------------------------"); + System.out.println(" Current Goal Credit: " + this.goalCredit); + System.out.println(" Mandatory: " + mandatory.size() + " courses"); + System.out.println(" Optional : " + optional.size() + " courses"); + System.out.println("------------------------------------------"); + System.out.println("1. Add/Edit MANDATORY Courses"); + System.out.println("2. Add/Edit OPTIONAL Courses"); + System.out.println("3. Remove a Course"); + System.out.println("4. Change Goal Credit"); + System.out.println("0. Finish Editing (Run Scheduler)"); + System.out.print("> Select: "); + + String choice = sc.nextLine().trim(); + + // Add + if (choice.equals("1")) { + System.out.println("\n[Add to Mandatory] Type 'done' to finish."); + ih.inputLoop(sc, mandatory); + } + else if (choice.equals("2")) { + System.out.println("\n[Add to Optional] Type 'done' to finish."); + ih.inputLoop(sc, optional); + } + else if (choice.equals("3")) { + removeCourseHelper(mandatory, optional); + } + else if (choice.equals("4")) { + System.out.print("Enter new max credit: "); + try { + int newCredit = Integer.parseInt(sc.nextLine().trim()); + if (newCredit > 0) { + this.goalCredit = newCredit; + System.out.println("Goal credit updated."); + } else { + System.out.println("Credit must be positive."); + } + } catch (Exception e) { + System.out.println("Invalid number."); + } + } + else if (choice.equals("0")) { + break; + } + else { + System.out.println("Invalid choice."); + } + } + } + + // helper of 'edit': remove + private void removeCourseHelper(List mandatory, List optional) { + System.out.println("\n[Remove Course]"); + System.out.println("1. From Mandatory"); + System.out.println("2. From Optional"); + System.out.print("> Select list: "); + String listType = sc.nextLine().trim(); + + List target = null; + if (listType.equals("1")) target = mandatory; + else if (listType.equals("2")) target = optional; + else { + System.out.println("Canceled."); + return; + } + + if (target.isEmpty()) { + System.out.println("This list is empty."); + return; + } + + for (int i = 0; i < target.size(); i++) { + System.out.println("[" + i + "] " + target.get(i).getName()); + } + + System.out.print("Enter index to remove (or -1 to cancel): "); + try { + int idx = Integer.parseInt(sc.nextLine().trim()); + if (idx >= 0 && idx < target.size()) { + Course removed = target.remove(idx); + System.out.println("Removed: " + removed.getName()); + } else if (idx != -1) { + System.out.println("Invalid index."); + } + } catch (Exception e) { + System.out.println("Invalid input."); + } + } + + public void quit(){ + System.out.println("[Bye] Closing system..."); + } +} \ No newline at end of file diff --git a/src/main/java/com/unitime/feature/InputHandler.java b/src/main/java/com/unitime/feature/InputHandler.java index 842bc42..82a71fd 100644 --- a/src/main/java/com/unitime/feature/InputHandler.java +++ b/src/main/java/com/unitime/feature/InputHandler.java @@ -63,12 +63,10 @@ public InputHandler() { System.out.println("=========================================="); System.out.println("Finding timetables..."); System.out.println("=========================================="); - - sc.close(); } // Get user input - private void inputLoop(Scanner sc, List targetList) { + public void inputLoop(Scanner sc, List targetList) { System.out.println("------------------------------------------------------------------"); System.out.println("Format: Name / Credit / Time"); System.out.println("Example: Data Structure / 3 / Mon 12:30 14:00"); @@ -143,7 +141,7 @@ private void inputLoop(Scanner sc, List targetList) { } // Change time into minutes - private int parseMin(String t) throws Exception { + public int parseMin(String t) throws Exception { try { String[] hhmm = t.split(":"); if (hhmm.length != 2) {