public ComboBoxCustomItem(Seat seat)
 {
     CustomItem = seat;
     Content = seat.SeatNumber;
 }
Пример #2
0
        private static void CreateTrip()
        {
            using (var db = new TicketDB())
            {
                Console.WriteLine("Creating trip!");

                Console.WriteLine("List of cities:");
                HashSet<int> cityIds = new HashSet<int>();
                foreach (var city in db.Cities)
                {
                    cityIds.Add(city.CityID);
                    Console.WriteLine($"{city.CityID} - {city.Name}");
                }

                Console.Write("Start City(Write Id): ");
                var startCityArg = Console.ReadLine().Trim();
                Console.Write("End City(Write Id): ");
                var endCityArg = Console.ReadLine().Trim();

                int startCityId, endCityId;
                try
                {
                    startCityId = int.Parse(startCityArg);
                    endCityId = int.Parse(endCityArg);
                }
                catch (FormatException)
                {
                    Console.WriteLine("Invalid ids format! Operation aborted!");
                    return;
                }

                if (!cityIds.Contains(startCityId) || !cityIds.Contains(endCityId))
                {
                    Console.WriteLine("Ids provided does not exist! Operation aborted!");
                    return;
                }

                Console.Write("Departure time (yyyy-MM-dd HH:mm fromat): ");
                var departerTimeArg = Console.ReadLine().Trim();

                DateTime departureTime;
                if (!DateTime.TryParseExact(departerTimeArg, "yyyy-MM-dd HH:mm", null, System.Globalization.DateTimeStyles.None, out departureTime))
                {
                    Console.WriteLine("Invalid date and time format! Operation aborted!");
                    return;
                }

                Console.Write("Travel Time(Format hh:mm): ");
                var travelTimeArg = Console.ReadLine().Trim().Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);

                if (travelTimeArg.Length != 2)
                {
                    Console.WriteLine("Invalid time format! Operation aborted!");
                    return;
                }

                TimeSpan travelTime;
                try
                {
                    travelTime = new TimeSpan(int.Parse(travelTimeArg[0]), int.Parse(travelTimeArg[1]), 0);
                }
                catch (FormatException)
                {
                    Console.WriteLine("Invalid id format! Operation aborted!");
                    return;
                }

                Console.WriteLine("List of trains:");
                HashSet<int> trainIds = new HashSet<int>();
                foreach (var train in db.Trains)
                {
                    trainIds.Add(train.TrainID);
                    Console.WriteLine($"{train.TrainID} - {train.BriefDescription}");
                }

                Console.Write("Train(Write Id): ");
                var trainArg = Console.ReadLine().Trim();

                int trainId;
                try
                {
                    trainId = int.Parse(trainArg);
                }
                catch (FormatException)
                {
                    Console.WriteLine("Invalid id format! Operation aborted!");
                    return;
                }

                if (!trainIds.Contains(trainId))
                {
                    Console.WriteLine("Id provided does not exist! Operation aborted!");
                    return;
                }

                Console.Write("Ticket Price: ");
                var ticketPriceArg = Console.ReadLine().Trim();

                decimal ticketPrice;
                try
                {
                    ticketPrice = decimal.Parse(ticketPriceArg);
                }
                catch (FormatException)
                {
                    Console.WriteLine("Invalid price format! Operation aborted!");
                    return;
                }

                try
                {
                    Schedule schedule = new Schedule
                    {
                        StartCityID = startCityId,
                        EndCityID = endCityId,
                        DepartureTime = departureTime,
                        TravelTime = travelTime,
                        TrainID = trainId,
                        TicketPrice = ticketPrice
                    };

                    db.Schedules.Add(schedule);

                    db.SaveChanges();

                    var numberOfSeatsInTrain = (from train in db.Trains
                                                where train.TrainID == trainId
                                                select train.NumberOfSeats).Single();

                    for (int i = 0; i < numberOfSeatsInTrain; i++)
                    {
                        Seat seat = new Seat
                        {
                            SeatNumber = i + 1,
                            TrainID = trainId,
                            ScheduleID = schedule.ScheduleID,
                            Taken = false,
                            Class = SeatClass.SecondClass
                        };

                        db.Seats.Add(seat);
                    }

                    db.SaveChanges();
                }
                catch (OptimisticConcurrencyException ex)
                {
                    Console.WriteLine("Cannot insert trip in database! Operation exited with message:");
                    Console.WriteLine(ex.Message);
                    return;
                }

                Console.WriteLine("Trip created successfully!");
            }
        }