KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > store > Price


1 /*
2  * Created on Oct 11, 2004
3  */

4 package com.openedit.store;
5
6 import org.openedit.money.CurrencyFormat;
7 import org.openedit.money.Money;
8
9 /**
10  * @author Matthew Avery, mavery@einnovation.com
11  */

12 public class Price
13 {
14     protected Money fieldRetailPrice;
15     protected Money fieldSalePrice;
16     protected String JavaDoc fieldRegion;
17     public static final String JavaDoc REGION_EU = "eu";
18     public static final String JavaDoc REGION_AMERICA = "a";
19
20     public Price()
21     {
22     }
23
24     public Price(Money inRetailPrice)
25     {
26         setRetailPrice(inRetailPrice);
27     }
28
29     public Money getRetailPrice()
30     {
31         return fieldRetailPrice;
32     }
33     public void setRetailPrice( Money retailPrice )
34     {
35         fieldRetailPrice = retailPrice;
36         adjustRegion(fieldRetailPrice);
37     }
38     public Money getSalePrice()
39     {
40         return fieldSalePrice;
41     }
42     public void setSalePrice( Money salePrice )
43     {
44         fieldSalePrice = salePrice;
45         adjustRegion(fieldSalePrice);
46     }
47     
48     public boolean isOnSale()
49     {
50         return getSalePrice() != null && !getSalePrice().equals(getRetailPrice());
51         
52     }
53     public Money getValue()
54     {
55         Money value = null;
56         if ( isOnSale() )
57         {
58             value = getSalePrice();
59         }
60         else if ( getRetailPrice() != null )
61         {
62             value = getRetailPrice();
63         }
64         else
65         {
66             value = Money.ZERO;
67         }
68         return adjustRegion(value);
69         
70     }
71
72     public boolean equals(Object JavaDoc inObject)
73     {
74         if ( inObject instanceof Price )
75         {
76             Price price = (Price)inObject;
77             if ( getRetailPrice() != null )
78             {
79                 if ( !getRetailPrice().equals( price.getRetailPrice() ) )
80                 {
81                     return false;
82                 }
83             }
84             else if ( price.getRetailPrice() != null )
85             {
86                 return false;
87             }
88             if ( getSalePrice() != null )
89             {
90                 if ( !getSalePrice().equals( price.getSalePrice() ) )
91                 {
92                     return false;
93                 }
94             }
95             else if ( price.getSalePrice() != null )
96             {
97                 return false;
98             }
99             return true;
100         }
101         else
102         {
103             return false;
104         }
105     }
106
107     public int hashCode()
108     {
109         int code = 0;
110         if ( getRetailPrice() != null )
111         {
112             code ^= getRetailPrice().hashCode();
113         }
114         if ( getSalePrice() != null )
115         {
116             code ^= (getSalePrice().hashCode() << 1);
117         }
118         return code;
119     }
120     public String JavaDoc getRegion()
121     {
122         return fieldRegion;
123     }
124     public void setRegion(String JavaDoc inRegion)
125     {
126         fieldRegion = inRegion;
127     }
128     public Money adjustRegion(Money inTotalPrice)
129     {
130         if ( fieldRegion != null && inTotalPrice != null )
131         {
132             if ( REGION_EU.equals(getRegion()))
133             {
134                 inTotalPrice.setFormat(CurrencyFormat.EURO);
135             }
136             else
137             {
138                 inTotalPrice.setFormat(CurrencyFormat.US);
139             }
140         }
141         return inTotalPrice;
142     }
143
144     
145 }
146
Popular Tags