package ws.prova.examples;

/*
 * Copyright (C) 2002-2007 <a href="mailto:a.kozlenkov@city.ac.uk">Alex Kozlenkov</a>
 * Copyright (C) 1999-2005 <a href="mailto:mandarax@jbdietrich.com">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
 */

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * An example class for use in Prova tests implementing the usual order discount scenario
 * <p>Title: Prova</p>
 * <p>Description: An example class for use in Prova tests implementing the usual order discount scenario</p>
 * <p>Copyright: Copyright (C) 2002-2007</p>
 * <p>Company: City University, London, United Kingdom</p>
 *
 * @author Alex Kozlenkov
 * @version 2.0 <25 March 2007>
 */
public class Order implements Serializable {
	private String customer;
	private List items;
	private Map discounts;

	public Order(String customer) {
		this.customer = customer;
		items = new ArrayList();
		discounts = new HashMap();
	}

	public String toString() {
		String res = "Order(" + customer + ",(";
		for (int i = 0; i < items.size(); i++) {
			if (i != 0)
				res += ',';
			final Object item = items.get(i);
			res += item + ":" + discounts.get(item);
		}
		res += "))";
		return res;
	}

	public String getCustomer() {
		return customer;
	}

	public List getItems() {
		return items;
	}

	public void addItem(Object item) {
		items.add(item);
		discounts.put(item, new Double(1.0));
	}

	public void setDiscount(String item, double d) {
		Double discount0 = (Double) discounts.get(item);
		if (discount0 == null) {
			discounts.put(item, new Double(1 - d));
			return;
		}
		discounts.put(item, new Double(discount0.doubleValue() * (1 - d)));
	}
}
