Promotional Email Allocation logic.

input :
          input has been totally captured from app.configuration, where we can edit or add more items.

App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <appSettings>
    <add key="inputData" value="P1,145,07-01-2017,07-03-2017|P2,145,07-04-2017,07-06-2017|P3,198,07-07-2017,07-09-2017|P4,72,07-10-2017,07-12-2017"/>
    <add key="EmailDailylimit" value="100"/>
  </appSettings>

</configuration>


C# code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;

namespace Promotional_Email_Allocation
{
    class Program
    {
        static void Main(string[] args)
        {
            /* Getting Configuration Data */

            string configuration = ConfigurationManager.AppSettings["inputData"];
            int DailyLimit = Convert.ToInt32(ConfigurationManager.AppSettings["EmailDailylimit"]);
            string[] promotions = configuration.Split('|');
            List<promotionalData> promotionalOffer = new List<promotionalData>();

            foreach (string promotion in promotions)
            {
                string[] promotionaldetails = promotion.Split(',');
                promotionalData promotionl = new promotionalData
                {
                    promotionName = promotionaldetails[0],
                    count = Convert.ToInt32(promotionaldetails[1]),
                    fromDate = Convert.ToDateTime(promotionaldetails[2]),
                    toDate = Convert.ToDateTime(promotionaldetails[3])
                };
                promotionalOffer.Add(promotionl);
            }

            /* Allocation logic */

            double totalEmails = promotionalOffer.Sum(x => x.count);
            double numberOfdates = totalEmails / DailyLimit;

            if (Math.Round(numberOfdates) < numberOfdates)
            {
                numberOfdates = numberOfdates + 1;
            }
            var startDate = promotionalOffer.Select(x => x.fromDate).OrderBy(a => a).First();
            var endDate = promotionalOffer.Select(x => x.toDate).OrderBy(a => a).Last();
            List<AllocationStatus> allocation = new List<AllocationStatus>();
            var offers = promotionalOffer.OrderBy(x => x.fromDate);
          
           
            for (DateTime date = startDate; date.Date <= startDate.AddDays(numberOfdates); date = date.AddDays(1))
            {
                AllocationStatus allocate = new AllocationStatus();
                List<promotonalCount> lstpromotions = new List<promotonalCount>();
                int allocatedCount = 0;

                allocate.AllocatedDate = date;

                foreach (var promotion in offers.Select(X => X.promotionName))
                {
                    if (date <= offers.Where(a => a.promotionName == promotion).Select(x => x.toDate).First())
                    {
                        int currentpromotionCount = offers.Where(x => x.promotionName == promotion).Select(y => y.count).First();

                        if (currentpromotionCount > 0)
                        {
                            if (currentpromotionCount > DailyLimit)
                            {
                                promotonalCount pc = new promotonalCount{
                                promoionalCount=DailyLimit - allocatedCount,
                                promotionlName=promotion
                                };

                                allocatedCount = allocatedCount + pc.promoionalCount;
                                pc.promotionlName = promotion;
                                lstpromotions.Add(pc);
                             
                                offers.Where(w => w.promotionName == promotion).ToList().ForEach(s => s.count = currentpromotionCount-pc.promoionalCount);
                                currentpromotionCount = currentpromotionCount - DailyLimit;
                                break;
                            }
                            else
                            {
                                promotonalCount pc = new promotonalCount
                                {
                                    promoionalCount = allocatedCount==0?currentpromotionCount:(DailyLimit-allocatedCount),
                                    promotionlName = promotion
                                };
                                allocatedCount = allocatedCount + pc.promoionalCount;
                                lstpromotions.Add(pc);
                                offers.Where(w => w.promotionName == promotion).ToList().ForEach(s => s.count = currentpromotionCount-pc.promoionalCount);
                                if (allocatedCount == DailyLimit)
                                    break;
                            }
                        }
                    }
                }
                allocate.promotionalCount = lstpromotions;
                allocation.Add(allocate);
            }

            /* Display Output */
            DataTable dt = new DataTable();

            foreach (var items in allocation)
            {
              
                dt.Columns.Add(items.AllocatedDate.Date.ToString());
               
            }
            var maxRowCount = allocation.Select(i => i.promotionalCount).ToList().Max(z=>z.Count);
            for (int i = 0; i < maxRowCount; i++)
                {
                    var row = dt.NewRow();
                    dt.Rows.Add(row);
            }
           
            foreach (var items in allocation)
            {
                int r = 0;
                foreach (var data in items.promotionalCount)
                {
                    dt.Rows[r][items.AllocatedDate.Date.ToString()] = data.promotionlName + " - " + data.promoionalCount;
                    r++;
                }
            }

            foreach (DataRow dataRow in dt.Rows)
            {
                foreach (var item in dataRow.ItemArray)
                {
                    Console.WriteLine(item);
                }
            }
        }
    }

    class AllocationStatus
    {
        public DateTime AllocatedDate { get; set; }
        public List<promotonalCount> promotionalCount { get; set; }
    }
    class promotonalCount
    {
        public string promotionlName { get; set; }
        public int promoionalCount { get; set; }
    }

    class promotionalData
    {
        public string promotionName { get; set; }
        public int count { get; set; }
        public DateTime fromDate { get; set; }
        public DateTime toDate { get; set; }
    }
}

output :


Read More »

Create a program in C# by using Vehicle Car and Truck as a classes and implement polymorphism in it.


using System;

namespace POLYMORPHISM1
{
    public class Program
    {
        static void Main(string[] args)
        {
            Vehicle vehicle = new Vehicle();
            Vehicle vehicle1 = new Car();
            Vehicle vehicle2 = new Truk();

            vehicle.start();
            vehicle.horn();
            vehicle.stop();

            vehicle1.start();
            vehicle1.horn();
            vehicle1.stop();

            vehicle2.start();
            vehicle2.horn();
            vehicle2.stop();

            Console.ReadLine();
        }
    }


    public class Vehicle
    {
        public virtual void start() { Console.WriteLine("Vehicle Started"); }
        public virtual void horn() { Console.WriteLine("Vehicle Horned"); }
        public virtual void stop() { Console.WriteLine("Vehicle Stopped"); }
    }

    public class Car : Vehicle
    {
        public override void start() { Console.WriteLine("Car Started"); }
        public override void horn() { Console.WriteLine("Car Horned"); }
        public override void stop() { Console.WriteLine("Car Stopped"); }
    }
    public class Truk : Vehicle
    {
        public override void start() { Console.WriteLine("Truck Started"); }
        public override void horn() { Console.WriteLine("Truck Horned"); }
        public override void stop() { Console.WriteLine("Truck Stopped"); }
    }
}


output:
=====

Read More »