Java tutorial
/******************************************************************************* * Copyright (c) 2009 David Harrison. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Public License v3.0 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/gpl-3.0.html * * Contributors: * David Harrison - initial API and implementation ******************************************************************************/ package com.sfs.whichdoctor.formatter; import com.sfs.Formatter; import com.sfs.whichdoctor.beans.ExpenseClaimBean; import org.apache.commons.lang.StringUtils; public class ExpenseClaimFormatter { /** The Constant DEFAULT_CURRENCY_ABBR. */ private static final String DEFAULT_CURRENCY_ABBR = "NZD"; /** The Constant MAX_QUANTITY. */ private static final int MAX_QUANTITY = 15; public static String getField(final ExpenseClaimBean expenseClaim, final String field) { String value = ""; if (StringUtils.equalsIgnoreCase(field, "Quantity")) { value = getQuantity(expenseClaim); } return value; } private static String getQuantity(final ExpenseClaimBean expenseClaim) { final StringBuffer value = new StringBuffer(); if (StringUtils.equals(expenseClaim.getExpenseClass(), "Travel") && StringUtils.equals(expenseClaim.getExpenseType(), "Petrol (Lump sum)")) { if (StringUtils.isNotBlank(expenseClaim.getCurrencyAbbreviation()) && !StringUtils.equals(expenseClaim.getCurrencyAbbreviation(), DEFAULT_CURRENCY_ABBR)) { value.append(expenseClaim.getCurrencyAbbreviation()); } value.append(Formatter.toCurrency(expenseClaim.getRate())); } if (StringUtils.equals(expenseClaim.getExpenseClass(), "Travel") && StringUtils.equals(expenseClaim.getExpenseType(), "Mileage Reimbursement")) { value.append(expenseClaim.getQuantity() + " kilometers @ "); value.append(Formatter.toCurrency(expenseClaim.getRate())); if (StringUtils.isNotBlank(expenseClaim.getCurrencyAbbreviation()) && !StringUtils.equals(expenseClaim.getCurrencyAbbreviation(), DEFAULT_CURRENCY_ABBR)) { value.append(expenseClaim.getCurrencyAbbreviation()); } value.append(" per kilometer"); } if (value.length() == 0) { value.append(expenseClaim.getQuantity() + " @ "); if (StringUtils.isNotBlank(expenseClaim.getCurrencyAbbreviation()) && !StringUtils.equals(expenseClaim.getCurrencyAbbreviation(), DEFAULT_CURRENCY_ABBR)) { value.append(expenseClaim.getCurrencyAbbreviation()); } value.append(Formatter.toCurrency(expenseClaim.getRate())); } if (StringUtils.isNotBlank(expenseClaim.getDescription())) { if (value.length() > 0) { value.append(" - "); } value.append(expenseClaim.getDescription()); } if (StringUtils.isNotBlank(expenseClaim.getCurrencyAbbreviation()) && !StringUtils.equals(expenseClaim.getCurrencyAbbreviation(), DEFAULT_CURRENCY_ABBR)) { if (value.length() > MAX_QUANTITY) { value.append("<br/>"); } value.append(" (Rate: " + expenseClaim.getCurrencyAbbreviation() + "$1 = " + DEFAULT_CURRENCY_ABBR + "$" + expenseClaim.getExchangeRate() + ")"); } return value.toString(); } }