コード例 #1
0
        public ITravelCompany Get(string name)
        {
            UtilityClass.CheckNotNull(name);
            UtilityClass.CheckNameLength(name);
            UtilityClass.CheckOnlyAlphanumChar(name);

            try
            {
                using (var brokerDBContext = new TravelCompanyBrokerContext(dbConnectionString))
                {
                    var TCConnString = (from tc in brokerDBContext.travelCompanies
                                        where tc.TravelCompanyName == name
                                        select tc.TravelCompanyConnectionString).Single();

                    return(new TravelCompany(name, TCConnString));
                }
            }
            catch (InvalidOperationException)
            {
                throw new NonexistentTravelCompanyException();
            }
            catch (Exception e)
            {
                throw new DbConnectionException(e.Message, e);
            }
        }
コード例 #2
0
        public ReadOnlyCollection <ILegDTO> FindDepartures(string @from, TransportType allowedTransportTypes)
        {
            UtilityClass.CheckNotNull(from);
            UtilityClass.CheckOnlyAlphanumChar(from);
            UtilityClass.CheckNameLength(from);
            UtilityClass.CheckTransportType(allowedTransportTypes);

            try
            {
                using (var travelCompanyDBContext = new TravelCompanyContext(TravelCompanyConnectionString))
                {
                    List <ILegDTO> legsDTO       = new List <ILegDTO>();
                    var            elementsLegDb = from l in travelCompanyDBContext.legs
                                                   where (l.From == @from && allowedTransportTypes.HasFlag(l.TransportT))
                                                   select l;

                    foreach (var leg in elementsLegDb)
                    {
                        legsDTO.Add(new LegDTO(leg.From, leg.To, leg.Distance, leg.Cost, leg.TransportT));
                    }
                    return(legsDTO.AsReadOnly());
                }
            }
            catch (Exception e)
            {
                throw new DbConnectionException(e.Message, e);
            }
        }
コード例 #3
0
        public ITrip FindTrip(string source, string destination, FindOptions options, TransportType allowedTransportTypes)
        {
            UtilityClass.CheckNotNull(source);
            UtilityClass.CheckNotNull(destination);
            UtilityClass.CheckOnlyAlphanumChar(source);
            UtilityClass.CheckOnlyAlphanumChar(destination);
            UtilityClass.CheckNameLength(source);
            UtilityClass.CheckNameLength(destination);
            UtilityClass.CheckTransportType(allowedTransportTypes);

            CityCostComparer cityCostComparer = new CityCostComparer();
            Dictionary <string, (int Cost, ILegDTO LegUsedToBeReahed)> cityNodesDictionary = new Dictionary <string, ValueTuple <int, ILegDTO> >();
            HashSet <string> citysVisited = new HashSet <string>();
            SortedSet <(string CityName, int Cost)> citysNotVisited = new SortedSet <ValueTuple <string, int> >(cityCostComparer);

            cityNodesDictionary.Add(source, (0, null));
            citysNotVisited.Add((source, 0));

            while (citysNotVisited.Any())
            {
                var cityVisiting = citysNotVisited.First();
                citysNotVisited.Remove(cityVisiting);
                citysVisited.Add(cityVisiting.CityName);

                if (cityVisiting.CityName.Equals(destination))
                {
                    return(GetBestTrip(cityNodesDictionary, source, destination));
                }

                int costToReachCity = cityVisiting.Cost;
                foreach (var travelCompany in TravelCompanySet)
                {
                    var legsOfTravelCompanyFromCity = travelCompany.FindDepartures(cityVisiting.CityName, allowedTransportTypes);
                    foreach (var leg in legsOfTravelCompanyFromCity)
                    {
                        if (!citysVisited.Contains(leg.To))
                        {
                            int legCost = GetLegCost(leg, options);

                            if (cityNodesDictionary.ContainsKey(leg.To))
                            {
                                if ((costToReachCity + legCost) < cityNodesDictionary[leg.To].Cost)
                                {
                                    cityNodesDictionary[leg.To] = (costToReachCity + legCost, leg);
                                    citysNotVisited.Remove((leg.To, cityNodesDictionary[leg.To].Cost));
                                    citysNotVisited.Add((leg.To, costToReachCity + legCost));
                                }
                            }
                            else
                            {
                                cityNodesDictionary.Add(leg.To, (costToReachCity + legCost, leg));
                                citysNotVisited.Add((leg.To, costToReachCity + legCost));
                            }
                        }
                    }
                }
            }
            return(null);
        }
コード例 #4
0
        public ITravelCompany CreateNew(string travelCompanyConnectionString, string name)
        {
            UtilityClass.CheckNotNull(travelCompanyConnectionString);
            UtilityClass.CheckNotNull(name);
            UtilityClass.CheckNotEmpty(travelCompanyConnectionString);
            UtilityClass.CheckNotEmpty(name);
            UtilityClass.CheckConnectionStringLength(travelCompanyConnectionString);
            UtilityClass.CheckNameLength(name);
            UtilityClass.CheckOnlyAlphanumChar(name);

            if (dbConnectionString.Equals(travelCompanyConnectionString))
            {
                throw new SameConnectionStringException();
            }

            try
            {
                using (var brokerDBContext = new TravelCompanyBrokerContext(dbConnectionString))
                {
                    var travelCompany = new TravelCompanyDB()
                    {
                        TravelCompanyName             = name,
                        TravelCompanyConnectionString = travelCompanyConnectionString
                    };

                    brokerDBContext.travelCompanies.Add(travelCompany);
                    brokerDBContext.SaveChanges();

                    using (var travelCompanyDBContext = new TravelCompanyContext(travelCompanyConnectionString))
                    {
                        travelCompanyDBContext.Database.Delete();
                        travelCompanyDBContext.Database.Create();
                    }

                    return(new TravelCompany(name, travelCompanyConnectionString));
                }
            }
            catch (DbUpdateException e)
            {
                if (e.ToString().Contains("KEY"))
                {
                    throw new TapDuplicatedObjectException();
                }

                throw new SameConnectionStringException();
            }
            catch (Exception e)
            {
                throw new DbConnectionException(e.Message, e);
            }
        }
コード例 #5
0
        public int CreateLeg(string @from, string to, int cost, int distance, TAP2017_2018_TravelCompanyInterface.TransportType transportType)
        {
            UtilityClass.CheckNotNull(from);
            UtilityClass.CheckNotNull(to);
            UtilityClass.CheckNameLength(from);
            UtilityClass.CheckNameLength(to);
            UtilityClass.CheckOnlyAlphanumChar(from);
            UtilityClass.CheckOnlyAlphanumChar(to);
            UtilityClass.CheckNotEquals(from, to);
            UtilityClass.CheckStrictlyPositive(cost);
            UtilityClass.CheckStrictlyPositive(distance);
            UtilityClass.CheckTransportType(transportType);

            try
            {
                using (var travelCompanyDBContext = new TravelCompanyContext(travelCompanyConnectionString))
                {
                    var leg = new LegDB()
                    {
                        From       = from,
                        To         = to,
                        Cost       = cost,
                        Distance   = distance,
                        TransportT = transportType
                    };
                    var l = travelCompanyDBContext.legs.Add(leg);
                    travelCompanyDBContext.SaveChanges();
                    return(l.LegID);
                }
            }
            catch (DbUpdateException)
            {
                throw new TapDuplicatedObjectException();
            }
            catch (Exception e)
            {
                throw new DbConnectionException(e.Message, e);
            }
        }