package aood.ex7;

import java.util.Collection;
import java.util.Vector;

/**
 * Statistics on customer.
 */
public class Statistics {
	private Customer bestCustomer = null;
	private Customer[] customers = null;
	private Collection bigCustomers = new Vector();
	private Collection noRentalCustomers = new Vector();
	
	public void setCustomers(Customer[] customers) {
		this.customers = customers;		
	}
	
	public void calculate() {
		bestCustomer = ((customers == null) || (customers.length == 0)) ? null : customers[0];
		bigCustomers.clear();
		noRentalCustomers.clear();
			 
		for (int i = 1; i < customers.length; i++) {
			if (bestCustomer.getFrequentRenterPoints() < customers[i].getFrequentRenterPoints())
				bestCustomer = customers[i];
			
			if (checkTotalAmount(customers[i]) && checkFrequentRenterPoints(customers[i]))
				bigCustomers.add(customers[i]);
			
			if (!hasRental(customers[i]))
				noRentalCustomers.add(customers[i]);
		}
	}
	
	public String getBestCustomerStatement() {
		if (bestCustomer == null) return "none";
		return bestCustomer.statement();
	}
	
	public Collection getBigCustomers() {
		return bigCustomers;
	}
	
	public Collection getNoRentalCustomers() {
		return noRentalCustomers;
	}
	
	protected boolean checkTotalAmount(Customer customer) {
		double totalAmount;
		
		if (customer != null) totalAmount = customer.getTotalAmount();
		else totalAmount = 0;
				
		return (totalAmount > 10);
	}
	
	protected boolean checkFrequentRenterPoints(Customer customer) {
		int frequentRenterPoints;
		
		if (customer == null) frequentRenterPoints = 0;
		else frequentRenterPoints = customer.getFrequentRenterPoints();
		
		return (frequentRenterPoints > 5);
	}
	
	protected boolean hasRental(Customer customer) {
		int frequentRenterPoints;
		double totalAmount;
		
		if (customer != null) frequentRenterPoints = customer.getFrequentRenterPoints();
		else frequentRenterPoints = 0;
		if (customer == null) totalAmount = 0;
		else totalAmount = customer.getTotalAmount();
		
		return (totalAmount > 0) && (frequentRenterPoints > 0);		
	}
}
