Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using System.Diagnostics;
- using System.Net;
- namespace Train_Updater
- {
- class Program
- {
- const string TrainCodesFilepath = "traincodes.txt";
- const char Limiter = '-';
- const string DepartureTimeFind = "\"departureTime\":\"";
- const string ArrivalTimeFind = "\"arrivalTime\":\"";
- const int TimeSize = 5;
- public static List<string> GetTimes(string SourceCode, string FindingString, int TimeSize)
- {
- List<string> Times = new List<string>(); //Creates a new list to add strings to
- int Start = 0; //Defines the start of the substring
- for (int x = 0; x < 5; x++)
- {
- Start = SourceCode.IndexOf(FindingString) + FindingString.Length; //Finds the index of the first instance
- Times.Add(SourceCode.Substring(Start, TimeSize)); //Adds this to the list of times
- SourceCode = SourceCode.Substring(Start + TimeSize); //Redefines the current code string to remove the old instance
- }
- return Times;
- }
- public static List<string> GetDurations(List<string> DepartureTimes, List<string> ArrivalTimes)
- {
- List<string> RList = new List<string>(); //Creates a new list to return at the end of the function
- for (int x = 0; x < DepartureTimes.Count; x++)
- {
- int CurrentDepartureTime = Convert.ToInt32(DepartureTimes[x].Split(':')[0]) * 60 +
- (Convert.ToInt32(DepartureTimes[x].Split(':')[1])); //Converts the departure and arrival times into minutes from strings
- int CurrentArrivalTime = Convert.ToInt32(ArrivalTimes[x].Split(':')[0]) * 60 +
- (Convert.ToInt32(ArrivalTimes[x].Split(':')[1]));
- int TimeTaken = CurrentArrivalTime - CurrentDepartureTime; //Works out the difference in times between the two
- if (TimeTaken < 60)
- {
- RList.Add(Convert.ToString(TimeTaken) + " min");
- }
- else
- {
- int Hours = TimeTaken / 60;
- int Remaining = TimeTaken - (Hours * 60);
- RList.Add(Convert.ToString(Hours) + " h " + Convert.ToString(Remaining) + " min");
- }
- }
- return RList;
- }
- public static string CreateUrl(string StartCode, string EndCode, string Date, string Time, string ArrDep)
- {
- return "http://ojp.nationalrail.co.uk/service/timesandfares/" + StartCode + "/" + EndCode + "/" + Date + "/" + Time + "/" + ArrDep;
- }
- public static Dictionary<string, string> GetCodes(string Filepath, char Limiter)
- {
- Dictionary<string, string> RDict = new Dictionary<string, string>();
- StreamReader sr = new StreamReader(Filepath);
- string[] SplitString;
- string CurrentLine = sr.ReadLine();
- while (CurrentLine != null)
- {
- SplitString = CurrentLine.ToLower().Split(Limiter);
- if (!(RDict.ContainsKey(SplitString[0])))
- {
- RDict.Add(SplitString[0], SplitString[1]);
- }
- CurrentLine = sr.ReadLine();
- }
- sr.Close();
- return RDict;
- }
- static void Main(string[] args)
- {
- Dictionary<string, string> StationCodes = GetCodes(TrainCodesFilepath, Limiter);
- /*Console.Write("What station are you starting at?: ");
- string StartCode = StationCodes[Console.ReadLine().ToLower()];
- Console.Write("What station are you ending at?: ");
- string EndCode = StationCodes[Console.ReadLine().ToLower()];
- Console.Write("What date are you leaving? (Today, tomorrow or a date): ");
- string Date = Console.ReadLine().ToLower().Replace("/", "");
- Console.Write("What time are you needing?: ");
- string Time = Console.ReadLine().Replace(":", "");
- Console.Write("Do you want to depart or arrive at that time?: ");
- string ArrDep = Console.ReadLine().ToLower().Substring(0, 3);*/
- Console.Clear();
- string StartCode = "eps", EndCode = "wdo", Date = "today", Time = "1500", ArrDep = "dep";
- //string StartCode = "gld", EndCode = "wat", Date = "tomorrow", Time = "0930", ArrDep = "dep";
- string TrainTimeUrl = CreateUrl(StartCode, EndCode, Date, Time, ArrDep);
- string SourceCode = new WebClient().DownloadString(TrainTimeUrl);
- List<string> DepartureTimes = GetTimes(SourceCode, DepartureTimeFind, TimeSize);
- List<string> ArrivalTimes = GetTimes(SourceCode, ArrivalTimeFind, TimeSize);
- List<string> JourneyMinutes = GetDurations(DepartureTimes, ArrivalTimes);
- Console.WriteLine("Departing\tArriving\tDuration");
- for (int x = 0; x < DepartureTimes.Count; x++)
- {
- Console.WriteLine("{0}\t\t{1}\t\t{2}", DepartureTimes[x], ArrivalTimes[x], JourneyMinutes[x]);
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement