public static DogsContainer ReadDogs(string fileName) { DogsContainer dogs = new DogsContainer(); string[] lines = File.ReadAllLines(fileName, Encoding.UTF8); foreach (string line in lines) { string[] values = line.Split(';'); int id = int.Parse(values[0]); string name = values[1]; string breed = values[2]; DateTime birthDate = DateTime.Parse(values[3]); Gender gender; Enum.TryParse(values[4], out gender); //tries to convert value to enum Dog dog = new Dog(id, name, breed, birthDate, gender); if (!dogs.Contains(dog)) { dogs.Add(dog); } } return(dogs); }
public static void PrintDogsToCSVFile(string fileName, DogsContainer dogs) { string[] lines = new string[dogs.Count + 1]; lines[0] = String.Format("{0};{1};{2};{3};{4}", "Reg.Nr.", "Vardas", "Veislė", "Gimimo data", "Lytis"); for (int i = 0; i < dogs.Count; i++) { Dog dog = dogs.Get(i); lines[i + 1] = String.Format("{0};{1};{2};{3};{4}", dog.ID, dog.Name, dog.Breed, dog.BirthDate, dog.Gender); } File.WriteAllLines(fileName, lines, Encoding.UTF8); }
private Dog FindOldestDog(DogsContainer dogs) { Dog oldest = dogs.Get(0); for (int i = 1; i < dogs.Count; i++) { if (DateTime.Compare(oldest.BirthDate, dogs.Get(i).BirthDate) > 0) { oldest = dogs.Get(i); } } return(oldest); }
public DogsContainer FilterByBreed(string breed) { DogsContainer filtered = new DogsContainer(); for (int i = 0; i < this.allDogs.Count; i++) { Dog dog = this.allDogs.Get(i); if (dog.Breed.Equals(breed)) { filtered.Add(dog); } } return(filtered); }
public DogsContainer FilterByVaccination() { DogsContainer filtered = new DogsContainer(); for (int i = 0; i < this.allDogs.Count; i++) { Dog dog = this.allDogs.Get(i); if (dog.RequiresVaccination) { filtered.Add(dog); } } return(filtered); }
static void Main(string[] args) { DogsContainer allDogs = InOutUtils.ReadDogs(@"Dogs.csv"); allDogs.Sort(); DogsRegister register = new DogsRegister(allDogs); InOutUtils.PrintDogs("Registro informacija", allDogs); Console.WriteLine("Iš viso šunų: {0}", register.DogsCount()); Console.WriteLine("Patinų: {0}", register.CountByGender(Gender.Male)); Console.WriteLine("Patelių: {0}", register.CountByGender(Gender.Female)); Console.WriteLine(); Dog oldest = register.FindOldestDog(); Console.WriteLine("Seniausias šuo"); Console.WriteLine("Vardas: {0}, Veislė: {1}, Amžius: {2}", oldest.Name, oldest.Breed, oldest.Age); Console.WriteLine(); List <string> breeds = register.FindBreeds(); Console.WriteLine("Šunų veislės:"); InOutUtils.PrintBreeds(breeds); Console.WriteLine(); Console.WriteLine("Kokios veislės šunis atrinkti?"); string selectedBreed = Console.ReadLine(); DogsContainer filteredByBreed = register.FilterByBreed(selectedBreed); InOutUtils.PrintDogs(selectedBreed, filteredByBreed); string fileName = selectedBreed + ".csv"; InOutUtils.PrintDogsToCSVFile(fileName, filteredByBreed); List <Vaccination> vaccinationsData = InOutUtils.ReadVaccinations(@"Vaccinations.csv"); register.UpdateVaccinationsInfo(vaccinationsData); Console.WriteLine(); DogsContainer requiresVaccination = register.FilterByVaccination(); InOutUtils.PrintDogs("Reikia skiepyti (Visos veislės)", requiresVaccination); Console.WriteLine(); InOutUtils.PrintDogs("Reikia skiepyti (" + selectedBreed + ")", requiresVaccination.Intersect(filteredByBreed)); }
public static void PrintDogs(string label, DogsContainer dogs) { Console.WriteLine(new string('-', 74)); Console.WriteLine("| {0,-70} |", label); Console.WriteLine(new string('-', 74)); Console.WriteLine("| {0,8} | {1,-15} | {2,-15} | {3,-12} | {4,-8} |", "Reg.Nr.", "Vardas", "Veislė", "Gimimo data", "Lytis"); Console.WriteLine(new string('-', 74)); for (int i = 0; i < dogs.Count; i++) { Dog dog = dogs.Get(i); Console.WriteLine("| {0,8} | {1,-15} | {2,-15} | {3,-12:yyyy-MM-dd} | {4,-8} |", dog.ID, dog.Name, dog.Breed, dog.BirthDate, dog.Gender); } Console.WriteLine(new string('-', 74)); }
public DogsRegister(DogsContainer dogs) { this.allDogs = new DogsContainer(dogs); }
public Dog FindOldestDog(string breed) { DogsContainer filtered = this.FilterByBreed(breed); return(this.FindOldestDog(filtered)); }