Forums Register Login

Need help with direction in assignment

+Pie Number of slices to send: Send
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?
1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
1
+Pie Number of slices to send: Send
Also, make a class called Inventory with methods like total(), toString() and sort().
+Pie Number of slices to send: Send
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.

1
+Pie Number of slices to send: Send
 

Jorge Gutierrez wrote:


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

Then just use System.out.println( myGames[i] );
1
+Pie Number of slices to send: Send
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
>
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.




+Pie Number of slices to send: Send
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
1
+Pie Number of slices to send: Send
 

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() ).
+Pie Number of slices to send: Send
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:

1
+Pie Number of slices to send: Send
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.
1
+Pie Number of slices to send: Send
 

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 ).

+Pie Number of slices to send: Send
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.
1
+Pie Number of slices to send: Send
This is where I tried it. Not sure where you ultimately need to put it. At what point do you need it sorted?

+Pie Number of slices to send: Send
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"

1
+Pie Number of slices to send: Send
That gets back to the comment I made that you are declaring myGames twice.
1
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
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.

1
+Pie Number of slices to send: Send
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.


+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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!

+Pie Number of slices to send: Send
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.


+Pie Number of slices to send: Send
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)
+Pie Number of slices to send: Send
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?
An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1096 times.
Similar Threads
Super class and Sub class inheritance not working
I keep getting an error message in my code
New and desperately seeking help.
Help with a for loop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:13:05.