1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
package design;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import design.model.course_search.*;
import design.model.*;
import java.util.List;
class TestCourseList {
// A dummy Course implementation for testing
private static class DummyCourse implements ICourse {
private final String name;
private final float difficulty;
public DummyCourse(String name, float difficulty) {
this.name = name;
this.difficulty = difficulty;
}
@Override
public String getName() { return name; }
@Override
public float getDifficultyRating() { return difficulty; }
@Override
public String getLocation() { return ""; }
@Override
public int getTotalPar() { return 0; }
@Override
public int getHoleCount() { return 0; }
@Override
public List<Hole> getHoles() { return null; }
}
// A simple sorter that sorts courses by difficulty
private static class SortByDifficultyTest implements CourseSorter {
@Override
public void sortCourses(List<ICourse> courses) {
courses.sort((c1, c2) -> Float.compare(c1.getDifficultyRating(), c2.getDifficultyRating()));
}
}
@Test
public void testAddAndRemoveCourses() {
CourseList courseList = new CourseList();
ICourse course1 = new DummyCourse("Course A", 2.0f);
ICourse course2 = new DummyCourse("Course B", 5.0f);
courseList.add(course1);
courseList.add(course2);
assertEquals(2, courseList.getCourses().size(), "Should have 2 courses after adding");
assertTrue(courseList.getCourses().contains(course1), "Course A should be in the list");
assertTrue(courseList.getCourses().contains(course2), "Course B should be in the list");
courseList.remove(course1);
assertEquals(1, courseList.getCourses().size(), "Should have 1 course after removal");
assertTrue(!courseList.getCourses().contains(course1), "Course A should no longer be in the list");
}
@Test
public void testSortCourses() {
CourseList courseList = new CourseList();
courseList.add(new DummyCourse("Course A", 3.0f));
courseList.add(new DummyCourse("Course B", 1.0f));
courseList.add(new DummyCourse("Course C", 2.0f));
// Set sorting strategy
courseList.setSorter(new SortByDifficultyTest());
courseList.sort();
List<ICourse> sorted = courseList.getCourses();
assertEquals("Course B", sorted.get(0).getName(), "First course should have lowest difficulty");
assertEquals("Course C", sorted.get(1).getName(), "Second course should have medium difficulty");
assertEquals("Course A", sorted.get(2).getName(), "Last course should have highest difficulty");
}
}
|