warlockune 0 Newbie Poster

We are supposed to make a frame eventually that shows different buttons corresponding to methods, allowing for someone to switch, add, remove, find (using object name find number in array), and get(use number position in array to find other pieces of data), and readData, allowing someone to read data from a file into the array instead of inputting it a piece at a time. This is supposed to "resemble" a CD collection, and thus has four instance fields.

//class CD, to form the base object used for the array and rest of the project
public class CD
{
    //The purpose of this class is to create the object of the CD with a title, category,
    //rating, artist
    private String artist;
    private String title;
    private String category;
    private String rating;

    public CD(String inputTitle, String inputCategory, String inputRating, String inputArtist)
    {
        //constructor
        artist = inputArtist;
        title = inputTitle;
        category = inputCategory;
        rating = inputRating;
    }

    public CD()
    {
    }
    //methods to retrieve information
    public String getArtist()
    {
        return artist;
    }
    public String getTitle()
    {
        return title;
    }
    public String getCategory()
    {
        return category;
    }
    public String getRating()
    {
        return rating;
    }
    /**public String void labelCD(String newTitle, String newCategory, String newRating, String newArtist)
    {
        title = newTitle;
        category = newRating;
        rating = newRating;
        artist = newArtist;

    }
    */
}    


//[B][U]Exception classes[/U][/B]
public class ListIndexOutOfBoundsException extends IndexOutOfBoundsException
    {
        public ListIndexOutOfBoundsException(String s)
        {
             super(s);
        }
    }

public class ListException extends RuntimeException
    {
    public ListException(String s)
    {
        super(s);
    }
}

//[B][U]class collection, where the array is better defined, and the methods are defined, and collections second part[/U][/B]
import java.io.*;
import java.util.Scanner;
public class Collection 
{
   // public does not need to see this
    private File fileName;
    private final int MAX_LIST = 30;
    private CD[] album;
    private Scanner input;
    private int numItems=0;
    private CD compactDisk1;
    private CD compactDisk2;

     public void Collection() 
   {
       album = new CD[MAX_LIST];
       numItems = 0;
    }// end default constructor


    public void add(int index, CD cd)
    throws IndexOutOfBoundsException
    {
        if (numItems>MAX_LIST)
        {
            throw new ListException("ListException on add");
        }
        if (index>=1 && index<= numItems+1)
        {
            for (int pos = numItems; pos>= index; pos--)
            {
                album[pos++]= album[pos];
            }
            album[index]=cd;
            numItems++;
        }
        else{
            throw new ListIndexOutOfBoundsException("ListIndexOutOfBoundsException on add");
        }
    }


    public boolean isEmpty() 
    {
        return (numItems == 0);
    }

    public int size() 
    {
        return numItems;
    }

    public void removeAll()

    {
    album = new CD[MAX_LIST];
    numItems = 0;
    }


    public CD get (int index)
                  throws ListIndexOutOfBoundsException
                  {
                      if (index >= 1 && index <= numItems)
                      {
                          return album[translate(index)];
                        }

                        else
                        {
                            throw new ListIndexOutOfBoundsException( "ListIndexOutOfBoundsException on get");

                        }
                    }


    public void remove(int index)
                throws ListIndexOutOfBoundsException
         {
             if (index >= 0 && index <= numItems )
             {
                 //delete items by shifting all items at positions > index toward begining of the list
                 for (int pos = index+1; pos <=size(); pos++)
                 {
                     album[translate(pos-1)] = album[translate(pos)];
              }
            }
          }
        }



//[B][U]collection part 2[/U][/B]
import java.io.*;
import java.util.Scanner;
public class CollectionPart2 
{
   // public does not need to see this
    private File fileName;
    private final int MAX_LIST = 30;
    private CD[] album;
    private Scanner input;
    private int numItems=0;
    private CD compactDisk1;
    private CD compactDisk2;




    public void switchPosition(int index1, int index2)
     {


         compactDisk1 = album[index1];
         compactDisk2 = album[index2];
         album.remove(index1);
         album.remove(index2);
         album.add(index1, compactDisk2);
         album.add(index2, compactDisk1);
      }

      public int findPosition(CD superCd)
      {

              for (int m = 0; m <= numItems; m++)
              {
                  album.get(m) = superCd;
                  if ( superCd == cd)
                  {
                      return m+1;
                  }
               }
            }






     private int translate(int position)
          {
              return position -1;
          }        
}

        //[B][U]Our viewer[/U][/B]
import javax.swing.JFrame;
import java.io.*;
// this program tests the CDFrame

public class CDFrameViewer
{
   // this is our main
     public static void main(String[]args) throws IOException
    {
        // make instance of CDFrame
        JFrame frame = new CDFrame();
        //required for interface
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}


//[B][U]our actual frame setup, based off another  program done, so know it requires a bit of tweaking, but not exactly sure in which directions and how much[/U][/B]

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import java.io.*;

// The purpose of this class is to create the input window interface.
// Needs to extends JFrame so we can make the parameters.
public class CDFrame extends JFrame 
{
    //make instance of Collection class.
    Collection C = new Collection();

    //throws IOException to catch illegal input/output
    public CDFrame() throws IOException
    {
       setSize(FRAME_WIDTH,FRAME_HEIGHT);
       //creates panels that perform methods.
       JPanel addCDPanel = addCD();
       JPanel displayCDPanel = displayCD();
       //adds border
       displayCDPanel.setBorder(new TitledBorder(new EtchedBorder(), "Your CDs"));
       add(displayCDPanel, BorderLayout.CENTER);
       //adds border
       addCDPanel.setBorder(new TitledBorder(new EtchedBorder(), "Add CDs"));
       add(addCDPanel, BorderLayout.WEST);
       displayCollection();
       //C.readData();
    }
    public JPanel displayCD()
    {

      //makes the CD display window and sets the dimensions
        JPanel panel2 = new JPanel();
        panel2.setPreferredSize(new Dimension(500,500));
        //panel.setLocation(100,100);
        panel2.setBackground(Color.WHITE);
        box5.setLineWrap(true);
        box5.setWrapStyleWord(true);
        box5.setEditable(false);
        panel2.add(box5);
        return panel2;
    }

    public JPanel addCD() throws IOException
   // throws IOException to allow illegal input/output
    {
        //needed to access data for graphical interface
        class addNewCD implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            try
            {
            // This adds the CD to the viewing window
            String artistTemp = box.getText();
            String titleTemp = box2.getText();
            String categoryTemp = box3.getText();
            String ratingTemp = box4.getText();
            C.addCD(titleTemp, artistTemp, categoryTemp,ratingTemp);
            //readDataOntoScreen();
            JPanel displayCDPanel = displayCD();
            add(displayCDPanel, BorderLayout.CENTER);
            displayCollection();
            clearInput();
            }
            // try catch to bypass illegal values
            catch (NumberFormatException e)
            {
                System.out.println(e);    
                label5.setText("One or more fields are invalid.");
                label6.setText("Check your input.");
                System.out.println("NOES!");
            }

           // C.writeData();


        }
    }
        ActionListener listener = new addNewCD();
        button.addActionListener(listener);
   // creates interface
        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(260,50));
        //panel.setLocation(100,100);
        panel.setBackground(Color.WHITE);
    //creates interface by adding labels and text boxes    
        panel.add(label);
        panel.add(box);
        panel.add(label2);
        panel.add(box2);
        panel.add(label3);
        panel.add(box3);
        panel.add(label4);
        panel.add(box4);
        panel.add(button);
        panel.add(label5);
        panel.add(label6);

        return panel;
    }
   // method to display cdcollection 
    public void displayCollection()
    {
       String tempString = "";
        for (int x = 0; x < C.getCount(); x++)
        {
            CD mike = C.getData(x);
            //writes data in order that user is instructed to input it
            tempString += "Artist: " + mike.getArtist() + "\nTitle: " + mike.getTitle() + "\nCategory: " + mike.getCategory() + "\nRating: " + mike.getRating() + "\n\n"; 
        }
        System.out.println(tempString);
        box5.append(tempString);

    }
  // this is created to display the current CD collection on the viewing window.  
    public void readDataOntoScreen()
    {
        String str;
       // BufferedReader br;
        try
        {
            // our file will be read from C:\\file.txt
            File f = new File("C:\\file.txt");
            br = new BufferedReader(new FileReader(f));
            while((str = br.readLine()) != null)
            {
                box5.append(str);
            }
        }
        // add catch to state wheather or not file exists
        catch (FileNotFoundException e)
        {
            System.out.println(e);
        }

        catch (IOException ioe)
        {
            System.out.println(ioe);
        }
        finally
        {
            try
            {
                //required. you must close a FileReader
                br.close();
            }
            catch (Exception ignored)
            {
            }
        }
    public void readData()
    {
        while (input.hasNext())
        {
            String title = input.next();
            String category = input.next();
            String rating = input.next();
            String artist = input.next();
            CD myCD = new CD(title, category, rating, artist);
            album.add(myCD);
        }
    }
      // this resets your data fields every time      
    public void clearInput()
    {
            box.setText(null);
            box2.setText(null);
            box3.setText(null);
            box4.setText(null);
            label5.setText(null);
            label6.setText(null);
    }

       //this creates all of our labels and text fields. These are where the user interacts
        JLabel label = new JLabel ("Enter Artist name:");
        final JTextField box = new JTextField(null,20);       
        JLabel label2 = new JLabel ("Enter CD Title:");
        final JTextField box2 = new JTextField(null,20);
        JLabel label3 = new JLabel ("Enter Category:");
        final JTextField box3 = new JTextField(null,20);
        JLabel label4 = new JLabel ("Enter your rating for CD between 1 and 5:");
        JLabel label5 = new JLabel();
        JLabel label6 = new JLabel();
        final JTextField box4 = new JTextField(null,20);
        // button to implement methods
        JButton button = new JButton("Add CD");
        final JTextArea box5 = new JTextArea(5,50);

        // intilize frame
        public BufferedReader br;
        final int FRAME_WIDTH = 500;
        final int FRAME_HEIGHT = 500;
}

thank you for any advice you can give, can really use it.