Exemplo n.º 1
0
        public override LinkStation ReadJson(
            JsonReader reader, Type objectType, LinkStation existingValue, bool hasExistingValue,
            JsonSerializer serializer
            )
        {
            if (reader.TokenType.Equals(JsonToken.StartObject))
            {
                JObject obj = JObject.Load(reader);

                string rawType            = obj["type"].Value <string>();
                RattlerTransportType type = RattlerTransportType.ofValue(rawType);

                int    id1      = obj["station1"] != null ? obj["station1"].Value <int>() : 0;
                int    id2      = obj["station2"] != null ? obj["station2"].Value <int>() : 0;
                double distance = obj["station2"] != null ? obj["distance"].Value <double>() : 0;

                RattlerStation station1 = core.stationService.getById(id1);
                RattlerStation station2 = core.stationService.getById(id2);

                LinkStation linkStation = new LinkStation(station1, station2, distance);

                return(linkStation);
            }

            throw new JsonException("Неудалось прочесть");
        }
Exemplo n.º 2
0
 public static void init()
 {
     values = new[] {
         TRAIN         = new RattlerTransportType(0, "Train"),
         METRO         = new RattlerTransportType(1, "Metro"),
         TRAM          = new RattlerTransportType(2, "Tram"),
         EXPRESS_TRAIN = new RattlerTransportType(3, "ExpressTrain"),
         COMPLEX       = new RattlerTransportType(4, "Complex")
     };
 }
Exemplo n.º 3
0
 public bool hasLink(RattlerTransportType type, RattlerStation first, RattlerStation second)
 {
     return(links.Find(
                i => i.getType().Equals(type) &&
                (
                    i.getA().Equals(first) && i.getB().Equals(second) ||
                    i.getA().Equals(second) && i.getB().Equals(first)
                )
                ) != null);
 }
Exemplo n.º 4
0
 public ComplexRattlerStation(string name)
 {
     this.name = name;
     allLinks  = new Dictionary <RattlerTransportType, List <LinkStation> >();
     foreach (var type in RattlerTransportType.getValues())
     {
         List <LinkStation> links = new List <LinkStation>();
         allLinks[type] = links;
     }
 }
Exemplo n.º 5
0
        public override RattlerTransport ReadJson(JsonReader reader, Type objectType, RattlerTransport existingValue,
                                                  bool hasExistingValue, JsonSerializer serializer
                                                  )
        {
            if (reader.TokenType.Equals(JsonToken.StartObject))
            {
                JObject obj = JObject.Load(reader);

                if (obj["type"] != null)
                {
                    string rawType            = obj["type"].Value <string>();
                    RattlerTransportType type = RattlerTransportType.ofValue(rawType);

                    int    id           = obj["id"] != null ? obj["id"].Value <int>() : 0;
                    string name         = obj["name"] != null ? obj["name"].Value <string>() : "unnamed";
                    int    capacity     = obj["capacity"] != null ? obj["capacity"].Value <int>() : 1;
                    double averageSpeed = obj["average-speed"] != null ? obj["average-speed"].Value <double>() : 1;
                    long[] stationsId   = obj["stations"] != null?JsonConvert.DeserializeObject <long[]>(obj["stations"].ToString()) : new long[]
                    {
                    };

                    RattlerTransport transport = null;

                    if (type.Equals(RattlerTransportType.METRO))
                    {
                        transport = new Metro(name);
                    }
                    else if (type.Equals(RattlerTransportType.TRAM))
                    {
                        transport = new Tram(name);
                    }
                    else if (type.Equals(RattlerTransportType.TRAIN))
                    {
                        transport = new Train(name);
                    }
                    else if (type.Equals(RattlerTransportType.EXPRESS_TRAIN))
                    {
                        transport = new ExpressTrain(name);
                    }

                    if (transport != null)
                    {
                        transport.id           = id;
                        transport.capacity     = capacity;
                        transport.averageSpeed = averageSpeed;

                        addStations(stationsId, transport);
                    }

                    return(transport);
                }
            }

            throw new JsonException("Неудалось прочесть");
        }
Exemplo n.º 6
0
        public override RattlerStation ReadJson(
            JsonReader reader, Type objectType, RattlerStation existingValue, bool hasExistingValue,
            JsonSerializer serializer
            )
        {
            if (reader.TokenType.Equals(JsonToken.StartObject))
            {
                JObject obj = JObject.Load(reader);

                string rawType            = obj["type"].Value <string>();
                RattlerTransportType type = RattlerTransportType.ofValue(rawType);

                int    id   = obj["id"] != null ? obj["id"].Value <int>() : 0;
                string name = obj["name"] != null ? obj["name"].Value <string>() : "unnamed";

                if (type.Equals(RattlerTransportType.COMPLEX))
                {
                    ComplexRattlerStation station = new ComplexRattlerStation(name);
                    station.id   = id;
                    station.name = name;
                    return(station);
                }
                else if (type.Equals(RattlerTransportType.METRO))
                {
                    SimpleRattlerStation <Metro> station = new SimpleRattlerStation <Metro>(name, type);
                    station.id   = id;
                    station.name = name;
                    return(station);
                }
                else if (type.Equals(RattlerTransportType.TRAIN))
                {
                    SimpleRattlerStation <Train> station = new SimpleRattlerStation <Train>(name, type);
                    station.id   = id;
                    station.name = name;
                    return(station);
                }
                else if (type.Equals(RattlerTransportType.TRAM))
                {
                    SimpleRattlerStation <Tram> station = new SimpleRattlerStation <Tram>(name, type);
                    station.id   = id;
                    station.name = name;
                    return(station);
                }
                else if (type.Equals(RattlerTransportType.EXPRESS_TRAIN))
                {
                    SimpleRattlerStation <ExpressTrain> station = new SimpleRattlerStation <ExpressTrain>(name, type);
                    station.id   = id;
                    station.name = name;
                    return(station);
                }
            }

            throw new JsonException("Неудалось прочесть");
        }
Exemplo n.º 7
0
        public void removeLink(LinkStation linkStation)
        {
            RattlerTransportType type = linkStation.getType();

            if (!allLinks.ContainsKey(type))
            {
                return;
            }

            allLinks[type].Remove(linkStation);
        }
Exemplo n.º 8
0
        public void addLink(LinkStation linkStation)
        {
            RattlerTransportType type = linkStation.getType();
            List <LinkStation>   links;

            if (allLinks.ContainsKey(type))
            {
                links = allLinks[type];
            }
            else
            {
                links          = new List <LinkStation>();
                allLinks[type] = links;
            }

            if (links.Contains(linkStation) || hasLink(type, linkStation.getA(), linkStation.getB()))
            {
                throw new ApplicationException("Данная связь уже установлена");
            }

            links.Add(linkStation);
        }
Exemplo n.º 9
0
        public LinkStation(RattlerStation A, RattlerStation B, double distance)
        {
            if (A.Equals(B))
            {
                throw new ArgumentException("Станция A и станция B одинаковы!");
            }

            if (!A.getType().Equals(B.getType()))
            {
                throw new ArgumentException("Соедиенение станций возможно лишь когда они одного типа!");
            }

            if (distance <= 0)
            {
                throw new ArgumentException("Дистанция должна быть положительной");
            }

            this.type = A.getType();

            this.A        = A;
            this.B        = B;
            this.distance = distance;
        }
Exemplo n.º 10
0
 public SimpleRattlerStation(string name, RattlerTransportType type)
 {
     this.name  = name;
     this.type  = type;
     this.links = new List <LinkStation>();
 }
Exemplo n.º 11
0
 private List <LinkStation> getLinksByType(RattlerTransportType type)
 {
     return(allLinks.ContainsKey(type) ? allLinks[type] : null);
 }