Code Kommentare verbessert

main
Sultan Kayhan 2026-11-13 12:03:28 +01:00
parent c6ca7d7401
commit 2079718bdf
1 changed files with 28 additions and 8 deletions

View File

@ -11,68 +11,88 @@ import java.io.File;
public class SpotifyAnalyse {
public static void main(String[] args) throws Exception {
//open the CSV file.
Scanner scanner = new Scanner(new File("spotify.csv"), "UTF-8");
//Create an array for 10 songs.
Song[] songs = new Song[10];
int anzahlSongs = 0;
//read songs from CSV file
//read the file line by line.
while(scanner.hasNextLine()) {
String zeile = scanner.nextLine();
//Split the line into artist, title and duration
String[] daten = zeile.split(",");
//Check if the line contains valid song data
if(daten.length == 3 && !daten[0].equals("Künstler")) {
String künstler = daten[0];
String titel = daten[1];
int dauer = Integer.parseInt(daten[2]);
//Create and saave a new song
Song song = new Song(künstler, titel, dauer);
songs[anzahlSongs] = song;
anzahlSongs++;
}
}
//close the file
scanner.close();
//Display the playlist
zeigeSongs(songs, anzahlSongs);
//Display song statistics
System.out.println("Anzahl der Songs: " + anzahlSongs);
System.out.println("Gesamtdauer: " + gesamtDauer(songs, anzahlSongs) + " Sekunden ");
//start the artist search
sucheSongs(songs, anzahlSongs);
}
//display all songs
//Display all songs with artist and title
public static void zeigeSongs(Song[] songs, int anzahlSongs) {
System.out.println("Meine Spotify Playlist: ");
//Go trough all saved songs
for(int i = 0; i < anzahlSongs; i++) {
System.out.println(songs[i].getKünstler() + "-" + songs[i].getTitel());
}
}
//calculate total duration
//calculate the total duration of all songs
public static int gesamtDauer(Song[] songs, int anzahlSongs) {
int gesamt = 0;
//Add the duration of each song
for ( int i = 0; i < anzahlSongs; i++) {
gesamt = gesamt + songs[i].getDauer();
}
return gesamt;
}
//search songs by artist
//search for songs by artist name
public static void sucheSongs(Song[] songs, int anzahlSongs) {
//Read the artist name entered bx the user
Scanner eingabe = new Scanner(System.in);
System.out.println("Welchen Künstler suchst du?");
String gesucht = eingabe.nextLine();
int gefunden = 0;
//check each song for the entered artist
for(int i = 0; i < anzahlSongs; i++) {
if(songs[i].getKünstler().equalsIgnoreCase(gesucht)) {
//Display matching songs
System.out.println(songs[i].getKünstler() + " - " + songs[i].getTitel());
gefunden++;
}
}
//Display the search result
if(gefunden == 0) {
System.out.println("Kein Künstler gefunden.");
} else {