Inventory Java Project Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ITQUEST
    New Member
    • Feb 2007
    • 11

    Inventory Java Project Help

    I am on the second step of my Inventory Project and can properly run my Inventory2.java file but not my Product2.java file. I get the following error: 71:cannot find symbol, symbol: method getItemTitle() location: class Product
    returntitle.com pareTo(p.getIte mTitle());
    ^

    Here is my code:
    Code:
    // Product2 class
    
    import java.util.*;
    
    class Product2 implements Comparable {
    	
    
        private String title; 	  // class variable stores the item title
        private long number;	  // class variable stores the item number
        private long stockQuantity;   // class variable stores the quantity in stock
        private double price;	  // class variable stores the item price
    
        public Product2() {
                title = "";
    	    number = 0L;
    	    stockQuantity = 0L;
    	    price = 0.0;
        }
    
        public Product2(String title, long number, long stockQuantity, double price) {
    	    this.title = title;
    	    this.number = number;
    	    this.stockQuantity = stockQuantity;
    	    this.price = price;
       	}
    
        public void setItemTitle(String title) {
    		this.title = title;
        }
    
        public String getItemTitle() {
    		return title;
        }
    
        public void setItemNumber(long number) {
    		this.number = number;
        }
    
        public long getItemNumber() {
    		return number;
        }
    
        public void setStockQuantity(long quantity) {
    		stockQuantity = quantity;
        }
    
        public long getStockQuantity() {
    		return stockQuantity;
        }
    
        public void setItemPrice(double price) {
    		this.price = price;
        }
    
        public double getItemPrice() {
    		return price;
        }
    
        public double calculateInventoryValue() {
    		return price * stockQuantity;
        }
    
        public int compareTo (Object o) {
    		Product p = null;
    		try {
    			p = (Product)o;
    		}
    		catch(ClassCastException cE) {
    			cE.printStackTrace();
    		}
    		return title.compareTo(p.getItemTitle());
    	}
    
    	public String toString() {
    		return "DVD Title: "+title + "\nNumber: "+number+"\nPrice: $"+price+"\nQuantity: "+stockQuantity + "\nValue: $"+calculateInventoryValue();
    	}
    
    } // end class Product2

    Any suggestions? Thanks! ITQUEST
    Last edited by horace1; Feb 18 '07, 04:22 PM. Reason: added code tags
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    do you have a method getItemTitle() in Product? I can see a getItemName()? assuming I am looking at the latest version of Product!

    Comment

    • ITQUEST
      New Member
      • Feb 2007
      • 11

      #3
      Originally posted by horace1
      do you have a method getItemTitle() in Product? I can see a getItemName()? assuming I am looking at the latest version of Product!
      Yes this is my latest version of Product2. I do not see a getItemName().. .where do you see that? I guess I have been working so long I am getting confused. Do you see the error in my posted code?

      Thanks for your help! ITQUEST

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by ITQUEST
        Yes this is my latest version of Product2. I do not see a getItemName().. .where do you see that? I guess I have been working so long I am getting confused. Do you see the error in my posted code?

        Thanks for your help! ITQUEST
        it looks like the error is in
        Code:
            public int compareTo (Object o) {
        		Product p = null;
        		try {
        			p = (Product)o;
        		}
        		catch(ClassCastException cE) {
        			cE.printStackTrace();
        		}
        		return title.compareTo(p.getItemTitle());
        	}
        in line
        Code:
        returntitle.compareTo(p.getItemTitle());
        where p is an object of type Product which does not have a method getItemTitle() but - in the post I am looking at

        has a method getItemName()

        should p be an object of type Product2??

        Comment

        • ITQUEST
          New Member
          • Feb 2007
          • 11

          #5
          Originally posted by horace1
          it looks like the error is in
          Code:
              public int compareTo (Object o) {
          		Product p = null;
          		try {
          			p = (Product)o;
          		}
          		catch(ClassCastException cE) {
          			cE.printStackTrace();
          		}
          		return title.compareTo(p.getItemTitle());
          	}
          in line
          Code:
          returntitle.compareTo(p.getItemTitle());
          where p is an object of type Product which does not have a method getItemTitle() but - in the post I am looking at

          has a method getItemName()

          should p be an object of type Product2??

          Yes I believe so. I changed that part of the code to read as this:

          public int compareTo (Object o) {
          Product2 p = null;
          try {
          p = (Product2)o;
          }
          catch(ClassCast Exception cE) {
          cE.printStackTr ace();
          }
          return title.compareTo (p.getItemTitle ());
          }

          And tried to run it. It gives me the error: Exception in Thread "Main" java lang. NoSuchMethodErr or: main

          ITQUEST

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            you need a main() method, something along the lines of
            Code:
            public static void main(String s[])
            {
              Product2 p = new Product2();
              // more code????
            }

            Comment

            • ITQUEST
              New Member
              • Feb 2007
              • 11

              #7
              Originally posted by horace1
              you need a main() method, something along the lines of
              Code:
              public static void main(String s[])
              {
                Product2 p = new Product2();
                // more code????
              }
              I am not sure if I understand. I have changed the code in the beginning to look like this:

              // Product2 class

              import java.util.*;

              public static void main (String args[])
              Product2 p = new Product2();

              class Product2 implements Comparable {


              private String title; // class variable stores the item title
              private long number; // class variable stores the item number
              private long stockQuantity; // class variable stores the quantity in stock
              private double price; // class variable stores the item price
              ...

              And it doesn't work either. I think I am confusing myself more than I should be.
              ITQUEST

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                Well, in order for you to execute a command like

                Code:
                java Product2
                there has to be a function main() in the class, as horace said. You will use this main function to test out the class, etc. Alternatively, you may have been given a 'main' class to execute for testing, in which case you would use

                Code:
                java MainClass
                instead of Product2.

                Comment

                • horace1
                  Recognized Expert Top Contributor
                  • Nov 2006
                  • 1510

                  #9
                  Originally posted by ITQUEST
                  I am not sure if I understand. I have changed the code in the beginning to look like this:

                  // Product2 class

                  import java.util.*;

                  public static void main (String args[])
                  Product2 p = new Product2();

                  class Product2 implements Comparable {


                  private String title; // class variable stores the item title
                  private long number; // class variable stores the item number
                  private long stockQuantity; // class variable stores the quantity in stock
                  private double price; // class variable stores the item price
                  ...

                  And it doesn't work either. I think I am confusing myself more than I should be.
                  ITQUEST
                  put the main() including the {} inside the class not outside

                  Comment

                  • ITQUEST
                    New Member
                    • Feb 2007
                    • 11

                    #10
                    Originally posted by horace1
                    put the main() including the {} inside the class not outside

                    I worked on it and got it to run! Thanks so much for your help!
                    When I get to the next step of my assignment can I post any questions under this same thread or should I start a new one?

                    ITQUEST

                    Comment

                    • Ganon11
                      Recognized Expert Specialist
                      • Oct 2006
                      • 3651

                      #11
                      You should probably start a new thread if you have any questions.

                      Comment

                      Working...