Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you fix this code? It is important that the user receives a response as to what happened after each button press, so certain the

Can you fix this code? 

It is important that the user receives a response as to what happened after each button press, so certain the result of these in a text area.

4. Make a GUI program, see example below, with JFrame and suitable Swing components where you can:

a. Add a CD discs to an array of CD objects.

b. Change information about CD based on CD ID

c. Remove CD based on CD ID

d. Search by music genre and list all CDs for the searched genre.

e. Search for the artist and list all CDs for the searched artist.

i. Talk about how many records this particular artist has made

f. List data about all CDs in the array of CD objects

g. A sort button that sorts by artist name. And one on production year. Use Arrays.sort and a Comparator class.

My code so far.
  import CDComparator.CDComparator; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Collections; public class CD { private int id; private String artist; private String title; private String genre; private int year; public CD(int id, String artist, String title, String genre, int year) { this.id = id; this.artist = artist; this.title = title; this.genre = genre; this.year = year; } // getters and setters for the properties go here public String getArtist() { return artist; } public void setArtist(String artist) { this.artist = artist; } public int getId() { return id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getGenre() { return genre; } public void setGenre(String genre) { this.genre = genre; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } } public class CDGUI extends JFrame { private JTextArea outputArea; private JTextField idField; private JTextField artistField; private JTextField titleField; private JTextField genreField; private JTextField yearField; private ArrayList cdList; public static void main(String[] args) { CDGUI cdgui = new CDGUI(); } public CDGUI() { // initialize the UI components outputArea = new JTextArea(); idField = new JTextField(); artistField = new JTextField(); titleField = new JTextField(); genreField = new JTextField(); yearField = new JTextField(); // add event listeners to the buttons JButton addButton = new JButton("Add CD"); addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // get the values from the text fields int id = Integer.parseInt(idField.getText()); String artist = artistField.getText(); String title = titleField.getText(); String genre = genreField.getText(); int year = Integer.parseInt(yearField.getText()); // create a new CD object and add it to the array list cdList.add(new CD(id, artist, title, genre, year)); outputArea.setText("CD added successfully"); // clear the text fields idField.setText(""); artistField.setText(""); titleField.setText(""); genreField.setText(""); yearField.setText(""); } }); JButton updateButton = new JButton("Update CD"); updateButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // get the values from the text fields int id = Integer.parseInt(idField.getText()); String artist = artistField.getText(); String title = titleField.getText(); String genre = genreField.getText(); int year = Integer.parseInt(yearField.getText()); // search for the CD object with the specified ID in the array list for (CD cd : cdList) { if (cd.getId() == id) { // update the CD object's properties cd.setArtist(artist); cd.setTitle(title); cd.setGenre(genre); cd.setYear(year); outputArea.setText("CD updated successfully"); // clear the text fields idField.setText(""); artistField.setText(""); titleField.setText(""); genreField.setText(""); yearField.setText(""); return; } } // if the CD object is not found, display an error message outputArea.setText("CD not found"); } }); JButton deleteButton = new JButton("Delete CD"); deleteButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // get the ID from the text field int id = Integer.parseInt(idField.getText()); // search for the CD object with the specified ID in the array list for (CD cd : cdList) { if (cd.getId() == id) { // remove the CD object from the array list cdList.remove(cd); outputArea.setText("CD deleted successfully"); // clear the text field idField.setText(""); return; } } // if the CD object is not found, display an error message outputArea.setText("CD not found"); } }); JButton searchGenreButton = new JButton("Search by Genre"); searchGenreButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // get the genre from the text field String genre = genreField.getText(); // search for CD objects with the specified genre in the array list StringBuilder sb = new StringBuilder(); for (CD cd : cdList) { if (cd.getGenre().equals(genre)) { sb.append(cd.getArtist()).append(" - ").append(cd.getTitle).append(" "); } } // display the search results in the output area if (sb.length() > 0) { outputArea.setText(sb.toString()); } else { outputArea.setText("No CDs found for the specified genre"); } // clear the text field genreField.setText(""); } }); JButton searchArtistButton = new JButton("Search by Artist"); searchArtistButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // get the artist from the text field String artist = artistField.getText(); // search for CD objects with the specified artist in the array list StringBuilder sb = new StringBuilder(); int count = 0; for (CD cd : cdList) { if (cd.getArtist().equals(artist)) { sb.append(cd.getTitle()).append(" "); count++; } } // display the search results in the output area if (count > 0) { sb.insert(0, "Number of records: " + count + " "); outputArea.setText(sb.toString()); } else { outputArea.setText("No CDs found for the specified artist"); } // clear the text field artistField.setText(""); } }); JButton listButton = new JButton("List CDs"); listButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // display a list of all CD objects in the array list in the output area StringBuilder sb = new StringBuilder(); for (CD cd : cdList) { sb.append(cd.getId()).append(". ").append(cd.getArtist()).append(" - ").append(cd.getTitle()).append(" "); } outputArea.setText(sb.toString()); } }); JButton sortArtistButton = new JButton("Sort by Artist"); sortArtistButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // sort the array list of CD objects by artist name using the comparator class Collections.sort(cdList, new CDComparator("artist")); outputArea.setText("CDs sorted by artist name"); } }); JButton sortYearButton = new JButton("Sort by Year"); sortYearButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // sort the array list of CD objects by production year using the comparator class Collections.sort(cdList, new CDComparator("year")); outputArea.setText("CDs sorted by production year"); } }); // add the UI components to the frame add(outputArea); add(idField); add(artistField); add(titleField); add(genreField); add(yearField); add(addButton); add(updateButton); add(deleteButton); add(searchGenreButton); add(searchArtistButton); add(listButton); add(sortArtistButton); add(sortYearButton); // set the layout and size of the frame setLayout(new FlowLayout()); setSize(400, 600); // initialize the array list of CD objects cdList = new ArrayList(); } } 

Can you fix this code.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions