From 748f3bc02eec9bfc1067ff762f916cd55be37222 Mon Sep 17 00:00:00 2001 From: "Lee, Jiseop" Date: Fri, 12 Dec 2025 05:30:33 +0900 Subject: [PATCH 1/2] fix loop errors when integration --- src/main/java/com/unitime/App.java | 70 +++++++++---------- src/main/java/com/unitime/UI/ResultView.java | 5 +- src/main/java/com/unitime/feature/Editor.java | 28 +++++--- .../com/unitime/feature/InputHandler.java | 11 +-- 4 files changed, 61 insertions(+), 53 deletions(-) diff --git a/src/main/java/com/unitime/App.java b/src/main/java/com/unitime/App.java index b423ab9..e817a5b 100644 --- a/src/main/java/com/unitime/App.java +++ b/src/main/java/com/unitime/App.java @@ -2,52 +2,48 @@ import java.util.List; import java.util.Scanner; - import com.unitime.UI.IntroScreen; -import com.unitime.UI.ResultView; import com.unitime.algorthm.Scheduler; import com.unitime.feature.Course; -import com.unitime.feature.InputHandler; +import com.unitime.feature.Editor; +import com.unitime.feature.InputHandler; public class App { - public static void main( String[] args ) { + public static void main(String[] args) { + // 1. Scanner는 프로그램 전체에서 단 하나만 생성합니다. Scanner sc = new Scanner(System.in); + IntroScreen.start(); System.out.println("Press [ENTER] to start..."); sc.nextLine(); - while(true) { - - InputHandler inputHandler = new InputHandler(); - - System.out.println("\n[Algorithm] Generating optimal timetables..."); - Scheduler scheduler = new Scheduler(); - List> results = scheduler.schedule( - inputHandler.getMandatoryList(), - inputHandler.getOptionalList(), - inputHandler.getMaxCredit() - ); - - int currentIndex = 0; - boolean viewingResults = true; - - while (viewingResults) { - String command = ResultView.printBatchAndGetInput(results, currentIndex, sc); - - if (command.equals("next")) { - - if (currentIndex + 5 < results.size()) { - currentIndex += 5; - } else { - - } - } - else if (command.equals("edit")) { - viewingResults = false; - } - } - } - } + // 2. 초기 입력 받기 + InputHandler inputHandler = new InputHandler(); + inputHandler.handle(sc); // 생성한 스캐너 전달 + + // 3. 첫 스케줄 계산 + System.out.println("\n[Algorithm] Generating optimal timetables..."); + Scheduler scheduler = new Scheduler(); + List> results = scheduler.schedule( + inputHandler.getMandatoryList(), + inputHandler.getOptionalList(), + inputHandler.getMaxCredit() + ); + + // 4. Editor 생성 및 제어권 넘기기 (이후 처리는 Editor가 담당) + // Editor가 'next'나 'edit' 루프를 모두 관리하도록 합니다. + Editor editor = new Editor( + results, + inputHandler.getMandatoryList(), + inputHandler.getOptionalList(), + inputHandler.getMaxCredit(), + sc // 스캐너 전달 + ); + + editor.start(); -} + // Editor 내부에서 System.exit(0) 하거나 루프가 끝나면 여기로 옴 + sc.close(); + } +} \ No newline at end of file diff --git a/src/main/java/com/unitime/UI/ResultView.java b/src/main/java/com/unitime/UI/ResultView.java index 699d397..8d389af 100644 --- a/src/main/java/com/unitime/UI/ResultView.java +++ b/src/main/java/com/unitime/UI/ResultView.java @@ -63,7 +63,7 @@ public static String printBatchAndGetInput(List> allSchedules, int String input = scanner.nextLine().trim().toLowerCase(); //Edit: return only 'next' or 'edit' - if (input.equals("next") || input.equals("edit")) { + if (input.equals("next") || input.equals("edit") || input.equals("quit")) { return input; } @@ -114,7 +114,7 @@ private static void printNavigationMenu(boolean hasNext) { System.out.println(CYAN + "\n-------------------------------------------------------------" + RESET); if (hasNext) { - System.out.println(" ( > ω < ) Select: [" + BLUE + "next" + RESET + "] or [" + RED + "edit" + RESET + "] "); + System.out.println(" ( > ω < ) Select: [" + BLUE + "next" + RESET + "] or [" + RED + "edit" + RESET + "] or [" + YELLOW + "quit" + RESET + "] "); System.out.println(" Type '" + BLUE + "next" + RESET + "' to see more timetables."); } else { System.out.println(" ( > ω < ) Select: [" + RED + "edit" + RESET + "] "); @@ -122,6 +122,7 @@ private static void printNavigationMenu(boolean hasNext) { } System.out.println(" Type '" + RED + "edit" + RESET + "' to modify your courses."); + System.out.println(" Type '" + YELLOW + "quit" + RESET + "' to close UniTime-Solver."); System.out.println(CYAN + "-------------------------------------------------------------" + RESET); } diff --git a/src/main/java/com/unitime/feature/Editor.java b/src/main/java/com/unitime/feature/Editor.java index f0ae8c3..7d949ad 100644 --- a/src/main/java/com/unitime/feature/Editor.java +++ b/src/main/java/com/unitime/feature/Editor.java @@ -22,6 +22,10 @@ public Editor(List> schedules, List mandatory, List this.sc = scanner; } + public void start() { + viewLoop(0); + } + // Routing: send to function depending on its input public void route(String input, List> currentSchedules) { if (currentSchedules != null) { @@ -48,18 +52,19 @@ private void viewLoop(int startIndex) { 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; + if(nextInput.equals("quit")) { + quit(); + break; + } + + int nextIndex = startIndex; + if(nextInput.equals("next")) { + if (startIndex + 5 < schedules.size()) nextIndex += 5; + else System.out.println("[Info] Last page."); } - break; - } - route(nextInput, this.schedules); + route(nextInput, this.schedules); + } } // 'edit': edit lists and send it back to InputHandler @@ -71,6 +76,9 @@ private void editList() { Scheduler scheduler = new Scheduler(); this.schedules = scheduler.schedule(this.mandatoryList, this.optionList, this.goalCredit); + + System.out.println("Calculation Done. Showing results..."); + viewLoop(0); } // helper of 'edit' diff --git a/src/main/java/com/unitime/feature/InputHandler.java b/src/main/java/com/unitime/feature/InputHandler.java index 82a71fd..b6f477d 100644 --- a/src/main/java/com/unitime/feature/InputHandler.java +++ b/src/main/java/com/unitime/feature/InputHandler.java @@ -10,9 +10,7 @@ public class InputHandler { private List optionalList = new ArrayList<>(); private int maxCredit = 0; - public InputHandler() { - Scanner sc = new Scanner(System.in); - + public void handle(Scanner sc) { System.out.println("===== UniTime-Solver: Input Courses ====="); // Maximum credit @@ -40,7 +38,12 @@ public InputHandler() { System.out.println("\n[2] Enter OPTIONAL Courses"); inputLoop(sc, optionalList); - // Show result + printSummary(); + } + + + // Show result + public void printSummary() { System.out.println("\n=========================================="); System.out.println(" Check Input Summary "); System.out.println("=========================================="); From dd9137a3af22d27d289f9c1954654f41e1a60d31 Mon Sep 17 00:00:00 2001 From: "Lee, Jiseop" Date: Fri, 12 Dec 2025 05:37:01 +0900 Subject: [PATCH 2/2] fixed loop errors that take place when integrating classes --- src/main/java/com/unitime/App.java | 67 +++++++++---------- src/main/java/com/unitime/UI/ResultView.java | 5 +- src/main/java/com/unitime/feature/Editor.java | 28 +++++--- .../com/unitime/feature/InputHandler.java | 13 ++-- 4 files changed, 59 insertions(+), 54 deletions(-) diff --git a/src/main/java/com/unitime/App.java b/src/main/java/com/unitime/App.java index b423ab9..d1b3e96 100644 --- a/src/main/java/com/unitime/App.java +++ b/src/main/java/com/unitime/App.java @@ -2,52 +2,45 @@ import java.util.List; import java.util.Scanner; - import com.unitime.UI.IntroScreen; -import com.unitime.UI.ResultView; import com.unitime.algorthm.Scheduler; import com.unitime.feature.Course; -import com.unitime.feature.InputHandler; +import com.unitime.feature.Editor; +import com.unitime.feature.InputHandler; public class App { - public static void main( String[] args ) { + public static void main(String[] args) { + // Only one scanner !!!!!!! Scanner sc = new Scanner(System.in); + IntroScreen.start(); System.out.println("Press [ENTER] to start..."); sc.nextLine(); - while(true) { - - InputHandler inputHandler = new InputHandler(); - - System.out.println("\n[Algorithm] Generating optimal timetables..."); - Scheduler scheduler = new Scheduler(); - List> results = scheduler.schedule( - inputHandler.getMandatoryList(), - inputHandler.getOptionalList(), - inputHandler.getMaxCredit() - ); - - int currentIndex = 0; - boolean viewingResults = true; - - while (viewingResults) { - String command = ResultView.printBatchAndGetInput(results, currentIndex, sc); - - if (command.equals("next")) { - - if (currentIndex + 5 < results.size()) { - currentIndex += 5; - } else { - - } - } - else if (command.equals("edit")) { - viewingResults = false; - } - } - } - } + // initial input + InputHandler inputHandler = new InputHandler(); + inputHandler.handle(sc); // pass sc + + // first 5 + System.out.println("\n[Algorithm] Generating optimal timetables..."); + Scheduler scheduler = new Scheduler(); + List> results = scheduler.schedule( + inputHandler.getMandatoryList(), + inputHandler.getOptionalList(), + inputHandler.getMaxCredit() + ); + + // post-operations(next,edit,quit) are tossed to editor + Editor editor = new Editor( + results, + inputHandler.getMandatoryList(), + inputHandler.getOptionalList(), + inputHandler.getMaxCredit(), + sc + ); + editor.start(); -} + sc.close(); + } +} \ No newline at end of file diff --git a/src/main/java/com/unitime/UI/ResultView.java b/src/main/java/com/unitime/UI/ResultView.java index 699d397..8d389af 100644 --- a/src/main/java/com/unitime/UI/ResultView.java +++ b/src/main/java/com/unitime/UI/ResultView.java @@ -63,7 +63,7 @@ public static String printBatchAndGetInput(List> allSchedules, int String input = scanner.nextLine().trim().toLowerCase(); //Edit: return only 'next' or 'edit' - if (input.equals("next") || input.equals("edit")) { + if (input.equals("next") || input.equals("edit") || input.equals("quit")) { return input; } @@ -114,7 +114,7 @@ private static void printNavigationMenu(boolean hasNext) { System.out.println(CYAN + "\n-------------------------------------------------------------" + RESET); if (hasNext) { - System.out.println(" ( > ω < ) Select: [" + BLUE + "next" + RESET + "] or [" + RED + "edit" + RESET + "] "); + System.out.println(" ( > ω < ) Select: [" + BLUE + "next" + RESET + "] or [" + RED + "edit" + RESET + "] or [" + YELLOW + "quit" + RESET + "] "); System.out.println(" Type '" + BLUE + "next" + RESET + "' to see more timetables."); } else { System.out.println(" ( > ω < ) Select: [" + RED + "edit" + RESET + "] "); @@ -122,6 +122,7 @@ private static void printNavigationMenu(boolean hasNext) { } System.out.println(" Type '" + RED + "edit" + RESET + "' to modify your courses."); + System.out.println(" Type '" + YELLOW + "quit" + RESET + "' to close UniTime-Solver."); System.out.println(CYAN + "-------------------------------------------------------------" + RESET); } diff --git a/src/main/java/com/unitime/feature/Editor.java b/src/main/java/com/unitime/feature/Editor.java index f0ae8c3..7d949ad 100644 --- a/src/main/java/com/unitime/feature/Editor.java +++ b/src/main/java/com/unitime/feature/Editor.java @@ -22,6 +22,10 @@ public Editor(List> schedules, List mandatory, List this.sc = scanner; } + public void start() { + viewLoop(0); + } + // Routing: send to function depending on its input public void route(String input, List> currentSchedules) { if (currentSchedules != null) { @@ -48,18 +52,19 @@ private void viewLoop(int startIndex) { 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; + if(nextInput.equals("quit")) { + quit(); + break; + } + + int nextIndex = startIndex; + if(nextInput.equals("next")) { + if (startIndex + 5 < schedules.size()) nextIndex += 5; + else System.out.println("[Info] Last page."); } - break; - } - route(nextInput, this.schedules); + route(nextInput, this.schedules); + } } // 'edit': edit lists and send it back to InputHandler @@ -71,6 +76,9 @@ private void editList() { Scheduler scheduler = new Scheduler(); this.schedules = scheduler.schedule(this.mandatoryList, this.optionList, this.goalCredit); + + System.out.println("Calculation Done. Showing results..."); + viewLoop(0); } // helper of 'edit' diff --git a/src/main/java/com/unitime/feature/InputHandler.java b/src/main/java/com/unitime/feature/InputHandler.java index 82a71fd..fe2af10 100644 --- a/src/main/java/com/unitime/feature/InputHandler.java +++ b/src/main/java/com/unitime/feature/InputHandler.java @@ -10,9 +10,7 @@ public class InputHandler { private List optionalList = new ArrayList<>(); private int maxCredit = 0; - public InputHandler() { - Scanner sc = new Scanner(System.in); - + public void handle(Scanner sc) { System.out.println("===== UniTime-Solver: Input Courses ====="); // Maximum credit @@ -36,11 +34,16 @@ public InputHandler() { System.out.println("\n[1] Enter MANDATORY Courses"); inputLoop(sc, mandatoryList); - // OptionalList에 담기 + // OptionalList System.out.println("\n[2] Enter OPTIONAL Courses"); inputLoop(sc, optionalList); - // Show result + printSummary(); + } + + + // Show result + public void printSummary() { System.out.println("\n=========================================="); System.out.println(" Check Input Summary "); System.out.println("==========================================");