001: /*
002: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package org.mandarax.examples.db;
020:
021: /**
022: * Discount. A discount can be given in percent (relative
023: * discount) or in $ (absolute discount). This class is a very simple business object type
024: * and shows how (arbitrary) objects are integrated. Being serializable is not required but recommended to support
025: * technologies such as Serialization, RMI and EJB.
026: * @since 2.2
027: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
028: * @version 3.4 <7 March 05>
029: */
030: public class Discount implements java.io.Serializable {
031: private double rate = 0;
032: private boolean relative = true;
033:
034: /**
035: * Constructor.
036: */
037: public Discount() {
038: super ();
039: }
040:
041: /**
042: * Constructor. The discount will be relative (a percentage)
043: * @param d the discount
044: */
045: public Discount(double d) {
046: super ();
047: rate = d;
048: }
049:
050: /**
051: * Constructor. The discount will be relative (a percentage)
052: * @param d the discount
053: * @param rel - if true, the discount will be relative (a percentage),
054: * if false, the discount will be absolute (an amount of US $)
055: */
056: public Discount(double d, boolean rel) {
057: super ();
058: rate = d;
059: relative = rel;
060: }
061:
062: /**
063: * Apply the discount to an amount.
064: * @return the final amount (-discount)
065: * @param amount an amount
066: */
067: public double apply(double amount) {
068: if (relative)
069: return (100 - rate) * amount / 100;
070: else
071: return Math.max(0, amount - rate);
072: }
073:
074: /**
075: * Get the rate.
076: * @return the discount rate (either an absolute amount or a percentage value)
077: */
078: public double getRate() {
079: return rate;
080: }
081:
082: /**
083: * Indicates whether the discount is relative.
084: * @return true if the discount is a percentage, false otherwise (= if it is an absolute amount)
085: */
086: public boolean isRelative() {
087: return relative;
088: }
089:
090: /**
091: * Set the rate.
092: * @param r a value
093: */
094: public void setRate(double r) {
095: rate = r;
096: }
097:
098: /**
099: * Set the relative property.
100: * @param flag true if this should be a percentage value, false otherwise
101: */
102: public void setRelative(boolean flag) {
103: relative = flag;
104: }
105:
106: /**
107: * Convert the object to a string.
108: */
109: public String toString() {
110: return String.valueOf(rate) + (relative ? "%" : "$");
111:
112: }
113: }
|