Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ public void add(City city) {
cities.add(city);
}

/**
* This delete a city to the list if the city exists
* @param city
* This is a candidate city to delete
*/
public void delete(City city) {
if (cities.contains(city)) {
cities.remove(city);

}
else{
throw new IllegalArgumentException();
}

}

/**
* This returns a sorted list of cities
* @return
Expand All @@ -32,5 +48,14 @@ public List<City> getCities() {
Collections.sort(list);
return list;
}

/**
* This returns a number of cities
* @return
* Return the total sum of city number
*/
public int getCount() {
return cities.size();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,45 @@ void testGetCities() {
assertEquals(0, city.compareTo(cityList.getCities().get(0)));
assertEquals(0, mockCity().compareTo(cityList.getCities().get(1)));
}

@Test
void testDelete() {
CityList cityList = mockCityList();

assertEquals(1, cityList.getCities().size());

City city = new City("Edmonton", "Alberta");
cityList.delete(city);

assertEquals(0, cityList.getCities().size());
assertFalse(cityList.getCities().contains(city));
}

@Test
void testDeleteException() {
CityList cityList = mockCityList();

City city = new City("Yellowknife", "Northwest Territories");
cityList.delete(city);

assertThrows(IllegalArgumentException.class, () -> {
cityList.delete(city);
});
}
@Test
void testGetCount()
{
CityList cityList = mockCityList();

assertEquals(0, cityList.getCount());

City city = new City("Charlottetown", "Prince Edward Island");
cityList.add(city);
assertEquals(1, cityList.getCount());

City city2 = new City("Yellowknife", "Northwest Territories");
cityList.add(city2);
assertEquals(2, cityList.getCount());
}

}
}
Loading