hw3: PhotoAlbumModel: fix out-of-bounds errors on deletePhoto

This commit is contained in:
Yuri Tatishchev 2025-03-26 20:52:50 -07:00
parent 19141831c8
commit f2ee03502d
Signed by: CaZzzer
GPG Key ID: E0EBF441EA424369

View File

@ -59,18 +59,14 @@ public class PhotoAlbumModel {
}
/**
* Deletes a photo from the album by name.
* If the deleted photo is the current photo or the album is empty,
* the iterator is reset.
* Deletes a photo from the album by name (if it exists).
* Resets the iterator and notifies the listeners.
*
* @param name the name of the photo to delete
*/
public void deletePhoto(String name) {
Photo currentPhoto = iterator.current();
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();
}