aspose file tools*
The moose likes Beginning Java and the fly likes New and desperately seeking help. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "New and desperately seeking help." Watch "New and desperately seeking help." New topic
Author

New and desperately seeking help.

Timothy Stockdale
Greenhorn

Joined: Sep 13, 2012
Posts: 8
Desperately seeking help guys. I quite literally just started working with Java. I am in a Java class in college and am drowning. This is my code.

I keep getting a message saying that "The product class is already defined". Not sure what is wrong with it. I put this together from examples in the book and finding scraps of code online that dealt with classes using constructors. The instructor replies to questions with less than helpful information (If you are lost like I am at least), so please dumb the instructions down. Thanks guys, if I learn enough from you guys to pass this class I'll treat ya to steak .
Rishi Shah
Ranch Hand

Joined: Sep 05, 2012
Posts: 43

It means you have another class called 'Product' in your package. Rename your class or delete/rename the other one to fix the issue.
Nikhil Sagar
Ranch Hand

Joined: Apr 21, 2012
Posts: 216

And Welcome to coderanch Timothy Stockdale.


OCPJP 6 86%
Ankush Kaundal
Ranch Hand

Joined: May 12, 2011
Posts: 36

"@Timothy Stockdale" The code is absolutely fine, please check for the duplicate class name as said by Rishi.
Timothy Stockdale
Greenhorn

Joined: Sep 13, 2012
Posts: 8
I tried what you guys suggested, but even changing the "Product" name to something else leaves me with the same error.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

Timothy Stockdale wrote:I keep getting a message saying that "The product class is already defined".


Could you explain what you mean by "getting" in that sentence? I mean, where does that message come from? Or what did you do to make the message appear? You can't expect us to know how you have things set up on your computer.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11417
    
  16

is that ALL it says? Usually you get something pointing the exact line where the error is.

I do know that I took your code, pasted it into a new file, named the file InventoryProgram.java, and it compiled and ran with NO issues.

I then changed the name to Product1, resaved the file (with the same name), and again, it worked. So the problem is NOT with this code. do a dir of the .class files in your current directory and make sure there isn't a Product.class file there somewhere.

Or, save this file in a new directory and compile it there and see what happens.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Timothy Stockdale
Greenhorn

Joined: Sep 13, 2012
Posts: 8
I changed the code and kept getting the error until I actually tried to compile and run the code again. It worked after that. Thanks for the help on that part guys. The next is what is really got me lost. I am supposed to make it where my program now allows for multiple items using an array. This is the actual info on the assignment.

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

If I run into a snag I'll be right back here. You guys have been a big help so far. Thanks everyone.

PS: I just realized that the first part of the requirement (being able to handle multiple items) may already have been done. I added multiple items to my initial program when it only called for one. Am I misunderstanding this or would that be complete?
Timothy Stockdale
Greenhorn

Joined: Sep 13, 2012
Posts: 8
In my original post I mentioned that the second part (which I hadn't posted yet) was due a few days ago. I am currently 3 days past due for this and have another part due this Sunday. I am going to post the entire requirement in advance. I have a strong feeling I'll be coming back to try to figure this out. For the moment, I am trying to sort my array by the name of the dvd's that I have entered, create a method to display the entire total of the inventory (the ProductValue displays the total inventory cost of each dvd, but I need the total inventory cost of the entire array), and I am supposed to have the entire program display the items one line at a time. So far I am failing at each of these. I will add comments for the parts that I am having trouble with.

I had thought maybe I could use a counter to keep track of the ProductValue, but I am not sure if that is possible. The next part is

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

That is due Sunday. Not asking you guys to do it for me, but I am probably going to have questions. I'll go back through the book chapters to see if I can find a clue. Probably looking like an idiot at the moment, but I could not be more lost when it comes to this.
Timothy Stockdale
Greenhorn

Joined: Sep 13, 2012
Posts: 8
I have become utterly lost and have scrapped my entire program that I posted. I am going back from scratch to try to get this fixed. This is my current code.

I know it is asking a lot, but can anyone walk me through this? My question at the moment is how do I get the Arrays that I have created (In the DVD class) to work in my Inventory Program class? I need them to display one at a time. Would I use the set and get feature? If so, how? If anyone can walk me through this one program, I may finally catch on.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11417
    
  16

This is not the right approach.

You're idea of using parallel arrays to store these values is not an easy one to maintain. If a copy of "Saw" is lost, it gets tricky on how you delete it, and deleting the last one is much trickier than deleting the 4th from last one.

The idea in OOP is that you try to model the real world with your Classes. Think about a DVD. It would have a title, a price, and perhaps an Inventory Control (IC) number. These become your class variables. A given DVD has no idea if there is another copy of it.

A store might have a collection of DVDs. So you would create a DVD object (with a title, price, and IC number), then put that into your collection. You may want to consider using a database, although that may be beyond your skill level at this point.

So, looking at java's Collection library, you may want to use an ArrayList (easy to use for beginners). It lets you add or remove objects quite easily, and grows as needed.
Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

fred rosenberger wrote:This is not the right approach.

You're idea of using parallel arrays to store these values is not an easy one to maintain. If a copy of "Saw" is lost, it gets tricky on how you delete it, and deleting the last one is much trickier than deleting the 4th from last one.

The idea in OOP is that you try to model the real world with your Classes. Think about a DVD. It would have a title, a price, and perhaps an Inventory Control (IC) number. These become your class variables. A given DVD has no idea if there is another copy of it.


Howdy Timothy

Fear not! Help is on the way. What Fred said is correct. Your approach is not correct. All you really need is a single array (you could use a List as, like Fred mentioned, but lets stick to arrays for now to keep it simple). Each element of the array corresponds to a stock of a specific DVD. And each stock contains a price, stock value and which DVD we're referencing to (you could include the price in the DVD class instead if you want, it's up to you).



Note that I have let the fields as public. Normally you should have these fields as private and create public setters and getters for them like in your Product class.
Understood more or less? Let me know if anything is unclear


Olly
Timothy Stockdale
Greenhorn

Joined: Sep 13, 2012
Posts: 8
Thank you so much guys. I finally understand enough to make an array thanks to your example Oliver. I may be back before too long because I still have no idea about how to manipulate the array, but with it working correctly I might be able to figure it out.
Timothy Stockdale
Greenhorn

Joined: Sep 13, 2012
Posts: 8
And surprise, I'm back again. Sorry for asking so many basic questions guys, but my professor is not answering my calls or e-mails and the book is hardly helpful. This is my code.

I took Oliver's advice and created my arrays differently than before. I am a bit confused about why i needed a DVD class and a Stock class. I am not certain what each one does or which one should receive the set/get methods. I attempted using the set/get methods on my item number but it did not change the result of what my array displayed when it ran (which was gibberish). I actually thought the array held the information about the dvd's and that since the array had it stored, I could just use "System.out.println(stocks[0])" and that it would print out that line. I know that the "this" keyword in java is used mostly if a field is shadowed (although not entirely sure what that means), but as my thought process was that the information was stored in the arrays, I am not sure how or where to input the information so that the arrays display correctly. Also, my loop at the top was supposed to make the data display one line at a time. It is not shown, but I attempted to use Array.sort earlier and that failed miserably. What is the proper way to use that or am I going in the wrong direction? The array needs to be sorted by the name of the dvd.

Thanks in advance for the help everyone.
Olivier Legat
Ranch Hand

Joined: Nov 17, 2007
Posts: 176

Concerning the setters and getters it's simply a matter of convention. You could ignore these fnction and keep your variables public, but this might be seen as untidy or unprofessional by some programmers. So, not knowing your Professor personally, it's best to stick to conventional coding

Regarding the for-loop, I don't think you've understood what a for-loop does. Think of it like this:


So all you in fact is:
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

Timothy Stockdale wrote: What is the proper way to use that or am I going in the wrong direction? The array needs to be sorted by the name of the dvd.
Thanks in advance for the help everyone.

Hello Timothy,
I would suggest you to break your task in small-small chunks... And take on piece at a time..

1). The OOP is all about classes and objects.. So, one simple thing you can follow is, "Whatever you think can be framed as class (can have attribute or functionality). Frame it"
Having different classes for different tasks is better way to solve your problem (so DVD class and the other class)..

2). Also, your main method is way too long.. You are doing everything in that only.. Rather you can have different methods for doing different tasks...
For Eg. ,you can have an initializer method to initialize your array.. An iterator method to iterate over it print elements.. and a getter method to return the array..
And you can invoke those methods in order from your main... This way it becomes easy to analyze the code you have written, if you somewhere get stuck in your code..

3). Regarding the for-loop you have used, I think Olivier has given a good look-inside for you..

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: New and desperately seeking help.