task2/solution05/src/org/apidesign/apifest08/currency/Amount.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 01 Oct 2008 10:43:05 +0200
changeset 29 f6073056b9fe
parent 6 task1/solution05/src/org/apidesign/apifest08/currency/Amount.java@97662396c0fd
permissions -rw-r--r--
Getting ready for task2: copying all solutions to new locations
     1 package org.apidesign.apifest08.currency;
     2 
     3 /**
     4  * Amount is a class reprezenting an amount of many. It consist of
     5  * whole currency amount and of pence amount. Both items are long values
     6  * and it's not defined that the 100 pences = 1 amount. It's up to the converter
     7  * to verify such invariants.
     8  *
     9  * @author jindra
    10  */
    11 public final class Amount {
    12 
    13     private long amount;
    14     private long pence;
    15 
    16     /**
    17      * Construct Amount with no pences.
    18      *
    19      * @param amount the amount in some currency
    20      *
    21      */
    22     public Amount(long amount) {
    23         this.amount = amount;
    24         this.pence = 0;
    25     }
    26 
    27     /**
    28      * Construct Amount with the pences.
    29      *
    30      * @param amount the amount in some currency
    31      * @param pence the pence count
    32      */
    33     public Amount(long amount, long pence) {
    34         this.amount = amount;
    35         this.pence = pence;
    36     }
    37 
    38     /**
    39      * @return the amount
    40      */
    41     public long getAmount() {
    42         return amount;
    43     }
    44 
    45     /**
    46      * @param amount the amount to set
    47      */
    48     public void setAmount(long amount) {
    49         this.amount = amount;
    50     }
    51 
    52     /**
    53      * @return the pence
    54      */
    55     public long getPence() {
    56         return pence;
    57     }
    58 
    59     /**
    60      * @param pence the pence to set
    61      */
    62     public void setPence(long pence) {
    63         this.pence = pence;
    64     }
    65 
    66     @Override
    67     public boolean equals(Object obj) {
    68         if (obj == null) {
    69             return false;
    70         }
    71         if (!(obj instanceof Amount)) {
    72             return false;
    73         }
    74         Amount other = (Amount) obj;
    75         return (amount == other.amount) && (pence == other.pence);
    76     }
    77 
    78     @Override
    79     public int hashCode() {
    80         int hash = 7;
    81         hash = 79 * hash + (int) (this.amount ^ (this.amount >>> 32));
    82         hash = 79 * hash + (int) (this.pence ^ (this.pence >>> 32));
    83         return hash;
    84     }
    85 
    86     @Override
    87     public String toString() {
    88         return amount + "." + pence;
    89     }
    90 
    91 
    92 }