import java.awt.*;
import java.lang.*;
import java.io.*;
import java.util.*;

public class CustomerList
{
  protected long customerNo, PIN, cardNo;
  protected Account[] accounts = new Account[3];

  private static String fileName = new String ("customers.rpt");
  private static int MaxNumAccounts = 3;

  private FileInputStream	fileIn;
  private FileOutputStream	fileOut;
  private FilterInputStream	filterIn;
  private FilterOutputStream	filterOut;
  private DataInputStream	dataIn;
  private DataOutputStream	dataOut;
  private BufferedInputStream	bufferedIn;
  private BufferedOutputStream	bufferedOut;


  public CustomerList ()
  {
    this (0, 0, 0);
  }

  public CustomerList (long customerNo, long cardNo, long PIN)
  {
    this.customerNo = customerNo;
    this.cardNo = cardNo;
    this.PIN = PIN;

    for (int i=0; i<MaxNumAccounts; i++)
      accounts[i] = new Account();
  }

  public String toString ()
  {
    String out = new String();

    out += customerNo +"\t";
    out += cardNo +"\t";
    out += PIN +"\n\t";

    for (int i=0; i<MaxNumAccounts; i++)
      out += accounts[i] +"\n\t";

    out.trim();
    return out;
  }

  public void addCustomer ()
  {
    try
    {
      fileOut = new FileOutputStream (fileName, true);
      bufferedOut = new BufferedOutputStream (fileOut);
      dataOut = new DataOutputStream (bufferedOut);

      try
      {
        dataOut.writeUTF (this.toString()+"\n");
      }
      catch (IOException e)
      {
        System.out.println ("Could not write customer "+ this.toString());
      }
    }
    catch (FileNotFoundException e)
    {
      System.out.println ("Could not open file to write customer "+ this.toString());
    }
    finally
    {
      try
      {
        dataOut.close();
        bufferedOut.close();
        fileOut.close();
      }
      catch (IOException e)
      {
        System.out.println ("Could not close dataOut, bufferedOut or fileOut during customer "+ this.toString());
      }
    }
  }

  public void setCustomer (long customerNo, long cardNo, long PIN)
  {
    this.customerNo = customerNo;
    this.cardNo = cardNo;
    this.PIN = PIN;
  }

  public void setCustomer (String customer)
  {
    String tempPIN = new String();
    String tempCardNo = new String();
    String tempCustomerNo = new String();

    int i, flag = 0, start = 0;
    boolean add = true;

    while (!(Character.isDigit(customer.charAt(start))) && !(Character.isLetter(customer.charAt(start))))
      start++;

    for (i = start; i < customer.length(); i++)
    {
      if (!(Character.isDigit(customer.charAt(i))) && !(Character.isLetter(customer.charAt(i))))
      {
        add = false;
        flag++;
      }
      else
        add = true;

//System.out.println ("i == "+ i +"\tcustomer.charAt(i) == "+ customer.charAt(i) +"\tflag == "+ flag +"\tadd == "+ add);
      if (add)
      {
        switch (flag)
        {
          case (0):
            tempCustomerNo += customer.charAt(i);
            break;
          case (1):
            tempCardNo += customer.charAt(i);
            break;
          case (2):
            tempPIN += customer.charAt(i);
            break;
        }
      }
    }

    this.customerNo = Long.decode(tempCustomerNo).longValue();
    this.cardNo = Long.decode(tempCardNo).longValue();
    this.PIN = Long.decode(tempPIN).longValue();
  }


  public boolean authorizeCustomerNo (long inputCustomerNo, long inputPIN)
  {
    this.searchCustomer (inputCustomerNo);
    return (this.PIN == inputPIN);
  }

  public boolean authorizeCardNo (long inputCardNo, long inputPIN)
  {
    this.searchCard (inputCardNo);
    return (this.PIN == inputPIN);
  }


  public void searchCustomer (long customerNo)
  {
    String customerFromFile = new String();
    String accountFromFile = new String();

    try
    {
      fileIn = new FileInputStream (fileName);
      bufferedIn = new BufferedInputStream (fileIn);
      dataIn = new DataInputStream (bufferedIn);

      /*  read cstLst from file  */
      /*  while not EOF  */

      try
      {
        customerFromFile = dataIn.readUTF();
        while (customerFromFile != null)
        {
          this.setCustomer (customerFromFile);
          for (int i=0; i<MaxNumAccounts; i++)
          {
            try
            {
              accountFromFile = dataIn.readUTF();
              this.accounts[i].setAccount (accountFromFile);
            }
            catch (IOException e) {}
          }
          if (this.customerNo == customerNo)
            break;
          customerFromFile = dataIn.readUTF();
        }
        System.out.println ("\n");
      }
      catch (IOException e)
      {
        System.out.println ("\n");
        //System.out.println ("Could not read dataIn...");
      }
    }
    catch (FileNotFoundException e)
    {
      System.out.println ("Could not open file to read customer "+ this.toString());
    }
    finally
    {
      try
      {
        dataIn.close();
        bufferedIn.close();
        fileIn.close();
      }
      catch (IOException e)
      {
        System.out.println ("Could not close dataIn, bufferedIn or fileIn during customer "+ this.toString());
      }
    }
  }


  public int searchAccount (long accountNo)
  {
    String customerFromFile = new String();
    String accountFromFile = new String();
    int acct = -1;

    try
    {
      fileIn = new FileInputStream (fileName);
      bufferedIn = new BufferedInputStream (fileIn);
      dataIn = new DataInputStream (bufferedIn);

      /*  read cstLst from file  */
      /*  while not EOF  */

      try
      {
        customerFromFile = dataIn.readUTF();
        while (customerFromFile != null)
        {
          this.setCustomer (customerFromFile);
          try
          {
            for (int i=0; i<MaxNumAccounts; i++)
            {
              accountFromFile = dataIn.readUTF();
              this.accounts[i].setAccount (accountFromFile);
            }

            for (int i=0; i<MaxNumAccounts; i++)
              if (this.accounts[i].checkAccount (accountNo))
              {
                acct = i;
                break;
              }
          }
          catch (IOException e) {}
          customerFromFile = dataIn.readUTF();
        }
        System.out.println ("\n");
      }
      catch (IOException e)
      {
        System.out.println ("\n");
        //System.out.println ("Could not read dataIn...");
      }
    }
    catch (FileNotFoundException e)
    {
      System.out.println ("Could not open file to read customer "+ this.toString());
    }
    finally
    {
      try
      {
        dataIn.close();
        bufferedIn.close();
        fileIn.close();
      }
      catch (IOException e)
      {
        System.out.println ("Could not close dataIn, bufferedIn or fileIn during customer "+ this.toString());
      }
    }

    return acct;
  }


  public void searchCard (long cardNo)
  {
    String customerFromFile = new String();
    String accountFromFile = new String();

    try
    {
      fileIn = new FileInputStream (fileName);
      bufferedIn = new BufferedInputStream (fileIn);
      dataIn = new DataInputStream (bufferedIn);

      /*  read cstLst from file  */
      /*  while not EOF  */

      try
      {
        customerFromFile = dataIn.readUTF();
        while (customerFromFile != null)
        {
          this.setCustomer (customerFromFile);
          for (int i=0; i<MaxNumAccounts; i++)
          {
            try
            {
              accountFromFile = dataIn.readUTF();
              this.accounts[i].setAccount (accountFromFile);
            }
            catch (IOException e) {}
          }
          if (this.cardNo == cardNo)
            break;
          customerFromFile = dataIn.readUTF();
        }
        System.out.println ("\n");
      }
      catch (IOException e)
      {
        System.out.println ("\n");
        //System.out.println ("Could not read dataIn...");
      }
    }
    catch (FileNotFoundException e)
    {
      System.out.println ("Could not open file to read customer "+ this.toString());
    }
    finally
    {
      try
      {
        dataIn.close();
        bufferedIn.close();
        fileIn.close();
      }
      catch (IOException e)
      {
        System.out.println ("Could not close dataIn, bufferedIn or fileIn during customer "+ this.toString());
      }
    }
  }


}
