KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Created on Sep 23, 2004
3  */

4 package com.openedit.store;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9
10 import org.openedit.money.Money;
11
12
13 /**
14  * @author Matthew Avery, mavery@einnovation.com
15  */

16 public class PriceSupport
17 {
18     protected List JavaDoc fieldTiers;
19     
20     public Money getYourPriceByQuantity(int inQuantity)
21     {
22         Money price = Money.ZERO;
23         // This code assumes that the tier prices must be entered in ascending price order.
24
for (Iterator JavaDoc iter = getTiers().iterator(); iter.hasNext();)
25         {
26             PriceTier tier = (PriceTier)iter.next();
27             if( inQuantity >= tier.getThresholdQuantity())
28             {
29                 price = tier.getValue();
30             }
31             
32         }
33         return price;
34
35     }
36     
37     
38     public List JavaDoc getTiers()
39     {
40         if (fieldTiers == null)
41         {
42             fieldTiers = new ArrayList JavaDoc();
43         }
44         return fieldTiers;
45     }
46     
47     public void addTierPrice( int inQuantity, Price inPrice )
48     {
49         
50         int finalPlace = -1;
51         for (int i = 0; i < getTiers().size(); i++)
52         {
53             PriceTier tier = (PriceTier) getTiers().get(i);
54             if( tier.getThresholdQuantity() == inQuantity )
55             {
56                 finalPlace = i;
57                 getTiers().remove(tier);
58                 break;
59             }
60             if( tier.getThresholdQuantity() > inQuantity )
61             {
62                 finalPlace = i-1;
63                 break;
64             }
65             
66         }
67         //Cound not find a place to put tier so append it to end of list
68
if ( finalPlace == -1)
69         {
70             finalPlace = getTiers().size();
71         }
72         PriceTier tier = new PriceTier();
73         tier.setPrice( inPrice );
74         tier.setThresholdQuantity( inQuantity );
75         getTiers().add( finalPlace, tier );
76     }
77
78     /**
79      * @return
80      */

81     public Money getRetailPrice()
82     {
83         Money price = Money.ZERO;
84         // This code assumes that the tier prices must be entered in ascending price order.
85
for (Iterator JavaDoc iter = getTiers().iterator(); iter.hasNext();)
86         {
87             PriceTier tier = (PriceTier)iter.next();
88             if( 1 >= tier.getThresholdQuantity())
89             {
90                 price = tier.getPrice().getRetailPrice();
91             }
92             
93         }
94         return price;
95     }
96     
97     public boolean isOnSale()
98     {
99         if ( getTiers().isEmpty() )
100         {
101             return false;
102         }
103         PriceTier tier = (PriceTier)getTiers().get(0);
104         return tier.getPrice().isOnSale();
105     }
106
107
108     /**
109      *
110      */

111     public void clear()
112     {
113         getTiers().clear();
114     }
115 }
116
Popular Tags