• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

classes and methods

 
Dominic Griffin
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the help.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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").
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're probably correct, Jesper, but it might have been "discount per unit".
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean a separate class which incorporates price?
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic