This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Randal Mortell
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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.




 
Paul Clapham
Sheriff
Posts: 22502
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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.)
 
Campbell Ritchie
Marshal
Posts: 55734
163
  • Mark post as helpful
  • send pies
  • 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.
 
Liutauras Vilda
Marshal
Posts: 4647
318
BSD
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 4647
318
BSD
  • Mark post as helpful
  • send pies
  • 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.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!