View Javadoc

1   /********************************************************************************
2    * InternetCafe is a software solution that helps the management of Cybercafes 
3    * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4    * Copyright (C) 2006  Guido Angelo Ingenito
5   
6    * This program is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU General Public License
8    * as published by the Free Software Foundation; either version 2
9    * of the License, or (at your option) any later version.
10  
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   * 
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19   *******************************************************************************/
20  package base.service;
21  
22  import java.util.Date;
23  
24  import org.w3c.dom.Document;
25  import org.w3c.dom.Element;
26  import org.w3c.dom.Node;
27  
28  import ui.Messages;
29  import base.ICXmlTags;
30  import base.IXMLSaveable;
31  
32  public class Service implements IXMLSaveable {
33  
34  	private final int id;
35  
36  	private String name;
37  
38  	private String description;
39  
40  	private String rateType = ServiceRateType.RATE_TYPE[0];
41  
42  	private double baseRate = Double.parseDouble(Messages.getString("service.default.baserate")); //$NON-NLS-1$
43  
44  	private int minDurationInMinute = Integer.parseInt(Messages.getString("service.default.mindurationinminute")); //$NON-NLS-1$
45  
46  	private boolean careAboutMinDuration = true;
47  
48  	private int maxDurationInMinute = Integer.parseInt(Messages.getString("service.default.maxdurationinminute")); //$NON-NLS-1$
49  
50  	private boolean careAboutMaxDuration = false;
51  
52  	/***
53  	 * @param id
54  	 *            The Service's id.
55  	 * @param name
56  	 *            The Service's name.
57  	 * @param description
58  	 *            The Service's description.
59  	 * @param rateType
60  	 *            The Service's rateType.
61  	 * @param baseRate
62  	 *            The Service's baseRate.
63  	 * @param minDurationInMinute
64  	 *            The Service's minDurationInMinute.
65  	 * @param maxDurationInMinute
66  	 *            The Service's maxDurationInMinute.
67  	 * @param careAboutMaxDuration
68  	 *            States if must be applied the max duration or not.
69  	 */
70  	protected Service(int id, String name, String description, String rateType,
71  			double baseRate, int minDurationInMinute,
72  			boolean careAboutMinDuration, int maxDurationInMinute,
73  			boolean careAboutMaxDuration) {
74  		this.id = id;
75  		this.name = name;
76  		this.description = description;
77  		this.rateType = rateType;
78  		this.baseRate = baseRate;
79  		this.minDurationInMinute = minDurationInMinute;
80  		this.careAboutMinDuration = careAboutMinDuration;
81  		this.maxDurationInMinute = maxDurationInMinute;
82  		this.careAboutMaxDuration = careAboutMaxDuration;
83  	}
84  
85  	/***
86  	 * @return Returns the baseRate.
87  	 */
88  	public double getBaseRate() {
89  		return this.baseRate;
90  	}
91  
92  	/***
93  	 * @param baseRate
94  	 *            The baseRate to set.
95  	 */
96  	public void setBaseRate(double baseRate) {
97  		this.baseRate = baseRate;
98  	}
99  
100 	/***
101 	 * @return Returns the description.
102 	 */
103 	public String getDescription() {
104 		return this.description;
105 	}
106 
107 	/***
108 	 * @param description
109 	 *            The description to set.
110 	 */
111 	public void setDescription(String description) {
112 		this.description = description;
113 	}
114 
115 	/***
116 	 * @return Returns the name.
117 	 */
118 	public String getName() {
119 		return this.name;
120 	}
121 
122 	/***
123 	 * @param name
124 	 *            The name to set.
125 	 */
126 	public void setName(String name) {
127 		this.name = name;
128 	}
129 
130 	/***
131 	 * @return Returns the id.
132 	 */
133 	public int getId() {
134 		return id;
135 	}
136 
137 	/***
138 	 * @return Returns the rateType.
139 	 */
140 	public String getRateType() {
141 		return rateType;
142 	}
143 
144 	/***
145 	 * @param rateType
146 	 *            The rateType to set.
147 	 */
148 	public void setRateType(String rateType) {
149 		this.rateType = rateType;
150 	}
151 
152 	/***
153 	 * @return Returns the maxDurationInMinute.
154 	 */
155 	public int getMaxDurationInMinute() {
156 		return maxDurationInMinute;
157 	}
158 
159 	/***
160 	 * @param maxDurationInMinute
161 	 *            The maxDurationInMinute to set.
162 	 */
163 	public void setMaxDurationInMinute(int maxDurationInMinute) {
164 		this.maxDurationInMinute = maxDurationInMinute;
165 	}
166 
167 	/***
168 	 * @return Returns the minDurationInMinute.
169 	 */
170 	public int getMinDurationInMinute() {
171 		return minDurationInMinute;
172 	}
173 
174 	/***
175 	 * @param minDurationInMinute
176 	 *            The minDurationInMinute to set.
177 	 */
178 	public void setMinDurationInMinute(int minDurationInMinute) {
179 		this.minDurationInMinute = minDurationInMinute;
180 	}
181 
182 	/***
183 	 * This method determines a service's cost on the bases of the provided
184 	 * arguments.
185 	 * 
186 	 * @param startTime
187 	 *            The service's start time.
188 	 * @param endTime
189 	 *            The service's end time.
190 	 * @param rateType
191 	 *            The service's rate type.
192 	 * @param baseRate
193 	 *            The service's test.base rate.
194 	 * @return The cost of a service according to the provided arguments.
195 	 */
196 	public static double cost(Date startTime, Date endTime, String rateType,
197 			double baseRate) {
198 		double cost = 0.0;
199 		if (rateType
200 				.equals(ServiceRateType.RATE_TYPE[ServiceRateType.UNDEFINED]))
201 			cost = 0.0;
202 		else if (rateType
203 				.equals(ServiceRateType.RATE_TYPE[ServiceRateType.DAY]))
204 			cost = baseRate * durationInMinute(startTime, endTime) / 60 / 24;
205 		else if (rateType
206 				.equals(ServiceRateType.RATE_TYPE[ServiceRateType.HOUR]))
207 			cost = baseRate * durationInMinute(startTime, endTime) / 60;
208 		else if (rateType
209 				.equals(ServiceRateType.RATE_TYPE[ServiceRateType.MINUTE]))
210 			cost = baseRate * durationInMinute(startTime, endTime);
211 		return cost;
212 	}
213 
214 	/*
215 	 * (non-Javadoc)
216 	 * 
217 	 * @see java.lang.Object#toString()
218 	 */
219 	@Override
220 	public String toString() {
221 		return "Service: " + this.getName() + " Rate Type: " //$NON-NLS-1$ //$NON-NLS-2$
222 				+ this.getRateType() + " Base Rate: " + this.getBaseRate(); //$NON-NLS-1$
223 	}
224 
225 	/***
226 	 * @param startTime
227 	 *            A service's start time.
228 	 * @param endTime
229 	 *            A service's end time.
230 	 * @return A minute rappresentation of a service's duration.
231 	 */
232 	public static double durationInMinute(Date startTime, Date endTime) {
233 		return (endTime.getTime() - startTime.getTime()) / 1000 / 60;
234 	}
235 
236 	/***
237 	 * @return Returns the careAboutMaxDuration.
238 	 */
239 	public boolean careAboutMaxDuration() {
240 		return careAboutMaxDuration;
241 	}
242 
243 	/***
244 	 * @param careAboutMaxDuration
245 	 *            The careAboutMaxDuration to set.
246 	 */
247 	public void setCareAboutMaxDuration(boolean careAboutMaxDuration) {
248 		this.careAboutMaxDuration = careAboutMaxDuration;
249 	}
250 
251 	/***
252 	 * @return Returns the careAboutMinDuration.
253 	 */
254 	public boolean careAboutMinDuration() {
255 		return careAboutMinDuration;
256 	}
257 
258 	/***
259 	 * @param careAboutMinDuration
260 	 *            The careAboutMinDuration to set.
261 	 */
262 	public void setCareAboutMinDuration(boolean careAboutMinDuration) {
263 		this.careAboutMinDuration = careAboutMinDuration;
264 	}
265 
266 	/*
267 	 * (non-Javadoc)
268 	 * 
269 	 * @see test.base.IXMLSaveable#toXml(org.w3c.dom.Document)
270 	 */
271 	public Node toXml(Document document) {
272 		Element serviceElement = document
273 				.createElement(ICXmlTags.IC_SERVICE_TAG);
274 		serviceElement.setAttribute(ICXmlTags.IC_SERVICE_ID_ATTRIBUTE, "" //$NON-NLS-1$
275 				+ this.id);
276 
277 		serviceElement.setAttribute(
278 				ICXmlTags.IC_SERVICE_MIN_DURATION_IN_MINUTE_ATTRIBUTE, "" //$NON-NLS-1$
279 						+ this.minDurationInMinute);
280 		serviceElement.setAttribute(
281 				ICXmlTags.IC_SERVICE_CARE_ABOUT_MIN_DURATION_ATTRIBUTE, "" //$NON-NLS-1$
282 						+ this.careAboutMinDuration);
283 
284 		serviceElement.setAttribute(
285 				ICXmlTags.IC_SERVICE_MAX_DURATION_IN_MINUTE_ATTRIBUTE, "" //$NON-NLS-1$
286 						+ this.maxDurationInMinute);
287 		serviceElement.setAttribute(
288 				ICXmlTags.IC_SERVICE_CARE_ABOUT_MAX_DURATION_ATTRIBUTE, "" //$NON-NLS-1$
289 						+ this.careAboutMaxDuration);
290 
291 		Element nameElement = document
292 				.createElement(ICXmlTags.IC_SERVICE_NAME_TAG);
293 		nameElement.setAttribute(ICXmlTags.IC_VALUE_ATTRIBUTE, this.name);
294 		serviceElement.appendChild(nameElement);
295 
296 		Element rateTypeElement = document
297 				.createElement(ICXmlTags.IC_SERVICE_RATE_TYPE_TAG);
298 		rateTypeElement.setAttribute(ICXmlTags.IC_VALUE_ATTRIBUTE,
299 				this.rateType);
300 		serviceElement.appendChild(rateTypeElement);
301 
302 		Element baseRateElement = document
303 				.createElement(ICXmlTags.IC_SERVICE_BASE_RATE_TAG);
304 		baseRateElement.setAttribute(ICXmlTags.IC_VALUE_ATTRIBUTE, "" //$NON-NLS-1$
305 				+ this.baseRate);
306 		serviceElement.appendChild(baseRateElement);
307 
308 		Element descriptionElement = document
309 				.createElement(ICXmlTags.IC_SERVICE_DESCRIPTION_TAG);
310 		descriptionElement.setAttribute(ICXmlTags.IC_VALUE_ATTRIBUTE,
311 				this.description);
312 		serviceElement.appendChild(descriptionElement);
313 
314 		return serviceElement;
315 	}
316 
317 }