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("Неудалось прочесть"); }
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") }; }
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); }
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; } }
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("Неудалось прочесть"); }
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("Неудалось прочесть"); }
public void removeLink(LinkStation linkStation) { RattlerTransportType type = linkStation.getType(); if (!allLinks.ContainsKey(type)) { return; } allLinks[type].Remove(linkStation); }
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); }
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; }
public SimpleRattlerStation(string name, RattlerTransportType type) { this.name = name; this.type = type; this.links = new List <LinkStation>(); }
private List <LinkStation> getLinksByType(RattlerTransportType type) { return(allLinks.ContainsKey(type) ? allLinks[type] : null); }