|
|
| 706 |
945 |
|
| 11 | 11 | import java.util.Locale;
|
| 12 | 12 | import java.util.Map;
|
| 13 | 13 | import java.util.HashMap;
|
| 14 | import java.beans.PersistenceDelegate;
|
| 15 | import java.beans.DefaultPersistenceDelegate;
|
| 14 | 16 |
|
| 15 | 17 | public class Currency implements Comparable {
|
| 16 | 18 | private static NumberFormat df = NumberFormat.getNumberInstance(Locale.US); // We create a lot of these, so minimizing memory usage is good.
|
| --- | --- | |
| 32 | 34 | return _noValue;
|
| 33 | 35 | }
|
| 34 | 36 |
|
| 35 | | private int _whatCurrency;
|
| 36 | | private double _value;
|
| 37 | protected int mCurrencyType;
|
| 38 | protected double mValue;
|
| 37 | 39 | private static final char pound = '\u00A3';
|
| 38 | 40 | private static final Character objPound = '\u00A3';
|
| 39 | 41 |
|
| --- | --- | |
| 393 | 395 | * @param startValue - The amount represented.
|
| 394 | 396 | */
|
| 395 | 397 | private void setValues(int whatType, double startValue) {
|
| 396 | | _whatCurrency = whatType;
|
| 397 | | _value = startValue;
|
| 398 | mCurrencyType = whatType;
|
| 399 | mValue = startValue;
|
| 398 | 400 | df.setMinimumFractionDigits(2);
|
| 399 | 401 | df.setMaximumFractionDigits(2);
|
| 400 | 402 | }
|
| --- | --- | |
| 406 | 408 | * @return A string containing a full ISO currency name.
|
| 407 | 409 | */
|
| 408 | 410 | public String fullCurrencyName() {
|
| 409 | | switch(_whatCurrency) {
|
| 411 | switch(mCurrencyType) {
|
| 410 | 412 | case US_DOLLAR: return("USD");
|
| 411 | 413 | case AU_DOLLAR: return("AUD");
|
| 412 | 414 | case NT_DOLLAR: return("NTD");
|
| --- | --- | |
| 425 | 427 | }
|
| 426 | 428 | }
|
| 427 | 429 |
|
| 428 | | public double getValue() { return _value; }
|
| 430 | public double getValue() { return mValue; }
|
| 429 | 431 |
|
| 430 | 432 | public String fullCurrency() {
|
| 431 | 433 | return fullCurrencyName() + " " + getValueString();
|
| --- | --- | |
| 446 | 448 | public Currency add(Currency addValue) throws CurrencyTypeException {
|
| 447 | 449 | if(addValue == null) throw new CurrencyTypeException("Cannot add null Currency.");
|
| 448 | 450 |
|
| 449 | | if(addValue.getCurrencyType() == _whatCurrency) {
|
| 450 | | return new Currency(_whatCurrency, _value + addValue.getValue());
|
| 451 | if(addValue.getCurrencyType() == mCurrencyType) {
|
| 452 | return new Currency(mCurrencyType, mValue + addValue.getValue());
|
| 451 | 453 | }
|
| 452 | 454 |
|
| 453 | 455 | // If only one currency is known, return the result as the known currency.
|
| 454 | | if (_whatCurrency == NONE) return new Currency(addValue.getCurrencyType(), _value + addValue.getValue());
|
| 455 | | if (addValue.getCurrencyType() == NONE) return new Currency(_whatCurrency, _value + addValue.getValue());
|
| 456 | if (mCurrencyType == NONE) return new Currency(addValue.getCurrencyType(), mValue + addValue.getValue());
|
| 457 | if (addValue.getCurrencyType() == NONE) return new Currency(mCurrencyType, mValue + addValue.getValue());
|
| 456 | 458 |
|
| 457 | 459 | throw new CurrencyTypeException("Cannot add " + fullCurrencyName() + " to " + addValue.fullCurrencyName() + ".");
|
| 458 | 460 | }
|
| --- | --- | |
| 472 | 474 | public Currency subtract(Currency subValue) throws CurrencyTypeException {
|
| 473 | 475 | if(subValue == null) throw new CurrencyTypeException("Cannot subtract null Currency.");
|
| 474 | 476 |
|
| 475 | | if(subValue.getCurrencyType() == _whatCurrency) {
|
| 476 | | return new Currency(_whatCurrency, _value - subValue.getValue());
|
| 477 | if(subValue.getCurrencyType() == mCurrencyType) {
|
| 478 | return new Currency(mCurrencyType, mValue - subValue.getValue());
|
| 477 | 479 | }
|
| 478 | 480 |
|
| 479 | 481 | // If only one currency is known, return the result as the known currency.
|
| 480 | | if(_whatCurrency == NONE) return new Currency(subValue.getCurrencyType(), _value - subValue.getValue());
|
| 481 | | if(subValue.getCurrencyType() == NONE) return new Currency(_whatCurrency, _value - subValue.getValue());
|
| 482 | if(mCurrencyType == NONE) return new Currency(subValue.getCurrencyType(), mValue - subValue.getValue());
|
| 483 | if(subValue.getCurrencyType() == NONE) return new Currency(mCurrencyType, mValue - subValue.getValue());
|
| 482 | 484 |
|
| 483 | 485 | throw new CurrencyTypeException("Cannot subtract " + fullCurrencyName() + " from " + subValue.fullCurrencyName() + ".");
|
| 484 | 486 | }
|
| 485 | 487 |
|
| 486 | | public int getCurrencyType() { return _whatCurrency; }
|
| 488 | public int getCurrencyType() { return mCurrencyType; }
|
| 487 | 489 |
|
| 488 | 490 | public String getCurrencySymbol() {
|
| 489 | | switch(_whatCurrency) {
|
| 491 | switch(mCurrencyType) {
|
| 490 | 492 | case US_DOLLAR: return("$");
|
| 491 | 493 | case NT_DOLLAR: return("nt$");
|
| 492 | 494 | case HK_DOLLAR: return("hk$");
|
| --- | --- | |
| 522 | 524 | } else {
|
| 523 | 525 | String cvtToString = getCurrencySymbol();
|
| 524 | 526 |
|
| 525 | | cvtToString += df.format(_value);
|
| 527 | cvtToString += df.format(mValue);
|
| 526 | 528 |
|
| 527 | 529 | return(cvtToString);
|
| 528 | 530 | }
|
| --- | --- | |
| 542 | 544 | if(isNull()) {
|
| 543 | 545 | return("null");
|
| 544 | 546 | } else {
|
| 545 | | return df.format(_value);
|
| 547 | return df.format(mValue);
|
| 546 | 548 | }
|
| 547 | 549 | }
|
| 548 | 550 |
|
| --- | --- | |
| 579 | 581 | if(!(inValue instanceof Currency)) return false;
|
| 580 | 582 | // Okay, now cast it because it's safe.
|
| 581 | 583 | Currency otherValue = (Currency) inValue;
|
| 582 | | boolean sameCurrency = (otherValue.getCurrencyType() == _whatCurrency);
|
| 583 | | boolean sameValue = ((int) (otherValue.getValue() * 1000)) == ((int) (_value * 1000));
|
| 584 | boolean sameCurrency = (otherValue.getCurrencyType() == mCurrencyType);
|
| 585 | boolean sameValue = ((int) (otherValue.getValue() * 1000)) == ((int) (mValue * 1000));
|
| 584 | 586 |
|
| 585 | 587 | return(sameCurrency && sameValue);
|
| 586 | 588 | }
|
| --- | --- | |
| 605 | 607 | // Shortcut
|
| 606 | 608 | if(otherValue == this) return false;
|
| 607 | 609 |
|
| 608 | | boolean sameCurrency = (otherValue.getCurrencyType() == _whatCurrency);
|
| 610 | boolean sameCurrency = (otherValue.getCurrencyType() == mCurrencyType);
|
| 609 | 611 | if(!sameCurrency) {
|
| 610 | 612 | throw new CurrencyTypeException("Cannot compare different currencies.");
|
| 611 | 613 | }
|
| 612 | 614 |
|
| 613 | | boolean lowerValue = Double.compare((double) ((int) (otherValue.getValue() * 1000)), (double) (int) (_value * 1000)) == 1;
|
| 615 | boolean lowerValue = Double.compare((double) ((int) (otherValue.getValue() * 1000)), (double) (int) (mValue * 1000)) == 1;
|
| 614 | 616 |
|
| 615 | 617 | return(lowerValue);
|
| 616 | 618 | }
|
| --- | --- | |
| 623 | 625 | * @return True if this is a 'null currency' object.
|
| 624 | 626 | */
|
| 625 | 627 | public boolean isNull() {
|
| 626 | | return(_value == 0.0 && _whatCurrency == NONE);
|
| 628 | return(mValue == 0.0 && mCurrencyType == NONE);
|
| 627 | 629 | }
|
| 628 | 630 |
|
| 629 | 631 | /**
|
| --- | --- | |
| 669 | 671 | if(equals(otherValue)) return 0;
|
| 670 | 672 | return 1;
|
| 671 | 673 | }
|
| 674 |
|
| 675 | public static PersistenceDelegate getDelegate() {
|
| 676 | return new DefaultPersistenceDelegate(new String[]{"mCurrencyType", "mValue"});
|
| 677 | }
|
| 672 | 678 | }
|