So i created 3 class Song class Album class and Main class, Song class title and duration variable, Album class have ArrayList<Song>
this class have method addSong(Song song)
its return type is boolean fo what this method is doing is if song is already is the list then its not going to add it mean its return false else it will add, So from Main class i am calling this method. BUt the problem is its giving output like this linkedList.practic.task.Song@76fb509a
Song.java
package linkedList.practic.task;
public class Song {
private String title;
private String duration;
public Song(String title, String duration) {
this.title = title;
this.duration = duration;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDuration() {
return duration;
}
public void setDuration(String duration) {
this.duration = duration;
}
}
Album.java
package linkedList.practic.task;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Album {
private ArrayList<Song> songs;
public Album() {
songs = new ArrayList<>();
}
public boolean addSong(Song song){
if(!this.songs.contains(song)){
this.songs.add(song);
return true;
}
return false;
}
public void printSongs(){
Iterator<Song> iterator = songs.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next() + " ");
}
}
}
Main.java
package linkedList.practic.task;
import java.util.Scanner;
public class Main {
static Scanner scan = new Scanner(System.in);
static Album album = new Album();
public static void main(String[] args) {
boolean flag = true;
while(flag){
System.out.println("Enter your choice");
int action = scan.nextInt();
switch (action){
case 0:
flag = false;
break;
case 1:
addSong();
break;
case 2:
album.printSongs();
}
}
}
private static void addSong(){
System.out.println("Enter the name of the song");
String name = scan.nextLine();
scan.nextLine();
System.out.println("Enter the duration of song");
String duration = scan.nextLine();
if(album.addSong(new Song(name, duration))){
System.out.println( name + " is added to the list");
}
else{
System.out.println( name + " is already in the list");
}
}
}
also after adding song its not printing name of song its only printing is added to the list
and same for the else part