Calculating the sum of digit

public int SumOfDigit(long num)
{
   long sum = 0;
   while (num > 0)
   {
      sum = sum + (num % 10);
      num = num / 10;
   }
   return sum;
}
Advertisements

Retrieving the conditional intersection of the tables

First we create three tables and fill them with sample data.

CREATE TABLE [dbo].[DataSet1](
	[Id] [INT] IDENTITY(1,1) NOT NULL,
	[Col1] [CHAR](1) NOT NULL
) ON [PRIMARY]

GO

CREATE TABLE [dbo].[DataSet2](
	[Id] [INT] IDENTITY(1,1) NOT NULL,
	[Col1] [CHAR](1) NOT NULL
) ON [PRIMARY]

GO

CREATE TABLE [dbo].[DataSet3](
	[Id] [INT] IDENTITY(1,1) NOT NULL,
	[Col1] [CHAR](1) NOT NULL
) ON [PRIMARY]

GO

INSERT INTO dbo.DataSet1(Col1)
SELECT 'A'
UNION ALL
SELECT 'Z'
UNION ALL
SELECT 'Y'

INSERT INTO dbo.DataSet2(Col1)
SELECT 'Z'
UNION ALL
SELECT 'X'
UNION ALL
SELECT 'Y'

INSERT INTO dbo.DataSet3(Col1)
SELECT 'A'
UNION ALL
SELECT 'Z'
UNION ALL
SELECT 'X'
UNION ALL
SELECT 'B'

We have to retrieve the intersection of these tables based on conditions.

DECLARE @takeDataSet1Dimension BIT = 1;
DECLARE @takeDateSet2Dimension BIT = 0;
DECLARE @takeDateSet3Dimension BIT = 1;

DECLARE @dimensionCounter INT = 0;

IF (@takeDataSet1Dimension = 1)
    SET @dimensionCounter = @dimensionCounter + 1;

IF (@takeDateSet2Dimension = 1)
    SET @dimensionCounter = @dimensionCounter + 1;
    
IF (@takeDateSet3Dimension = 1)
    SET @dimensionCounter = @dimensionCounter + 1;
    
WITH d AS (
SELECT Col1
FROM dbo.DataSet1
WHERE @takeDataSet1Dimension = 1
UNION ALL
SELECT Col1
FROM dbo.DataSet2
WHERE @takeDateSet2Dimension = 1
UNION ALL
SELECT Col1
FROM dbo.DataSet3
WHERE @takeDateSet3Dimension = 1
), dd AS (
    SELECT ROW_NUMBER() OVER (PARTITION BY Col1 ORDER BY Col1) AS dimensionAmount, Col1
   FROM d
)
select Col1, dimensionAmount
from dd
where dimensionAmount = @dimensionCounter

In this example, we take the intersection of the first and third table. We will get two rows: A and Z

Command Pattern

Command pattern allows you to encapsulate a command as an object.

Interface ICommand has only one method: Execute. In this place you can also put Undo method if you would like to have this kind of functionality.

    public interface ICommand
    {
        void Execute();
    }

I would like to have possibitity to register person for some kind of training. To do this I am creating RegisterForTrainingCommand class that implements ICommand interface.

 
    public class RegisterForTrainingCommand : ICommand
    {
        private readonly ITraining _training;
        private readonly IPerson _person;

        public RegisterForTrainingCommand(ITraining training, IPerson person)
        {
            _training = training;
            _person = person;
        }

        public void Execute()
        {
            _training.Register(_person);
        }
    }

Once I have possibility to register for training I can also create command for paying for training.

    public class PayForTrainingCommand : ICommand
    {
        private readonly ITraining _training;
        private readonly IPerson _person;
        private readonly decimal _amount;

        public PayForTrainingCommand(ITraining training, IPerson person, decimal amount)
        {
            _training = training;
            _person = person;
            _amount = amount;
        }

        public void Execute()
        {
            _training.Pay(_person, _amount);
        }
    }

Now, I am creating Invoker class which is responsibility for executing all passing commands.

    public class Invoker
    {
        private readonly List _commands;

        public Invoker(List commands)
        {
            _commands = commands;
        }

        public void ExecuteCommands()
        {
            _commands.ForEach(x=>x.Execute());
        }
    }

We need an implementation of Traning class as well as Person class

    public class Training : ITraining
    {
        public string Name { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public decimal Price { get; set; }

        public void Register(IPerson person)
        {
            Console.WriteLine("{0} {1} has been registered for training {2}", person.FirstName, person.LastName, Name);
        }

        public void Pay(IPerson person, decimal amount)
        {
            Console.WriteLine("{0} {1} has been payed {2} for training {3}", person.FirstName, person.LastName, amount, Name);
        }
    }
public class Person : IPerson
    {
        public int Id { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }
    }

The main application looks like this

    class Program
    {
        static void Main(string[] args)
        {
            var vsTraining = new Training { Name = "Visual Studio 2012" };
            var sqlTraining = new Training {Name = "Microsoft SQL 2012"};

            var personKowalski = new Person {Id = 1, FirstName = "Jan", LastName = "Kowalski"};
            var personNowak = new Person {Id = 2, FirstName = "Krzysztof", LastName = "Nowak"};

            var commands = new List
            {                   
                new RegisterForTrainingCommand(vsTraining, personKowalski),
                new RegisterForTrainingCommand(sqlTraining, personNowak),
                new PayForTrainingCommand(sqlTraining, personNowak, 2200.99M)
            };

            var invoker = new Invoker(commands);
            invoker.ExecuteCommands();
        }
    }