using System.Collections.Generic; namespace Lab5.TouristInformationCenter { class LocationsContainer { private Location[] locations; private int Capacity; public int Count { get; private set; } public LocationsContainer(int capacity = 16) { Capacity = capacity; locations = new Location[capacity]; } public LocationsContainer(LocationsContainer container) : this(container.Capacity) { for (int i = 0; i < container.Count; i++) { Add(container.Get(i)); } } /// /// Add a location to the end of the container /// /// Target location public void Add(Location location) { if (Count == Capacity) { EnsureCapacity(Capacity * 2); } locations[Count] = location; Count++; } /// /// Get a musem by index from container /// /// Target index /// Location at given index public Location Get(int index) { return locations[index]; } /// /// Check if given location exists in container /// /// Target location /// True if contains public bool Contains(Location location) { for (int i = 0; i < Count; i++) { if (locations[i].Equals(location)) { return true; } } return false; } /// /// Put a location at a specific index /// /// Target index /// Target location public void Put(int index, Location location) { locations[index] = location; } /* /// /// Filter by property "HasGuide" /// /// Target value /// A filtered container of locations public LocationsContainer FilterByGuide(bool hasGuide) { LocationsContainer filtered = new LocationsContainer(); for (int i = 0; i < Count; i++) { Location location = locations[i]; if (location.HasGuide == hasGuide) { filtered.Add(location); } } return filtered; } /// /// Filter by property "Type". /// /// Target type /// A filtered container of locations public LocationsContainer FilterByType(string type) { LocationsContainer filtered = new LocationsContainer(); for (int i = 0; i < Count; i++) { Location location = locations[i]; if (location.Type == type) { filtered.Add(location); } } return filtered; } /// /// Filter by property "Price". /// /// Maximum allowed price to be included /// A container with locations that dont't have a larger price then "maxPrice" public LocationsContainer FilterByPrice(double maxPrice) { LocationsContainer filtered = new LocationsContainer(); for (int i = 0; i < Count; i++) { Location location = locations[i]; if (location.Price <= maxPrice) { filtered.Add(location); } } return filtered; } /// /// Return a list of "active" locations from the contaner. /// A location is considered active if it is working at least some amount a week. /// /// Threshold which determines what is active /// A container of "active" locations public LocationsContainer FilterByActivity(int threshold) { LocationsContainer filtered = new LocationsContainer(); for (int i = 0; i < Count; i++) { Location location = locations[i]; if (location.Workdays.Count >= threshold) { filtered.Add(location); } } return filtered; } /// /// Filter by property "Name" /// /// Target name /// A filtered container public LocationsContainer FilterByName(string name) { LocationsContainer filtered = new LocationsContainer(); for (int i = 0; i < Count; i++) { Location location = locations[i]; if (location.Name == name) { filtered.Add(location); } } return filtered; } */ /// /// Sort container by given comparator /// /// Defined how it should be sorted public void Sort(LocationsComparator comparator) { for (int i = 0; i < Count - 1; i++) { for (int j = i + 1; j < Count; j++) { Location a = locations[i]; Location b = locations[j]; if (comparator.Compare(a, b) > 0) { locations[i] = b; locations[j] = a; } } } } /// /// Sort container by "Name" in alphabetical order. /// public void Sort() { Sort(new LocationsComparator()); } private void EnsureCapacity(int minimumCapacity) { if (minimumCapacity <= Capacity) return; Location[] locationsClone = new Location[minimumCapacity]; for (int i = 0; i < Count; i++) { locationsClone[i] = locations[i]; } Capacity = minimumCapacity; locations = locationsClone; } } }