private void Add_BT_Click(object sender, RoutedEventArgs e) { try { Train t = new Train(Convert.ToInt32(lineNumberTBox.Text) - 1, Convert.ToInt32(numberOfTrainTBox.Text)); if(win.addTrain(t, t.LineNumber, Convert.ToInt32(numberOfPassageTBox.Text), departureTimeTBox.Text)) { AddedLb.Content = "Added"; } else { AddedLb.Content = "Not Added"; } } catch (Exception) { MessageBox.Show("Not all fields are filled in correctly!"); } }
public void addTrain(Train t) { _trains.Add(t); }
public bool addTrain(Train t, int i, int num, String time) { try { if (subway.GetRailways.ElementAt(i).GetStations.Count > 1) { int hour, min, sec; String[] arr = time.Split(':'); hour = Convert.ToInt32(arr[0]); min = Convert.ToInt32(arr[1]); sec = Convert.ToInt32(arr[2]); TimeSpan tp = new TimeSpan(hour, min, sec); foreach (Railway rl in subway.GetRailways) { foreach (Train tr in rl.Trains) { TimeSpan tps = tr.DepartureTime.Subtract(tp); if (tps < new TimeSpan(0, 0, 0)) { tps = -tps; } if (tps < new TimeSpan(0, 0, 10) && tr.LineNumber == t.LineNumber) { MessageBox.Show("Min interval across trains - 15 seconds"); return false; } if (t.Number == tr.Number) { MessageBox.Show("Already exists with this number!"); return false; } } } t.DepartureTime = tp; t.TrainPath.Stroke = Brushes.LightSlateGray; t.TrainPath.Fill = Brushes.AliceBlue; t.TrainPath.StrokeThickness = 2; t.TrainPath.HorizontalAlignment = HorizontalAlignment.Center; t.TrainPath.VerticalAlignment = VerticalAlignment.Center; RectangleGeometry myEllipseGeometry = new RectangleGeometry(); Point p = subway.GetRailways.ElementAt(i).GetStations.ElementAt(0).Coordinate; myEllipseGeometry.Rect = new Rect(p.X - 15, p.Y - 15, 30, 30); myEllipseGeometry.RadiusX = 10; myEllipseGeometry.RadiusY = 10; t.TrainPath.Data = myEllipseGeometry; t.numOfPassage = num + 1; t.passage = num + 1; subway.GetRailways.ElementAt(i).addTrain(t); return true; } else { MessageBox.Show("On the railway should be more than one station"); return false; } } catch (Exception) { MessageBox.Show("No this railway"); return false; } }
private void tick(object sender, EventArgs e) { Railway rl = new Railway(); Train t = new Train(); for (int i = 0; i < subway.GetRailways.Count; i++) { rl = subway.GetRailways.ElementAt(i); for (int j = 0; j < subway.GetRailways.ElementAt(i).Trains.Count; j++) { t = rl.Trains.ElementAt(j); if (rl.GetStations.Count > 1) { //////////////////////// if (subway.GetRailways.ElementAt(i).Trains.ElementAt(j).Start && subway.GetRailways.ElementAt(i).Trains.ElementAt(j).travelTime == 0 && subway.GetRailways.ElementAt(i).Trains.ElementAt(j).timeOnStation != 0 || !subway.GetRailways.ElementAt(i).Trains.ElementAt(j).Start && subway.GetRailways.ElementAt(i).Trains.ElementAt(j).DepartureTime == currentTime) { if (subway.GetRailways.ElementAt(i).Trains.ElementAt(j).DepartureTime == currentTime) { if (!fieldCanvas.Children.Contains(subway.GetRailways.ElementAt(i).Trains.ElementAt(j).TrainPath)) { subway.GetRailways.ElementAt(i).Trains.ElementAt(j).Stop(fieldCanvas, subway); fieldCanvas.Children.Add(subway.GetRailways.ElementAt(i).Trains.ElementAt(j).TrainPath); } subway.GetRailways.ElementAt(i).Trains.ElementAt(j).Start = true; } subway.GetRailways.ElementAt(i).Trains.ElementAt(j).onStation = true; subway.GetRailways.ElementAt(i).Trains.ElementAt(j).timeOnStation--; if (subway.GetRailways.ElementAt(i).Trains.ElementAt(j).timeOnStation != 0) { Random r = new Random(DateTime.Now.Millisecond); if (r.Next(1, 100) % 55 == 0) { String str = "Train number: " + subway.GetRailways.ElementAt(i).Trains.ElementAt(j).Number + " went ahead by " + subway.GetRailways.ElementAt(i).Trains.ElementAt(j).timeOnStation + " seconds at the station: " + subway.GetRailways.ElementAt(i).Trains.ElementAt(j).id; subway.GetRailways.ElementAt(i).Trains.ElementAt(j).timeOnStation = 0; subway.GetRailways.ElementAt(i).GetStations.ElementAt(subway.GetRailways.ElementAt(i).Trains.ElementAt(j).id).AddViolations(str); } } if (subway.GetRailways.ElementAt(i).Trains.ElementAt(j).away && subway.GetRailways.ElementAt(i).Trains.ElementAt(j).timeOnStation == 0) { if (t.Back) { rl.GetStations.ElementAt(t.id).Board.TrainLeft(true); } else { rl.GetStations.ElementAt(t.id).Board.TrainLeft(false); } subway.GetRailways.ElementAt(i).Trains.ElementAt(j).Back = !subway.GetRailways.ElementAt(i).Trains.ElementAt(j).Back; if (subway.GetRailways.ElementAt(i).Trains.ElementAt(j).numOfPassage != 1) { subway.GetRailways.ElementAt(i).Trains.ElementAt(j).timeOnStation = 3; } subway.GetRailways.ElementAt(i).Trains.ElementAt(j).currStationInd = 0; subway.GetRailways.ElementAt(i).Trains.ElementAt(j).away = false; } if (subway.GetRailways.ElementAt(i).Trains.ElementAt(j).timeOnStation == 0) { subway.GetRailways.ElementAt(i).Trains.ElementAt(j).onStation = false; subway.GetRailways.ElementAt(i).Trains.ElementAt(j).leftStation = true; } subway.GetRailways.ElementAt(i).Trains.ElementAt(j).SetCurrId(subway); } if (subway.GetRailways.ElementAt(i).Trains.ElementAt(j).numOfPassage == 1 && subway.GetRailways.ElementAt(i).Trains.ElementAt(j).travelTime == 0 && subway.GetRailways.ElementAt(i).Trains.ElementAt(j).timeOnStation == 0) { subway.GetRailways.ElementAt(i).Trains.ElementAt(j).Start = false; subway.GetRailways.ElementAt(i).Trains.ElementAt(j).Stop(fieldCanvas, subway); } ///////////////////////////////////// if (subway.GetRailways.ElementAt(i).Trains.ElementAt(j).travelTime != 0 && subway.GetRailways.ElementAt(i).Trains.ElementAt(j).travelTime > 0) { subway.GetRailways.ElementAt(i).Trains.ElementAt(j).travelTime--; } /////////////////////////// if (t.onStation) { if (t.Back) { rl.GetStations.ElementAt(t.id).Board.TrainOnStation(t.Number, true); } else { rl.GetStations.ElementAt(t.id).Board.TrainOnStation(t.Number, false); } } if (t.leftStation) { if (t.Back) { rl.GetStations.ElementAt(t.id).Board.TrainLeft(true); } else { rl.GetStations.ElementAt(t.id).Board.TrainLeft(false); } t.leftStation = false; } ////////////////////////// if (subway.GetRailways.ElementAt(i).Trains.ElementAt(j).Start && subway.GetRailways.ElementAt(i).Trains.ElementAt(j).travelTime == 0 && subway.GetRailways.ElementAt(i).Trains.ElementAt(j).timeOnStation == 0) { if (!fieldCanvas.Children.Contains(subway.GetRailways.ElementAt(i).Trains.ElementAt(j).TrainPath)) { subway.GetRailways.ElementAt(i).Trains.ElementAt(j).Stop(fieldCanvas, subway); fieldCanvas.Children.Add(subway.GetRailways.ElementAt(i).Trains.ElementAt(j).TrainPath); } subway.GetRailways.ElementAt(i).Trains.ElementAt(j).calcTime(subway, i, j); subway.GetRailways.ElementAt(i).Trains.ElementAt(j).Animate(subway); if (subway.GetRailways.ElementAt(i).Trains.ElementAt(j).currStationInd < subway.GetRailways.ElementAt(i).GetStations.Count - 2) { subway.GetRailways.ElementAt(i).Trains.ElementAt(j).currStationInd++; subway.GetRailways.ElementAt(i).Trains.ElementAt(j).timeOnStation = 3; } else { if (subway.GetRailways.ElementAt(i).Trains.ElementAt(j).numOfPassage != 1) { subway.GetRailways.ElementAt(i).Trains.ElementAt(j).numOfPassage--; subway.GetRailways.ElementAt(i).Trains.ElementAt(j).away = true; subway.GetRailways.ElementAt(i).Trains.ElementAt(j).currStationInd++; subway.GetRailways.ElementAt(i).Trains.ElementAt(j).timeOnStation = 3; } else { subway.GetRailways.ElementAt(i).Trains.ElementAt(j).timeOnStation = 3; } } } ////////////////////////////////////////// } } } AddTick(ref hour, ref min, ref sec); }