I am having problems with this part of the code in which I have to delete an Arraylist line by entering a song using the keyboard and then search the Arraylist and if it finds said song then it deletes it. I'll pass you the main and the Playlist class so that you can take a look at it and you can help me to solve it, since personally I have been trying it by myself but there is no way to get it out
Playlist class
import java.util.ArrayList;
import java.util.Scanner;
public class Playlist {
Scanner sc = new Scanner(System.in);
private String nombrecancion;
private String Artist;
private String Genre;
private int Year;
/**
* @param songName
* @param artist
* @param genre
* @param year
*/
private static ArrayList<Playlist> songs = new ArrayList<>();
public Playlist(String nombrecancion, String Artist, String genre, int year) {
this.nombrecancion = nombrecancion;
this.Artist = Artist;
this.Genre = genre;
this.Year = year;
}
public Playlist(){
}
public static void addSong(String SongName, String Artist, String Genre, int Year) {
songs.add(new Playlist(SongName, Artist, Genre, Year));
}
public void deleteSong(String SongName, String Artist, String Genre, int Year){
String cancion;
System.out.println("Entra canción");
cancion=sc.nextLine();
boolean busqueda=songs.contains(cancion);
songs.remove(SongName);
}
public static void listSong() {
for (int i=0;i<songs.size();i++){
System.out.println(songs.get(i));
}
}
@Override
public String toString() {
return "Nombre de la cancion : " + nombrecancion + " Artista : " + Artist + " Género: " + Genre + " Año :"
+ Year + "";
}
public String getArtist() {
return Artist;
}
public static void listmenu(){
System.out.println("1.Add a new song to Playlist");
System.out.println("2.Delete song from Playlist");
System.out.println("3.List songs by artist");
System.out.println("4.List top 5 songs with a specific hastag");
System.out.println("5.Exit");
}
}
Main
import java.util.InputMismatchException; import java.util.Scanner;
public class Main { public static void main(String[] args) {
boolean salida = false;
int option;
Scanner sc = new Scanner(System.in);
Playlist playlist=new Playlist();
String nombrecancion = null;
String Artista = null;
String Genero = null;
int popular = 0;
int Any = 0;
String hastags = null;
do {
Playlist.listmenu();
try {
System.out.print("Please choose an option:");
option = sc.nextInt();
switch (option) {
case 1:
System.out.print("SongName?:");
sc.nextLine();
nombrecancion = sc.nextLine();
System.out.print("Artist?:");
Artista = sc.nextLine();
System.out.print("Genre?:");
Genero = sc.nextLine();
System.out.print("Year?:");
Any = sc.nextInt();
Playlist.addSong(nombrecancion, Artista, Genero, Any);
System.out.println("How popular is this?[1-10]");
popular = sc.nextInt();
System.out.println("Add some hastags to the song: #w,#y : ");
sc.nextLine();
hastags = sc.nextLine();
System.out.println("You range is " + " " + popular + " And your hastags is " + " " + hastags);
break;
case 2:
System.out.println("You choose delete song");
playlist.deleteSong();
break;
case 3:
System.out.println("You choose List songs by artist");
String Artistas;
System.out.println("Enter an Artist");
Artistas=sc.nextLine();
Playlist.listSong();
break;
case 4:
System.out.println("You choose List top 5 songs with a specific hastag");
break;
case 5:
salida = true;
System.out.println("May the Music be with you");
break;
default:
System.out.println("Only numbers from 1 to 5");
}
} catch (InputMismatchException e) {
System.out.println("You need to enter an input");
sc.next(); // Evitamos que entre en bucle
}
} while (!salida);
}}
When you declare the
ArrayList<PlayList> songs
as an attribute of your class,PlayList
it can only be accessed from that same class and not in another, although your solution has been to declare it asstatic
and invoke it in the classmain
through the method that applies itthis makes it pointless to declare
Playlist playlist=new Playlist();
since you never use the object... so why use paradigmPOO
if you stick to a programming styleestructurada
, the information that A.cedano gave you in your previous question could well solve and clarify the problems in your code .. and it is that oneplayList
is a set ofcanciones
and in turn one of those songs has a title, artist, gender, year, etc. You could handle this well in an independent classhaving the class
canción
now you can generate the classPlayList
which will have a list of songsnow you only make use of your methods in the class
Main
Additional Mentions
-Make sure that the methods you declare make sense with the class where you declare them
-Use camelCase notation
-name variables with lowercase
-Only use Scanner in the class where your main method will be