import javax.swing.*;
import java.util.*;

public class Patron extends Person
{
	private Vector item = new Vector(); 	//vector can contain only objects

	public Patron()
	{
	}

	public Patron(int pid, String pname)
	{
		setID(pid);
		setName(pname);
	}


	public void checkItemOut(Item itemOut)
	{
		if (item.size() >= 3)
		{
			JOptionPane.showMessageDialog(null, "No more than 3 items please!",
					            "Checkout Error", JOptionPane.ERROR_MESSAGE);
		}
		else
		{
			if (itemOut.getHave() == true)
			{
				itemOut.checkOut();
				item.add(itemOut);	//vector has a method 'add' to add item
									//to vector of checked out items
									//(by the particular patron)
			}
			else
			{
				JOptionPane.showMessageDialog(null, "Item is not available",
					         "Checkout Error", JOptionPane.ERROR_MESSAGE);
			}
		}
	}


	/*
	Loop though all elements of vector to search for the element where
	the title from getTitle() is matched with the title, passed to
	the method,	what is String title.
	(In vector, item.size() is the same as item.length in array).

	Vector doesn't have a type String, or int... - it only has an Object type
	in return. Vector has a get() method (just like indexOf(), or add() ).
	((Item)item.get(i)) -- in return we get an object of an Object type.
	We covert then Object type into Item type (using cast - (Item) ).
	While it's of the Item type, we can get title of that item, and manipulate
	with it	(compare if title is matched in this case).
	*/


	public void checkItemIn(String title)
	{
		for ( int i = 0; i < item.size(); i++)
		{
			if (((Item)item.get(i)).getTitle().equals(title))
			{
				((Item)item.get(i)).checkIn();
				item.remove(i);		//remove from vector of borrowed items
			}
			else
			{
				JOptionPane.showMessageDialog(null, "Item was not checked out",
					               "CheckInError", JOptionPane.ERROR_MESSAGE);
			}
		}
	}


	public void checkItemIn(Item itemIn)
	{
		//vector has the 'contains' method that checks if item is
		//in the vector of checked out items (by the particular patron)

		if ( item.contains(itemIn))
		{
			int x =item.indexOf(itemIn);
			((Item)item.get(x)).checkIn();
			item.remove(itemIn);
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Item was not checked out",
				                  "CheckInError", JOptionPane.ERROR_MESSAGE);
		}
	}


	public String printPatron()			//public String toString()
	{
		String output = "";
		output+=getID()+", ";
		output+=getName()+": ";
		output+=item.size() + " item(s)\n";

		for (int i = 0; i< item.size(); i++)	//to print items that are checked out
		{											//(by the particular patron)
			if (item.size() == 0)		//if vector of checked out items is empty
			{
				output+= "no items checked out";
			}
			else
			{
				output+="--"+((Item)item.get(i)).getTitle();
			}
			output+="\n";
		}
		return output;
	}
}
