package io;

import java.util.*;
import convert.Hex;

/**
 * An example that uses the Checksum class
 *
 * @author  Prof. David Benrstein, James Madison University
 * @version 1.0
 */
public class ChecksumDriver
{
    /**
     * The entry point of the application
     *
     * @param args   The command line arguments
     */
    public static void main(String[] args)
    {
       boolean            ok;
       int                validationValue;
       String             checkValue, gpgga, token;
       StringTokenizer    tokenizer;
	

       // An NMEA  message from a GPS receiver
       gpgga = "$GPGGA,210230,3855.4487,N,09446.0071,W,"+
               "1,07,1.1,370.5,M,-29.5,M,,*7A";

       checkValue = "";
       validationValue  = 0;
       tokenizer = new StringTokenizer(gpgga, "$,");

       while (tokenizer.hasMoreElements())
       {
          token = (String)(tokenizer.nextElement());

          if (token.substring(0,1).equals("*"))
          {
             // The validation value (7A in the String above)
             checkValue = token.substring(1);
          } 
          else
          {
             // The check value
             validationValue = Checksum.addToChecksum(token, validationValue);
          }
       }

       ok = (validationValue == Hex.hexStringToInt(checkValue));


       if (ok) System.out.println("It's good!");
       else    System.out.println("No good!");
    }

}
