Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package assignment05;
- public class PhotoAlbum {
- //String to int: Integer.parseInt(myString);
- //int to string: String.valueOf(i)
- private String[] photos;
- private int nextOpenIndex;
- private String curPhoto;
- PhotoAlbum(int size) {
- this.photos = new String[size];
- }
- //initialize photos with already existing album
- PhotoAlbum(String[] givenPhotos) {
- setPhotos(givenPhotos);
- }
- //prints curPhoto, if no photo selected then prints "no photo selected"
- public void viewPhoto() {
- System.out.println(this.curPhoto != null ? this.curPhoto : "No photo selected.");
- }
- //clears curPhoto
- public void stop() {
- if (this.curPhoto != null) {
- this.curPhoto = null;
- System.out.println("Viewer cleared.");
- } else {
- System.out.println("No photo selected.");
- }
- }
- //returns index if photo exists, returns -1 if not found
- public int photoExists(String photoName) {
- for (int i = 0; i < this.nextOpenIndex; i++) {
- if (photoName.equals(this.photos[i])) {
- return i;
- }
- }
- return -1;
- }
- //returns curPhoto, else returns "No photo selected"
- public String getCurrentPhoto() {
- return this.curPhoto != null ? this.curPhoto : "No photo selected.";
- }
- //return true, sets curPhoto to photoName. Else returns false
- public boolean setCurrentPhoto(String photoName) {
- if (this.photoExists(photoName) > -1) {
- this.curPhoto = photoName;
- return true;
- } else {
- return false;
- }
- }
- //if photos isn't full and photo doesn't already exist, add to photos, increment nextOpenIndex, return true. Else return false
- public boolean addPhoto(String photoName) {
- if (this.nextOpenIndex < this.photos.length && this.photoExists(photoName) == -1) {
- this.photos[this.nextOpenIndex] = photoName;
- this.nextOpenIndex++;
- return true;
- } else {
- return false;
- }
- }
- //if exists, remove photo from this.photos, shifts remaining photos to fill gap, decrements nextOpenIndex.
- //Returns name of photo that was removed, else returns null
- public String removePhoto(String photoName) {
- int index = this.photoExists(photoName);
- if (index != -1) { // if photo exists
- //shift all photos down one and clear the copy of last picture
- for (int i = index; i < this.photos.length-1; i++) {
- this.photos[i] = this.photos[i+1];
- }
- this.nextOpenIndex--;
- this.photos[this.nextOpenIndex] = null;
- //update nextOpenIndex
- return photoName;
- } else {
- return null;
- }
- }
- //returns photos.length
- public int getMaxPhotoCapacity() {
- return this.photos.length;
- }
- //returns nextOpenIndex-1
- public int getCurNumPhotos() {
- return this.nextOpenIndex;
- }
- //returns if nextOpenIndex == photos.length
- public boolean isFull() {
- return this.nextOpenIndex == this.photos.length;
- }
- //returns empty space in photos[]
- public int spaceAvailable() {
- return this.photos.length - this.nextOpenIndex;
- }
- //print all photos
- public void listAllPhotos() {
- for(int i = 0; i < this.nextOpenIndex; i++) {
- System.out.println(this.photos[i]);
- }
- }
- //set this.photos to be twice givenPhotos. Add all givenPhtos elements to this.photos.
- //set nextOpenIndex to givenPhotos length
- public void setPhotos(String[] givenPhotos) {
- int givenPhotosLength = givenPhotos.length;
- this.photos = new String[givenPhotosLength*2];
- //copy photos from 'givenPhotos' to 'photos' array
- for (int i = 0; i < givenPhotosLength; i++) {
- this.photos[i] = givenPhotos[i];
- }
- this.nextOpenIndex = givenPhotosLength;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement