📄 discount.java
字号:
/*
* Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.mandarax.examples.crm.domainmodel;
/**
* Discount. A discount can be given in percent (relative
* discount) or in $ (absolute discount).
* @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
* @version 3.4 <7 March 05>
* @since 1.2
*/
public class Discount extends BusinessObject {
private double rate = 0;
private boolean relative = true;
/**
* Constructor. The discount will be relative (a percentage).
*/
public Discount() {
super ();
}
/**
* Constructor. The discount will be relative (a percentage)
* @param d the discount
*/
public Discount(double d) {
super ();
rate = d;
}
/**
* Constructor. The discount will be relative (a percentage)
* @param d the discount
* @param rel - if true, the discount will be relative (a percentage),
* if false, the discount will be absolute (an amount of US $)
*/
public Discount(double d, boolean rel) {
super ();
rate = d;
relative = rel;
}
/**
* Apply the discount to an amount.
* @return the final amount (-discount)
* @param amount an amount
*/
public double apply(double amount) {
if(relative) {
return(100 - rate) * amount / 100;
} else {
return Math.max (0, amount - rate);
}
}
/**
* Get the rate.
* @return the discount rate (either an absolute amount or a percentage value)
*/
public double getRate() {
return rate;
}
/**
* Indicates whether the discount is relative.
* @return true if the discount is a percentage, false otherwise (= if it is an absolute amount)
*/
public boolean isRelative() {
return relative;
}
/**
* Set the rate.
* @param r a value
*/
public void setRate(double r) {
rate = r;
}
/**
* Set the relative property.
* @param flag true if this should be a percentage value, false otherwise
*/
public void setRelative(boolean flag) {
relative = flag;
}
/**
* Convert the object to a string.
*/
public String toString() {
return String.valueOf (rate) + (relative
? "%"
: "$");
}
/**
* Compares objects.
* @return boolean
* @param obj java.lang.Object
*/
public boolean equals(Object obj) {
return obj!=null && (obj instanceof Discount) && ((Discount) obj).relative==relative && ((Discount) obj).rate==rate;
}
/**
* Get the hash code of the object.
* @return int
*/
public int hashCode() {
return (int)rate;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -