Tuesday, May 22, 2012

Code for Scanner and Testing

We programmed the core functionality of the scanner in order to test it. First here are some diagrams of the code

Class Diagram


Sequence Diagram


Now here is the code for the classes

Program.cs

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

namespace DoorCardProofOfConcept
{
    class Program
    {
      
    
        static void Main(string[] args)
        {
            int exit = 1;
            while (exit != -1)
            {
                Console.WriteLine("Enter Card Number");
                string card = Console.ReadLine();
                Console.WriteLine("enter the Door number");
                string door = Console.ReadLine();
                Scanner s = new Scanner(card, door);
                Console.WriteLine(s.DoorState);
                Console.WriteLine("Enter -1 to Exit");
                exit = int.Parse(Console.ReadLine());
            }

           // Console.ReadKey();

        }
    }
}


Scanner.cs

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

namespace DoorCardProofOfConcept
{
    /// 
    /// This class represents the physical
    /// scanner. It reads the card
    /// creates a scan object and sends it
    /// off to be validated
    /// if the validation returns true
    /// it opens the door
    /// 
    /// 
    class Scanner
    {
        string cardNumber;
        string scannerID;
        string doorState;
        Scan scan;

        public Scanner(string card, string ID)
        {
            cardNumber = card;
            scannerID = ID;
            doorState = "closed";
            CreateScan();
            getScanValidated();

        }

        public string DoorState
        {
            get { return doorState; }
        }

        internal Validator Validator
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }

        internal Scan Scan
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }

        private void CreateScan()
        {
            DateTime date = DateTime.Now;
            scan = new Scan(scannerID,cardNumber, date);
        }

        public void getScanValidated()
        {
            bool isValid = false;
            Validator v = new Validator(scan);
            isValid = v.Validate();
            if (isValid)
            {
                doorState = "Open";
            }
            
        }
    }
}


Scan.cs

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

namespace DoorCardProofOfConcept
{
    class Scan
    {
        /// 
        /// this class is simply a collection
        /// of data so that it can be passed
        /// as a single package
        /// 
        /// 
        /// 
        /// 

        public Scan(string number, string card, DateTime date)
        {
            scannerNumber = number;
            cardNumber = card;
            this.date = date;

        }
        private string scannerNumber;

        public string ScannerNumber
        {
            get { return scannerNumber; }
           
        }
        private string cardNumber;

        public string CardNumber
        {
            get { return cardNumber; }
           
        }
        private DateTime date;

        public DateTime Date
        {
            get { return date; }
           
        }
        

    }
}


Validator

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DoorCardProofOfConcept
{
    /// 
    /// this class recieve the scan
    /// and validate its contents
    /// against a database
    /// (for now an xmlFile)
    /// 
    class Validator
    {

        private Scan scan;

        public Validator(Scan s)
        {
            scan = s;
        }

        public bool Validate()
        {
            bool valid = false;
            DataSet table = new DataSet();
            table.ReadXml(@"ScheduleData.xml");

            foreach (DataRow row in table.Tables[0].Rows)
            {
                if (scan.CardNumber.Equals(row["card"].ToString()) &&
                    scan.ScannerNumber.Equals(row["door"].ToString()) &&
                    scan.Date.ToShortDateString().Equals(row["date"].ToString())
                    && scan.Date>=DateTime.Parse(row["begintime"].ToString())
                     && scan.Date<=DateTime.Parse(row["endtime"].ToString()))
                {
                    valid = true;
                }


            }

            return valid;
        }
    }
}


ScannerData.xml

<?xml version="1.0" encoding="utf-8" ?>
<schedule>
  <item>
    <card>100</card>
    <door>1</door>
    <date>5/22/2012</date>
    <begintime>9:00</begintime>
    <endtime>17:00</endtime>
  </item>
  <item>
    <card>100</card>
    <door>2</door>
    <date>5/23/2012</date>
    <begintime>9:00</begintime>
    <endtime>13:00</endtime>
  </item>
  <item>
    <card>101</card>
    <door>2</door>
    <date>5/22/2012</date>
    <begintime>10:00</begintime>
    <endtime>15:00</endtime>
  </item>
</schedule>

Here is the spreadsheet of our results

No comments:

Post a Comment