1
0
oop-1-labs/Lab1/Lab1.Exercises.Register/InOutUtils.cs

65 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Lab1.Exercises.Register
{
class InOutUtils
{
public static List<Dog> ReadDogs(string fileName)
{
List<Dog> Dogs = new List<Dog>();
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);
Dogs.Add(dog);
}
return Dogs;
}
public static void PrintDogs(List<Dog> Dogs)
{
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));
foreach (Dog dog in Dogs)
{
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 static void PrintDog(Dog dog)
{
Console.WriteLine("Vardas: {0}, Veislė: {1}, Amžius: {2}", dog.Name, dog.Breed, dog.CalculateAge());
}
public static void PrintBreeds(List<string> breeds)
{
foreach (string breed in breeds)
{
Console.WriteLine(breed);
}
}
public static void PrintDogsToCSVFile(string fileName, List<Dog> 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++)
{
lines[i + 1] = String.Format("{0};{1};{2};{3};{4}", Dogs[i].ID, Dogs[i].Name, Dogs[i].Breed, Dogs[i].BirthDate, Dogs[i].Gender);
}
File.WriteAllLines(fileName, lines, Encoding.UTF8);
}
}
}