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 model.PhotoAlbumModel;
import strategy.SortByDate;
import view.PhotoAlbumView;
public class PhotoAlbumApp {
public static void main(String[] args) {
// Create MVC components
PhotoAlbumModel model = new PhotoAlbumModel();
PhotoAlbumModel model = new PhotoAlbumModel(new SortByDate());
PhotoAlbumView view = new PhotoAlbumView();
PhotoAlbumController controller = new PhotoAlbumController(model, view);

View File

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

View File

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