0

What should I name my class that stores costs/prices/amounts of money in CAD/USD as separate longs for dollars and cents? I has "PriceInDollars", but I don't really like that, and I always forget it. Any ideas?

EDIT: Apparently I should use BigDecimal, not reinvent the wheel. I guess I'll just use that. I did learn a lot trying to make my own class though.

Please see my other question here!

Community
  • 1
  • 1
mk12
  • 25,873
  • 32
  • 98
  • 137
  • 1
    Have you considered not naming it but using one of the many existing classes? http://jscience.org/ – Jens Schauder Aug 31 '09 at 18:27
  • Agreed; This is what BigDecimal is for. While a long-based solution is much better than using float, you're still probably not covering rounding modes, which can be mandated by law in financial applications. – Michael Borgwardt Aug 31 '09 at 18:32
  • I disagree. Primitives and similar should be used to construct more specific objects, but in the end, he should have a something that uniquely captures the concept he's going for. An IPv4 address is not an int, nor is it a String; but it can be represented with either. One would use ints and Strings to build an IPv4Address class. Above all else though, don't reinvent the wheel. If someone already made a class that captures the concept of your object, use it. – Omniwombat Aug 31 '09 at 19:16
  • BigDecimal does not carry the currency. Java needs a Money class. – Thorbjørn Ravn Andersen Aug 31 '09 at 19:27
  • 1
    If you nedded need the currency bundled with the amount, the sure, write a class for that. And use BigDecimal for the amount. Apart from the (maybe) included currency the OP's long-based implementation is not a more specific representation, it's pure reinventing the wheel (badly). A "more specific representation" that does nothing but wrap a base class under a different name is IMO pure useless and destructive abstraction. – Michael Borgwardt Aug 31 '09 at 21:12
  • Oh.. So I should use BigDecimal? – mk12 Aug 31 '09 at 21:45
  • @Mk12: Dosh, Moolah, Brass, FilthyLucre ... need I go on? :-) – Stephen C Aug 31 '09 at 22:57
  • Yes, because it's much less likely to contain hidden bugs, allows you to specify rounding modes if necessary, and is much more likely to be supported by third party libraries such as OR mappers. – Michael Borgwardt Aug 31 '09 at 22:59

8 Answers8

6

I would go with either Price or Cost. Whichever one you choose, you can subclass it to get more specific.

Money and Currency seem more like they would be attributes of the price, not really suitable as the name of the price itself. Just my 2 cents (couldn't resist the pun).

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • Currency I agree with, but how could "Money" be construed as an attribute of the price? – Laurence Gonsalves Aug 31 '09 at 21:45
  • @Laurence: I was thinking of a Money class that comes in different Currencies. Being an *attribute* of the Price might not be a totally accurate description of what I'm thinking. Money does feel like too abstract a concept for the class Mk12 is describing, though. – Bill the Lizard Aug 31 '09 at 22:07
  • I disagree with `Currency` because there is already a standard API class `java.util.Currency`. (Note that it only represents a currency code, not an amount). – Jesper Dec 14 '11 at 21:48
3

Firstly, I disagree with everyone who says to use BigDecimal. Even if your 'backing storage' is BigDecimal (which is very sensible), you should use your own class because having BigDecimal fields, method parameters, etc, conveys no semantics. Commonly re-used types like currency amounts should have their own classes.

All that said, don't write your own. There are plenty of libraries for this which already exist and have thought through many of the issues for you. Personally, I'd start with Joda-Money, by Stephen Colebourne (of Joda-Time fame).

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
Cowan
  • 37,227
  • 11
  • 66
  • 65
2

MonetaryValue :-)

Scott S. McCoy
  • 407
  • 4
  • 2
1
  • Currency
  • Money
  • Price
  • Value (I wouldn't, though)
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Thanks for the fast answer. I don't want to go with Currency, because it is an amount in a particular currency, not a currency itself. Same goes for money, Value isn't specific enough, that leaves Price, which is better than what I had, I guess, I guess I don't need to specify the InDollars... I'll go with that unless somebody else says something I like better. Thanks! – mk12 Aug 31 '09 at 18:30
  • In a previous life we went with the more verbose CurrencyUnits. Meh. – DarkSquid Aug 31 '09 at 18:32
1

Why is Money a bad choice? Encapsulating whole and fractional parts along with java.util.Currency into a class is a better design than BigDecimal, IMO.

I don't like anything that ends in "InDollars", because it unnecessarily prejudices your design to USD or CAD. Why do that if the idea is more general?

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Sure, bundling the currency with the amount is good, if needed. But name a single advantage of "whole and fractional parts" as longs to represent the amount, compared to a BigDecimal. – Michael Borgwardt Aug 31 '09 at 23:02
0

I would use "Cash"

http://www.answers.com/topic/cash Money in the form of bills or coins; currency. Payment for goods or services in currency or by check.

it's not specific to any currency type, lots of verbs you can add to it for conversions and such.

jmq
  • 10,110
  • 16
  • 58
  • 71
  • 2
    Except cash has very specific connotations, at least to native-English speakers, denoting the specific form of currency issued by governments as bills and coins. One does not consider a check to be cash. Nor is the abstract concept of the cost of some good or service "cash"; rather "cash" is one form used to effect the transfer of some monetary amount. – Lawrence Dol Aug 31 '09 at 21:29
0

I've called mine MonetaryAmount.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
Emerson Farrugia
  • 11,153
  • 5
  • 43
  • 51
0

I would call it Amount, holding a java.util.Currency and a long with the units.

Here is my own Amount class that I've used in different projects. It implements Comparable and has a toString method to format the amount for display (although it does not take your locale into account for formatting the amount).

Jesper
  • 202,709
  • 46
  • 318
  • 350