• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Issues with using instances in a method defined in a subclass.

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm working on a program that reads int values in via the Scanner as a String and parses them out as "traits" for an object of Inventory and an object for Sales. My issue is when I create a method called postSale() that requires the quantity of both inventory and sales and subtracts one from the other. (The method is defined in the Inventory Class.) I receive errors until I convert anything involved with quantitySold in the Sales Class to static. At that point it allows me to run the files but since quantitySold is now static the information will not update. I have my objects loaded in ArrayLists and the plan was to walk through the arrayList and compare the productCode then subtract the value on hand if the productCode is the same. Here is my code to the classes. Basically my question is:
Is there a way I can have quantitySold stay as an instance or if not are there any suggestions on how I can use this with static? Thank you for any suggestions.

***Sidenote*** The most of the comments are me just blocking things out that I'm trying til I find a path that works.




 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randal Mortell wrote:I receive errors until I convert anything involved with quantitySold in the Sales Class to static.



Don't do that. Deal with the errors instead. (If you can't explain why your design requires the variable to be static then don't make it static.)
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randal Mortell wrote:. . . I receive errors until I convert anything . . . to static. . . .

As Paul C has already suggested, the correct answer to that error is not to make the target static. It is to move the calling code out of the static context into a non‑static instance context, which means into an instance method. You have got variables inside the main method; those variables don't belong there. They should probably all be fields, but I haven't read the code so I am not sure on that point. The ideal length for a main method is one statement, as shown here. Yes, that example does show only one statement. The other line is a declaration.

I wish those error messages would say instance rather than non‑static.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. I'm sorry, what's happened, why you extending Main class with your Inventory and Sales classes?
2. You got inconsistent code indentation, that is not good, you need to be consistent how you place curly braces, if you leave empty lines or not in between successor methods (read coding convention).
3. Lots of commented out code needs to be removed too, that adds its own bit to a total mess.
4. In Inventory and Sales classes constructors you got lots of duplicated code. Have you ever encountered keyword this used in constructors context?
5. Why have you got no argument constructor with an empty body?
6. When you use Scanner for files reading - use Path, instead FileReader. That would look (what is in quotes needs to be full path where the file is located):
7. As Campbell Ritchie already mentioned, most likely you'll need to rewrite your main method. That should be quick - just delete everything what is in there. The code which is currently laying down in there you'll need to move somewhere else to a non static method. Follow instructions CR gave you as a link.
 
Randal Mortell
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for the replys. I'm very new to this and coding in general. The way Scanner is used is how my instructor suggested we use it. I ended up starting over without ArrayLists and didn't have the issues I was having with the postSale method. Just had the objects get evaluated/compared as they were coming in. Instead of loading a List then comparing. I had a snag with my postSale method calculating duplicate orders on the Sales class but put a fix in there for that also. Usually the commented out code is just so I can back track if I need to. When I finish with the project I delete and respace everything. Granted its probably not the best but I'm trying to get better. Thank you all again for the advice. Here's the project I turned in last night. If you have any suggestions on where my weak points are and maybe where or what I can research to improve I would appreciate it. Also I'm not exactly sure what you mean by extending Main class.





 
Randal Mortell
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry. Its me again. So as far as that link for running a run() method in main. Would I call any variables or is that handled in the run method also?
or basically am I mirroring the program to a window? if that makes sense.
 
Liutauras Vilda
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randal Mortell wrote:Sorry. Its me again. So as far as that link for running a run() method in main. Would I call any variables or is that handled in the run method also?
or basically am I mirroring the program to a window? if that makes sense.

Hi Randall once again. No need to sorry, all good.

Now about the code. There are many issues with it. The problem I see, that you're trying to do too many things in one go without having confident that what you did so far at least contains 1 part which is correct. It is not a Hello, World problem. You need to think about the project as a whole first, might draw some diagrams, one of those could be a Class diagram, so you could see relations of classes. Have you learned about drawing class diagrams?

By extending Main class I meant couple of the very first lines in your Sales and Inventory classes:

Randal Mortell wrote:

Randal Mortell wrote:

Please explain why you're doing that?

I'd suggest you to forget about the Main class and all interaction of the program and choose one class to work on, either Sales or Inventory. You know that you can add static main method to one of those classes just for a test purpose to test it independently from the rest of your program? Later of course you'd need to remove it, but in that manner you wouldn't need to to think about writing 2 classes at the same time, so in that main method you could pass some dummy data to simulate sales or inventory files content.

dummy example:
Once you are confident, that your class has functionality you want it to have and the outcome of it is as expected, you can remove that temporary main method and move on with another class. ONCE you got Sales and Inventory classes, you can think of the other class where program interaction happens. It could be in the Main class as you have now, but it should be at least in other method, check the tutorial written by one of moderators Winston, kudos to him for that, this tutorial helps thousands of people, you can go through it here (<-- link). [interim comment] Campbell Ritchie gave you this tutorial link already, so now you really have to go through it.

When you override methods as you do with toString method, add @Override notation. Read about it what it does.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When I finish with the project I delete and respace everything.


If I'm understanding you, you leave formatting to the end, as if it's a "make things pretty" step. I believe this is an error. Good formatting is vital for good programming. It helps you avoid many kinds of errors and helps to organize your code.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic