Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Cannot find symbol method getPrice()?

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Cannot find symbol method getPrice()?

    Hey guys,
    So I have 3 constructor classes and a main method. The three constructors are Product, LineItem, and Invoice. The main method is Cashier. In class Invoice, I'm trying to create a method called getRunningTotal() that scans through all the positions in the array and returns it's price and quantity. This is my method.
        public double getRunningTotal() {
            double runningTotal=0;
            for(int i=0; i<numItems; i++) {
                runningTotal=runningTotal+collection[i].getPrice()*collection[i].getQuantity();
            }
            return runningTotal;
        }

    The method getPrice() is declared in the Product class, and getQuantity() is in my LineItem class.. What do I have to do to make Invoice recognize those methods?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Cannot find symbol method getPrice()?

    collection[i].getPrice()
    and
    method getPrice() is declared in the Product class
    The array: collection must be defined as an array of Product objects. collection[i] is must be a Product object.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    EDale (March 30th, 2013)

  4. #3
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find symbol method getPrice()?

    Fixed it! I realized I had a method in LineItem that already multiplied price andd quantity so I called that one instead since I was directed that my collection should be a collection of LineItems not Products. Here is what I did:

        public double getRunningTotal() {
            double runningTotal=0;
            for(int i=0; i<numItems; i++) {
                runningTotal=runningTotal+collection[i].getAmount();
            }
            return runningTotal;
        }

    I just have one last method to correct if you could bare with me I would appreciate it. I have a method called update(java.lang.String name, int quantity) which updates the quantity for a line item. This method searches through the collection and finds the one that has the same name as the parameter. It then sets the quantity of that product to the parameter value. If the quantity is 0, the LineItem is removed from the collection and count is adjusted accordingly (this is achieved by calling a private method removeFromInvoice that takes the paramter name). If there is no LineItem with a name that matches the parameter name, this method has no effect.

    Does this mean I have to create a method removeFromInvoice within the Invoice class? It takes the name as a parameter, but this is giving me the same problem as getRunningTotal() was.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Cannot find symbol method getPrice()?

    I have a method called update(java.lang.String name, int quantity) which ...
    Does this mean I have to create a method
    First quote says "I have...". The second asks about creating a method. I'm confused.

    You need to look at the classes and where the data is that is to be updated and have the method located where it will have access to the data that is to be changed.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Cannot find symbol method getPrice()?

    I apologize. I don't have the method update(java.lang.String name, int quantity) finished. The description says the update method called a private removeFromInvoice method if the updated quantity entered is 0. The remove method takes the parameter name, so it looks like I only have access to the parameter name in the LineItem class. So I put the removeFromInvoice method in the LineItem class?

    Here is the removeFromInvoice method I created:
        public boolean removeFromInvoice(java.lang.String name) {
            for(int i=0; i<numItems; i++) {
                if(collection[i].getName().toUpperCase().equals(name.toUpperCase())) {
                    collection[i]=collection[numItems-1];
                    numItems--;
                    return true;
                }
                    else return false;
                }
                return false;
            }

    it gives me an error when I put it in the Invoice class, it says cannot find symbol method getName() again.

    This is what I have so far for the update method:
        public void update(java.lang.String name, int quantity) {
            for (int i=0; i<numItems; i++) {
                if(collection[i].getProduct().toUpperCase().equals(p.toUpperCase())) {
     
                }
            }
        }

    I am told this method goes in the Invoice class, but it has a name parameter again. I'm not sure what to do from here.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Cannot find symbol method getPrice()?

    I'd work on one problem at a time. Trying to do more will be confusing.
    cannot find symbol method getName()
    When you call a method that belongs to a class, you must have a reference to an instance of that class.
    What data type/class is collection[i]? Does that class has a method: getName()? You can not call a method that is not in the class. What class is getName() defined in? Where is there a reference to an instance of that class?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. "cannot find symbol" error when trying to use the getInt() method.
    By simpson_121919 in forum Collections and Generics
    Replies: 6
    Last Post: February 21st, 2013, 12:48 PM
  2. Remaining compile errors: no suitable method found for & cannot find symbol
    By ChuckLep in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 12th, 2011, 03:33 PM
  3. [SOLVED] Cannot find symbol - Calling method passing array Pets
    By Rilstin81 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 11th, 2011, 03:27 PM
  4. Using Queue, cannot find symbol method enqueue
    By firebluetom in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 17th, 2011, 01:41 PM
  5. cannot find symbol - method
    By kyuss in forum Object Oriented Programming
    Replies: 2
    Last Post: December 7th, 2009, 01:01 PM

Tags for this Thread