001: /*
002: * Created on Oct 31, 2004
003: *
004: * TODO To change the template for this generated file go to
005: * Window - Preferences - Java - Code Style - Code Templates
006: */
007: package net.sf.jmoney.stocks;
008:
009: import java.util.ArrayList;
010: import java.util.Collections;
011: import java.util.List;
012:
013: /**
014: * This class contains a table of rates that can be used to calculate
015: * commissions, taxes etc.
016: * <P>
017: * This is an immutable class. If you want to modify a rates table,
018: * you must create a new rates table and set the new table as the
019: * property value. (If you were to be allowed to modify a rates table
020: * then there would be no easy means of ensuring listeners are notified
021: * of the changes).
022: *
023: * @author Nigel Westbury
024: */
025: public class RatesTable {
026: /*
027: * An immutable class containing details of a single band in a rates
028: * table.
029: */
030: static class Band {
031: private long bandStart;
032: private double percentage;
033:
034: public Band(long bandStart, double percentage) {
035: this .bandStart = bandStart;
036: this .percentage = percentage;
037: }
038:
039: public long getBandStart() {
040: return bandStart;
041: }
042:
043: public double getPercentage() {
044: return percentage;
045: }
046: }
047:
048: private long fixedAmount = 0;
049:
050: private ArrayList<Band> bands;
051:
052: // Construct a new rates table.
053: public RatesTable() {
054: bands = new ArrayList<Band>();
055: }
056:
057: // Construct a rates table from a String object.
058: // All classes created by plug-ins to contain JMoney properties
059: // must have both a toString() method and a constructor from
060: // String that re-constructs an object from the string.
061: public RatesTable(String stringValue) {
062: bands = new ArrayList<Band>();
063:
064: String[] numbers = stringValue.split(";");
065:
066: fixedAmount = new Long(numbers[0]).longValue();
067:
068: int i = 1;
069: while (i < numbers.length) {
070: long bandStart = new Long(numbers[i++]).longValue();
071: double percentage = new Double(numbers[i++]).doubleValue();
072: Band band = new Band(bandStart, percentage);
073: bands.add(band);
074: }
075: }
076:
077: public RatesTable(long fixedAmount, ArrayList<Band> bands) {
078: this .fixedAmount = fixedAmount;
079: this .bands = bands;
080: }
081:
082: @Override
083: public String toString() {
084: String result = new Long(fixedAmount).toString();
085: for (int i = 0; i < bands.size(); i++) {
086: Band band = bands.get(i);
087: result += ";" + band.bandStart;
088: result += ";" + band.percentage;
089: }
090: return result;
091: }
092:
093: public long calculateRate(long amount) {
094: double total = 0;
095: int i = 1;
096: while (i < bands.size() && amount > bands.get(i).getBandStart()) {
097: total += (bands.get(i).getBandStart() - bands.get(i - 1)
098: .getBandStart())
099: * bands.get(i - 1).getPercentage();
100: i++;
101: }
102:
103: total += (amount - bands.get(i - 1).getBandStart())
104: * bands.get(i - 1).getPercentage();
105:
106: return (long) total;
107: }
108:
109: public long getFixedAmount() {
110: return fixedAmount;
111: }
112:
113: public List<Band> getBands() {
114: return Collections.unmodifiableList(bands);
115: }
116: }
|