-
-
Save trikitrok/f632d82c6f7e70ce1c7609becd1f72c4 to your computer and use it in GitHub Desktop.
refactored code resulting from the same prompt using Junie + Gemini Flash 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package course_duration.domain; | |
| import java.time.Duration; | |
| import java.time.Instant; | |
| import java.time.temporal.ChronoUnit; | |
| public class Course { | |
| private static final Duration MAX_MINUTES_SHORT_COURSES = Duration.ofMinutes(10); | |
| private final String name; | |
| private final Configuration configuration; | |
| private final Clock clock; | |
| private Duration durationInMinutes; | |
| private final CourseView courseView; | |
| private CourseState state; | |
| public Course(String name, Configuration configuration, Clock clock, CourseView courseView) { | |
| this.name = name; | |
| this.configuration = configuration; | |
| this.clock = clock; | |
| this.courseView = courseView; | |
| durationInMinutes = Duration.ofMinutes(0); | |
| this.state = new YetToStartCourseState(clock, configuration, courseView).attach(this); | |
| } | |
| public void showDetails() { | |
| state.showDetails(); | |
| } | |
| public void start() { | |
| state = state.start(); | |
| } | |
| public void end() { | |
| state = state.end(); | |
| } | |
| boolean isShort() { | |
| return durationInMinutes.compareTo(MAX_MINUTES_SHORT_COURSES) < 0; | |
| } | |
| String getTitle() { | |
| return name + " course in " + getCollege() + " college"; | |
| } | |
| Duration getDurationInMinutes() { | |
| return durationInMinutes; | |
| } | |
| void setDurationInMinutes(Duration durationInMinutes) { | |
| this.durationInMinutes = durationInMinutes; | |
| } | |
| Configuration getConfiguration() { | |
| return configuration; | |
| } | |
| Clock getClock() { | |
| return clock; | |
| } | |
| CourseView getCourseView() { | |
| return courseView; | |
| } | |
| private String getCollege() { | |
| String college = configuration.getValue("COLLEGE"); | |
| if (college == null) { | |
| return "not found"; | |
| } | |
| return college; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package course_duration.domain; | |
| import java.time.Duration; | |
| abstract class CourseState { | |
| protected final Clock clock; | |
| protected final Configuration configuration; | |
| protected final CourseView courseView; | |
| protected Course course; | |
| protected CourseState(Clock clock, Configuration configuration, CourseView courseView) { | |
| this.clock = clock; | |
| this.configuration = configuration; | |
| this.courseView = courseView; | |
| } | |
| public CourseState attach(Course course) { | |
| this.course = course; | |
| return this; | |
| } | |
| public void showDetails() { | |
| this.courseView.displayLine("Title: " + course.getTitle()); | |
| Duration duration = course.getDurationInMinutes(); | |
| this.courseView.displayLine("Duration: " + duration.toMinutes() + " minutes"); | |
| this.courseView.displayLine("Type: " + (course.isShort() ? "short" : "long")); | |
| } | |
| public abstract CourseState start(); | |
| public abstract CourseState end(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package course_duration.domain; | |
| import java.time.Duration; | |
| class FinishedCourseState extends CourseState { | |
| private final Duration durationInMinutes; | |
| FinishedCourseState(Duration durationInMinutes, Clock clock, Configuration configuration, CourseView courseView) { | |
| super(clock, configuration, courseView); | |
| this.durationInMinutes = durationInMinutes; | |
| } | |
| @Override | |
| public CourseState start() { | |
| // Final state; no transitions | |
| return this; | |
| } | |
| @Override | |
| public CourseState end() { | |
| // Final state; no transitions | |
| return this; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package course_duration.domain; | |
| import java.time.Duration; | |
| import java.time.Instant; | |
| import java.time.temporal.ChronoUnit; | |
| class OnGoingCourseState extends CourseState { | |
| private final Instant startTime; | |
| OnGoingCourseState(Instant startTime, Clock clock, Configuration configuration, CourseView courseView) { | |
| super(clock, configuration, courseView); | |
| this.startTime = startTime; | |
| } | |
| @Override | |
| public CourseState start() { | |
| // Already started | |
| return this; | |
| } | |
| @Override | |
| public CourseState end() { | |
| Instant endTime = clock.now(); | |
| Duration duration = Duration.ofMinutes(ChronoUnit.MINUTES.between(startTime, endTime)); | |
| course.setDurationInMinutes(duration); | |
| return new FinishedCourseState(duration, clock, configuration, courseView).attach(course); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package course_duration.domain; | |
| class YetToStartCourseState extends CourseState { | |
| YetToStartCourseState(Clock clock, Configuration configuration, CourseView courseView) { | |
| super(clock, configuration, courseView); | |
| } | |
| @Override | |
| public CourseState start() { | |
| return new OnGoingCourseState(clock.now(), clock, configuration, courseView).attach(course); | |
| } | |
| @Override | |
| public CourseState end() { | |
| // No change in duration, remains zero | |
| return this; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment