public static void Load(string timeTableFilePath)
        {
            //var timeTable = new List<TimeTable>();
            string[] lines = File.ReadAllLines(timeTableFilePath);

            var timeTables = new List <TimeTable>();

            foreach (var line in lines)
            {
                string[] parts   = line.Split(',');
                var      trainId = int.Parse(parts[0]);

                if (timeTables.TrueForAll(x => x.TrainId != trainId))
                {
                    var newTable = new TimeTable()
                    {
                        TrainId = trainId
                    };
                    timeTables.Add(newTable);
                }
            }

            foreach (var line in lines)
            {
                string[] parts     = line.Split(',');
                var      trainId   = int.Parse(parts[0]);
                var      timeTable = timeTables.Find(x => x.TrainId == trainId);

                timeTable.Stops.Add(new Stop()
                {
                    Station       = (Station)Railway.GetPartFromId(int.Parse(parts[1])),
                    DepartureTime = parts[2],
                    ArrivalTime   = parts[3]
                });;
            }
            Railway.timeTables.AddRange(timeTables);
        }
Exemplo n.º 2
0
        public void LoadFile(string path, string fileName)
        {
            string separator = GetSplitCharacter(path, fileName);

            if (separator == "Empty")
            {
                throw new Exception("File is empty or unable to find the separator");
            }

            using (var reader = new StreamReader(path + fileName))
            {
                int count = 0;
                while (!reader.EndOfStream)
                {
                    var line   = reader.ReadLine();
                    var values = line.Split(separator);

                    if (count == 0)
                    {
                        count++;
                        continue;
                    }
                    else
                    {
                        switch (fileName)
                        {
                        case "trains.txt":
                            var train = new Train(int.Parse(values[0]), values[1], int.Parse(values[2]), bool.Parse(values[3]));
                            Trains.Add(train);
                            break;

                        case "passengers.txt":
                            var passenger = new Passenger(int.Parse(values[0]), values[1]);
                            Passengers.Add(passenger);
                            break;

                        case "stations.txt":
                            var stations = new Station(int.Parse(values[0]), values[1], bool.Parse(values[2]));
                            Stations.Add(stations);
                            break;

                        case "timetable.txt":
                            TimeTable timeTable;
                            if (values[2] == "null" && values[3] == "null")
                            {
                                timeTable = new TimeTable(int.Parse(values[0]), int.Parse(values[1]), DateTime.Parse("00:00"), DateTime.Parse("00:00"));
                            }
                            else if (values[2] == "null")
                            {
                                timeTable = new TimeTable(int.Parse(values[0]), int.Parse(values[1]), DateTime.Parse("00:00"), DateTime.Parse(values[3]));
                            }
                            else if (values[3] == "null")
                            {
                                timeTable = new TimeTable(int.Parse(values[0]), int.Parse(values[1]), DateTime.Parse(values[2]), DateTime.Parse("00:00"));
                            }
                            else
                            {
                                timeTable = new TimeTable(int.Parse(values[0]), int.Parse(values[1]), DateTime.Parse(values[2]), DateTime.Parse(values[3]));
                            }
                            TimeTables.Add(timeTable);
                            break;

                        case "traintrack.txt":
                            var track = new Track(int.Parse(values[0]), int.Parse(values[1]), int.Parse(values[2]), int.Parse(values[3]));
                            Tracks.Add(track);
                            break;

                        default:
                            break;
                        }
                    }
                }
            }
        }
 public TrainPlanner FollowSchedule(TimeTable timeTable)
 {
     return(this);
 }