Page 2 of 2 FirstFirst 12
Results 21 to 29 of 29
  1. #21
    ladykrimson is offline Member
    Join Date
    Oct 2010
    Posts
    69
    Rep Power
    0

    Default

    Oh, I know how to write the method for sorting...I am just having a problem adding elements from the array to the list.

    I don't understand how to get each of my DVD titles on an Array list so that I can write a method to sort them. (Writing the method to sort them is one of the requirements.)

    Why doesn't this work?

    Java Code:
    package inventory;
    
    import java.util.*;
    import java.awt.List;
    import java.util.ArrayList;
    import java.util.Arrays;
    
    public class Inventory 
    {
    	
        public static void main(String args []) 
        {   	  	
        	DVD[] dvd = new DVD[4];
                             
        	dvd[0] = new DVD("Independence Day", 6, 5.23, 1);      
            System.out.println(dvd);
             
            dvd[1]  = new DVD ("X-Men", 8, 4.73, 2);         
            System.out.println(dvd);
             
            dvd[2] = new DVD ("Jurassic Park", 3, 6.01, 3);
            System.out.println(dvd);
             
            dvd[3] = new DVD ("My Cousin Vinny", 4, 3.15, 4);
            System.out.println(dvd);
                   
    		double total = 0;
    		
    		ArrayList<DVD> l1 = new ArrayList<DVD>();
    					
    		// loop to repeat for each dvd title in inventory
    		for (int i = 0; i < dvd.length; i++)
    		{
                System.out.println("Product Title is " + dvd[i].getDvdTitle());                    
                System.out.println("The number of units in stock is " + dvd[i].getDvdStock());
                System.out.println("The price of each DVD is $" + dvd[i].getDvdPrice());
                System.out.println("The item number is " + dvd[i].getDvdItem());
                System.out.println("The value of the title is $" + dvd[i].value());
                total += dvd[i].getTotal(); // adds the total value of all inventory
                System.out.println(); // prints a blank line
               [COLOR="Red"][B] l1.add(dvd[i].getDvdTitle());[/B][/COLOR]
            }
            
            System.out.println("The total value of the entire inventory is $" + total);
    
    }
    }// end class Inventory
    
    class DVD 
    {
        String dvdTitle;
        int dvdStock;
        double dvdPrice;
        int dvdItem;
    	double total;
        double titleValue;
        double totalValue;
        
     
        public DVD(String title, int stock, double price, int item) 
        {
        	
    		dvdTitle = title;
            dvdStock = stock;
            dvdPrice = price;
            dvdItem  = item;
        } //end four-argument constructor
    
    	// set DVD name
        public void setDvdTitle(String title) 
        {
            dvdTitle = title;
        } //end method  setDvdTitle
     
        //return DVD Title
        public String getDvdTitle() 
        {
            return dvdTitle;
        } //end method getDvdTitle
     
        //set DVD Stock
        public void setDvdStock(int stock) 
        {
            dvdStock = stock;
        } //end method setDvdStock
     
        //return DvdStock
        public int getDvdStock() 
        {
            return dvdStock;
        } //end method getDvdStock
     
        public void setDvdPrice(double price) 
        {
            dvdPrice = price;
        } //end method setDvdPrice
     
        //return dvdPrice
        public double getDvdPrice() 
        {
            return dvdPrice;
        } //end method getDvdPrice
     
        public void setDvdItem(int item) 
        {
            dvdItem = item;
        } //end method setdvdItem
     
        //return DVD item
        public int getDvdItem() 
        {
            return dvdItem;
        } //end  method getDvdItem
     
        //	calculate inventory value for each title
        public double value() 
        {
            titleValue = dvdPrice * dvdStock;
            totalValue = titleValue + totalValue;
            return dvdPrice * dvdStock;
    
        } //end method value
        
        // calculate total inventory value
        public double getTotal()
        {
             return totalValue;
        } // end method totalValue
           
    } //end class DVD
    It says "The method add(DVD) in the type ArrayList<DVD> is not applicable for the arguments (String)"
    Last edited by ladykrimson; 10-31-2010 at 12:32 AM.

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

    Default

    Nevermind! I found it!

    *Cue the "Hallelujah" music*

  3. #23
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Congrats! :)
    Sincerely, Joshua Green
    Please REP if I help :)

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

    Default

    OK, I am almost done with part three of my program, but I have a small issue. One of the calculations is resulting in 0, which is the wrong result. Here is the code:

    Java Code:
    package inventoryp3;
    import java.util.*;
    
    
    public class Inventory 
    {
    	
        public static void main(String args []) 
        {   	  	
        	DVD[] dvd = new DVD[4];
        	BlurayDVD[] blu = new BlurayDVD[2];
                             
        	dvd[0] = new DVD("Independence Day", 6, 5.23, 1);      
            System.out.println(dvd);
             
            dvd[1]  = new DVD ("X-Men", 8, 4.73, 2);         
            System.out.println(dvd);
             
            dvd[2] = new DVD ("Jurassic Park", 3, 6.01, 3);
            System.out.println(dvd);
             
            dvd[3] = new DVD ("My Cousin Vinny", 4, 3.15, 4);
            System.out.println(dvd);
            
            blu[0] = new BlurayDVD ("The Mist", 3, 6.58, 5, "BluRay DVD");
            System.out.println(blu);
            
            blu[1] = new BlurayDVD ("Independence Day", 4, 7.81, 6, "BluRay DVD");
            System.out.println(blu);
            
    		double total = 0;
    		
    		ArrayList<String> l1 = new ArrayList<String>();
    				
    		// loop to repeat for each dvd title in inventory
    		for (int i = 0; i < dvd.length; i++)
    		{
                System.out.println("Product Title is " + dvd[i].getDvdTitle());                    
                System.out.println("The number of units in stock is " + dvd[i].getDvdStock());
                System.out.println("The price of each DVD is $" + dvd[i].getDvdPrice());
                System.out.println("The item number is " + dvd[i].getDvdItem());
                System.out.println("The value of the title is $" + dvd[i].value());
                total += dvd[i].getTotal(); // adds the total value of all inventory
                System.out.println(); // prints a blank line
                l1.add(dvd[i].getDvdTitle());
    
    		}
    		for (int i = 0; i < blu.length; i++)
    		{
                System.out.println("Product Title is " + blu[i].getDvdTitle() + ".");   
                System.out.println("The number of units in stock is " + blu[i].getDvdStock() + ".");
                System.out.println("The price of each DVD is $" + blu[i].getDvdPrice() + ".");
                System.out.println("The item number is " + blu[i].getDvdItem() + ".");
                System.out.println("The value of the title is $" + blu[i].value() + ".");
                System.out.println("This is a " + blu[i].getCategory() + ".");
                System.out.println("The restock fee for this title is $" + blu[i].getRestockFee());
                total += blu[i].getTotal(); // adds the total value of all inventory
                System.out.println(); // prints a blank line
                l1.add(blu[i].getDvdTitle());
    		}
    		
            System.out.println("The total value of the entire inventory is $" + total);
            Collections.sort(l1); 
            System.out.println(l1);
            
    }
    }// end class Inventory
    
    class DVD 
    {
        String dvdTitle;
        int dvdStock;
        double dvdPrice;
        int dvdItem;
    	double total;
        double titleValue;
        double totalValue;
        
     
        public DVD(String title, int stock, double price, int item) 
        {
        	
    		dvdTitle = title;
            dvdStock = stock;
            dvdPrice = price;
            dvdItem  = item;
        } //end four-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
        
        // retrieve total value of the entire inventory
        public double getTotal()
        {
             return totalValue;
        } // end method getTotal
           
    } //end class DVD
    
    // subclass BluRayDVD
    class BlurayDVD extends DVD
    {
    	// adds one field
    	String dvdCategory = "Bluray DVD";
    	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);		
    	}
    	
    	// 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
    	
    	public void setRestockFee()
    	{
    [COLOR="Red"][B][SIZE="5"]		restockFee = (titleValue * .05);[/SIZE][/B][/COLOR]
    	}
        
        // retrieve the restocking fee
        public double getRestockFee()
        {
        	return restockFee;
        }
    The restock fee results in a 0.0 amount, and I can't figure out why.

  5. #25
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I don't think you should even have a restock variable nor a setRestockFee method since it is purely a calculated value. Rather, why not move the calculation that you currently have in your "setRestockFee" method into the getRestockFee and return the result from it?

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

    Default

    Thank you. That worked.

    I wonder why it didn't work the other way, though...

  7. #27
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ladykrimson View Post
    Thank you. That worked.

    I wonder why it didn't work the other way, though...
    The other way, the restocking fee is never set unless you explicitly call setRestockingFee which also can only be called after the titleValue has been appropriately set, and I'll bet this wasn't done.

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

    Default

    Quote Originally Posted by Fubarable View Post
    The other way, the restocking fee is never set unless you explicitly call setRestockingFee which also can only be called after the titleValue has been appropriately set, and I'll bet this wasn't done.
    Ahhhh.....that makes sense. Thanks!

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

    Default

    Oh, the reason I had a variable for the restock fee was because I need to add the restock fee to the title value of just the Bluray DVDs. When the program lists the value of the title, it has to add a 5% restocking fee.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Help with inventory program part 5 PLEASE!
    By Exether in forum New To Java
    Replies: 2
    Last Post: 08-09-2010, 06:25 AM
  2. Help with Inventory Program, Please
    By NewKidjava in forum New To Java
    Replies: 4
    Last Post: 08-21-2009, 11:09 AM
  3. Inventory Program
    By tlouvierre in forum New To Java
    Replies: 5
    Last Post: 05-17-2009, 05:09 AM
  4. Help with Errors in Inventory Program
    By ljk8950 in forum AWT / Swing
    Replies: 3
    Last Post: 08-08-2008, 11:49 PM
  5. Inventory program
    By Nexcompac in forum New To Java
    Replies: 3
    Last Post: 07-27-2007, 05:51 PM

Posting Permissions

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