I'm currently doing an assignment which requires me to use classes to make an album collection manager. I've been debugging and came across a problem. The Album[] is supposed to hold all the Album objects while numAlbums is supposed to keep track of the number of albums, but when I run the add method, it will increment the numAlbums and add the album to Album[], but once the method ends, it will reset instead of staying where it should be.
This is a problem especially since I can't print the album list since it relies on the numAlbums incrementing in value.
public class Collection {
private Album[] albums;
private int numAlbums; //number of albums currently in the collection
//find the album index, or return NOT_FOUND
private int find(Album album)
{
String title = album.getTitle(); // First, put the title and artist in string variables.
String artist = album.getArtist();
for (int i = 0; i < albums.length; i++) // Let's find this album.
{
Album c = new Album(); // Make Album c so we can compare every album.
c = albums[i];
if (title == c.getTitle() && artist == c.getArtist()) // If the title and artist match, then return the index.
{
return i; // We found the album!
}
}
return -1; // Album doesn't exist.
}
//increase the capacity of the array list by 4
private void grow()
{
Album[] temp = new Album[getAlbums() + 4]; // Create new temp array with 4 more spaces.
for (int i = 0; i < albums.length; i++) // Use loop to insert all values from albums to temp.
{
temp[i] = albums[i];
}
albums = temp; // Overwrite previous albums with new one.
}
// Add an Album. Command A.
public boolean add(Album album)
{
int counter = 1;
if (getAlbums() == 0) // If there are no albums, grow the list to the first 4 indexes.
{
Album[] temp = new Album[numAlbums + 4];
albums = temp;
}
else if (getAlbums() > 0)
{
Album search = new Album();
for (int i = 0; i < albums.length; i++) // Check if the album has the same title and artist in the collection.
{
search = albums[i];
if (album.getTitle().equals(search.getTitle()) && album.getArtist().equals(search.getArtist()) == true)
{
return false;
}
}
}
int a = getAlbums();
albums[a] = album; // Put the album in the collection.
a++; // Increase the amount of albums.
setAlbums(a);
if (numAlbums == 4 * counter) // If the number of albums reach multiples of 4, then increase the array and counter.
{
grow();
counter++;
}
for (int i = 0; i < albums.length; i++)
{
System.out.println(albums[i]); // Simple print of the list.
}
return true;
}
// Remove an Album. Command D.
public boolean remove(Album album)
{
if (find(album) == -1) // First, find the album to remove.
{
return false; // That album does not exist.
}
else // The album exists.
{
int index = find(album); // Copy the index number.
for (int i = index; i < albums.length; i++)
{
albums[i] = albums[i + 1]; // Shift the elements to the left by 1.
}
}
int a = getAlbums();
a--; // Decrease the amount of albums.
setAlbums(a);
return true; // We removed it.
}
//set to not available. Command L.
public boolean lendingOut(Album album)
{
if (find(album) == -1) // First, find the album to lend.
{
return false; // That album does not exist.
}
else // The album exists.
{
if (album.isAvailable() == false)
{
return false;
}
else
{
album.setAvailable(false);
}
}
return true; // We removed it.
}
//set to available. Command R.
public boolean returnAlbum(Album album)
{
if (find(album) == -1) // First, find the album to lend.
{
return false; // That album does not exist.
}
else // The album exists.
{
if (album.isAvailable() == true)
{
return false;
}
else
{
album.setAvailable(true);
}
}
return true; // We removed it.
}
//display the list without specifying the order. Command P.
public void print()
{
int y = getAlbums();
if (y == 0) // If the collection is empty, then return empty.
{
System.out.println("The collection is empty!");
return;
}
for (int i = 0; i < albums.length; i++)
{
System.out.println(albums[i]); // Simple print of the list.
}
}
//display the list sorted by the release date. Command PD.
public void printByReleaseDate()
{
if (getAlbums() == 0) // If the collection is empty, then return empty.
{
System.out.println("The collection is empty!");
return;
}
Album[] SortAlbumsRelease = new Album[numAlbums]; // Make another array to store the album list order.
for (int i = 0; i < albums.length; i++)
{
SortAlbumsRelease[i] = albums[i]; // Copy the contents of the array.
}
Album First = new Album(); // Create temp albums so we can compare.
Album Second = new Album();
Album temp = new Album();
for (int o = 0; o < albums.length; o++) // Go through every single album.
{
for (int p = 0 + 1; p < albums.length; p++)
{
First = SortAlbumsRelease[o];
Second = SortAlbumsRelease[p];
int compare = First.getReleaseDate().compareTo(Second.getReleaseDate()); // Use the compare method from date to get a value.
if (compare == -1) // If the first value is lower, then put that album in the previous slot.
{
temp = SortAlbumsRelease[o];
SortAlbumsRelease[o] = SortAlbumsRelease[p];
SortAlbumsRelease[p] = temp;
}
}
}
for (int i = 0; i < albums.length; i++) // Print the list.
{
System.out.println(SortAlbumsRelease[i]);
}
}
//display the list sorted by the genres. Command PG.
public void printByGenre()
{
if (getAlbums() == 0) // If the collection is empty, then return empty.
{
System.out.println("The collection is empty!");
return;
}
Genre type = null; // Set the genre type.
Album Temp = new Album(); // Make a temp album.
for (int i = 0; i <= 4; i++) // Iterate through all the genre cases.
{
switch(i)
{
case 0:
type = Genre.Classical;
case 1:
type = Genre.Country;
case 2:
type = Genre.Jazz;
case 3:
type = Genre.Pop;
case 4:
type = Genre.Unknown;
}
for (int j = 0; j < albums.length; j++) // Look through the albums that have the matching genre.
{
Temp = albums[j];
if (Temp.getGenre() == type) // If they match, print in that order.
{
System.out.println(albums[j]);
}
}
}
}
public int getAlbums()
{
return numAlbums;
}
public void setAlbums(int m)
{
numAlbums = m;
}
}
Below is the manager we have to create when running the program. We also use an album, genre, and date class, but I feel like the problem is somewhere in these areas.
import java.util.Scanner;
public class CollectionManager
{
public void run()
{
System.out.println("Collection Manager starts running."); // Begin.
while(true)
{
Scanner input = new Scanner(System.in);
String collect = input.nextLine(); // Take the input in.
String[] output = collect.split(","); // Creates an array "output" and splits the input by commas.
// Below is the designated spots for array "output" after the split.
// A,Change,Anika,pop,7/23/2021
//[0] [1] [2] [3] [4]
switch(output[0])
{
case "A": // Add the album.
Album a = new Album();
if (a.MakeAlbum(output) == true) // If the output goes well and returns true, then add to the collection.
{
Collection add = new Collection();
if (add.add(a) == true) // If true, add the album.
{
System.out.println(a + " >> added.");
break;
}
else // Otherwise, it's already in there.
{
System.out.println(a + "is already in the collection.");
break;
}
}
else // If the date is invalid, then return Invalid Date!
{
System.out.println("Invalid Date!");
break;
}
case "D": // Delete an Album.
Album d = new Album();
d.MakeAlbum(output);
Collection delete = new Collection();
if (delete.remove(d) == true) // If the album is in the collection, delete.
{
System.out.println(d.getTitle() + "::" + d.getArtist() + " >> deleted.");
break;
}
else // Otherwise, it doesn't exist.
{
System.out.println(d.getTitle() + "::" + d.getArtist() + " >> is not in the collection.");
break;
}
case "L": // Lend an Album.
Album l = new Album();
l.MakeAlbum(output);
Collection lend = new Collection();
if (lend.lendingOut(l) == true) // If the album is in the collection, lend.
{
System.out.println(l.getTitle() + "::" + l.getArtist() + " >> lending out and set to not available.");
break;
}
else // Otherwise, it doesn't exist or can't be lend out.
{
System.out.println(l.getTitle() + "::" + l.getArtist() + " >> is not available.");
break;
}
case "R": // Return the album.
Album r = new Album();
r.MakeAlbum(output);
Collection ret = new Collection();
if (ret.returnAlbum(r) == true) // If the album is in the collection, return.
{
System.out.println(r.getTitle() + "::" + r.getArtist() + " >> returning and set to available.");
break;
}
else // Otherwise, it doesn't exist or can't be returned.
{
System.out.println(r.getTitle() + "::" + r.getArtist() + " return cannot be completed.");
break;
}
case "P": // Print with no order.
Collection Print = new Collection();
Print.print(); // Print the album list.
break;
case "PD": // Print with release date order.
Collection PrintD = new Collection();
PrintD.printByReleaseDate();; // Print the album list by release date.
break;
case "PG": // Print with genre order.
Collection PrintG = new Collection();
PrintG.printByGenre(); // Print the album list by genre.
break;
case "Q": // Quit the program.
System.out.println("Collection Manager terminated.");
return; // The end.
default: // Invalid. Try again.
System.out.println("Invalid command!");
break;
}
}
}
}
I can't find anything on google relating to the problem so any help is appreciated.