hw3: convert class members to final where possible, make Photo a record class

This commit is contained in:
Yuri Tatishchev 2025-03-24 23:28:39 -07:00
parent 31302dd0d5
commit f8b38bae8f
Signed by: CaZzzer
GPG Key ID: E0EBF441EA424369
8 changed files with 15 additions and 58 deletions

View File

@ -1,5 +1,7 @@
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;
@ -14,8 +16,8 @@ import java.util.Date;
import java.util.List; import java.util.List;
public class PhotoAlbumController { public class PhotoAlbumController {
private PhotoAlbumModel model; private final PhotoAlbumModel model;
private PhotoAlbumView view; private final PhotoAlbumView view;
public PhotoAlbumController(PhotoAlbumModel model, PhotoAlbumView view) { public PhotoAlbumController(PhotoAlbumModel model, PhotoAlbumView view) {
this.model = model; this.model = model;

View File

@ -5,7 +5,7 @@ import java.util.List;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
public class PhotoIterator implements AlbumIterator { public class PhotoIterator implements AlbumIterator {
private List<Photo> photos; private final List<Photo> photos;
private int currentPosition; private int currentPosition;
public PhotoIterator(List<Photo> photos) { public PhotoIterator(List<Photo> photos) {

View File

@ -2,48 +2,5 @@ package model;
import java.util.Date; import java.util.Date;
public class Photo { public record Photo(String name, String filePath, Date dateAdded, long fileSize) {
private String name;
private String filePath;
private Date dateAdded;
private long fileSize;
public Photo(String name, String filePath, Date dateAdded, long fileSize) {
this.name = name;
this.filePath = filePath;
this.dateAdded = dateAdded;
this.fileSize = fileSize;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public Date getDateAdded() {
return dateAdded;
}
public void setDateAdded(Date dateAdded) {
this.dateAdded = dateAdded;
}
public long getFileSize() {
return fileSize;
}
public void setFileSize(long fileSize) {
this.fileSize = fileSize;
}
} }

View File

@ -7,7 +7,7 @@ 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 List<ModelChangeListener> listeners; private final List<ModelChangeListener> listeners;
private Photo currentPhoto; private Photo currentPhoto;
public interface ModelChangeListener { public interface ModelChangeListener {
@ -29,9 +29,9 @@ public class PhotoAlbumModel {
} }
public void deletePhoto(String name) { public void deletePhoto(String name) {
photos.removeIf(photo -> photo.getName().equals(name)); photos.removeIf(photo -> photo.name().equals(name));
if (currentPhoto != null && currentPhoto.getName().equals(name)) { if (currentPhoto != null && currentPhoto.name().equals(name)) {
currentPhoto = photos.isEmpty() ? null : photos.get(0); currentPhoto = photos.isEmpty() ? null : photos.getFirst();
} }
notifyListeners(); notifyListeners();
} }

View File

@ -8,7 +8,7 @@ import java.util.List;
public class SortByDate implements SortingStrategy<Photo> { public class SortByDate implements SortingStrategy<Photo> {
@Override @Override
public List<Photo> sort(List<Photo> photos) { public List<Photo> sort(List<Photo> photos) {
photos.sort(Comparator.comparing(Photo::getDateAdded)); photos.sort(Comparator.comparing(Photo::dateAdded));
return photos; return photos;
} }
} }

View File

@ -8,7 +8,7 @@ import java.util.List;
public class SortByName implements SortingStrategy<Photo> { public class SortByName implements SortingStrategy<Photo> {
@Override @Override
public List<Photo> sort(List<Photo> photos) { public List<Photo> sort(List<Photo> photos) {
photos.sort(Comparator.comparing(Photo::getName)); photos.sort(Comparator.comparing(Photo::name));
return photos; return photos;
} }
} }

View File

@ -8,7 +8,7 @@ import java.util.List;
public class SortBySize implements SortingStrategy<Photo> { public class SortBySize implements SortingStrategy<Photo> {
@Override @Override
public List<Photo> sort(List<Photo> photos) { public List<Photo> sort(List<Photo> photos) {
photos.sort(Comparator.comparing(Photo::getFileSize)); photos.sort(Comparator.comparing(Photo::fileSize));
return photos; return photos;
} }
} }

View File

@ -9,7 +9,6 @@ import java.awt.*;
import java.util.List; import java.util.List;
public class PhotoAlbumView extends JFrame implements PhotoAlbumModel.ModelChangeListener { public class PhotoAlbumView extends JFrame implements PhotoAlbumModel.ModelChangeListener {
private PhotoAlbumController controller;
private PhotoAlbumModel model; private PhotoAlbumModel model;
private JList<String> photoList; private JList<String> photoList;
@ -68,7 +67,6 @@ public class PhotoAlbumView extends JFrame implements PhotoAlbumModel.ModelChang
} }
public void setController(PhotoAlbumController controller) { public void setController(PhotoAlbumController controller) {
this.controller = controller;
this.model = controller.getModel(); this.model = controller.getModel();
model.addListener(this); model.addListener(this);
@ -97,14 +95,14 @@ public class PhotoAlbumView extends JFrame implements PhotoAlbumModel.ModelChang
listModel.clear(); listModel.clear();
List<Photo> photos = model.getPhotos(); List<Photo> photos = model.getPhotos();
for (Photo photo : photos) { for (Photo photo : photos) {
listModel.addElement(photo.getName()); listModel.addElement(photo.name());
} }
} }
private void updateCurrentPhoto() { private void updateCurrentPhoto() {
Photo current = model.getCurrentPhoto(); Photo current = model.getCurrentPhoto();
if (current != null) { if (current != null) {
ImageIcon icon = loadImage(current.getFilePath()); ImageIcon icon = loadImage(current.filePath());
if (icon != null) { if (icon != null) {
currentPhotoLabel.setIcon(icon); currentPhotoLabel.setIcon(icon);
currentPhotoLabel.setText(""); currentPhotoLabel.setText("");