hw3: use iterator pattern for iterating through list

This commit is contained in:
Yuri Tatishchev 2025-03-24 23:59:34 -07:00
parent f8b38bae8f
commit 63864e4e25
Signed by: CaZzzer
GPG Key ID: E0EBF441EA424369
3 changed files with 51 additions and 35 deletions

View File

@ -1,11 +1,12 @@
import controller.PhotoAlbumController; import controller.PhotoAlbumController;
import model.PhotoAlbumModel; import model.PhotoAlbumModel;
import strategy.SortByDate;
import view.PhotoAlbumView; import view.PhotoAlbumView;
public class PhotoAlbumApp { public class PhotoAlbumApp {
public static void main(String[] args) { public static void main(String[] args) {
// Create MVC components // Create MVC components
PhotoAlbumModel model = new PhotoAlbumModel(); PhotoAlbumModel model = new PhotoAlbumModel(new SortByDate());
PhotoAlbumView view = new PhotoAlbumView(); PhotoAlbumView view = new PhotoAlbumView();
PhotoAlbumController controller = new PhotoAlbumController(model, view); PhotoAlbumController controller = new PhotoAlbumController(model, view);

View File

@ -1,7 +1,5 @@
package controller; package controller;
import iterator.AlbumIterator;
import iterator.PhotoIterator;
import model.Photo; import model.Photo;
import model.PhotoAlbumModel; import model.PhotoAlbumModel;
import strategy.SortByDate; import strategy.SortByDate;
@ -56,26 +54,14 @@ public class PhotoAlbumController {
} }
public void handleNext() { public void handleNext() {
List<Photo> photos = model.getPhotos(); if (model.hasNext()) {
if (photos.isEmpty()) return; model.next();
Photo current = model.getCurrentPhoto();
int index = photos.indexOf(current);
if (index < photos.size() - 1) {
model.setCurrentPhoto(photos.get(index + 1));
} }
} }
public void handlePrevious() { public void handlePrevious() {
List<Photo> photos = model.getPhotos(); if (model.hasPrevious()) {
if (photos.isEmpty()) return; model.previous();
Photo current = model.getCurrentPhoto();
int index = photos.indexOf(current);
if (index > 0) {
model.setCurrentPhoto(photos.get(index - 1));
} }
} }
@ -94,9 +80,10 @@ public class PhotoAlbumController {
} }
public void handlePhotoSelection(int index) { public void handlePhotoSelection(int index) {
// Reset iterator and loop to the selected index
List<Photo> photos = model.getPhotos(); List<Photo> photos = model.getPhotos();
if (index >= 0 && index < photos.size()) { for (int i = 0; i < index; i++) {
model.setCurrentPhoto(photos.get(index)); model.next();
} }
} }
} }

View File

@ -2,36 +2,40 @@ package model;
import strategy.SortingStrategy; import strategy.SortingStrategy;
import iterator.AlbumIterator; import iterator.AlbumIterator;
import iterator.PhotoIterator;
import java.util.*; import java.util.*;
public class PhotoAlbumModel { public class PhotoAlbumModel {
private List<Photo> photos; private List<Photo> photos;
private SortingStrategy<Photo> sortingStrategy; private SortingStrategy<Photo> sortingStrategy;
private final List<ModelChangeListener> listeners; private final List<ModelChangeListener> listeners;
private Photo currentPhoto; private AlbumIterator iterator;
public interface ModelChangeListener { public interface ModelChangeListener {
void onModelChanged(); void onModelChanged();
} }
public PhotoAlbumModel() { public PhotoAlbumModel(SortingStrategy<Photo> sortingStrategy) {
photos = new ArrayList<>(); photos = new ArrayList<>();
listeners = new ArrayList<>(); listeners = new ArrayList<>();
iterator = new PhotoIterator(photos);
this.sortingStrategy = sortingStrategy;
} }
public void addPhoto(Photo photo) { public void addPhoto(Photo photo) {
photos.add(photo); photos.add(photo);
if (photos.size() == 1) {
currentPhoto = photo;
}
sortPhotos(); sortPhotos();
iterator = new PhotoIterator(photos);
notifyListeners(); notifyListeners();
} }
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 (currentPhoto != null && currentPhoto.name().equals(name)) { if (photos.isEmpty()) {
currentPhoto = photos.isEmpty() ? null : photos.getFirst(); iterator = new PhotoIterator(photos);
} else if (currentPhoto != null && currentPhoto.name().equals(name)) {
iterator = new PhotoIterator(photos);
} }
notifyListeners(); notifyListeners();
} }
@ -39,14 +43,13 @@ public class PhotoAlbumModel {
public void setSortingStrategy(SortingStrategy<Photo> strategy) { public void setSortingStrategy(SortingStrategy<Photo> strategy) {
this.sortingStrategy = strategy; this.sortingStrategy = strategy;
sortPhotos(); sortPhotos();
iterator = new PhotoIterator(photos);
notifyListeners(); notifyListeners();
} }
private void sortPhotos() { private void sortPhotos() {
if (sortingStrategy != null) {
photos = sortingStrategy.sort(photos); photos = sortingStrategy.sort(photos);
} }
}
public void addListener(ModelChangeListener listener) { public void addListener(ModelChangeListener listener) {
listeners.add(listener); listeners.add(listener);
@ -63,13 +66,38 @@ public class PhotoAlbumModel {
} }
public Photo getCurrentPhoto() { public Photo getCurrentPhoto() {
return currentPhoto; try {
return iterator.current();
} catch (NoSuchElementException e) {
return null;
}
} }
public void setCurrentPhoto(Photo photo) { public boolean hasNext() {
if (photos.contains(photo)) { return iterator.hasNext();
currentPhoto = photo; }
public boolean hasPrevious() {
return iterator.hasPrevious();
}
public Photo next() {
try {
Photo next = iterator.next();
notifyListeners(); notifyListeners();
return next;
} catch (NoSuchElementException e) {
return null;
}
}
public Photo previous() {
try {
Photo prev = iterator.previous();
notifyListeners();
return prev;
} catch (NoSuchElementException e) {
return null;
} }
} }
} }