1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| const string INPUT = @"03/04/2023
Exposition du grand classique
243
Rue de LAMBREYAGE
59450
Sin-le-Noble
Liste des participants
Art Contemporain
Matthieu dethé
45
France
23
18 // Je rajoute ici
Benoit Breton
34
France
12
5 // ici
Jean Flou
65
Belgique
53
Nombre
3
2 // ici
Street Art
Sabart Deco
32
France
14
7 // ici
Laurent Pez
23
Suisse
4 // Mais pas là
Nombre
2
Nombre Total d'exposant
5";
Console.WriteLine("Hello, World!");
Exposition exposition = Exposition.Deserialize(INPUT);
Console.WriteLine(exposition.Name);
Console.WriteLine(exposition.Place.Street);
Console.WriteLine(exposition.Categories.Count);
foreach (Exposition.Category c in exposition.Categories)
{
Console.WriteLine(c.Name);
foreach (Exposition.Participant p in c.Participants)
{
Console.WriteLine(p.Name);
}
}
class Exposition
{
public DateOnly Date { get; set; }
public string Name { get; set; } = string.Empty;
public Address Place { get; set; } = new();
public List<Category> Categories { get; set; } = new();
public static Exposition Deserialize(string data)
{
// Nettoyage des données entrée
string[] lines = data.Split("\r\n");
for (int i = 0, cpt = lines.Length; i < cpt; i++)
{
if (lines[i].IndexOf("//") > 0)
{
lines[i] = lines[i][..lines[i].IndexOf("//")];
}
lines[i] = lines[i].TrimEnd();
}
Exposition exposition = new()
{
Date = DateOnly.ParseExact(lines[0], "dd/MM/yyyy", null),
Name = lines[1],
};
// Lecture de l'adresse
exposition.Place = Address.Deserialize(lines[2..6]);
if (lines[6] != "Liste des participants")
{
throw new FormatException("Le fichier ne semble pas convenir !");
}
int j = 7;
while (lines[j] != "Nombre Total d'exposant")
{
(Category category, int readlines) = Category.Deserialize(lines[j..]);
exposition.Categories.Add(category);
j += readlines;
}
return exposition;
}
public class Address
{
public string Number { get; set; } = string.Empty;
public string Street { get; set; } = string.Empty;
public string PostalCode { get; set; } = string.Empty;
public string City { get; set; } = string.Empty;
public static Address Deserialize(string[] lines)
{
return new()
{
Number = lines[0],
Street = lines[1],
PostalCode = lines[2],
City = lines[3],
};
}
}
public class Category
{
public string Name { get; set; } = string.Empty;
public List<Participant> Participants { get; set; } = new();
public static (Category, int) Deserialize(string[] lines)
{
Category category = new() { Name = lines[0] };
int i = 1;
while (lines[i] != "Nombre")
{
(Participant participant, int readlines) = Participant.Deserialize(lines[i..(i + 5)]);
category.Participants.Add(participant);
i += readlines;
}
bool thirdLine = int.TryParse(lines[i + 2], out _);
return (category, i + (thirdLine ? 3 : 2));
}
}
public class Participant
{
public string Name { get; set; } = string.Empty;
public int Age { get; set; }
public string Country { get; set; } = string.Empty;
public int WorkCount { get; set; }
public int SellingCount { get; set; }
public static (Participant, int) Deserialize(string[] lines)
{
bool fifthLine = int.TryParse(lines[4], out int sell);
return (new() { Name = lines[0], Age = int.Parse(lines[1]), Country = lines[2], WorkCount = int.Parse(lines[3]), SellingCount = sell }, fifthLine ? 5 : 4);
}
}
} |
Partager