public NormalUserWindow(TicketDB dbContext, User user)
 {
     InitializeComponent();
     this.dbContext = dbContext;
     this.user = user;
     selectedScheduleForTicket = null;
     populatedBuyTicketTabItem = false;
     originalTicketPrice = 0;
     discountedTicketPrice = 0;
 }
 public AdminWindow(TicketDB dbContext, User user)
 {
     InitializeComponent();
     this.dbContext = dbContext;
     this.user = user;
     startCityForSchedule = null;
     endCityForSchedule = null;
     trainForSchedule = null;
     beginEditCity = null;
     beginEditTrain = null;
     beginEditSchedule = null;
 }
 public bool AddTrip(Schedule schedule)
 {
     try
     {
         this.context.ScheduleSet.Add(schedule);
         this.context.SaveChanges();
         return true;
     }
     catch (SqlException e)
     {
         Console.WriteLine(e.Message);
         return false;
     }
 }
 private void scheduleDataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
 {
     if (!e.Row.IsEditing)
     {
         var schedule = e.Row.Item as Schedule;
         if (schedule != null)
         {
             beginEditSchedule = new Schedule();
             beginEditSchedule.ScheduleID = schedule.ScheduleID;
             beginEditSchedule.StartCityID = schedule.StartCityID;
             beginEditSchedule.EndCityID = schedule.EndCityID;
             beginEditSchedule.TrainID = schedule.TrainID;
             beginEditSchedule.DepartureTime = schedule.DepartureTime;
             beginEditSchedule.TravelTime = schedule.TravelTime;
             beginEditSchedule.TicketPrice = schedule.TicketPrice;
         }
     }
 }
        private void addScheduleButton_Click(object sender, RoutedEventArgs e)
        {
            TimeSpan travelTime;

            if (departureTimeDatePicker.SelectedDate == null)
            {
                MessageBox.Show("Invalid departure date format!");
                return;
            }

            if (departureTimeDatePicker.SelectedDate <= DateTime.Now)
            {
                MessageBox.Show("Cannot set past date for schedule!");
                return;
            }

            var travelTimeArgs = travelTimeTextBox.Text.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
            if (travelTimeArgs.Length != 2)
            {
                MessageBox.Show("Invalid travel time format! Please use HH:mm format!");
                return;
            }
            else
            {
                int hours, minutes;
                if (!int.TryParse(travelTimeArgs[0], out hours))
                {
                    MessageBox.Show("Invalid hours number format!");
                    return;
                }
                if (!int.TryParse(travelTimeArgs[1], out minutes))
                {
                    MessageBox.Show("Invalid minutes number format!");
                    return;
                }

                if ((hours < 0 || hours > 24) || (hours == 24 && minutes != 0))
                {
                    MessageBox.Show("Invalid travel time! Travel time cannot be more than 24 hours!");
                    return;
                }

                travelTime = new TimeSpan(hours, minutes, 0);
            }

            decimal ticketPrice;
            if (!decimal.TryParse(ticketPriceTextBox.Text, out ticketPrice))
            {
                MessageBox.Show("Invalid ticket price number format!");
                return;
            }

            var otherSchedulesForTrain = from s in dbContext.Schedules
                                         where s.TrainID == train.TrainID
                                         select s;

            DateTime beginOfSchedule = (DateTime)departureTimeDatePicker.SelectedDate;
            DateTime endOfSchedule = beginOfSchedule + travelTime;

            foreach (var curSchedule in otherSchedulesForTrain)
            {
                if (!((curSchedule.DepartureTime + curSchedule.TravelTime < beginOfSchedule)
                     || (curSchedule.DepartureTime > endOfSchedule)))
                {
                    MessageBox.Show("Train is not available in current time span!");
                    return;
                }
            }

            try
            {
                Schedule schedule = new Schedule()
                {
                    StartCityID = startCity.CityID,
                    EndCityID = endCity.CityID,
                    TrainID = train.TrainID,
                    DepartureTime = (DateTime)departureTimeDatePicker.SelectedDate,
                    TravelTime = travelTime,
                    TicketPrice = ticketPrice
                };

                dbContext.Schedules.Add(schedule);

                for (int i = 1; i <= schedule.Train.NumberOfSeats; i++)
                {
                    dbContext.Seats.Add(new Seat()
                    {
                        SeatNumber = i,
                        TrainID = schedule.TrainID,
                        ScheduleID = schedule.ScheduleID,
                        Taken = false,
                        Class = SeatClass.FirstClass
                    });
                }

                dbContext.SaveChanges();

                MessageBox.Show("Schedule added successfully!");
                Close();
            }
            catch (OptimisticConcurrencyException ex)
            {
                Console.WriteLine("Cannot add schedule in database! Operation exited with message:");
                Console.WriteLine(ex.Message);
                return;
            }
        }
        private void schedulesDataGrids_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            if (e.AddedCells.Any())
            {
                var selectedCell = e.AddedCells.First();

                var schedule = selectedCell.Item as Schedule;

                if (schedule != null)
                {
                    selectedScheduleForTicket = schedule;
                    populatedBuyTicketTabItem = false;
                }
            }

            if (tripsDataGrid.Items.Count == 0 && trainSchedulesDataGrid.Items.Count == 0)
            {
                selectedScheduleForTicket = null;
                populatedBuyTicketTabItem = false;
            }
        }
Esempio n. 7
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!");
            }
        }