All pastes #437738 Raw Edit

phool

public text v1 · immutable
·published 2007-04-13 07:02 UTC
rendered paste body
//this is from phool in #c# undernet


using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication3
{
    //up to requirements as far as how stateful this needs to be
    [Serializable]
    public class Barcode
    {
        private string raw;


        private int age = -1;
        private DateTime birthDate = DateTime.MinValue;
        private DateTime expireDate = DateTime.MinValue;

        public Barcode(string rawBarcode)
        {

            //todo: validation logic here, throw ArgumentException

            raw = rawBarcode;
        }
        
        public string IDNumber
        {
            get
            {
                return raw.Substring(0, 9);
            }
        }

        
        public DateTime BirthDate
        {
            get
            {

                if (birthDate == DateTime.MinValue)
                {
                    birthDate =
                    new DateTime(
                     Convert.ToInt32(raw.Substring(12, 4)),
                    Convert.ToInt32(raw.Substring(16, 2)),
                    Convert.ToInt32(raw.Substring(18, 2))
                    );
                }
                return birthDate;
            }     
        }

        public DateTime ExpiresDate
        {
            get
            {

                if (expireDate == DateTime.MinValue)
                {
                    expireDate =
                    new DateTime(
                    Convert.ToInt32(raw.Substring(20, 4)),
                    Convert.ToInt32(raw.Substring(24, 2)),
                    Convert.ToInt32(raw.Substring(26, 2))
                    );
                }
                return expireDate;
            }
        }

        public int Age
        {
            get
            {
                if (age == -1)
                {
                    age = DateTime.Now.Year - BirthDate.Year;
                }

                return age;
            }

        }

        public bool Expired
        {
            get { return (DateTime.Now.Year - ExpiresDate.Year) > 0; }
        }

        //i really wouldn't return a string array but if you needed it for legacy its like
        //by the way this is pretty worthless.. why not just access Barcode.IDNumber etc.

        public String[] ToStringArray()
        {
            return new String[] { 
                this.IDNumber, 
                this.BirthDate.Month.ToString(), this.BirthDate.Day.ToString(), this.BirthDate.Year.ToString(),
                this.ExpiresDate.Month.ToString(), this.ExpiresDate.Day.ToString(), this.BirthDate.Year.ToString(),
                this.Age.ToString(),
                this.Expired.ToString() };

        }

        public override string ToString()
        {
            return raw;
        }
      

    }


    class Program
    {
        static void Main(string[] args)
        {
        }


        //this would be the method replacement
        //    //but even better would be to scrap this method and just pass the raw string right to your barcode instance in the code that gets it and uses it
        public static string[] idScanToStringArray(string sText)
        {
            Barcode bc = new Barcode(sText);

            Console.WriteLine(bc.ToString());

            return bc.ToStringArray();

        }

    

       

        


        //public static String[] idScanToStringArray(String sText)
        //{
        //    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
        //     * idScanToStringArray(1): 
        //     *  Extracts various information from a string representing a Virginia ID barcode scan.
        //     * 
        //     * Author: Ben VanDenburg
        //     * Date: 04 April 2007
        //     * Copyright: 2007 Ben VanDenburg. ALL RIGHTS RESERVED EXCEPT THOSE EXPLICITLY GRANTED.
        //     *      You may use this code provided you insert a comment in your code referencing my name 
        //     *          AND display that part of the program which uses this code has a visual reference 
        //     *          to my name in an obvious location (like the About box).
        //     * 
        //     * Precondition: sText is a string in the non 2D virginia ID barcode format (see below).
        //     * Postcondition: Returns String array in the following format:
        //                 *                  {   [0]=>IDNumber,
        //                 *                      [1]=>BirthDateM,
        //     *                      [2]=>BirthDateD,
        //     *                      [3]=>BirthDateY,
        //     *                      [4]=>ExpiryDateM,
        //     *                      [5]=>ExpiryDateD,
        //     *                      [6]=>ExpiryDateY,
        //                 *                      [7]=>Age,
        //                 *                      [8]=>Exired }
        //     |* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *|
        //                 * Virginia ID barcode format:
        //                 *  000000000   0000000000000000
        //                 *  [ID number][spaces][birthday][expiry]
        //                 *  [XXXXXXXXX][---][YYYMMDD][YYYYMMDD]
        //                 *  [123121234][123][12341212][12341212] 
        //                \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

        //    String sIdNum; //A00-00-0000 format or SSN (000-00-0000)
        //    String sBirthDate; //YYYMMDD
        //    String sExpDate; //YYYMMDD

        //    int iBYear, iBMonth, iBDay; //birthday vars
        //    int iEYear, iEMonth, iEDay; //expiration vars
        //    int iAge; //years
        //    bool bExpired = true; //has the license expired?

        //    //The entire length of the string is 9+3+8+8=12+16=28
        //    // 0 + 9 = [9 + 3] = 12 + 8 = 20 (substrings to take)
        //    sIdNum = sText.Substring(0, 9); //ID number (there are no dashes stored)

        //    sBirthDate = sText.Substring(12, 8);
        //    iBYear = Convert.ToInt32(sBirthDate.Substring(0, 4));//we want these in integer values to make it easier to compute age
        //    iBMonth = Convert.ToInt32(sBirthDate.Substring(4, 2));
        //    iBDay = Convert.ToInt32(sBirthDate.Substring(6));

        //    sExpDate = sText.Substring(20);
        //    iEYear = Convert.ToInt32(sExpDate.Substring(0, 4)); //we need these as ints as well for the same reason as above (how "old" is the expiration date?)
        //    iEMonth = Convert.ToInt32(sExpDate.Substring(4, 2));
        //    iEDay = Convert.ToInt32(sExpDate.Substring(6));

        //    iAge = CurrentAge(iBYear, iBMonth, iBDay); //get age in whole years

        //    if (CurrentAge(iEYear, iEMonth, iEDay) < 0) //is the expiration date in the future (negative age)?
        //        bExpired = false;


        //    String[] sResults = { 
        //        Convert.ToString(sIdNum),
        //                    Convert.ToString(iBMonth),
        //                Convert.ToString(iBDay),
        //                        Convert.ToString(iBYear),
        //                        Convert.ToString(iEMonth),
        //                        Convert.ToString(iEDay),
        //                        Convert.ToString(iEYear),
        //                        Convert.ToString(iAge),
        //                        Convert.ToString(bExpired)
        //    };//See comment above for human readable explination of this data structure.

        //    return sResults;
        //} 

    }
}