hw3: convert class members to final where possible, make Photo a record class
This commit is contained in:
parent
31302dd0d5
commit
f8b38bae8f
@ -1,5 +1,7 @@
|
||||
package controller;
|
||||
|
||||
import iterator.AlbumIterator;
|
||||
import iterator.PhotoIterator;
|
||||
import model.Photo;
|
||||
import model.PhotoAlbumModel;
|
||||
import strategy.SortByDate;
|
||||
@ -14,8 +16,8 @@ import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class PhotoAlbumController {
|
||||
private PhotoAlbumModel model;
|
||||
private PhotoAlbumView view;
|
||||
private final PhotoAlbumModel model;
|
||||
private final PhotoAlbumView view;
|
||||
|
||||
public PhotoAlbumController(PhotoAlbumModel model, PhotoAlbumView view) {
|
||||
this.model = model;
|
||||
|
@ -5,7 +5,7 @@ import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class PhotoIterator implements AlbumIterator {
|
||||
private List<Photo> photos;
|
||||
private final List<Photo> photos;
|
||||
private int currentPosition;
|
||||
|
||||
public PhotoIterator(List<Photo> photos) {
|
||||
|
@ -2,48 +2,5 @@ package model;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Photo {
|
||||
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;
|
||||
}
|
||||
public record Photo(String name, String filePath, Date dateAdded, long fileSize) {
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import java.util.*;
|
||||
public class PhotoAlbumModel {
|
||||
private List<Photo> photos;
|
||||
private SortingStrategy<Photo> sortingStrategy;
|
||||
private List<ModelChangeListener> listeners;
|
||||
private final List<ModelChangeListener> listeners;
|
||||
private Photo currentPhoto;
|
||||
|
||||
public interface ModelChangeListener {
|
||||
@ -29,9 +29,9 @@ public class PhotoAlbumModel {
|
||||
}
|
||||
|
||||
public void deletePhoto(String name) {
|
||||
photos.removeIf(photo -> photo.getName().equals(name));
|
||||
if (currentPhoto != null && currentPhoto.getName().equals(name)) {
|
||||
currentPhoto = photos.isEmpty() ? null : photos.get(0);
|
||||
photos.removeIf(photo -> photo.name().equals(name));
|
||||
if (currentPhoto != null && currentPhoto.name().equals(name)) {
|
||||
currentPhoto = photos.isEmpty() ? null : photos.getFirst();
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import java.util.List;
|
||||
public class SortByDate implements SortingStrategy<Photo> {
|
||||
@Override
|
||||
public List<Photo> sort(List<Photo> photos) {
|
||||
photos.sort(Comparator.comparing(Photo::getDateAdded));
|
||||
photos.sort(Comparator.comparing(Photo::dateAdded));
|
||||
return photos;
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import java.util.List;
|
||||
public class SortByName implements SortingStrategy<Photo> {
|
||||
@Override
|
||||
public List<Photo> sort(List<Photo> photos) {
|
||||
photos.sort(Comparator.comparing(Photo::getName));
|
||||
photos.sort(Comparator.comparing(Photo::name));
|
||||
return photos;
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import java.util.List;
|
||||
public class SortBySize implements SortingStrategy<Photo> {
|
||||
@Override
|
||||
public List<Photo> sort(List<Photo> photos) {
|
||||
photos.sort(Comparator.comparing(Photo::getFileSize));
|
||||
photos.sort(Comparator.comparing(Photo::fileSize));
|
||||
return photos;
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,6 @@ import java.awt.*;
|
||||
import java.util.List;
|
||||
|
||||
public class PhotoAlbumView extends JFrame implements PhotoAlbumModel.ModelChangeListener {
|
||||
private PhotoAlbumController controller;
|
||||
private PhotoAlbumModel model;
|
||||
|
||||
private JList<String> photoList;
|
||||
@ -68,7 +67,6 @@ public class PhotoAlbumView extends JFrame implements PhotoAlbumModel.ModelChang
|
||||
}
|
||||
|
||||
public void setController(PhotoAlbumController controller) {
|
||||
this.controller = controller;
|
||||
this.model = controller.getModel();
|
||||
model.addListener(this);
|
||||
|
||||
@ -97,14 +95,14 @@ public class PhotoAlbumView extends JFrame implements PhotoAlbumModel.ModelChang
|
||||
listModel.clear();
|
||||
List<Photo> photos = model.getPhotos();
|
||||
for (Photo photo : photos) {
|
||||
listModel.addElement(photo.getName());
|
||||
listModel.addElement(photo.name());
|
||||
}
|
||||
}
|
||||
|
||||
private void updateCurrentPhoto() {
|
||||
Photo current = model.getCurrentPhoto();
|
||||
if (current != null) {
|
||||
ImageIcon icon = loadImage(current.getFilePath());
|
||||
ImageIcon icon = loadImage(current.filePath());
|
||||
if (icon != null) {
|
||||
currentPhotoLabel.setIcon(icon);
|
||||
currentPhotoLabel.setText("");
|
||||
|
Loading…
x
Reference in New Issue
Block a user