import java.math.BigDecimal;
import java.util.Vector;

public class MultipleCurrency
{

 protected Encap totalAP = new Encap("AP");
 protected Encap totalAR = new Encap("AR");

 public MultipleCurrency()
 { 
  initCurr(); 
 }

 public MultipleCurrency(String[] s)
 {
  initCurr(s);
 }

 private void initCurr()
 {
  initCurr("IDR");
  initCurr("USD");
 }

 public void initCurr(String a)
 {
  totalAR.setNameVal(a,new BigDecimal("0.00"));
  totalAP.setNameVal(a,new BigDecimal("0.00"));
 }

 public void initCurr(String[] s)
 {
  int len = s.length;
  for(int i=0;i<len;i++)
   initCurr(s[i]);
 }

 public int getSize()
 {
  return totalAR.getSize();
 }

 public int getCurr(String str)
 {
  return totalAP.getPosition(str);
 }

 public String getCurr(int i)
 {
  if(i<0 || i>totalAP.getSize())
   return null;
  return totalAP.getName(i);
 }

 public void sumAR(String curr,String amt)
 {
  int pos = getCurr(curr);
  if(pos>=0)
  {
   BigDecimal tmp = totalAR.getDecValue(pos);
   tmp = tmp.add(new BigDecimal(amt));
   totalAR.replaceValue(pos,tmp);
   tmp = null;
  }
  else
  {
   System.out.println("The currency is not found! : "+curr);
   Mandalatama.errorDisplay("The currency [A/R] of "+curr+
   " is not found\n","ERROR");
  } 
 }

 public void sumAP(String curr,String amt)
 {
  int pos = getCurr(curr);
  if(pos>=0)
  {
   BigDecimal tmp = totalAP.getDecValue(pos);
   tmp = tmp.add(new BigDecimal(amt));
   totalAP.replaceValue(pos,tmp);
   tmp = null;
  }
  else
  {
   System.out.println("The currency is not found!");
   Mandalatama.errorDisplay("The currency [A/P] of "+curr+
   " is not found\n","ERROR");
  } 
 }

 public void subAP(String curr,String amt)
 {
  int pos = getCurr(curr);
  if(pos>=0)
  {
   BigDecimal tmp = totalAP.getDecValue(pos);
   tmp = tmp.subtract(new BigDecimal(amt));
   totalAP.replaceValue(pos,tmp);
   tmp = null;
  }
  else
  {
   System.out.println("The currency is not found!");
   Mandalatama.errorDisplay("The currency [A/P] of "+curr+
   " is not found\n","ERROR");
  } 
 }

 public void subAR(String curr,String amt)
 {
  int pos = getCurr(curr);
  if(pos>=0) 
  {
   BigDecimal tmp = totalAR.getDecValue(pos);
   tmp = tmp.subtract(new BigDecimal(amt));
   totalAR.replaceValue(pos,tmp);
   tmp = null;
  }
  else
  {
   System.out.println("The currency is not found! : "+curr);
   Mandalatama.errorDisplay("The currency [A/R] of "+curr+
   " is not found\n","ERROR");
  } 
 }

 public boolean checkAP(int i)
 {
  if(i<0 || i>totalAP.getSize()) return false;
  if(!(totalAP.getValue(i)).equalsIgnoreCase("0.00"))
   return true;
  return false;
 }

 public boolean checkAR(int i)
 {
  if(i<0 || i>totalAR.getSize()) return false;
  if(!(totalAR.getValue(i)).equalsIgnoreCase("0.00"))
   return true;
  return false;
 }

 public boolean checkAllFilledAR()
 {
  int size = totalAR.getSize();
  for(int i=0;i<size;i++)
  { if(checkAR(i)) return true; } 
  return false;
 }

 public boolean checkAllFilledAP()
 {
  int size = totalAP.getSize();
  for(int i=0;i<size;i++)
  { if(checkAP(i)) return true; } 
  return false;
 }

 public boolean checkAllFilled()
 {
  if(checkAllFilledAR()) return true;
  if(checkAllFilledAP()) return true;
  return false;
 }

 public BigDecimal getSumAR(int i)
 {
  if(i<0 || i>totalAR.getSize()) return null;
  return totalAR.getDecValue(i);
 }

 public BigDecimal getSumAP(int i)
 {
  if(i<0 || i>totalAP.getSize()) return null;
  return totalAP.getDecValue(i);
 }

 public BigDecimal getSumAR(String str)
 {
  return totalAR.extractBigDecimal(str);
 }

 public BigDecimal getSumAP(String str)
 {
  return totalAP.extractBigDecimal(str);
 }

 private void clearSumAP()
 {
  int size = totalAP.getSize();
  for(int i=0;i<size;i++)
   totalAP.replaceValue(i,new BigDecimal("0.00"));
 }

 private void clearSumAR()
 {
  int size = totalAR.getSize();
  for(int i=0;i<size;i++)
   totalAR.replaceValue(i,new BigDecimal("0.00"));
 }

 public void clearVariable()
 {
  clearSumAR();
  clearSumAP();
 }

 public void eraseAll()
 {
  totalAR.clear();
  totalAP.clear();
 }
}
