Advertisement
jig487

albumclass_assignment05

Nov 28th, 2023 (edited)
923
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.56 KB | None | 1 0
  1. package assignment05;
  2.  
  3. public class PhotoAlbum {
  4.    
  5.     //String to int: Integer.parseInt(myString);
  6.     //int to string: String.valueOf(i)
  7.    
  8.     private String[] photos;
  9.     private int nextOpenIndex;
  10.     private String curPhoto;
  11.    
  12.     PhotoAlbum(int size) {
  13.         this.photos = new String[size];
  14.     }
  15.    
  16.     //initialize photos with already existing album
  17.     PhotoAlbum(String[] givenPhotos) {
  18.         setPhotos(givenPhotos);
  19.     }
  20.    
  21.     //prints curPhoto, if no photo selected then prints "no photo selected"
  22.     public void viewPhoto() {
  23.         System.out.println(this.curPhoto != null ? this.curPhoto : "No photo selected.");
  24.     }
  25.    
  26.     //clears curPhoto
  27.     public void stop() {
  28.         if (this.curPhoto != null) {
  29.             this.curPhoto = null;
  30.             System.out.println("Viewer cleared.");
  31.         } else {
  32.             System.out.println("No photo selected.");
  33.         }
  34.     }
  35.    
  36.     //returns index if photo exists, returns -1 if not found
  37.     public int photoExists(String photoName) {
  38.         for (int i = 0; i < this.nextOpenIndex; i++) {
  39.             if (photoName.equals(this.photos[i])) {
  40.                 return i;
  41.             }
  42.         }
  43.         return -1;
  44.     }
  45.    
  46.     //returns curPhoto, else returns "No photo selected"
  47.     public String getCurrentPhoto() {
  48.         return this.curPhoto != null ? this.curPhoto : "No photo selected.";
  49.     }
  50.    
  51.     //return true, sets curPhoto to photoName. Else returns false
  52.     public boolean setCurrentPhoto(String photoName) {
  53.         if (this.photoExists(photoName) > -1) {
  54.             this.curPhoto = photoName;
  55.             return true;
  56.         } else {
  57.             return false;
  58.         }
  59.     }
  60.    
  61.     //if photos isn't full and photo doesn't already exist, add to photos, increment nextOpenIndex, return true. Else return false
  62.     public boolean addPhoto(String photoName) {
  63.         if (this.nextOpenIndex < this.photos.length && this.photoExists(photoName) == -1) {
  64.             this.photos[this.nextOpenIndex] = photoName;
  65.             this.nextOpenIndex++;
  66.             return true;
  67.         } else {
  68.             return false;
  69.         }
  70.     }
  71.    
  72.     //if exists, remove photo from this.photos, shifts remaining photos to fill gap, decrements nextOpenIndex.
  73.     //Returns name of photo that was removed, else returns null
  74.     public String removePhoto(String photoName) {
  75.         int index = this.photoExists(photoName);
  76.        
  77.         if (index != -1) { // if photo exists
  78.            
  79.             //shift all photos down one and clear the copy of last picture                    
  80.             for (int i = index; i < this.photos.length-1; i++) {               
  81.                 this.photos[i] = this.photos[i+1];
  82.             }
  83.             this.nextOpenIndex--;
  84.             this.photos[this.nextOpenIndex] = null;
  85.             //update nextOpenIndex
  86.             return photoName;
  87.            
  88.         } else {
  89.             return null;
  90.         }
  91.     }
  92.    
  93.     //returns photos.length
  94.     public int getMaxPhotoCapacity() {
  95.         return this.photos.length;
  96.     }
  97.    
  98.     //returns nextOpenIndex-1
  99.     public int getCurNumPhotos() {
  100.         return this.nextOpenIndex;
  101.     }
  102.    
  103.     //returns if nextOpenIndex == photos.length
  104.     public boolean isFull() {
  105.         return this.nextOpenIndex == this.photos.length;
  106.     }
  107.    
  108.     //returns empty space in photos[]
  109.     public int spaceAvailable() {
  110.         return this.photos.length - this.nextOpenIndex;
  111.     }
  112.    
  113.     //print all photos
  114.     public void listAllPhotos() {
  115.         for(int i = 0; i < this.nextOpenIndex; i++) {
  116.             System.out.println(this.photos[i]);
  117.         }
  118.     }
  119.    
  120.     //set this.photos to be twice givenPhotos. Add all givenPhtos elements to this.photos.
  121.     //set nextOpenIndex to givenPhotos length
  122.     public void setPhotos(String[] givenPhotos) {
  123.         int givenPhotosLength = givenPhotos.length;
  124.        
  125.         this.photos = new String[givenPhotosLength*2];
  126.        
  127.         //copy photos from 'givenPhotos' to 'photos' array
  128.         for (int i = 0; i < givenPhotosLength; i++) {
  129.             this.photos[i] = givenPhotos[i];
  130.         }
  131.         this.nextOpenIndex = givenPhotosLength;
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement