Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextArea; import javafx.scene.layout.VBox; import javafx.stage.Stage; import java.io . * ; import java.util. * ; public class

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.*;
import java.util.*;
public class SongListManager extends Application {
private static final String FILE_NAME = "songs.txt";
private Song[] songs; // Array to store songs
private TextArea displayArea; // TextArea to display song list
@Override
public void start(Stage primaryStage){
// Initialize songs array and read data from file
songs = readSongsFromFile(FILE_NAME);
// Initialize GUI components
displayArea = new TextArea();
displayArea.setEditable(false);
displaySongs();
Button addButton = new Button("Add Song");
addButton.setOnAction(e -> addSong());
Button deleteButton = new Button("Delete Song");
deleteButton.setOnAction(e -> deleteSong());
Button ascendingSortButton = new Button("Sort Ascending");
ascendingSortButton.setOnAction(e -> sortAscending());
Button descendingSortButton = new Button("Sort Descending");
descendingSortButton.setOnAction(e -> sortDescending());
Button randomizeButton = new Button("Randomize List");
randomizeButton.setOnAction(e -> randomizeList());
VBox root = new VBox(10);
root.getChildren().addAll(displayArea, addButton, deleteButton, ascendingSortButton,
descendingSortButton, randomizeButton);
Scene scene = new Scene(root,600,400);
primaryStage.setScene(scene);
primaryStage.setTitle("Song List Manager");
primaryStage.show();
}
// Method to read songs from file
private Song[] readSongsFromFile(String filename){
List songList = new ArrayList<>();
try (Scanner scanner = new Scanner(new File(filename))){
while (scanner.hasNextLine()){
String line = scanner.nextLine();
String[] parts = line.split(",");
String title = parts[0].trim();
String artist = parts[1].trim();
String genre = parts[2].trim();
int duration = Integer.parseInt(parts[3].trim());
songList.add(new Song(title, artist, genre, duration));
}
} catch (FileNotFoundException e){
System.err.println("File not found: "+ filename);
}
return songList.toArray(new Song[0]);
}
// Method to display songs in the TextArea
private void displaySongs(){
displayArea.clear();
for (Song song : songs){
displayArea.appendText(song +"
");
}
}
// Method to add a new song
private void addSong(){
// You can implement the logic to add a new song here
}
// Method to delete an existing song
private void deleteSong(){
// You can implement the logic to delete a song here
}
// Method to sort songs in ascending order
private void sortAscending(){
// You can implement the logic to sort songs in ascending order here
}
// Method to sort songs in descending order
private void sortDescending(){
// You can implement the logic to sort songs in descending order here
}
// Method to randomize the song list
private void randomizeList(){
// You can implement the logic to randomize the song list here
}
public static void main(String[] args){
launch(args);
}
} Add the logic to the methods above. Method to add a new song, Method to delete an existing song, Method to sort songs in ascending order, Method to sort songs in descending order, and Method to randomize the song list please.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions