Results 1 to 4 of 4
  1. #1
    ladykrimson is offline Member
    Join Date
    Oct 2010
    Posts
    69
    Rep Power
    0

    Default Inventory Part 4 - Almost done!!!

    Hi there.

    So I am so close to finished on this part, but I am having an issue. Here is the code:

    Java Code:
    import java.util.*;
    import javax.swing.*;
    
    public class InventoryGui extends javax.swing.JFrame {
    
        /** Creates new form JF1 */
        public InventoryGui() {
            initComponents();
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            inventoryGuiPanel = new javax.swing.JPanel();
            inventoryTotalWindow = new javax.swing.JTextField();
            jScrollPane1 = new javax.swing.JScrollPane();
            inventoryListWindow = new javax.swing.JTextArea();
            totalInventoryWindowLabel = new javax.swing.JLabel();
    
                    String sOutPut = "";
    
            BlurayDVD[] blu = new BlurayDVD[6];
    
        	blu[0] = new BlurayDVD("Independence Day", 6, 5.23, 1, "Standard DVD");
            System.out.println(blu);
    
            blu[1] = new BlurayDVD("X-Men", 8, 4.73, 2, "Standard DVD");
            System.out.println(blu);
    
            blu[2] = new BlurayDVD("Jurassic Park", 3, 6.01, 3, "Standard DVD");
            System.out.println(blu);
    
            blu[3] = new BlurayDVD ("My Cousin Vinny", 4, 3.15, 4, "Standard DVD");
            System.out.println(blu);
    
            blu[4] = new BlurayDVD ("The Mist", 3, 6.58, 5, "BluRay DVD");
            System.out.println(blu);
    
            blu[5] = new BlurayDVD ("Independence DaySE", 4, 7.81, 6, "BluRay DVD");
            System.out.println(blu);
    
    
    	ArrayList<BlurayDVD> bluList = new ArrayList<BlurayDVD>();
    
             BlurayDVD b0 = new BlurayDVD ("The Mist", 3, 6.58, 5, "Standard DVD");
             BlurayDVD b1 = new BlurayDVD ("Independence Day", 4, 7.81, 6, "BluRay DVD");
             BlurayDVD b2 = new BlurayDVD ("X-Men", 8, 4.73, 2,"BluRay DVD");
             BlurayDVD b3 = new BlurayDVD ("Jurassic Park", 3, 6.01, 3,"BluRay DVD");
             BlurayDVD b4 = new BlurayDVD ("My Cousin Vinny", 4, 3.15, 4, "Standard DVD");
             BlurayDVD b5 = new BlurayDVD("Independence DaySE", 4, 7.81, 6, "BluRay DVD");
    
              bluList.add(b0);
              bluList.add(b1);
              bluList.add(b2);
              bluList.add(b3);
              bluList.add(b4);
              bluList.add(b5);
    
                  Collections.sort(bluList, new Comparator<BlurayDVD>()
                  {
                        public int compare(BlurayDVD o1, BlurayDVD o2)
                        {
                            BlurayDVD p1 = (BlurayDVD) o1;
                            BlurayDVD p2 = (BlurayDVD) o2;
                            return p1.getDvdTitle().compareToIgnoreCase(p2.getDvdTitle());
                        }
    
                  });
    
                [B]  for (int i = 0; i < bluList._______; i++)
                {
    
                     sOutPut += String.format(" %d\t%18s\t%d\t $%.2f\t$%.2f\t$%.2f\t%s\n", bluList(i).dvdItem, bluList(i).getDvdTitle(),  bluList(i).getDvdStock(), bluList(i).getDvdPrice(), bluList(i).value(), bluList(i).getRestockFee(), bluList(i).getCategory());
    [/B]
                     System.out.printf(" %d\t%18s\t%d\t $%.2f\t$%.2f\t$%.2f\t%s\n", blu[i].dvdItem, blu[i].getDvdTitle(),  blu[i].getDvdStock(), blu[i].getDvdPrice(),  blu[i].value(), blu[i].getRestockFee(), blu[i].getCategory());
                     System.out.println(); // prints a blank line
                }
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("DVD Inventory");
            setBackground(new java.awt.Color(255, 255, 255));
            setFont(new java.awt.Font("Verdana", 1, 14)); // NOI18N
            addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowActivated(java.awt.event.WindowEvent evt) {
                    formWindowActivated(evt);
                }
            });
            getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.LINE_AXIS));
    
            inventoryTotalWindow.setBackground(new java.awt.Color(255, 255, 255));
            inventoryTotalWindow.setEditable(false);
    
            inventoryTotalWindow.setText(String.format("$%.2f",b1.getTotal(blu)));
    
            inventoryListWindow.setColumns(20);
            inventoryListWindow.setRows(5);
            jScrollPane1.setViewportView(inventoryListWindow);
    
            inventoryListWindow.setText(sOutPut);
    
            totalInventoryWindowLabel.setFont(new java.awt.Font("Verdana", 1, 13)); // NOI18N
            totalInventoryWindowLabel.setText("The total value of the inventory is");
    
            javax.swing.GroupLayout inventoryGuiPanelLayout = new javax.swing.GroupLayout(inventoryGuiPanel);
            inventoryGuiPanel.setLayout(inventoryGuiPanelLayout);
            inventoryGuiPanelLayout.setHorizontalGroup(
                inventoryGuiPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(inventoryGuiPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(inventoryGuiPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 553, Short.MAX_VALUE)
                        .addGroup(inventoryGuiPanelLayout.createSequentialGroup()
                            .addComponent(totalInventoryWindowLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(inventoryTotalWindow, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap())
            );
            inventoryGuiPanelLayout.setVerticalGroup(
                inventoryGuiPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(inventoryGuiPanelLayout.createSequentialGroup()
                    .addGap(21, 21, 21)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(28, 28, 28)
                    .addGroup(inventoryGuiPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(inventoryTotalWindow, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(totalInventoryWindowLabel))
                    .addContainerGap(29, Short.MAX_VALUE))
            );
    
            getContentPane().add(inventoryGuiPanel);
    
            pack();
        }// </editor-fold>
    
        private void formWindowActivated(java.awt.event.WindowEvent evt) {
            // TODO add your handling code here:
        }
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new InventoryGui().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify
        private javax.swing.JPanel inventoryGuiPanel;
        private javax.swing.JTextArea inventoryListWindow;
        private javax.swing.JTextField inventoryTotalWindow;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JLabel totalInventoryWindowLabel;
        // End of variables declaration
    
    }
    
    class DVD
    {
        String dvdTitle;
        int dvdStock;
        double dvdPrice;
        int dvdItem;
        double titleValue;
        double totalValue;
        String dvdCategory;
    
        public DVD(String title, int stock, double price, int item, String category)
        {
    
            dvdTitle = title;
            dvdStock = stock;
            dvdPrice = price;
            dvdItem  = item;
            dvdCategory = category;
        } //end five-argument constructor
    
    	// set DVD title
        public void setDvdTitle(String title)
        {
            dvdTitle = title;
        } //end method  setDvdTitle
    
        //return DVD title
        public String getDvdTitle()
        {
            return dvdTitle;
        } //end method getDvdTitle
    
        //set number if DVDs in stock
        public void setDvdStock(int stock)
        {
            dvdStock = stock;
        } //end method setDvdStock
    
        //retrieve the number of DVD's in stock
        public int getDvdStock()
        {
            return dvdStock;
        } //end method getDvdStock
    
        // set the price of the title
        public void setDvdPrice(double price)
        {
            dvdPrice = price;
        } //end method setDvdPrice
    
        //retrieve the price of this title
        public double getDvdPrice()
        {
            return dvdPrice;
        } //end method getDvdPrice
    
        // set the DVD item number
        public void setDvdItem(int item)
        {
            dvdItem = item;
        } //end method setDvdItem
    
        //retrieve the DVD item number
        public int getDvdItem()
        {
            return dvdItem;
        } //end  method getDvdItem
    
        //	calculate inventory value for each title and cumulatively calculate the total value of the entire inventory
        public double value()
        {
            titleValue = dvdPrice * dvdStock;
            totalValue = titleValue + totalValue;
            return titleValue;
    
        } //end method value
    
       public  double getTotal(BlurayDVD[] blu)
        {
             for (int i = 0; i < blu.length; i++)
    	{
    		totalValue += blu[i].value();
    	}
    	return totalValue;
        } // end method getTotal
    
    
    } //end class DVD
    
    // subclass BluRayDVD
    class BlurayDVD extends DVD
    {
    	// adds one field
    	double restockFee;
    
    	// constructor for BlurayDVD
    	public BlurayDVD(String title, int stock, double price, int item, String category)
    	{
    		// add these from superclass DVD
    		super(title, stock, price, item, category);
    	}
    
    	// method to set the category
    	public void setCategory(String category)
    	{
    		dvdCategory = category;
    	} // end method setCategory
    
    	// method to retrieve the category
    	public String getCategory()
    	{
    		return dvdCategory;
    	} // end method getCategory
    
        //	calculate inventory value for each title and cumulatively calculate the total value of the entire inventory
        @Override
        public double value()
        {
        	double initialTitleValue;
    
            initialTitleValue = (dvdPrice * dvdStock); // initial value
            restockFee = (initialTitleValue * .05); // calculate restocking fee
            titleValue = initialTitleValue + restockFee; // add restock fee to the initial value
            totalValue = titleValue + totalValue; // add to the cumulative value
            return titleValue;
        } // end method value
    
        // retrieve the restocking fee
        public double getRestockFee()
        {
        	return restockFee;
        } // end method getRestockFee
    
    } // end subclass BlurayDVD
    The part that I made in bold font is the problem. I am trying to get the program to display the bluList sorted in alphabetical order. I have the sorting all coded out, but now I am unsure what to put in the "for" loop since "length" does not work.

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Check out the API for the List class. There should be a method in there that is similar to length for arrays.
    List (Java Platform SE 6)

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    ladykrimson is offline Member
    Join Date
    Oct 2010
    Posts
    69
    Rep Power
    0

    Default

    Thank you. Here was the problem:

    When I used size, I would get errors for each of the items.

    Java Code:
                  for (int i = 0; i < bluList.size(); i++)
                {
    
                     sOutPut += String.format(" %d\t%18s\t%d\t $%.2f\t$%.2f\t$%.2f\t%s\n", [B][COLOR="Red"]bluList[/COLOR][/B](i).dvdItem, [B][COLOR="Red"]bluList[/COLOR][/B](i).getDvdTitle(),  [B][COLOR="Red"]bluList[/COLOR][/B](i).getDvdStock(), [B][COLOR="Red"]bluList[/COLOR][/B](i).getDvdPrice(), [B][COLOR="Red"]bluList[/COLOR][/B](i).value(), [B][COLOR="Red"]bluList[/COLOR][/B](i).getRestockFee(), [B][COLOR="Red"]bluList[/COLOR][/B](i).getCategory());
    
                     System.out.printf(" %d\t%18s\t%d\t $%.2f\t$%.2f\t$%.2f\t%s\n", blu[i].dvdItem, blu[i].getDvdTitle(),  blu[i].getDvdStock(), blu[i].getDvdPrice(),  blu[i].value(), blu[i].getRestockFee(), blu[i].getCategory());
                     System.out.println(); // prints a blank line
                }
    I had to insert the "get" between each one.

    Java Code:
                  for (int i = 0; i < bluList.size(); i++)
                {
                     sOutPut += String.format(" %d\t%18s\t%d\t $%.2f\t$%.2f\t$%.2f\t%s\n", bluList.get(i).dvdItem, bluList.get(i).getDvdTitle(),  bluList.get(i).getDvdStock(), bluList.get(i).getDvdPrice(), bluList.get(i).value(), bluList.get(i).getRestockFee(), bluList.get(i).getCategory());
    
                     System.out.printf(" %d\t%18s\t%d\t $%.2f\t$%.2f\t$%.2f\t%s\n", blu[i].dvdItem, blu[i].getDvdTitle(),  blu[i].getDvdStock(), blu[i].getDvdPrice(),  blu[i].value(), blu[i].getRestockFee(), blu[i].getCategory());
                     System.out.println(); // prints a blank line
                }
    I am having one more minor issue. I specifically coded the output so that each of the titles would be 18 character spaces long. It does this for the first four items, but the last two items won't display that way.

    Any ideas why?
    Last edited by ladykrimson; 11-09-2010 at 06:30 PM.

Similar Threads

  1. Inventory Part 4 assistance
    By ladykrimson in forum New To Java
    Replies: 26
    Last Post: 11-05-2010, 09:56 PM
  2. Help with inventory program part 5 PLEASE!
    By Exether in forum New To Java
    Replies: 2
    Last Post: 08-09-2010, 06:25 AM
  3. Inventory Program Part 2 of 6
    By tlouvierre in forum New To Java
    Replies: 2
    Last Post: 05-28-2009, 01:30 AM
  4. Inventory Program Part 3 ~ please help!
    By marMcD in forum New To Java
    Replies: 13
    Last Post: 02-25-2009, 05:57 AM
  5. Inventory part 2 help please
    By badness in forum New To Java
    Replies: 1
    Last Post: 12-12-2007, 07:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •