aspose file tools*
The moose likes Beginning Java and the fly likes classes and methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "classes and methods" Watch "classes and methods" New topic
Author

classes and methods

Dominic Griffin
Greenhorn

Joined: Jun 10, 2009
Posts: 16
I am trying to design a class that stores the units sold and returns the total cost. How do I know if units sold is being stored? And also is my method correct for returning the total cost? Please Help

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Because the * operator has a higher precedence than the - operator, your arithmetic might not work as you expect.

Why are you hard-coding the price rather than setting it in the constructor? Yo aren't setting the discount anywhere, so it will always be 0. No need to set us to 0, since that variable will vanish anyway. You have a totalCost field which you are never using.

Apart from that, it will work. You can tell whether units sold is stored by calling the getUnitsSold method and printing out what it returns.

That class will give the price for n discs sold @ 99 each, minus a discount. Beware: if you sell 2 units @ 99 each and give a discount of 200 . . .
Dominic Griffin
Greenhorn

Joined: Jun 10, 2009
Posts: 16
Thank you for the help.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
You're welcome.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14273
    
  21

Campbell Ritchie wrote:Because the * operator has a higher precedence than the - operator, your arithmetic might not work as you expect.

Looking at the code, and the comment at the top of the code about 'discount', I think the code does do what Dominic means. ("Discount on final cost", not "Discount per unit").


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
You're probably correct, Jesper, but it might have been "discount per unit".
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Campbell Ritchie wrote:Because the * operator has a higher precedence than the - operator, your arithmetic might not work as you expect.

Why are you hard-coding the price rather than setting it in the constructor? Yo aren't setting the discount anywhere, so it will always be 0. No need to set us to 0, since that variable will vanish anyway. You have a totalCost field which you are never using.

Apart from that, it will work. You can tell whether units sold is stored by calling the getUnitsSold method and printing out what it returns.

That class will give the price for n discs sold @ 99 each, minus a discount. Beware: if you sell 2 units @ 99 each and give a discount of 200 . . .


Nitpicking time (for me ) . SoftwareSales looks like it will have an instance for each sale. Intuitively you don't want to have to set the price each time there is a sale. So I don't have a problem with hard-coding the price in this particular design. Maybe we should be asking a different question.

regards.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
You mean a separate class which incorporates price?
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Campbell Ritchie wrote:You mean a separate class which incorporates price?


perhaps, I'm not sure.

When you consider a particular piece of software, then its price is not going to change in the short term. Maybe there will be price changes once a year. There might thousands of sales of a particular product before the price changes. So you don't want to be setting the price for each sale.

In a professional application, this sort of information might be stored in a relational database. In a beginner's case like this, hard coding the price seems like a reasonable alternative. Maybe it could be made a static variable.

however, having said all this, I suppose you could make a case for passing price to the constructor, if you also had a another instance variable that identified the product. But I get the feeling the OP hasn't thought that far ahead.

see what I mean?

Anyways, I suppose it depends on how this SoftwareSales class will be used. You might have another class which represented the type of software, and an instance of SoftwareSales class could become an instance variable in this other class. But I don't think this is what the OP has in mind. In any case, somewhere along the line price is going to be hard-coded, or stored in a relational database.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
I think we are actually in agreement about this exercise, but we need to avoid trying to make him run before he can walk.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Campbell Ritchie wrote:I think we are actually in agreement about this exercise, but we need to avoid trying to make him run before he can walk.


I'd say we agree about that too, that's why I said leave price hard coded in this example, it makes more sense.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: classes and methods