• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with direction in assignment  RSS feed

 
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, it has been a while since I posted; thanks to everyone's help last time, I have been able to scrape by in my course. I have come into a new problem though, with an inventory program I have to create for my final assignment. This is what Ive made so far:








For the next step in the assignment, I am required to:

Modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the output should display the value of the entire inventory.
Create a method to calculate the value of the entire inventory.
Create another method to sort the array items by the name of the product.

At this point I am just a little lost and Im not sure where to start. Can someone point me in the right direction?
 
Bartender
Posts: 1840
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some feedback in terms of code styling and consistency on your existing code:

Rename the class "Games" to "Game" - it represents one "Game"
All of your get/set methods should be camel case. A property "gameNumber" should have a method "getGameNumber", not "getgameNumber"
Yes I'm being nit-picky, but so will your marker if there are any style/convention points assigned.

In terms of pointing you in the right direction.

You have created 10 variables in your main program, each of which is a Game.
Instead of that, create one variable which is an Array of games.
Loop through the array and print out each game.
you should be able to replicate exactly what you have now, but only using ONE System.out.println call.
 
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Further to Stefan's comments, variables like gameNumber and gameName should be called number and name respectively. They are defined in a class called Game so there is little need for the game prefix.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, make a class called Inventory with methods like total(), toString() and sort().
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright guys, thank you for your suggestions. I will make the adjustments you guys mentioned before turning in the project, but for right now Im just trying to figure out how to make this work. My understanding of arrays is poor, and my text doesn't help so much. I cant figure out how to make it one variable with one print statement, so this is what I came up with, and its not working for reasons that are not obvious to me.

 
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jorge Gutierrez wrote:

Why not take those printf's and use them in the toString() method?

Then just use System.out.println( myGames[i] );
 
Stefan Evans
Bartender
Posts: 1840
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, first thing - when dealing with Arrays in java, the index starts at 0.
So if you have 10 entries in an array, they are numbered from 0 to 9.

I would go through the steps
>
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright guys, thanks to your direction I got the array up and working. Now I just have to create these methods:

Create a method to calculate the value of the entire inventory.

Create another method to sort the array items by the name of the product.

Im going to see if i can figure this part out on my own, but if you guys have any tips for me I would greatly appreciate it.
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone Im back. After doing some work on the code I added a few things and changed some stuff around, but the code doesnt actually do what it is supposed to do. I got the array part down, but the methods I came up with dont seem to be actually printing anything. Any help would be greatly appreciated.




 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still havent figured out how to make it print the variables one at a time, or how to come up with the method to sort everything by name :c
 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jorge Gutierrez wrote:I still havent figured out how to make it print the variables one at a time, or how to come up with the method to sort everything by name :c

It looks like you're printing one game at a time and that for each game it prints all of the game's variables. Not sure what you mean by print one variable at a time. You can modify toString() to print a game in any fashion that suites you. Or, if you want to print just one variable: System.out.println( myGames[0].getGameName() ).
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Carey, that is what I meant to say. I fixed the toString to:



And now it prints everything nicely. Do you have any suggestions for my methods? This step in particular gives me no errors but doesnt print anything:

 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To sort the games you'll need a comparator like this:

And to sort them use this:

You should rename your getters and setters to use camel case: "getGameName()" not "getgameName()".
Your class name should probably be singular: Game.
Then field names like "gameNumber" should then be "number" because you already know it's a Game.
 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jorge Gutierrez wrote:And now it prints everything nicely. Do you have any suggestions for my methods? This step in particular gives me no errors but doesnt print anything:


Why are you extending IT215Inventory?
At this point I think you should do away with using arrays and use ArrayList<Game> instead, this would size itself dynamically as you add new Games.
Your IT215Inventory defines myGames[] twice, once at the class level and once local to main(). I might suggest keep the one in main() and then add that as a parameter to: getInventoryValue( List<Game> gameList ).

 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey, I was extending the array in preparation for the next step in this assignment;

Modify the Inventory Program by creating a subclass of the product class that uses one additional unique feature of the product you chose (for the DVDs subclass, you could use movie title, for example). In the subclass, create a method to calculate the value of the inventory of a product with the same name as the method previously created for the product class. The subclass method should also add a 5% restocking fee to the value of the inventory of that product.
Modify the output to display this additional feature you have chosen and the restocking fee.

I was trying to tackle both of these together because they are both due this week.

Where does the Arrays.sort(myGames, new GameOrderByName()); you posted go? I cant seem to fit in anywhere in the code without it saying that it was expecting an identifier, or that it has an illegal expression.
 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is where I tried it. Not sure where you ultimately need to put it. At what point do you need it sorted?

 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The games should print out sorted. I must be putting both sections of code you shared with me in the wrong place because the Array sort line doesnt seem to work anywhere I put it... This placement gave me the error "non static variable, this cannot be referenced from a static context"

 
Carey Brown
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That gets back to the comment I made that you are declaring myGames twice.
 
Stefan Evans
Bartender
Posts: 1840
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My suggestion right now: Look at your notes on loops.

Any time you are copying and pasting code , the only difference being the index into the array, you want to encapsulate that into a loop instead.
Your sequence of System.out.println for the data in the array is a perfect example of this.

 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I went back and slowly went over everything you guys have said and implemented it. This is what I have so far:





I have two issues right now. The loop I came up with for printing the array prints everything in a very long straight line, and I cant figure out why. In addition to that, I read up on comparators and I still cant figure out how to get the Arrays.sort( myGames, new GameOrderByName() ); to work.

 
Stefan Evans
Bartender
Posts: 1840
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Issue#1. It used to print on multiple lines. What (apart from being in a loop) is different from code you have posted before?

Issue#2: Your code (copied and pasted from your last post into Eclipse) currently has a compilation error

IT215Inventory.java line 32 : "Arrays.sort(myGames, new GameOrderByName());"
No enclosing instance of type IT215Inventory is accessible. Must qualify the allocation with an enclosing instance of type IT215Inventory (e.g. x.new A() where x is an instance of IT215Inventory)

Interpreting this message:
you are currently in a static method (main)
The Nested class GameOrderByName is not static.
Static methods can't access non-static/instance classes/variables.

Two possible solutions:
#1: Make GameOrderByName a static class. It can then be used from main.
#2: Construct an instance of your class


I think option #2 is the better one even if it involves more work, because it will make later work on this class easier.
Basically the process is
#1: Declare a method of IT215Inventory
#2: Move the code you currently have in main to this new method
#3: in main, create a new instance of your IT215Inventory class, and invoke the method on it.

Here is the basic refactoring for you.


I would suggest you change the method name from runProgram to something more appropriate.


 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:Issue#1. It used to print on multiple lines. What (apart from being in a loop) is different from code you have posted before?


Im an idiot
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:Issue#1. It used to print on multiple lines. What (apart from being in a loop) is different from code you have posted before?

Issue#2: Your code (copied and pasted from your last post into Eclipse) currently has a compilation error

IT215Inventory.java line 32 : "Arrays.sort(myGames, new GameOrderByName());"
No enclosing instance of type IT215Inventory is accessible. Must qualify the allocation with an enclosing instance of type IT215Inventory (e.g. x.new A() where x is an instance of IT215Inventory)

Interpreting this message:
you are currently in a static method (main)
The Nested class GameOrderByName is not static.
Static methods can't access non-static/instance classes/variables.

Two possible solutions:
#1: Make GameOrderByName a static class. It can then be used from main.
#2: Construct an instance of your class


I think option #2 is the better one even if it involves more work, because it will make later work on this class easier.
Basically the process is
#1: Declare a method of IT215Inventory
#2: Move the code you currently have in main to this new method
#3: in main, create a new instance of your IT215Inventory class, and invoke the method on it.

Here is the basic refactoring for you.


I would suggest you change the method name from runProgram to something more appropriate.




This is pretty advanced compared to what I have seen so far in class but I think I understand it. Thanks to you guys I was able to get through this part of the assignment, but it isnt over for me yet. I am going to try and use these concepts you have taught me to figure out the next step on my own. I will post my results once I have come up with something. Thanks again!

 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So after working for a while I came up with the code that I think will perform what I need for this assignment, but it has an issue, which Im sure will be solved by one of you in seconds. The compiler shows no errors, but my code doesnt print anything. This is what its supposed to do:

Modify the Inventory Program by creating a subclass of the product class that uses one additional unique feature of the product you chose (for the DVDs subclass, you could use movie title, for example). In the subclass, create a method to calculate the value of the inventory of a product with the same name as the method previously created for the product class. The subclass method should also add a 5% restocking fee to the value of the inventory of that product.
Modify the output to display this additional feature you have chosen and the restocking fee.


 
Stefan Evans
Bartender
Posts: 1840
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm.
Not having read the assignment, I'm not sure what the DVD example was about.
But I think the 'product' they are referring to is your Game class.
So given "modify the Inventory Program by creating a subclass of the product class", I would expect to see a class that extends Game (and thus inherits the inStock and unitPrice attributes so that you don't need to duplicate the code)
 
Jorge Gutierrez
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Update on this assignment: In the end, I was unable to make everything work in the way that it should, and unfortunately I had to turn it in for a poor grade. Thanks to all for your help, I just wasnt able to put your advice into effect this tome. Moving on, I now have a new situation; I have to make my code run using a GUI. I have read a few guides on using the GUI builder in Netbeans to create the GUI, and I came up with a simple GUI. My issue is, when I run the code, the GUI doesnt pop up or anything, it just shows me my output like normal. I have followed these tutorials to a T and I have no idea what is going on. Does anyone have experience with GUIs?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!