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

    Default My Inventory Program

    Thank you for all your help with my Payroll program. It was greatly appreciated.

    My next project is an inventory program that comes in 6 parts.

    This is part 1 of the assignment:
    Java Code:
    Choose a product that lends itself to an inventory (for example, products at your workplace, office supplies, music CDs, DVD movies, or software).
    
    Create a product class that holds the item number, the name of the product, the number of units in stock, and the price of each unit.
    
    Create a Java application that displays the product number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory (the number of units in stock multiplied by the price of each unit). Pay attention to the good programming practices in the text to ensure your source code is readable and well documented.
    I have this so far:

    Java Code:
    package inventory;
    
    //import predefined Scanner from Java Library
    import java.util.Scanner;
    
    public class Inventory
    {
        public static void main(String[] args) 
        {
                // create Scanner to obtain input from command window
                Scanner input = new Scanner(System.in);
                
                System.out.println("Welcome to the Inventory Program.");
                System.out.println("Enter an item number to display its properties,");
                System.out.println("or type the word - exit - to exit the program.");
                String itemNumber = input.nextLine(); // read a line of text
                if (itemNumber.equalsIgnoreCase("exit"))
                {
                    System.exit(0);
                }
                else
                {
                    
                }
        }
    }
    
    class Product
    {
        // Declare variables title, stock, itemNumber, and value
        String title;
        int stock;
        String itemNumber;
        int itemPrice;
        int value;
        
        // method to set item number
        public void setItemNumber(String number)
        {
            itemNumber = number;
        } // end method setItemNumber
        
        // method to retrieve item number
        public String getItemNumber(String itemNumber)
        {
            return itemNumber;
        } // end method getItemNumber
        
        // method to count number of items in inventory
        public void setStock(int count)
        {
            stock = (count< 0) ? 0 : count;
        } // end method setStock
        
        //method to retrieve the number of items in inventory
        public int getStock()
        {
            return stock;
        } // end method getStock
        
        // method to set the title
        public void setTitle (String title)
        {
            this.title = title;
        } // end method setTitle
        
        // method to retrieve the title
        public String getTitle()
        {
            return title;
        } // end method getTitle
        
        // method to set individual item price
        public void setItemPrice(int price)
        {
            itemPrice = price;
        }
        
        // method to retrieve individual item price
        public int getItemPrice(int price)
        {
            return itemPrice;
        }
        
        // method to set the total value of items in inventory
        public void setValue()
        {
            value = itemPrice * stock;
        }
        
        public int getValue()
        {
            return value;
        }
        
        public String toString()
        {
            return String.format("The product for item number","is", "of which there are", "in stock.", "The price per unit is $", "and the total value of the inventory is $", getItemNumber(itemNumber), getTitle(), getStock(), getItemPrice(itemPrice), getValue());
        }
    }
    Notice, there is nothing after the "else." I have tried to call the methods from the Product class, but I am getting the same error that I got with my other program: static can't call non-static methods. How can I get around that?

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    You have to create an instance of your Product class before you can call it's non-static methods.
    Ever seen a dog chase its tail? Now that's an infinite loop.

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

    Default

    Good lord, I am so frustrated.

    My Eclipse program gives me an error that says "Cannot invoke getDvdTitle() on the array type DVD[]."

    I don't understand the error. Can anyone help me?

    Java Code:
    package inventory;
    
    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);
             
            for (int i = 0; i < dvd.length; i++){
                System.out.println("Product Title is " + [COLOR="Red"][B]dvd.getDvdTitle[/B][/COLOR]());
                System.out.println("The number of units in stock is " + [COLOR="Red"][B]dvd.getDvdStock()[/B][/COLOR]);
                System.out.println("The price of each DVD is $" + [COLOR="Red"][B]dvd.getDvdPrice()[/B][/COLOR]);
                System.out.println("The item number is " + [COLOR="Red"][B]dvd.getDvdItem()[/B][/COLOR]);
                System.out.println("The value of the inventory is $" + [COLOR="Red"][B]dvd.value()[/B][/COLOR]);
            }
    
    }
    }// end class Inventory
    
    class DVD 
    {
        String dvdTitle;
        int dvdStock;
        double dvdPrice;
        int dvdItem;
     
        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
        public double value() 
        {
            return dvdPrice * dvdStock;
        } //end method value
        
    } //end class DVD

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

    Default

    "dvd" is an array of DVD's. You can't get the title of all of the dvd's from your get DVD method. You would have to say something like... dvd[0].getDVDTitle();
    Sincerely, Joshua Green
    Please REP if I help :)

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

    Default

    Do I need to do that for each item in the array? :eek:

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

    Default

    Quote Originally Posted by ladykrimson View Post
    Do I need to do that for each item in the array? :eek:

    Yes, but since you already have a loop set up you can just use a statement like...dvd[i].getDVDTitle();
    Sincerely, Joshua Green
    Please REP if I help :)

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

    Default

    Thank you thank you thank you!!!!!

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

    Default

    I'm guessing that means it worked haha. No problem man.
    Sincerely, Joshua Green
    Please REP if I help :)

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

    Default

    One more question, if that is all right...

    I need to have the program display the total inventory value of all of the DVDs. I have added a method to calculate this, but I am not certain how to call the method from the DVD class.

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

    Default

    Can you post an updated code.
    Sincerely, Joshua Green
    Please REP if I help :)

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

    Default

    Java Code:
    package inventory;
    
    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);
            
            
            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 inventory is $" + dvd[i].value());
            }
            
            System.out.println("The total value of the entire inventory is" + ________);
    
    }
    }// end class Inventory
    
    class DVD 
    {
        String dvdTitle;
        int dvdStock;
        double dvdPrice;
        int dvdItem;
        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 DisplayTotalValue(double totalValue)
        {
        	return totalValue;    	
        }
        
    } //end class DVD

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

    Default

    Remember that the DVD class is an array of DVD's. You are trying to return a totalValue that belongs to the entire array and not from each individual DVD. You'll need to run through the array, getting each value and adding it to totalValue.

    Java Code:
    int totalValue = 0;
    for(int i = 0; i < this.length; i++)
    {
         totalValue += this[i].totalValue;
    }

    Something like that, I may be wrong because I was coding it blind. Hopefully you get the idea though.
    Sincerely, Joshua Green
    Please REP if I help :)

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

    Default

    I get the idea. I am still thinking in a database mindset...I really need to stop doing that. :P

    So, to get the total value, the method will have to run through the array. There's no way to collect the values while the value method is running through the loop? Because the loop which displays all that information is totaling up each title. Does that number just disappear into nothingness once the loop is finished?

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

    Default

    Doing it like that would be fine. If I'm understanding correctly, you want to do this right:

    Java Code:
    int total = 0;
    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 inventory is $" + dvd[i].value());
                total += dvd[i].getTotal(); // THIS LINE ADDED
            }

    Where getTotal is:

    Java Code:
    public Double getTotal()
    {
         return totalValue;
    }
    Sincerely, Joshua Green
    Please REP if I help :)

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

    Default

    Correct me if I am wrong, but that won't display the totalValue. It calculates it, but it doesn't display it. And if we added a system.out to it, it would display the totalValue with each pass through the loop.

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

    Default

    The code below doesn't output the variable we created. Simply change:

    Java Code:
    System.out.println("The total value of the entire inventory is" + ________)

    TO:

    Java Code:
    System.out.println("The total value of the entire inventory is" + total);
    Sincerely, Joshua Green
    Please REP if I help :)

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

    Default

    Thank you very much. That worked. And I learned that arrays are a pain in the butt!

    Seriously, thanks!

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

    Default

    No problem :) And YES! They are!
    Sincerely, Joshua Green
    Please REP if I help :)

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

    Default

    OK, another array question.

    I have to create a method to alphabetically sort the DVD titles. I added this in the Inventory class:

    Java Code:
           
        	// create the Array list
        	ArrayList<DVD> l1 = new ArrayList<DVD>();
    What I would like to do is to use the loop that I already have. When the program goes through the loop, I would like it to add the dvdTitle to the ArrayList l1. Then I can write a method to sort and display the items in the ArrayList.

    Whenever I try to use l1.add(dvd[i].getDvdTitle()), it gives me an error that underlines the "add" expression.

    What am I doing wrong?
    Last edited by ladykrimson; 10-30-2010 at 09:07 PM.

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

    Default

    Here's an example of sorting an array of Strings alphabetically. There should be a way you can apply this in your program:

    Java Code:
    import java.util.*;
    
    public class arrayTest 
    {
    	public static void main(String[] args) 
    	{
    		String[] temp = new String[]{"Bobby", "Josh", "Billy", "Jack"};
    		Arrays.sort(temp);
    		
    		for(int i = 0; i < temp.length; i++)
    			System.out.println(temp[i]);
    	}
    }
    Sincerely, Joshua Green
    Please REP if I help :)

Page 1 of 2 12 LastLast

Similar Threads

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