public void ChangeStatus(int tripId, Status status) { using (var context = new BusTicketSystemContext()) { Trip trip = context.Trips.FirstOrDefault(t => t.Id == tripId); Status oldStatus = trip.Status; trip.Status = status; int ticketCount = 0; ticketCount = context.Tickets.Where(t => t.TripId == tripId).Count(); if (status == Status.arrived) { ArrivedTrip arTrip = new ArrivedTrip { ActualArrivalTime = DateTime.Now, OriginStation = trip.OriginStation, DestinationStation = trip.DestinationStation, PassengerCount = ticketCount }; context.ArrivedTrips.Add(arTrip); } context.SaveChanges(); Console.WriteLine($"Trip from {trip.OriginStation.Town.Name} to {trip.DestinationStation.Town.Name} on {trip.DepartureTime}"); Console.WriteLine($"Status changed from {oldStatus} to {trip.Status}"); } }
public void Buy(int customerId, int tripId, decimal price, string seatNumber) { using (var context = new BusTicketSystemContext()) { Customer customer = context.Customers.FirstOrDefault(c => c.Id == customerId); Trip trip = context.Trips.FirstOrDefault(t => t.Id == tripId); Ticket ticket = new Ticket { Customer = customer, Trip = trip, Price = price, Seat = seatNumber }; customer.BankAccount.Balance -= price; context.Tickets.Add(ticket); context.SaveChanges(); Console.WriteLine($"Customer {customer.Fullname} bought a ticket for trip {tripId} for {price:F2} on seat {seatNumber}"); } }
public void PublishReview(Customer customer, float grade, BusCompany busCompany, string content) { var review = new Review { Customer = customer, BusCompany = busCompany, ReviewContent = content, Grade = grade }; context.Reviews.Add(review); context.SaveChanges(); }
public string Execute(string[] data) { if (data.Length != 4) { throw new InvalidOperationException("Invalid parameters!"); } int customerId = int.Parse(data[0]); int tripId = int.Parse(data[1]); decimal price = decimal.Parse(data[2]); string seat = data[3]; string result = string.Empty; using (var db = new BusTicketSystemContext()) { var customer = db.Customers.Where(c => c.Id == customerId).Include(c => c.BankAccount).SingleOrDefault(); if (customer == null || !db.Trips.Any(t => t.Id == tripId)) { throw new InvalidOperationException("There is no such customer or trip!"); } if (db.Tickets.Any(t => t.CustomerId == customerId && t.TripId == tripId)) { throw new ArgumentException($"You have already bought ticket for trip {tripId}"); } if (price <= 0) { throw new ArgumentException("Invalid price!"); } if (customer.BankAccount.Balance - price < 0) { throw new ArgumentException("Insufficient funds!"); } customer.BankAccount.Balance -= price; var ticket = new Ticket() { CustomerId = customerId, Price = price, Seat = seat, TripId = tripId }; db.Tickets.Add(ticket); db.SaveChanges(); result = $"Customer {customer.FirstName} {customer.LastName} bought ticket for trip {tripId} for ${price} on seat {seat}"; } return(result); }
public Ticket BuyTicket(Customer customer, Trip trip, decimal price, string seat) { Ticket ticket = new Ticket { Customer = customer, Trip = trip, Price = price, Seat = seat }; context.Tickets.Add(ticket); context.SaveChanges(); return(ticket); }
public string Execute(string[] data) { if (data.Length < 4) { throw new InvalidOperationException("Invalid parameters!"); } int customerId = int.Parse(data[0]); double grade = double.Parse(data[1]); string busCompany = data[2]; string content = string.Join(" ", data.Skip(3).ToArray()); string result = string.Empty; using (var db = new BusTicketSystemContext()) { if (!db.Customers.Any(c => c.Id == customerId) || !db.BusCompanies.Any(b => b.Name == busCompany)) { throw new ArgumentException("Invalid Custumer or Bus Company!"); } if (grade < 1 || grade > 10) { throw new ArgumentException("Grade can be between 1 and 10"); } var review = new Review() { BusCompanyId = db.BusCompanies.SingleOrDefault(b => b.Name == busCompany).Id, Content = content, CustomerId = customerId, Grade = grade }; db.Customers.Find(customerId).Reviews.Add(review); db.SaveChanges(); string customerFullName = db.Customers.Find(customerId).FirstName + " " + db.Customers.Find(customerId).LastName; result = $"{customerFullName} review was succesfully published"; } return(result); }
public void Publish(int customerId, double grade, string busCompanyName, string content) { using (BusTicketSystemContext context = new BusTicketSystemContext()) { Customer customer = context.Customers.FirstOrDefault(c => c.Id == customerId); BusCompany busCompany = context.BusCompanies.FirstOrDefault(b => b.Name == busCompanyName); Review review = new Review { BusCompany = busCompany, Customer = customer, Grade = grade, Content = content, PublishDate = DateTime.Now }; context.Reviews.Add(review); context.SaveChanges(); Console.WriteLine($"Customer {customer.Fullname} published a review for company {busCompanyName}"); } }
public string Execute(string[] data) { if (data.Length != 2) { throw new InvalidOperationException("Invalid parameters!"); } int tripId = int.Parse(data[0]); string newStatus = data[1]; var result = new StringBuilder(); using (var db = new BusTicketSystemContext()) { var trip = db.Trips .Include(t => t.OriginBusStation) .ThenInclude(s => s.Town) .Include(t => t.DestinationBusStation) .ThenInclude(s => s.Town) .Include(t => t.Tickets) .SingleOrDefault(t => t.Id == tripId); if (trip == null) { throw new InvalidOperationException("There is no such trip!"); } var statusNames = Enum.GetNames(typeof(Status)).ToList(); if (!statusNames.Contains(newStatus)) { throw new InvalidOperationException("Invalid status name!"); } string oldStatus = trip.Status.ToString(); trip.Status = (Status)statusNames.IndexOf(newStatus); string originTown = trip.OriginBusStation.Town.Name; string destinationTown = trip.DestinationBusStation.Town.Name; result.AppendLine($"Trip from {originTown} to {destinationTown} on {trip.DepartureTime}"); result.AppendLine($"Status changed from {oldStatus} to {newStatus}"); if (newStatus == "Arrived") { int passengersCount = trip.Tickets.Count; result.Append($"On {trip.ArrivalTime} - {passengersCount} passengers arrived at {destinationTown} from {originTown}"); var arrivedTrip = new ArrivedTrip() { ActualArrivedTime = trip.ArrivalTime, DestinationBusStation = destinationTown, OriginBusStation = originTown, PassengersCount = passengersCount }; db.ArrivedTrips.Add(arrivedTrip); } db.SaveChanges(); } return(result.ToString()); }
private static void Seed(BusTicketSystemContext context) { var towns = new[] { new Town { Name = "Sofia", Country = "Bulgaria", }, new Town { Name = "New York", Country = "USA" }, }; context.Towns.AddRange(towns); var busCompanies = new[] { new BusCompany { Name = "Union Ivkoni", Nationality = "BG", Rating = 7.6, }, new BusCompany { Name = "American", Nationality = "US", Rating = 9.0 }, }; context.BusCompanies.AddRange(busCompanies); var busStations = new[] { new BusStation { Name = "Union", Town = towns[1] }, new BusStation { Name = "Centralna", Town = towns[0] }, }; context.BusStations.AddRange(busStations); var bankAccounts = new[] { new BankAccount { AccountNumber = "1231236", Balance = 2000, }, new BankAccount { AccountNumber = "5453789", Balance = 1000, } }; context.BankAccounts.AddRange(bankAccounts); var trips = new[] { new Trip { BusCompany = busCompanies[0], DestinationBusStation = busStations[1], OriginBusStation = busStations[0], DepartureTime = DateTime.Now, ArrivalTime = DateTime.Parse("12/01/2017"), Status = Status.Departed }, new Trip { BusCompany = busCompanies[1], DestinationBusStation = busStations[0], OriginBusStation = busStations[1], DepartureTime = DateTime.Now, ArrivalTime = DateTime.Parse("12/02/2017"), Status = Status.Delayed }, }; context.Trips.AddRange(trips); var customers = new[] { new Customer { FirstName = "Stiliyan", LastName = "Milanov", Gender = Gender.Male, HomeTown = towns[0], BankAccount = bankAccounts[0], }, new Customer { FirstName = "Emma", LastName = "Stone", Gender = Gender.Female, HomeTown = towns[1], BankAccount = bankAccounts[1], } }; context.Customers.AddRange(customers); var tickets = new[] { new Ticket { Price = 50, Seat = 23, Customer = customers[0], Trip = trips[0], }, new Ticket { Price = 100, Seat = 33, Customer = customers[1], Trip = trips[1], } }; context.Tickets.AddRange(tickets); var reviews = new[] { new Review { BusCompany = busCompanies[0], BusStation = busStations[0], Customer = customers[0], Content = "Mnogo zle", Grade = 3.6, }, new Review { BusCompany = busCompanies[1], BusStation = busStations[1], Customer = customers[1], Content = "Ekstra", Grade = 7.9, } }; context.Reviews.AddRange(reviews); context.SaveChanges(); }
public static void Seed(BusTicketSystemContext context) { var towns = new[] { new Town { Name = "Sofia", Country = "Bulgaria" }, new Town { Name = "Berlin", Country = "Germany" }, new Town { Name = "Paris", Country = "France" }, new Town { Name = "Madrid", Country = "Spain" }, new Town { Name = "Amsterdam", Country = "Holand" } }; context.Towns.AddRange(towns); var busStations = new[] { new Station { Name = "Sofia Bus Station", Town = towns[0] }, new Station { Name = "Berlin Bus Station", Town = towns[1] }, new Station { Name = "Paris Bus Station", Town = towns[2] }, new Station { Name = "Madrid Bus Station", Town = towns[3] }, new Station { Name = "Amsterdam Bus Station", Town = towns[4] } }; context.Stations.AddRange(busStations); var busCompanies = new[] { new Company { Name = "EtapAdress", Nationality = "Bugarien", Raiting = 6.5f }, new Company { Name = "Eurolines", Nationality = "French", Raiting = 7.5f }, new Company { Name = "SchnellBus", Nationality = "German", Raiting = 9.5f }, new Company { Name = "EspanaRapido", Nationality = "Spanish", Raiting = 6.5f }, new Company { Name = "OnZeitBus", Nationality = "Dutch", Raiting = 8.5f } }; context.Companies.AddRange(busCompanies); var customers = new[] { new Customer { FirstName = "Ivan", LastName = "Petrov", DateOfBirth = DateTime.Parse("14-11-1992"), Gender = Gender.Male, HomeTown = towns[0] }, new Customer { FirstName = "Gerald", LastName = "Hass", DateOfBirth = DateTime.Parse("10-04-1987"), Gender = Gender.Male, HomeTown = towns[1] }, new Customer { FirstName = "Jose", LastName = "Cortez", DateOfBirth = DateTime.Parse("26-06-1978"), Gender = Gender.Male, HomeTown = towns[2] }, new Customer { FirstName = "Eleonore", LastName = "Beavis", DateOfBirth = DateTime.Parse("11-10-1987"), Gender = Gender.Female, HomeTown = towns[3] }, new Customer { FirstName = "Erich", LastName = "Fulhert", DateOfBirth = DateTime.Parse("06-02-1969"), Gender = Gender.Male, HomeTown = towns[4] } }; context.Customers.AddRange(customers); var bankAccounts = new[] { new BankAccount { AccountNumber = "8267GTZ928JO1", Balance = 1450.00m, Customer = customers[0] }, new BankAccount { AccountNumber = "6272GT72519HPQZ", Balance = 500.00m, Customer = customers[1] }, new BankAccount { AccountNumber = "GSJAGI173027JS60", Customer = customers[2] }, new BankAccount { AccountNumber = "162066GAOZZJKA", Balance = 100.00m, Customer = customers[3] }, new BankAccount { AccountNumber = "BSOWVSG172628J", Balance = 2050.00m, Customer = customers[4] } }; context.BankAccounts.AddRange(bankAccounts); var trips = new[] { new Trip { Company = busCompanies[0], OriginBusStation = busStations[3], DestinationBusStation = busStations[4], DepartureTime = DateTime.Parse("29-11-2017 6:30"), ArrivalTime = DateTime.Parse("30-11-2017 23:30"), Status = Status.Departed }, new Trip { Company = busCompanies[1], OriginBusStation = busStations[0], DestinationBusStation = busStations[1], DepartureTime = DateTime.Parse("30-11-2017 12:30"), ArrivalTime = DateTime.Parse("01-11-2017 22:00"), Status = Status.Cancelled }, new Trip { Company = busCompanies[2], OriginBusStation = busStations[2], DestinationBusStation = busStations[3], DepartureTime = DateTime.Parse("01-12-2017 17:30"), ArrivalTime = DateTime.Parse("03-12-2017 09:00"), Status = Status.Delayed }, new Trip { Company = busCompanies[2], OriginBusStation = busStations[2], DestinationBusStation = busStations[0], DepartureTime = DateTime.Parse("30-11-2017 13:30"), ArrivalTime = DateTime.Parse("01-12-2017 21:30"), Status = Status.Arrived }, new Trip { Company = busCompanies[4], OriginBusStation = busStations[0], DestinationBusStation = busStations[4], DepartureTime = DateTime.Parse("04-12-2017 22:30"), ArrivalTime = DateTime.Parse("06-12-2017 14:00"), Status = Status.Departed }, }; context.Trips.AddRange(trips); var tickets = new[] { new Ticket { Customer = customers[0], Price = 150.00m, Seat = "A4", Trip = trips[0] }, new Ticket { Customer = customers[0], Price = 210.00m, Seat = "C2", Trip = trips[1] }, new Ticket { Customer = customers[1], Price = 265.50m, Seat = "F2", Trip = trips[0] }, new Ticket { Customer = customers[2], Price = 180.00m, Seat = "D5", Trip = trips[1] }, new Ticket { Customer = customers[2], Price = 120.00m, Seat = "H3", Trip = trips[3] }, new Ticket { Customer = customers[3], Price = 185.50m, Seat = "L2", Trip = trips[2] } }; context.Tickets.AddRange(tickets); var reviews = new[] { new Review { Content = "Excellent trip! Look forward to travel again.", Grade = 8.5f, Company = busCompanies[0], Customer = customers[0], DateTimeOfPublishing = DateTime.Now }, new Review { Content = "Very polite staff.", Grade = 8.5f, Company = busCompanies[1], Customer = customers[0], DateTimeOfPublishing = DateTime.Now }, new Review { Content = "The driver was careful.", Grade = 8.0f, Company = busCompanies[1], Customer = customers[1], DateTimeOfPublishing = DateTime.Now }, new Review { Content = "Would recommend it but the driver needs to stop smoking while driving.", Grade = 4.0f, Company = busCompanies[2], Customer = customers[3], DateTimeOfPublishing = DateTime.Now }, new Review { Content = "It was a pleasant trip.", Grade = 8.5f, Company = busCompanies[3], Customer = customers[4], DateTimeOfPublishing = DateTime.Now } }; context.Reviews.AddRange(reviews); context.SaveChanges(); }