hw3: PhotoAlbumModel: fix out-of-bounds errors on deletePhoto
This commit is contained in:
parent
19141831c8
commit
f2ee03502d
@ -59,18 +59,14 @@ public class PhotoAlbumModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes a photo from the album by name.
|
* Deletes a photo from the album by name (if it exists).
|
||||||
* If the deleted photo is the current photo or the album is empty,
|
* Resets the iterator and notifies the listeners.
|
||||||
* the iterator is reset.
|
|
||||||
*
|
*
|
||||||
* @param name the name of the photo to delete
|
* @param name the name of the photo to delete
|
||||||
*/
|
*/
|
||||||
public void deletePhoto(String name) {
|
public void deletePhoto(String name) {
|
||||||
Photo currentPhoto = iterator.current();
|
|
||||||
photos.removeIf(photo -> photo.name().equals(name));
|
photos.removeIf(photo -> photo.name().equals(name));
|
||||||
if (photos.isEmpty() || (currentPhoto != null && currentPhoto.name().equals(name))) {
|
iterator = new PhotoIterator(photos);
|
||||||
iterator = new PhotoIterator(photos);
|
|
||||||
}
|
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user