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 boolean checkItemOut(Item itemOut)
	{
		if (item.size() >= 3)
		{
				JOptionPane.showMessageDialog(null, "No more than 3 items please!","Checkout Error",
					JOptionPane.ERROR_MESSAGE);
					return false;
		}
		else
		{
			if (itemOut.getHave() == true)
			{
				itemOut.checkOut();
				item.add(itemOut);	//vertor has a method 'add'
				//to add item back to vertor (what is check item in)
				return true;
			}
			else
			{
				JOptionPane.showMessageDialog(null, "Item is not available","Checkout Error",
					JOptionPane.ERROR_MESSAGE);
				return false;
			}
		}
	}


	public boolean checkItemIn(Item itemIn)
	{
		//vector has the 'contains' method that checks if item is in the vector
		//of checked out items (if item is checked out)
		if ( item.contains(itemIn))
		{
			int x =item.indexOf(itemIn);
			((Item)item.get(x)).checkIn();
			item.remove(itemIn);
			return true;
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Item was not checked out","CheckInError",
				JOptionPane.ERROR_MESSAGE);
			return false;
		}
	}


	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 part. patron)
		{
			if (item.size() == 0)	//if vector of checked out items is empty
			{
				output+= "no items";
			}
			else
			{
				output+="--"+((Item)item.get(i)).getTitle();
			}
			output+="\n";
		}
		return output;
	}


	public Vector getItemVector ()
	{
		return item;
	}


	public Item getItem(int i)
	{
		return (Item)(item.get(i)); 	//returns object of type Item (cast)
	}


	public String[] getItemTitles()
	{
			String [] temp;
			if(item.size() == 0)
			{
				temp = new String[1];
				temp[0] = "No items checked out";
			}
			else
			{
				temp = new String[item.size()];
				for (int i = 0; i < temp.length; i++)
				{
					temp[i] = ((Item)(item.get(i))).getTitle();
				}
			}

			return temp;
	}
}
