I am writing a program that has a few requirements.

Here is what it looks like the picture that I attached.

The first button is used to add (append) a record to a text file containing MP3 records. When an MP3 is added to the file, all text fields should be cleared and the new record entered should be displayed by itself in the text area: If a field is missing inform the user. Add AT LEAST six MP3 records. The data must be written to the text file using a “:” as a separator: artist name:song name:album name:tract length(in seconds such as 435)

The second button displays all MP3 records from the file sorted by artist name. Store the MP3s in an ArrayList. The MP3 records should be displayed in sorted order by artist name using a sort method that is modified to work with MP3 objects. You should use compareTo to compare the MP3 artist names for sorting. You should call the toString method to display the MP3 records. artist name, song name, album name, 7:15

The third button will find and display a record if the user enters the song name. If the song cannot be found then inform the user.

The delete button will delete a record from the file based on the song name. Read the file data into an ArrayList. If the record to delete is found, do not place it in the ArrayList. After the records have been read in, if the delete record was found then close the file and reopen it in write mode. Use a Yes/No dialog box to confirm the delete. Write the records back into the file. If the record was not found, inform the user, close the file, and discard the ArrayList.

Use must use exception handling for the file I/O and for any other possible exceptions.

You should NOT leave the data file open.Open it ONLY when you need to access the data and close it immediately when you are finished.

Here is the MP3 class

public class MP3 {
    private String artist;
    private String song;
    private String album;
    private int trackLength;

    public MP3(String artistName, String songName, String albumName,
            int trackLeng) {
        setArtist(artistName);
        setSong(songName);
        setAlbum(albumName);
        setTrackLength(trackLeng);
    }

    public void setArtist(String artistName) {
        artist = artistName;
    }

    public String getArtist() {
        return artist;
    }

    public void setSong(String songName) {
        song = songName;
    }

    public String getSong() {
        return song;
    }

    public void setAlbum(String albumName) {
        album = albumName;
    }

    public String getAlbum() {
        return album;
    }

    public void setTrackLength(int trackLeng) {
       trackLength = (trackLeng > 0) ? trackLeng : 5 ;
    }

    public int getTrackLength() {
        return trackLength;
    }

    public String toString() {
        return String.format("%s, %s, %s, %d : %d", getArtist(), getSong(),
                getAlbum(), getTrackLength() / 60, getTrackLength() % 60);
    }
}


here is the partially done MP3Manager class, I need help on sorting this by artist name, and correctly fuctioned on writing in the "Record.txt" file and reading that file. In addition, I also need help to use dialog box to confirm the deleting information

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;


public class MP3Manager extends JFrame{
  
    private JButton addMP3Button, displayMP3sButton, findMP3Button, deleteMP3Button;
    private JLabel artistLabel, songLabel, albumLabel, trackLengthLabel;
    private JTextField artistField, songField, albumField, trackLengthField;
    private JPanel fieldsPanel;
    private JTextArea textArea;
    private ArrayList <MP3> mp3List;
   

    public MP3Manager(){
   
        super("MP3 Manager");
       
        fieldsPanel = new JPanel(new GridLayout(6, 2, 5, 5));
        mp3List = new ArrayList<MP3>();
       
        artistLabel = new JLabel("Artist Name ", JLabel.RIGHT);
        artistField = new JTextField();
        artistField.setHorizontalAlignment(JTextField.CENTER);
        fieldsPanel.add(artistLabel);
        fieldsPanel.add(artistField);
      
        songLabel = new JLabel("Song Title ", JLabel.RIGHT);
        songField = new JTextField();
        songField.setHorizontalAlignment(JTextField.CENTER);
        fieldsPanel.add(songLabel);
        fieldsPanel.add(songField);
       
        albumLabel = new JLabel("Album Name ", JLabel.RIGHT);
        albumField = new JTextField();
        albumField.setHorizontalAlignment(JTextField.CENTER);
        fieldsPanel.add(albumLabel);
        fieldsPanel.add(albumField);
       
        trackLengthLabel = new JLabel("Track Length (in seconds) ", JLabel.RIGHT);
        trackLengthField = new JTextField("", 16);
        trackLengthField.setHorizontalAlignment(JTextField.CENTER);
        fieldsPanel.add(trackLengthLabel);
        fieldsPanel.add(trackLengthField);
       
        addMP3Button = new JButton(" Add MP3 ");
        displayMP3sButton = new JButton(" Display MP3s ");
        findMP3Button = new JButton(" Find MP3 ");
        deleteMP3Button = new JButton(" Delete MP3 ");
       
        fieldsPanel.add(addMP3Button);
        fieldsPanel.add(displayMP3sButton);
        fieldsPanel.add(findMP3Button);
        fieldsPanel.add(deleteMP3Button);
       
        textArea = new JTextArea();
        textArea.setEditable(false);
        add(fieldsPanel, BorderLayout.NORTH);
        add(textArea, BorderLayout.CENTER);
       
        ButtonHandler handler = new ButtonHandler();
        addMP3Button.addActionListener(handler);
        displayMP3sButton.addActionListener(handler);
        findMP3Button.addActionListener(handler);
        deleteMP3Button.addActionListener(handler);
    }
   
    private class ButtonHandler implements ActionListener{

        public void actionPerformed(ActionEvent event){
           
            int length = 0;
            String artist = artistField.getText();
            String album = albumField.getText();
            String song = songField.getText();
           
            if(event.getSource() == addMP3Button){          

                try{
                    length = Integer.parseInt(trackLengthField.getText());
                }catch(Exception e){
                    textArea.setText("Track Length must be integer.");
                    return;
                }
               
                if(length <= 0){
                    textArea.setText("Track Length must be positive.");
                    return;
                }
               
                if(artist.trim().length() == 0){
                    textArea.setText("Please fill out complete infotmation. \nArtist Field must be not empty.");
                    return;
                }
               
                if(album.trim().length() == 0){
                    textArea.setText("Please fill out complete infotmation. \nAblum Field must be not empty.");
                    return;
                }
               
                if(song.trim().length() == 0){
                    textArea.setText("Please fill out complete infotmation. \nSong Field must be not empty.");
                    return;
                }
               
                MP3 songs = new MP3(artist, song, album, length);
                mp3List.add(songs);
                textArea.setText("Song added successfully");
                artistField.setText("");
                songField.setText("");
                albumField.setText("");
                trackLengthField.setText("");
               
                try{
                    File file = new File("Record.txt");
                    PrintWriter pw = new PrintWriter(new FileWriter(file, true));
                    for(MP3 mp3 : mp3List)
                        pw.print(mp3.getArtist() + ":" + mp3.getSong() + ":" + mp3.getAlbum() + ":" + mp3.getTrackLength() + "\n");
                    pw.close();
                }catch(IOException e){
                    System.out.println("Error creating file" + e);
                }
            }
           
            if(event.getSource() == displayMP3sButton){
               
                File file = new File("Record.txt");
                StringBuffer contents = new StringBuffer();
                BufferedReader reader = null;
               
                try{
                    reader = new BufferedReader(new FileReader(file));
                    String text = null;
                   
                    while((text = reader.readLine()) != null){
                        contents.append(text).append(System.getProperty("line.separator"));
                    }
                }catch(FileNotFoundException e){
                    e.printStackTrace();
                }catch(IOException e){
                    e.printStackTrace();
                }finally{
                    try{
                        if(reader != null){
                            reader.close();
                        }
                    }catch(IOException e){
                    e.printStackTrace();
                    }
                }
               
                textArea.setText("");
                textArea.append(contents.toString() + "\n");
            }
           
            if(event.getSource() == findMP3Button){
                if(song.equals(""))
                    textArea.setText("Please enter the song name");
                else{
                    boolean found = false;
                    for(MP3 songs : mp3List){
                        if(songs.getSong().equalsIgnoreCase(song)){
                            textArea.setText(songs.toString() + "\n");
                            found = true;
                            break;
                        }
                    }
                   
                    if(!found)
                        textArea.setText("Record cannot be found");
                }
            }
           
            if(event.getSource() == deleteMP3Button){
                if(song.equals(""))
                    textArea.setText("Please enter the song name");
                else{
                    boolean delete = false;
                    for(MP3 songs : mp3List){
                        if(songs.getSong().equalsIgnoreCase(song)){
                            mp3List.remove(songs);
                            textArea.setText(songs.toString() + "\n");
                            delete = true;
                            break;
                        }
                    }
                   
                    if(!delete)
                        textArea.setText("Record cannot be found");
                }
            }
        }
    }
}

And here is the MP3ManagerTest class

import javax.swing.*;
import java.awt.*;


public class MP3ManagerTest{
 
    public static void main(String[] args) {
        MP3Manager manager = new MP3Manager();
        manager.setSize(400, 500);
        manager.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        manager.setVisible(true);
    }
}

I am truly appreciated any contribution, If anyone could offer all the help that I need.

Thanks in advance.

Edited 5 Years Ago by Danielhuo: n/a

Attachments 1.png 120.93 KB

Do you have any specific questions or problems?
If so please ask them.
If you are getting errors, please copy and paste the full text of the messages here.

Do you have any specific questions or problems?
If so please ask them.
If you are getting errors, please copy and paste the full text of the messages here.

I high light the question. You should be able to see it if you read carefully.
It is I need help on sorting this by artist name, and correctly fuctioned on writing in the "Record.txt" file and reading that file. In addition, I also need help to use dialog box to confirm the deleting information

I high light the question. You should be able to see it if you read carefully.

There were 6 red sections in your post. After I read three of them I saw that they were the statement of your assignment so I stopped reading the red sections and looked at the end of the post for your questions. There were no questions there so I asked.

Work on one problem at a time
Where are the artist names? If they are in a collection, the Collection class has sort methods that should be helpful.

The artist name is in ArrayList, which contains MP3 objects. The thing is I cannot use any built in sort method, I have to write simple sort method.

OK, if you need to write a simple sort. I'd suggest that you write a small, simple program that loads an ArrayList with some objects and then calls a method to sort them. Write the method and test it.
Print the arraylist before the sort and after the sort to see if it worked.
When you get the method to work, you can copy it into your larger program and test it there.

Edited 5 Years Ago by NormR1: n/a

This article has been dead for over six months. Start a new discussion instead.