Calculating the number of trains required based on duration of journey

using System;

using System.Collections.Generic;

using System.Collections;

using System.Linq;

using System.Text;

using System.Text.RegularExpressions;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                string format = "ddd hh:mm tt";

                Console.WriteLine("Enter time in this format: " + DateTime.Now.ToString(format));

                Console.Write("Enter departure time at source: ");

                string sdStr = Console.ReadLine();//source departure

                DateTime sd = DateTimeConversion(sdStr);

                Console.Write("Enter departure time at destination: ");

                string ddStr = Console.ReadLine();//destination departure

                DateTime dd = DateTimeConversion(ddStr, sd);

                Console.Write("Enter duration of the journey in minutes: ");

                string durationStr = Console.ReadLine();

                DateTime sa = dd.AddMinutes(int.Parse(durationStr));

                Console.WriteLine("Arrival time at source: " + sa.ToString(format));

                DateTime da = sd.AddMinutes(int.Parse(durationStr));

                Console.WriteLine("Arrival time at destination: " + da.ToString(format));

                ArrayList dateTimeArray = new ArrayList();

                ArrayList codeArray = new ArrayList();

                dateTimeArray.Add(dd);

                codeArray.Add("DD");

                while (dd.AddDays(1) < da)

                {

                    dd = dd.AddDays(1);

                    //TODO: Apply check before adding to the array

                    //if the train runs on specific days of week

                    dateTimeArray.Add(dd);

                    codeArray.Add("DD");

                }

                dateTimeArray.Add(da);

                codeArray.Add("DA");

                dateTimeArray.Add(sd);

                codeArray.Add("SD");

                while (sd.AddDays(1) < sa)

                {

                    sd = sd.AddDays(1);

                    //TODO: Apply check before adding to the array

                    //if the train runs on specific days of week

                    dateTimeArray.Add(sd);

                    codeArray.Add("SD");

                }

                dateTimeArray.Add(sa);

                codeArray.Add("SA");

                DateTimeSort(dateTimeArray, codeArray);

                for (int i = 0; i < dateTimeArray.Count; i++)

                {

                    Console.WriteLine((DateTime)dateTimeArray[i] + " " + (string)codeArray[i]);

                }

                int total = 0, s = 0, d = 0;

                for (int i = 0; i < dateTimeArray.Count; i++)

                {

                    if ((string)codeArray[i] == "DD")

                    {

                        if (d == 0)

                            total++;

                        else

                            d–;

                    }

                    if ((string)codeArray[i] == "SD")

                    {

                        if (s == 0)

                            total++;

                        else

                            s–;

                    }

                    if ((string)codeArray[i] == "SA")

                        s++;

                    if ((string)codeArray[i] == "DA")

                        d++;

                }

                Console.WriteLine("Number of Trains Required: " + total);

            }

            catch (Exception e)

            {

                Console.WriteLine(e.Message);

            }

        }

        public static DateTime DateTimeConversion(string sdStr)

        {

            Regex reg = new Regex(" ");

            string[] str = reg.Split(sdStr);

            DateTime dt = DateTime.Today;

            while (dt.ToString("ddd") != str[0])

            {

                dt = dt.AddDays(1);

            }

            dt = dt.AddHours(int.Parse(str[1].Substring(0, str[1].IndexOf(‘:’))));

            dt = dt.AddMinutes(int.Parse(str[1].Substring(str[1].IndexOf(‘:’) + 1)));

            if (str[2].ToUpper() == "PM")

            {

                dt = dt.AddHours(12);

            }

            return dt;

        }

        public static DateTime DateTimeConversion(string sdStr, DateTime dtime)

        {

            Regex reg = new Regex(" ");

            string[] str = reg.Split(sdStr);

            DateTime dt = dtime.Date;

            while (dt.ToString("ddd") != str[0])

            {

                dt = dt.AddDays(1);

            }

            dt = dt.AddHours(int.Parse(str[1].Substring(0, str[1].IndexOf(‘:’))));

            dt = dt.AddMinutes(int.Parse(str[1].Substring(str[1].IndexOf(‘:’) + 1)));

            if (str[2].ToUpper() == "PM")

            {

                dt = dt.AddHours(12);

            }

            return dt;

        }

        public static void DateTimeSort(ArrayList dateTimeArray, ArrayList codeArray)

        {

            for (int i = 1; i < dateTimeArray.Count; i++)

            {

                for (int j = 0; j < dateTimeArray.Count – i; j++)

                {

                    if ((DateTime)dateTimeArray[j] > (DateTime)dateTimeArray[j + 1])

                    {

                        DateTime tempDt = (DateTime)dateTimeArray[j];

                        dateTimeArray[j] = (DateTime)dateTimeArray[j + 1];

                        dateTimeArray[j + 1] = tempDt;

                        string tempStr = (string)codeArray[j];

                        codeArray[j] = (string)codeArray[j + 1];

                        codeArray[j + 1] = tempStr;

                    }

                }

            }

        }

    }

}
 
Enter time in this format: Tue 12:16 AM
Enter departure time at source: Sun 10:00 AM
Enter departure time at destination: Sun 9:00 AM
Enter duration of the journey in minutes: 1920
Arrival time at source: Mon 05:00 PM
Arrival time at destination: Mon 06:00 PM
5/30/2010 9:00:00 AM DD
5/30/2010 10:00:00 AM SD
5/31/2010 9:00:00 AM DD
5/31/2010 10:00:00 AM SD
5/31/2010 5:00:00 PM SA
5/31/2010 6:00:00 PM DA
Number of Trains Required: 4
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s