/*
Copyright (c) 2000 Benjamin Crowell,
2008 Kostas Giannakakis. All rights reserved.
About the GPL License
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
using System;
using System.IO;
using System.Globalization;
using System.Collections.Generic;
using System.Threading;
namespace PlanetFinder
{
public class CityList
{
static private List<City> cities = new List<City>();
static private CityList instance = null;
private const String FILE_NAME = "cities.txt";
private CultureInfo cultureInfo = new CultureInfo("en-US");
private CityList()
{
}
static public CityList GetInstance()
{
if (instance == null)
{
instance = new CityList();
instance.Init();
}
return instance;
}
public City getCity(String name)
{
foreach(City city in cities)
{
if (city.Name.CompareTo(name) == 0)
{
return city;
}
}
return null;
}
public void Save()
{
try
{
FileStream fs = new FileStream(FILE_NAME, FileMode.Create);
StreamWriter writer = new StreamWriter(fs);
foreach (City city in cities)
{
writer.WriteLine(String.Format(cultureInfo,
"{0},{1},{2:0.00},{3:0.00},{4:0.00},{5:0.0}",
city.Name, city.Country,
city.PopMillions, city.LatDegrees,
city.LonDegrees, city.TimeZone));
}
writer.Close();
fs.Close();
}
catch (Exception)
{
}
}
private int Init() {
try {
FileStream fs = new FileStream(FILE_NAME, FileMode.Open);
StreamReader reader = new StreamReader(fs);
string line = reader.ReadLine();
int count = 0;
while(line != null)
{
if (AddCity(line))
{
count++;
}
line = reader.ReadLine();
}
reader.Close();
fs.Close();
return count;
} catch (Exception) {
}
return 0;
}
public List<City> getCities()
{
return cities;
}
public List<String> getCityNames()
{
List<String> cityNames = new List<String>();
foreach (City city in cities)
{
cityNames.Add(city.Name);
}
return cityNames;
}
public bool Add(String name, double latitude, double longitude,
double timezone)
{
foreach (City city in cities)
{
if (city.Name.CompareTo(name) == 0)
{
return false;
}
}
City newCity = new City(name, "-", 0,
latitude, longitude, timezone);
cities.Add(newCity);
return true;
}
public bool Edit(String oldName, String newName,
double latitude, double longitude, double timezone)
{
foreach (City city in cities)
{
if (city.Name.CompareTo(oldName) == 0)
{
city.Name = newName;
city.TimeZone = timezone;
city.LatDegrees = latitude;
city.LonDegrees = longitude;
return true;
}
}
return false;
}
public bool Delete(String name)
{
foreach (City city in cities)
{
if (city.Name.CompareTo(name) == 0)
{
cities.Remove(city);
return true;
}
}
return false;
}
public City GetBiggestInTimezone(double timezone)
{
City biggest = null;
foreach(City city in cities) {
if (Math.Abs(city.TimeZone - timezone) < 0.001)
{
if (biggest == null || biggest.PopMillions < city.PopMillions)
{
biggest = city;
}
}
}
return biggest;
}
private bool AddCity(string line)
{
string [] tokens = line.Split(',');
if (tokens.Length >= 6)
{
double population, latitude, longitude, timezone;
CultureInfo cultureInfo = new CultureInfo("en-US");
try {
population = Double.Parse(tokens[2], cultureInfo);
latitude = Double.Parse(tokens[3], cultureInfo);
longitude = Double.Parse(tokens[4], cultureInfo);
timezone = Double.Parse(tokens[5], cultureInfo);
} catch (Exception ) {
return false;
}
City city = new City(tokens[0], tokens[1],
population,
latitude, longitude, timezone);
cities.Add(city);
return true;
}
return false;
}
}
}