Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

My question is How to implement the code below so that when the delete button is clicked the selected song should be deleted from the

My question is How to implement the code below so that when the delete button is clicked the selected song should be deleted from the song list? Currently, the click of delete button does not delete the selected song. Thank you!

Complete code to copy:

For the class Song.java:

// Definition of the class Song. public class Song { private String itemCode; private String description; private String artist; private String album; private double price; // Definition of the constructor. public Song() { this.itemCode = ""; this.description = ""; this.artist = ""; this.album = ""; this.price = 0.0; } // Definition of the parameterized Constructor. public Song(String itemCode, String description, String artist, String album, double price) { this.itemCode = itemCode; this.description = description; this.artist = artist; this.album = album; this.price = price; }

// Definition of the method getItemCode(). public String getItemCode() { return itemCode; } //Definition of the method setItemCode(). public void setItemCode(String itemCode) { this.itemCode = itemCode; } //Definition of the method getDescription(). public String getDescription() { return description; } //Definition of the method setDescription(). public void setDescription(String description) { this.description = description; } //Definition of the method getArtist(). public String getArtist() { return artist; } //Definition of the method setArtist(). public void setArtist(String artist) { this.artist = artist; } //Definition of the method getAlbum(). public String getAlbum() { return album; } //Definition of the method setAlbum(). public void setAlbum(String album) { this.album = album; } //Definition of the method getPrice(). public double getPrice() { return price; } //Definition of the method setPrice(). public void setPrice(double price) { this.price = price; } //Definition of the method toString(). public String toString() { return description; } }

For the class SongDB.java:

// File: SongDB.java // Import the packages. import java.io.*; import java.awt.*; import java.util.*; import javax.swing.*; import java.awt.event.*;

// Definition of the class SongDB. public class SongDB extends JFrame implements ActionListener { // Declare an ArrayList. private ArrayList songs; // Create labels using JLabel class. private JLabel jlblSelect = new JLabel("Select Song:"); private JLabel jlblItemCode = new JLabel("Item Code:"); private JLabel jlblDescription = new JLabel("Description:"); private JLabel jlblArtist = new JLabel("Artist:"); private JLabel jlblAlbum = new JLabel("Album: "); private JLabel jlblPrice = new JLabel("Price:"); // Create a Combobox. private JComboBox combobox = new JComboBox(); // Create the text fields. private JTextField jtfItemCode = new JTextField(20); private JTextField jtfDescription = new JTextField(20); private JTextField jtfArtist = new JTextField(20); private JTextField jtfAlbum = new JTextField(20); private JTextField jtfPrice = new JTextField(20); // Create the buttons. private JButton jbtnAdd = new JButton("Add"); private JButton jbtnEdit = new JButton("Edit"); private JButton jbtnDelete = new JButton("Delete"); private JButton jbtnAccept = new JButton("Accept"); private JButton jbtnCancel = new JButton("Cancel"); private JButton jbtnExit = new JButton("Exit"); // Create 8 panels in the Frame. private JPanel panel1 = new JPanel(); private JPanel panel2 = new JPanel(); private JPanel panel3 = new JPanel(); private JPanel panel4 = new JPanel(); private JPanel panel5 = new JPanel(); private JPanel panel6 = new JPanel(); private JPanel panel7 = new JPanel(); private JPanel panel8 = new JPanel();

// Definition of the Constructor with parameter // ArrayList of songs. public SongDB(ArrayList list) { // Create an ArrayList. songs = new ArrayList(); // Find the size of the ArrayList. if (list.size() > 0) { // If the size is greater than 0, then add // the song in the combobox and sons List. for (Song song : list) { songs.add(song); combobox.addItem(song); } } // If condition fails, then add the fist song. else { Song oneSong = new Song("BT012", "Yellow Submarine", "The Beatles", "Beatles Greatest Hits 1", 1.99);

songs.add(oneSong); combobox.addItem(oneSong); } // Add a combo box in the panel1. panel1.add(jlblSelect); panel1.add(combobox); // Add ItemCode in the panel2. panel2.add(jlblItemCode); panel2.add(jtfItemCode); // Add Description in the panel3. panel3.add(jlblDescription); panel3.add(jtfDescription); // Add Artist in the panel4. panel4.add(jlblArtist); panel4.add(jtfArtist); // Add Album in the panel5. panel5.add(jlblAlbum); panel5.add(jtfAlbum); // Add Price in the panel6. panel6.add(jlblPrice); panel6.add(jtfPrice); // Add buttons Add, Delete, Accept,and Cancel // in the panel7. panel7.add(jbtnAdd); panel7.add(jbtnEdit); panel7.add(jbtnDelete); panel7.add(jbtnAccept); panel7.add(jbtnCancel); // Add Exit in the panel8. panel8.add(jbtnExit); // Set all the panels as Flow layout. setLayout(new FlowLayout()); add(panel1); add(panel2); add(panel3); add(panel4); add(panel5); add(panel6); add(panel7); add(panel8); // Select a song in the combobox. Song currentSong = (Song) combobox.getSelectedItem(); showSong(currentSong); // If the user press Accept or Cancel button, // then the operation is cancelled. jbtnAccept.setEnabled(false); jbtnCancel.setEnabled(false); // Call the method addActionListener(). combobox.addActionListener(this); jbtnAdd.addActionListener(this); jbtnEdit.addActionListener(this); jbtnDelete.addActionListener(this); jbtnAccept.addActionListener(this); jbtnCancel.addActionListener(this); jbtnExit.addActionListener(this); }// End of the class SongsDB. // Definition of the method actionPerformed().

public void actionPerformed(ActionEvent ae) { // create an object source and call // getSource() method. Object source = ae.getSource(); Song currentSong; // Define a boolean variable. boolean isNewSong = false; // If the user clicks the combobox, then // a song in the combobox get selected. if (source == combobox) { currentSong = (Song) combobox.getSelectedItem(); showSong(currentSong);

} // If the user clicks the Add button, then // Itemcode, Description, Artist, Album, // and Price fields are enabled and the // buttons Add, Edit, and Delete are disabled.. else if (source == jbtnAdd) { isNewSong = true;

jtfItemCode.setText(""); jtfDescription.setText(""); jtfArtist.setText(""); jtfAlbum.setText(""); jtfPrice.setText("0");

jtfItemCode.setEnabled(true); jtfDescription.setEnabled(true); jtfArtist.setEnabled(true); jtfAlbum.setEnabled(true); jtfPrice.setEnabled(true);

jbtnAdd.setEnabled(false); jbtnEdit.setEnabled(false); jbtnDelete.setEnabled(false);

jbtnAccept.setEnabled(true); jbtnCancel.setEnabled(true); } // If the user clicks the Edit button, then the // fields Description, Artist, Album, and Price // are enabled and the buttons Add, Delete, // Edit are disabled.the buttons Accept and // Cancel are enabled. else if (source == jbtnEdit) { isNewSong = false;

jtfDescription.setEnabled(true); jtfArtist.setEnabled(true); jtfAlbum.setEnabled(true); jtfPrice.setEnabled(true);

jbtnAdd.setEnabled(false); jbtnEdit.setEnabled(false); jbtnDelete.setEnabled(false);

jbtnAccept.setEnabled(true); jbtnCancel.setEnabled(true); } // if the user clicks the Delete button // the selected song is deleted. else if (source == jbtnDelete) { } // if the user clicks the Accept button then // a new song is added into the comobobox list. else if (source == jbtnAccept) { if (isNewSong) { currentSong = new Song(jtfItemCode.getText(), jtfDescription.getText(), jtfArtist.getText(), jtfAlbum.getText(), Double.parseDouble(jtfPrice.getText()));

songs.add(currentSong); combobox.addItem(currentSong);

combobox.setSelectedIndex(songs.size() - 1); currentSong = (Song) combobox.getSelectedItem(); showSong(currentSong); } else { }

jbtnAccept.setEnabled(false); jbtnCancel.setEnabled(false);

jbtnAdd.setEnabled(true); jbtnEdit.setEnabled(true); jbtnDelete.setEnabled(true); } else if (source == jbtnCancel) { jbtnAccept.setEnabled(false); jbtnCancel.setEnabled(false);

jbtnAdd.setEnabled(true); jbtnEdit.setEnabled(true); jbtnDelete.setEnabled(true); } else // source == jbtnExit { System.exit(0); } }// End of the method actionPerformed().

// Definition of the method showSong(). private void showSong(Song currentSong) { // Display the contents in the fields ItemCode, // Description, Artist, Album, and Price. jtfItemCode.setText(currentSong.getItemCode()); jtfDescription.setText(currentSong.getDescription()); jtfArtist.setText(currentSong.getArtist()); jtfAlbum.setText(currentSong.getAlbum()); jtfPrice.setText("" + currentSong.getPrice()); // Disable the fields. jtfItemCode.setEnabled(false); jtfDescription.setEnabled(false); jtfArtist.setEnabled(false); jtfAlbum.setEnabled(false); jtfPrice.setEnabled(false); }// End of the method showSong().

// Definition of the main() method. public static void main(String[] args) { // Create an object of the ArrayList. ArrayList list = new ArrayList();

Scanner input = new Scanner(System.in); // Prompt the user to enter the database // file name. System.out.print("Enter the database file name:"); // Read the file name. String filename = input.nextLine(); String choice = "";

try {

Scanner infile = new Scanner(new File(filename)); // Read the contents of the File. while (infile.hasNextLine()) { String line = infile.nextLine(); String[] tokens = line.split(","); // Add the songs in the file to the // songs list. Song newSong = new Song(tokens[0], tokens[1], tokens[2], tokens[3], Double.parseDouble(tokens[4])); list.add(newSong); }

} // If the file is not found, then prompt // the user to create a new database. catch (FileNotFoundException e) { System.out.println("The database " + "file does not exist."); System.out.print("Do you want to" + " create a new one? (Y/N): "); choice = input.nextLine(); }

if (choice.charAt(0) == 'Y' || choice.charAt(0) == 'y') { // if the user wants to create a // database, then create a frame. SongDB sdb = new SongDB(list); // Set the title. sdb.setTitle("Team Roster"); // Set the dimensions of the frame. sdb.setSize(400, 350); sdb.setVisible(true); sdb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } else { System.out.println("Thank you"); } } }// End of the main() method.

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part 2 Lnai 8725

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448505, 978-3662448502

More Books

Students also viewed these Databases questions

Question

Explain exothermic and endothermic reactions with examples

Answered: 1 week ago

Question

Write a short note on rancidity and corrosiveness.

Answered: 1 week ago