Here is the code i am using for my classes. I have tested it many times and the functions all work correctly if i don't serialize and deserialize.
public class Library {
private String libTitle;
private Vector<Album> albums;
public String getLibTitle() {
return libTitle;
}
public void setLibTitle(String libTitle) {
this.libTitle = libTitle;
}
public Vector<Album> getAlbums() {
return albums;
}
public void setAlbums(Vector<Album> albums) {
this.albums = albums;
}
public Library(){
}
public Library(String libTitle) {
this.libTitle = libTitle;
this.albums = new Vector<Album>();
albums.trimToSize();
}
public void addAlbum(String album){
boolean added = false;
for (Album alb: this.getAlbums()){
if (alb.getAlbum()==album){
added=true;
}
if (added){
break;
}
}
if (!added){
this.getAlbums().add(new Album(album));
this.getAlbums().trimToSize();
}
}
public void removeAlbum(String album){
for (Album alb : this.getAlbums()){
if (alb.getAlbum()==album){
this.getAlbums().remove(alb);
this.getAlbums().trimToSize();
break;
}
}
}
public void addSong(String title, String author, String album){
boolean added = false;
for (Album alb : this.getAlbums()){
if (alb.getAlbum()==album){
alb.addSong(title,author);
added=true;
}
if (added){
break;
}
}
if (!added){
this.addAlbum(album);
this.addSong(title, author, album);
}
}
public void removeSong(String title, String author, String album){
boolean removed = false;
for (Album alb : this.getAlbums()){
if (alb.getAlbum()==album){
alb.removeSong(title);
if(alb.getSongs().isEmpty()){
this.getAlbums().remove(alb);
this.getAlbums().trimToSize();
};
removed=true;
}
if (removed){
break;
}
}
}
public void save()
{
try {
FileOutputStream xmlos = new FileOutputStream(this.libTitle +".xml");
XMLEncoder encoder = new XMLEncoder(xmlos);
encoder.writeObject(this);
encoder.close();
xmlos.close();
System.out.println("Done exporting a user as xml to "+this.libTitle+".xml");
}catch(Exception e) {
e.printStackTrace();
}
}
public Library restore(String lib)
{
Library newLib = null;
try {
System.out.println("Importing a user as xml from "+lib+".xml");
FileInputStream inFileStream = new FileInputStream(lib +".xml");
XMLDecoder decoder = new XMLDecoder(inFileStream);
newLib = (Library)decoder.readObject();
decoder.close();
inFileStream.close();
System.out.println("Libloaded "+ newLib.getLibTitle());
return newLib;
}catch(Exception e) {
e.printStackTrace();
}
return newLib;
}
}
Next is album class Which is used in the library Class
public class Album {
private String album;
private Vector<Song> songs;
public Vector<Song> getSongs() {
return songs;
}
public void setSongs(Vector<Song> songs) {
this.songs = songs;
}
public Album(){
}
public Album(String album) {
this.album = album;
this.songs = new Vector<Song>();
songs.trimToSize();
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
public void addSong(String song, String author){
boolean added = false;
for (Song son : this.getSongs()){
if (son.getTitle()==song){
added=true;
}
if (added){
break;
}
}
if (!added){
this.getSongs().add(new Song(song, author));
this.getSongs().trimToSize();
}
}
public void removeSong(String song){
for (Song son : this.getSongs()){
if (son.getTitle()==song){
this.getSongs().remove(son);
this.getSongs().trimToSize();
break;
}
}
}
}
After that is the song class which is.
public class Song {
private String title,author;
public Song(){
}
public Song(String title, String author) {
this.title = title;
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
So basically its a library i am serializing to XML and then deserializing for an app i am making. My problem is when i run the following code.....
public class Run {
public static void main(String args[]){
Library lib = new Library("MYLibrary");
lib.addSong("Crawling", "Lincoln Park", "Hybrid Theory");
lib.addSong("In The End", "Lincoln Park", "Hybrid Theory");
lib.addSong("Fire", "Pyros", "Burning Up");
lib.addSong("Ocean", "Drowners", "Burning Up");
lib.save();
Library lib2 = new Library();
lib2 =lib2.restore("MYLibrary");
lib2.setLibTitle("test");
lib2.removeSong("In The End", "Lincoln Park", "Hybrid Theory");
lib2.addSong("Crawling in the dark", "me", "Hybrid Theory");
lib2.removeSong("Crawling in the dark", "me", "Hybrid Theory");
lib2.removeSong("Ocean", "Drowners", "Burning Up");
lib2.removeAlbum("Hybrid Theory");
lib2.save();
}
}
The XML file saved doesn't have just one song like it should both XML files produced are the same. My teacher couldn't figure out why it doesn't work and neither can I. Why doesn't the library get changed?