using System;
using System.Collections.Generic;
namespace Lab5.TouristInformationCenter
{
///
/// Class used to store multiple locations in one place
///
class Register
{
private LocationsContainer AllLocations;
public Register()
{
AllLocations = new LocationsContainer();
}
public Register(LocationsContainer locations)
{
AllLocations = new LocationsContainer(locations);
}
///
/// Add one location to the register.
///
/// Target location
public void Add(Location location)
{
AllLocations.Add(location);
}
///
/// The amount of stored locations in the register.
///
/// Location count
public int Count()
{
return AllLocations.Count;
}
///
/// Access location from register by index
///
/// Target index
/// Location
public Location GetByIndex(int index)
{
return AllLocations.Get(index);
}
public int CountLocationsThatHaveGuides()
{
int count = 0;
for (int i = 0; i < AllLocations.Count; i++)
{
Location museum = AllLocations.Get(i) as Location;
if (museum != null && museum.HasGuide)
{
count++;
}
}
return count;
}
///
/// Get all of the different types of cities.
///
/// A list of city names
public List GetAllCities()
{
List cities = new List();
for (int i = 0; i < AllLocations.Count; i++)
{
Location location = AllLocations.Get(i);
if (!cities.Contains(location.City))
{
cities.Add(location.City);
}
}
return cities;
}
public List GetAllTypes()
{
List types = new List();
for (int i = 0; i < AllLocations.Count; i++)
{
Location museum = AllLocations.Get(i) as Location;
if (museum != null && !types.Contains(museum.Type))
{
types.Add(museum.Type);
}
}
return types;
}
public List FindCommonTypesBetweenCities()
{
List allCities = GetAllCities();
Dictionary types = new Dictionary();
foreach (string type in GetAllTypes())
{
types.Add(type, 0);
foreach (string city in allCities)
{
for (int i = 0; i < AllLocations.Count; i++)
{
Location museum = AllLocations.Get(i) as Location;
if (museum != null && museum.Type == type && museum.City == city)
{
types[type]++;
}
}
}
}
List result = new List();
foreach (var item in types)
{
if (item.Value == allCities.Count)
{
result.Add(item.Key);
}
}
return result;
}
public bool HasGuideAtWeekendByType(string type)
{
for (int i = 0; i < AllLocations.Count; i++)
{
Museum museum = AllLocations.Get(i) as Museum;
if (museum != null && museum.Type == type && museum.HasGuide)
{
return true;
}
}
return false;
}
public List FindCommonTypesWithGuidesAtWeekends()
{
List types = new List();
foreach (string type in FindCommonTypesBetweenCities())
{
if (HasGuideAtWeekendByType(type))
{
types.Add(type);
}
}
return types;
}
public LocationsContainer FindLocationsByAuthor(string author)
{
LocationsContainer locations = new LocationsContainer();
for (int i = 0; i < AllLocations.Count; i++)
{
Statue statue = AllLocations.Get(i) as Statue;
if (statue != null && statue.Author == author)
{
locations.Add(statue);
}
}
return locations;
}
public LocationsContainer FindLocationsAfterYear(int year)
{
LocationsContainer locations = new LocationsContainer();
for (int i = 0; i < AllLocations.Count; i++)
{
Location location = AllLocations.Get(i);
if (location.Year > year)
{
locations.Add(location);
}
}
return locations;
}
/*
///
/// Return a list of active locations from the register.
/// A location is considered active if it is working at least some amount a week.
///
/// Threshold which determines what is active
/// A list of active locations
public LocationsContainer FilterByActiveLocations(int threshold)
{
return AllLocations.FilterByActivity(threshold);
}
///
/// Find location that work the most days in a week
///
/// Most active location
public Location FindMostActiveLocation()
{
if (AllLocations.Count == 0)
{
return null;
}
Location mostActive = AllLocations.Get(0);
for (int i = 0; i < AllLocations.Count; i++)
{
Location location = AllLocations.Get(i);
if (location.Workdays.Count > mostActive.Workdays.Count)
{
mostActive = location;
}
}
return mostActive;
}
///
/// Find all locations that work the most days in a week
///
///
public LocationsContainer FindMostActiveLocations()
{
Location mostActive = FindMostActiveLocation();
LocationsContainer activeLocations = new LocationsContainer();
for (int i = 0; i < AllLocations.Count; i++)
{
Location location = AllLocations.Get(i);
if (location.Workdays.Count == mostActive.Workdays.Count)
{
activeLocations.Add(location);
}
}
return activeLocations;
}
///
/// Filter by property "Type".
///
/// Target type
/// A filtered container of locations
public LocationsContainer FilterByType(string type)
{
return AllLocations.FilterByType(type);
}
private List GetLocationNames()
{
List result = new List();
for (int i = 0; i < AllLocations.Count; i++)
{
Location location = AllLocations.Get(i);
if (!result.Contains(location.Name))
{
result.Add(location.Name);
}
}
return result;
}
///
/// Find all locations that have matching names
///
/// A container with locations that have matching names
public LocationsContainer FindLocationsWithDuplicateNames()
{
LocationsContainer result = new LocationsContainer();
List names = GetLocationNames();
foreach (string name in names)
{
LocationsContainer locationsByName = AllLocations.FilterByName(name);
if (locationsByName.Count <= 1) continue;
for (int i = 0; i < locationsByName.Count; i++)
{
result.Add(locationsByName.Get(i));
}
}
return result;
}
*/
}
}