private void buyTicketButton_Click(object sender, RoutedEventArgs e) { var seatCBItem = seatComboBox.SelectedItem as ComboBoxCustomItem; if (seatCBItem != null) { var selectedSeat = seatCBItem.CustomItem as Seat; try { Ticket ticket = new Ticket { SeatNumber = selectedSeat.SeatNumber, TrainID = selectedScheduleForTicket.TrainID, ScheduleID = selectedScheduleForTicket.ScheduleID, TripDateAndTime = selectedScheduleForTicket.DepartureTime, OriginalPrice = originalTicketPrice, PriceSold = discountedTicketPrice, UserID = user.UserID }; dbContext.Tickets.Add(ticket); selectedSeat.Taken = true; dbContext.SaveChanges(); } catch (OptimisticConcurrencyException ex) { Console.WriteLine("Cannot buy ticket! Operation exited with message:"); Console.WriteLine(ex.Message); return; } MessageBox.Show("Ticket bought successfully!"); seatComboBox.Items.Remove(seatCBItem); } else MessageBox.Show("Please select seat to buy!"); }
// TODO : double check public bool BuyTicket(Ticket ticket) { if (!this.context.TicketSet.Contains(ticket)) { throw new ArgumentException("Ticket not available"); } if (this.context.TicketSet.FirstOrDefault(x => x.Id == ticket.Id).UserSoldTo == null) { throw new ArgumentException("Ticket is sold"); } try { this.context.TicketSet.FirstOrDefault(x => x.Id == ticket.Id).UserSoldTo = user.Id; return true; } catch (SqlException e) { Console.WriteLine(e.Message); return false; } }
static void Main(string[] args) { int choice; string ticketFilePath = "tickets.csv"; TicketFile ticketFile = new TicketFile(ticketFilePath); string enhancementFilePath = "enhancements.csv"; EnhancementFile enhancementFile = new EnhancementFile(enhancementFilePath); string taskFilePath = "tasks.csv"; TaskFile taskFile = new TaskFile(taskFilePath); do { Console.WriteLine("What file do you want to work with:\n1) Tickets\n2) Enhancements\n3) Tasks\n4) Search Files\n5) Exit the program"); choice = Convert.ToInt32(Console.ReadLine()); switch (choice) { case 1: Console.WriteLine("1) Write to the Tickets file\n2) Read from the Tickets file"); choice = Convert.ToInt32(Console.ReadLine()); if (choice == 1) { Ticket ticket = new Ticket(); // ask user to input ticket info Console.WriteLine("Enter the summary"); ticket.summary = Console.ReadLine(); Console.WriteLine("Enter the status"); ticket.status = Console.ReadLine(); Console.WriteLine("Enter the priority"); ticket.priority = Console.ReadLine(); Console.WriteLine("Enter the submitter"); ticket.submitter = Console.ReadLine(); Console.WriteLine("Enter the assigned"); ticket.assigned = Console.ReadLine(); // input watching string input; do { // ask user to enter watching Console.WriteLine("Enter people watching (or done to quit)"); // input genre input = Console.ReadLine(); // if user enters "done" // or does not enter a genre do not add it to list if (input != "done" && input.Length > 0) { ticket.watching.Add(input); } } while (input != "done"); // specify if no watching people are entered if (ticket.watching.Count == 0) { ticket.watching.Add("(no one watching listed)"); } Console.WriteLine("Enter the severity"); ticket.severity = Console.ReadLine(); // add ticket ticketFile.AddTicket(ticket); } else if (choice == 2) { // Display all tickets foreach (Ticket t in ticketFile.Ticket) { Console.WriteLine(t.Display()); } } break; case 2: Console.WriteLine("1) Write to the Enhancements file\n2) Read from the Enhancements file"); choice = Convert.ToInt32(Console.ReadLine()); if (choice == 1) { Enhancement enhancement = new Enhancement(); // ask user to input ticket info Console.WriteLine("Enter the summary"); enhancement.summary = Console.ReadLine(); Console.WriteLine("Enter the status"); enhancement.status = Console.ReadLine(); Console.WriteLine("Enter the priority"); enhancement.priority = Console.ReadLine(); Console.WriteLine("Enter the submitter"); enhancement.submitter = Console.ReadLine(); Console.WriteLine("Enter the assigned"); enhancement.assigned = Console.ReadLine(); // input watching string input; do { // ask user to enter watching Console.WriteLine("Enter people watching (or done to quit)"); // input genre input = Console.ReadLine(); // if user enters "done" // or does not enter a genre do not add it to list if (input != "done" && input.Length > 0) { enhancement.watching.Add(input); } } while (input != "done"); // specify if no watching people are entered if (enhancement.watching.Count == 0) { enhancement.watching.Add("(no one watching listed)"); } Console.WriteLine("Enter the software"); enhancement.software = Console.ReadLine(); Console.WriteLine("Enter the cost"); enhancement.cost = double.Parse(Console.ReadLine()); Console.WriteLine("Enter the reason"); enhancement.reason = Console.ReadLine(); Console.WriteLine("Enter the estimate"); enhancement.estimate = Console.ReadLine(); // add enhancement enhancementFile.AddEnhancement(enhancement); } else if (choice == 2) { // Display all enhancements foreach (Enhancement e in enhancementFile.Enhancement) { Console.WriteLine(e.Display()); } } break; case 3: Console.WriteLine("1) Write to the Task file\n2) Read from the Task file"); choice = Convert.ToInt32(Console.ReadLine()); if (choice == 1) { Task task = new Task(); // ask user to input ticket info Console.WriteLine("Enter the summary"); task.summary = Console.ReadLine(); Console.WriteLine("Enter the status"); task.status = Console.ReadLine(); Console.WriteLine("Enter the priority"); task.priority = Console.ReadLine(); Console.WriteLine("Enter the submitter"); task.submitter = Console.ReadLine(); Console.WriteLine("Enter the assigned"); task.assigned = Console.ReadLine(); // input watching string input; do { // ask user to enter watching Console.WriteLine("Enter people watching (or done to quit)"); // input genre input = Console.ReadLine(); // if user enters "done" // or does not enter a genre do not add it to list if (input != "done" && input.Length > 0) { task.watching.Add(input); } } while (input != "done"); // specify if no watching people are entered if (task.watching.Count == 0) { task.watching.Add("(no one watching listed)"); } Console.WriteLine("Enter the project name"); task.projectName = Console.ReadLine(); Console.WriteLine("Enter the due date"); task.dueDate = DateTime.Parse(Console.ReadLine()); // add task taskFile.AddTask(task); } else if (choice == 2) { // Display all tasks foreach (Task t in taskFile.Task) { Console.WriteLine(t.Display()); } } break; case 4: string search = ""; Console.WriteLine("What in the system to you want to search:\n1) status\n2) priority\n3) submitter"); choice = Convert.ToInt32(Console.ReadLine()); if (choice == 1) { Console.WriteLine("What is the status of the ticket you want to search:"); search = Console.ReadLine().ToLower(); var ticketSearch = ticketFile.Ticket.Where(t => t.status.ToLower().Contains($"{search}")); Console.WriteLine($"There are {ticketSearch.Count()} tickets with \"{search}\" in the status:"); foreach (Ticket t in ticketSearch) { Console.WriteLine($" {t.status}"); } var taskSearch = taskFile.Task.Where(t => t.status.Contains($"{search}")); Console.WriteLine($"There are {taskSearch.Count()} tasks with \"{search}\" in the status:"); foreach (Task t in taskSearch) { Console.WriteLine($" {t.status}"); } var enhanncementsSearch = enhancementFile.Enhancement.Where(t => t.status.Contains($"{search}")); Console.WriteLine($"There are {enhanncementsSearch.Count()} enhanncements with \"{search}\" in the status:"); foreach (Enhancement t in enhanncementsSearch) { Console.WriteLine($" {t.status}"); } } else if (choice == 2) { Console.WriteLine("What is the priority of the ticket you want to search:"); search = Console.ReadLine().ToLower(); var ticketSearch = ticketFile.Ticket.Where(t => t.priority.ToLower().Contains($"{search}")); Console.WriteLine($"There are {ticketSearch.Count()} tickets with \"{search}\" in the priority:"); foreach (Ticket t in ticketSearch) { Console.WriteLine($" {t.priority}"); } var taskSearch = taskFile.Task.Where(t => t.priority.Contains($"{search}")); Console.WriteLine($"There are {taskSearch.Count()} tasks with \"{search}\" in the priority:"); foreach (Task t in taskSearch) { Console.WriteLine($" {t.priority}"); } var enhanncementsSearch = enhancementFile.Enhancement.Where(t => t.priority.Contains($"{search}")); Console.WriteLine($"There are {enhanncementsSearch.Count()} enhanncements with \"{search}\" in the priority:"); foreach (Enhancement t in enhanncementsSearch) { Console.WriteLine($" {t.priority}"); } } else if (choice == 3) { Console.WriteLine("What is the submitter of the ticket you want to search:"); search = Console.ReadLine().ToLower(); var ticketSearch = ticketFile.Ticket.Where(t => t.submitter.ToLower().Contains($"{search}")); Console.WriteLine($"There are {ticketSearch.Count()} tickets with \"{search}\" in the submitter:"); foreach (Ticket t in ticketSearch) { Console.WriteLine($" {t.submitter}"); } var taskSearch = taskFile.Task.Where(t => t.submitter.Contains($"{search}")); Console.WriteLine($"There are {taskSearch.Count()} tasks with \"{search}\" in the submitter:"); foreach (Task t in taskSearch) { Console.WriteLine($" {t.submitter}"); } var enhanncementsSearch = enhancementFile.Enhancement.Where(t => t.submitter.Contains($"{search}")); Console.WriteLine($"There are {enhanncementsSearch.Count()} enhanncements with \"{search}\" in the submitter:"); foreach (Enhancement t in enhanncementsSearch) { Console.WriteLine($" {t.submitter}"); } } break; default: Console.WriteLine("Please enter one of the options"); break; } } while (choice != 5); }
private static void BuyTicket(string tripArg) { using (var db = new TicketDB()) { int scheduleId; if (!int.TryParse(tripArg, out scheduleId)) { Console.WriteLine("Invalid trip ID! Operation aborted!"); return; } var scheduleOfTicket = (from schedule in db.Schedules where schedule.ScheduleID == scheduleId select schedule).SingleOrDefault(); if (scheduleOfTicket == null) { Console.WriteLine("No such trip! Operation aborted!"); return; } var trainOfSchedule = scheduleOfTicket.Train; List<int> availableSeats = new List<int>(trainOfSchedule.NumberOfSeats); foreach (var seat in trainOfSchedule.Seats) { if (!seat.Taken) availableSeats.Add(seat.SeatNumber); } if (availableSeats.Count == 0) { Console.WriteLine("No more seats! Operation aborted!"); return; } Console.WriteLine($"Available seats: {string.Join(", ", availableSeats)}"); Console.WriteLine("Seat to buy: "); var seatArg = Console.ReadLine().Trim(); int seatNumber; if (!int.TryParse(seatArg, out seatNumber)) { Console.WriteLine("Invalid number format! Operation aborted!"); return; } if (!availableSeats.Contains(seatNumber)) { Console.WriteLine("Seat number provided does not exist! Operation aborted!"); return; } Console.WriteLine("Begining user authentication proccess!"); Console.Write("Username: "******"Password: "******"No such user! Operation aborted!"); return; } decimal discount = userBuyingTicket.DiscountCard?.Discount ?? 0; decimal originalPrice = scheduleOfTicket.TicketPrice; decimal priceSold = originalPrice - ((originalPrice * discount) / 100); if (priceSold < 0) priceSold = 0; try { Ticket ticket = new Ticket { SeatNumber = seatNumber, TrainID = trainOfSchedule.TrainID, ScheduleID = scheduleId, TripDateAndTime = scheduleOfTicket.DepartureTime, OriginalPrice = originalPrice, PriceSold = priceSold, UserID = userBuyingTicket.UserID }; db.Tickets.Add(ticket); db.SaveChanges(); } catch (OptimisticConcurrencyException ex) { Console.WriteLine("Cannot add ticket in database! Operation exited with message:"); Console.WriteLine(ex.Message); return; } Console.WriteLine("Ticket bought successfully!"); } }
static void Main(string[] args) { string ticketFilePath = Directory.GetCurrentDirectory() + "\\tickets.csv"; logger.Info("Program started"); TicketFile ticketFile = new TicketFile(ticketFilePath); string choice = ""; do { Console.WriteLine("1) Add Ticket"); Console.WriteLine("2) Display All Tickets"); Console.WriteLine("Enter to quit"); choice = Console.ReadLine(); logger.Info("User choice: {Choice}", choice); if (choice == "1") { Ticket ticket = new Ticket(); Console.WriteLine("Enter a summary -"); ticket.summary = Console.ReadLine(); Console.WriteLine("Enter a priority level -"); ticket.priority = Console.ReadLine(); Console.WriteLine("Enter your name -"); ticket.submitter = Console.ReadLine(); Console.WriteLine("Enter who this is assigned to -"); ticket.assigned = Console.ReadLine(); String input; do { Console.WriteLine("Enter a watcher (or done to quit)"); input = Console.ReadLine(); if (input != "done" && input.Length > 0) { ticket.watching.Add(input); } } while (input != "done"); if (ticket.watching.Count == 0) { ticket.watching.Add("(no one watching)"); } ticketFile.AddTicket(ticket); } else if (choice == "2") { foreach (Ticket m in ticketFile.Tickets) { Console.WriteLine(m.Display()); } } } while (choice == "1" || choice == "2"); logger.Info("Program ended"); }