From 532cd1e395a637a5e507bc3687a4a64e50e55b30 Mon Sep 17 00:00:00 2001 From: Clyhtsuriva Date: Fri, 11 Dec 2020 09:12:05 +0100 Subject: adding Serialization interface and methods --- Album.java | 27 +++++++++++++++++++-------- MusicalElement.java | 4 +++- Playlist.java | 13 ++++++++++--- Song.java | 25 ++++++++++++++++++++++++- jMusicHub.java | 8 +++++++- test.sql | 45 --------------------------------------------- 6 files changed, 63 insertions(+), 59 deletions(-) delete mode 100644 test.sql diff --git a/Album.java b/Album.java index e8766a3..34fae7a 100644 --- a/Album.java +++ b/Album.java @@ -1,15 +1,17 @@ -import java.util.ArrayList; +import java.util.*; +import java.io.Serializable; -public class Album { +public class Album implements Serializable { - private int id, duration, date; + private int id, duration; + private Date date; private String title, artist; - private ArrayList songs = new ArrayList(); + private ArrayList songs = new ArrayList(); - public Album(int id, String title, int duration, String artist, int date, ArrayList songs) { + public Album(int id, String title, int duration, String artist, Date date, ArrayList songs) { this.id=id; this.title=title; - this.duration=duration; + this.duration=this.getDuration(); this.artist=artist; this.date=date; this.songs=songs; @@ -17,12 +19,21 @@ public class Album { public int getId(){return id;} public String getTitle(){return title;} + public int getDuration(){ - //Addition of the duration of the songs inside the album + for (Song s : songs) {duration+=s.getDuration();} //increase with each song's getDuration() return duration; - } + public String getArtist(){return artist;} + public Date getDate(){return date;} + public ArrayList getSongs(){return songs;} + + + public void setId(){this.id=id;} + public void setTitle(){this.title=title;} + public void setArtist(){this.artist=artist;} + public void setDate(){this.date=date;} } diff --git a/MusicalElement.java b/MusicalElement.java index 5c24641..26c3808 100644 --- a/MusicalElement.java +++ b/MusicalElement.java @@ -1,10 +1,12 @@ +import java.io.Serializable; + /** The MusicalElement contains the base of songs and audibooks. * It is the abstract class they will take. * We use the classical get and set here. * Get is used to return the said value. * Set is used to modify the later.*/ -class MusicalElement { +class MusicalElement implements Serializable { //Our vars private int id, duration; diff --git a/Playlist.java b/Playlist.java index 2e19822..635f62a 100644 --- a/Playlist.java +++ b/Playlist.java @@ -1,12 +1,13 @@ import java.util.ArrayList; +import java.io.Serializable; -public class Playlist { +public class Playlist implements Serializable { private int id; private String name; - private ArrayList content = new ArrayList(); + private ArrayList content = new ArrayList(); - public Playlist(int id, String name, ArrayList content) { + public Playlist(int id, String name, ArrayList content) { this.id=id; this.name=name; this.content=content; @@ -14,4 +15,10 @@ public class Playlist { public int getId(){return id;} public String getName(){return name;} + public ArrayList getContent(){return content;} + + public void setID(){this.id=id;} + public void setName(){this.name=name;} +// public void setContent(){this.content=content;} + } diff --git a/Song.java b/Song.java index c4d1fc3..ecbb1eb 100644 --- a/Song.java +++ b/Song.java @@ -1,4 +1,4 @@ -public class Song extends MusicalElement { +public class Song extends MusicalElement{ private String artist, genre; public Song(String title, int duration, String content, String artist, Genre genre) { super(title, duration, content); @@ -15,5 +15,28 @@ public class Song extends MusicalElement { return getTitle()+";"+getDuration()+";"+getContent()+";"+artist+";"+genre; } + public void save(){ + try + { + //Saving of object in a file + FileOutputStream file = new FileOutputStream(songs.csv); + ObjectOutputStream out = new ObjectOutputStream(file); + + // Method for serialization of object + out.writeObject(this); + + out.close(); + file.close(); + + System.out.println("Object has been serialized"); + + } + + catch(IOException ex) + { + System.out.println("IOException is caught"); + } + } + } diff --git a/jMusicHub.java b/jMusicHub.java index 0cb43b5..2cbedc3 100644 --- a/jMusicHub.java +++ b/jMusicHub.java @@ -23,7 +23,7 @@ public class jMusicHub { System.out.printf("Duration (in seconds) : "); int duration = scan.nextInt(); System.out.printf("Content path : "); - String trash = scan.nextLine(); //Using this because the content scan is skippe after a nexInt + String trash = scan.nextLine(); //Using this because the content scan is skip after a nexInt String content = scan.nextLine(); System.out.printf("Artist : "); String artist = scan.nextLine(); @@ -117,6 +117,12 @@ public class jMusicHub { case "p": break; case "-": + break; + case "s": + for (Song s: songs){ + s.save(); + } + break; default : System.out.println("Unknown command. Type h for help."); diff --git a/test.sql b/test.sql deleted file mode 100644 index ae8d13b..0000000 --- a/test.sql +++ /dev/null @@ -1,45 +0,0 @@ ---album -SELECT - al.id as "ID", - al.name as "Title", - ar.name as "Artist", - al.original_year as "Release Year", - SUM(s.time) as "Duration" -FROM album al -JOIN artist ar ON ar.id=al.album_artist -JOIN song s ON s.album=al.id -GROUP BY al.id -INTO OUTFILE '/var/lib/mysql-files/albums.csv' -FIELDS ENCLOSED BY '"' -TERMINATED BY ';' -ESCAPED BY '"' -LINES TERMINATED BY '\r\n'; - ---song -SELECT - s.id as "ID", - s.title as "Title", - ar.name as "Artist", - s.time as "Duration", - s.file as "Content" -FROM song s -JOIN artist ar ON ar.id=s.artist -INTO OUTFILE '/var/lib/mysql-files/songs.csv' -FIELDS ENCLOSED BY '"' -TERMINATED BY ';' -ESCAPED BY '"' -LINES TERMINATED BY '\r\n'; - ---playlist -SELECT - p.id as "ID", - p.name as "Name", - s.title as "Song" -FROM playlist_data pd -JOIN playlist p ON p.id=pd.playlist -JOIN song s ON s.id=pd.object_id -INTO OUTFILE '/var/lib/mysql-files/playlists.csv' -FIELDS ENCLOSED BY '"' -TERMINATED BY ';' -ESCAPED BY '"' -LINES TERMINATED BY '\r\n'; -- cgit v1.2.3