-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex03_feed_the_animals.cs
83 lines (78 loc) · 2.9 KB
/
ex03_feed_the_animals.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace ex03_feed_the_animals
{
class AnimalsReefugee
{
public int FoodLimit { get; set; }
public string Area { get; set; }
public AnimalsReefugee()
{
this.FoodLimit = FoodLimit;
this.Area = Area;
}
}
class Program
{
static void Main()
{
var areaByHungryAnimals = new Dictionary<string, int>();
Dictionary<string, AnimalsReefugee> animals = new Dictionary<string, AnimalsReefugee>();
string command = "";
while((command = Console.ReadLine()) != "Last Info")
{
string[] execute = command.Split(':');
string name = execute[1];
int food = int.Parse(execute[2]);
string area = Convert.ToString(execute[3]);
if (execute[0] == "Add")
{
AnimalsReefugee c = new AnimalsReefugee { FoodLimit = food, Area = area };
if (!animals.ContainsKey(name))
{
animals.Add(name, c);
if (!areaByHungryAnimals.ContainsKey(area))
{
areaByHungryAnimals.Add(area, 0);
}
areaByHungryAnimals[area] += 1;
}
else
{
animals[name].FoodLimit += food;
}
}
else if(execute[0] == "Feed") //
{
if(animals.ContainsKey(name))// VVVVVVVVV
{
if ((animals[name].FoodLimit - food) > 0)
{
animals[name].FoodLimit -= food;
}
else
{
animals.Remove(name);
areaByHungryAnimals[area] -= 1;
Console.WriteLine($"{name} was successfully fed");
}
}
}
}
Console.WriteLine("Animals:");
var sorted = animals.OrderByDescending(x => x.Value.FoodLimit).ThenBy(x => x.Key);
foreach (var item in sorted.Where(x => x.Value.FoodLimit > 0))
{
Console.WriteLine($"{item.Key} -> {item.Value.FoodLimit}g");
}
//Ading each area to new Dictionary
Console.WriteLine("Areas with hungry animals:");
foreach (var item in areaByHungryAnimals.OrderByDescending(x => x.Value).Where(x => x.Value > 0))
{
Console.WriteLine($"{item.Key} : {item.Value}");
}
}
}
}