Skip to content

Commit 53c343e

Browse files
author
Vladimir Damov
committed
adding C# OOP Basic Exam
1 parent bdca8c2 commit 53c343e

File tree

21 files changed

+817
-0
lines changed

21 files changed

+817
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
using AnimalCentre.Entities;
2+
using AnimalCentre.Entities.Animals;
3+
using AnimalCentre.Entities.Procedures;
4+
using AnimalCentre.Models.Contracts;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Text;
9+
10+
namespace AnimalCentre.Core
11+
{
12+
public class AnimalCentre
13+
{
14+
private Hotel hotel;
15+
private Chip chip;
16+
private DentalCare dental;
17+
private Fitness fitness;
18+
private NailTrim nailTrim;
19+
private Play play;
20+
private Vaccinate vaccinate;
21+
22+
private Dictionary<string, List<string>> adopted;
23+
public IReadOnlyDictionary<string, List<string>> Adopted { get; }
24+
25+
public AnimalCentre()
26+
{
27+
hotel = new Hotel();
28+
adopted = new Dictionary<string, List<string>>();
29+
Adopted = adopted;
30+
chip = new Chip();
31+
dental = new DentalCare();
32+
fitness = new Fitness();
33+
nailTrim = new NailTrim();
34+
play = new Play();
35+
vaccinate = new Vaccinate();
36+
}
37+
public string RegisterAnimal(string type, string name, int energy, int happiness, int procedureTime)
38+
{
39+
if (hotel.Animals.ContainsKey(name))
40+
{
41+
throw new ArgumentException($"Animal {name} already exist");
42+
}
43+
44+
switch (type)
45+
{
46+
case "Cat": hotel.Accommodate(new Cat(name, energy, happiness, procedureTime)); break;
47+
case "Dog": hotel.Accommodate(new Dog(name, energy, happiness, procedureTime)); break;
48+
case "Lion": hotel.Accommodate(new Lion(name, energy, happiness, procedureTime)); break;
49+
case "Pig": hotel.Accommodate(new Pig(name, energy, happiness, procedureTime)); break;
50+
}
51+
52+
return $"Animal {name} registered successfully";
53+
54+
}
55+
56+
public string Chip(string name, int procedureTime)
57+
{
58+
59+
if (!hotel.Animals.ContainsKey(name))
60+
{
61+
throw new ArgumentException($"Animal {name} does not exist");
62+
}
63+
var animal = hotel.Animals[name];
64+
65+
chip.DoService(animal, procedureTime);
66+
67+
return $"{name} had chip procedure";
68+
}
69+
70+
public string Vaccinate(string name, int procedureTime)
71+
{
72+
if (!hotel.Animals.ContainsKey(name))
73+
{
74+
throw new ArgumentException($"Animal {name} does not exist");
75+
}
76+
var animal = hotel.Animals[name];
77+
78+
vaccinate.DoService(animal, procedureTime);
79+
80+
return $"{name} had vaccination procedure";
81+
}
82+
83+
public string Fitness(string name, int procedureTime)
84+
{
85+
if (!hotel.Animals.ContainsKey(name))
86+
{
87+
throw new ArgumentException($"Animal {name} does not exist");
88+
}
89+
var animal = hotel.Animals[name];
90+
91+
fitness.DoService(animal, procedureTime);
92+
93+
return $"{name} had fitness procedure";
94+
}
95+
96+
public string Play(string name, int procedureTime)
97+
{
98+
if (!hotel.Animals.ContainsKey(name))
99+
{
100+
throw new ArgumentException($"Animal {name} does not exist");
101+
}
102+
var animal = hotel.Animals[name];
103+
104+
play.DoService(animal, procedureTime);
105+
106+
return $"{name} was playing for {procedureTime} hours";
107+
}
108+
109+
public string DentalCare(string name, int procedureTime)
110+
{
111+
if (!hotel.Animals.ContainsKey(name))
112+
{
113+
throw new ArgumentException($"Animal {name} does not exist");
114+
}
115+
var animal = hotel.Animals[name];
116+
117+
dental.DoService(animal, procedureTime);
118+
119+
return $"{name} had dental care procedure";
120+
}
121+
122+
public string NailTrim(string name, int procedureTime)
123+
{
124+
if (!hotel.Animals.ContainsKey(name))
125+
{
126+
throw new ArgumentException($"Animal {name} does not exist");
127+
}
128+
var animal = hotel.Animals[name];
129+
130+
nailTrim.DoService(animal, procedureTime);
131+
132+
return $"{name} had nail trim procedure";
133+
}
134+
135+
public string Adopt(string animalName, string owner)
136+
{
137+
if (!hotel.Animals.ContainsKey(animalName))
138+
{
139+
throw new ArgumentException($"Animal {animalName} does not exist");
140+
}
141+
var animal = hotel.Animals.Values.First(x => x.Name == animalName);
142+
if (!Adopted.ContainsKey(owner))
143+
{
144+
adopted.Add(owner, new List<string>());
145+
}
146+
hotel.Adopt(animalName, owner);
147+
Adopted[owner].Add(animal.Name);
148+
if (animal.IsChipped)
149+
{
150+
return $"{owner} adopted animal with chip";
151+
}
152+
else
153+
{
154+
return $"{owner} adopted animal without chip";
155+
}
156+
157+
}
158+
159+
public string History(string type)
160+
{
161+
switch (type)
162+
{
163+
case "Chip": return chip.History();
164+
case "Vaccinate": return vaccinate.History();
165+
case "Fitness": return fitness.History();
166+
case "Play": return play.History();
167+
case "NailTrim": return nailTrim.History();
168+
case "DentalCare": return dental.History();
169+
default:
170+
return null;
171+
}
172+
173+
174+
}
175+
176+
}
177+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using System;
2+
using System.Linq;
3+
4+
namespace AnimalCentre.Core
5+
{
6+
public static class Engine
7+
{
8+
9+
public static void Run()
10+
{
11+
var args = Console.ReadLine().Split();
12+
var animalCentre = new AnimalCentre();
13+
var output = string.Empty;
14+
while (args[0] != "End")
15+
{
16+
var command = args[0];
17+
try
18+
{
19+
switch (command)
20+
{
21+
case "RegisterAnimal":
22+
{
23+
var type = args[1];
24+
var name = args[2];
25+
var energy = int.Parse(args[3]);
26+
var happiness = int.Parse(args[4]);
27+
var procedureTime = int.Parse(args[5]);
28+
output = animalCentre.RegisterAnimal(type, name, energy, happiness, procedureTime);
29+
}
30+
break;
31+
case "Chip":
32+
{
33+
var name = args[1];
34+
var procedureTime = int.Parse(args[2]);
35+
output = animalCentre.Chip(name, procedureTime);
36+
}
37+
break;
38+
case "Vaccinate":
39+
{
40+
var name = args[1];
41+
var procedureTime = int.Parse(args[2]);
42+
output = animalCentre.Vaccinate(name, procedureTime);
43+
}
44+
break;
45+
case "Fitness":
46+
{
47+
var name = args[1];
48+
var procedureTime = int.Parse(args[2]);
49+
output = animalCentre.Fitness(name, procedureTime);
50+
}
51+
break;
52+
case "Play":
53+
{
54+
var name = args[1];
55+
var procedureTime = int.Parse(args[2]);
56+
output = animalCentre.Play(name, procedureTime);
57+
}
58+
break;
59+
case "DentalCare":
60+
{
61+
var name = args[1];
62+
var procedureTime = int.Parse(args[2]);
63+
output = animalCentre.DentalCare(name, procedureTime);
64+
}
65+
break;
66+
case "NailTrim":
67+
{
68+
var name = args[1];
69+
var procedureTime = int.Parse(args[2]);
70+
output = animalCentre.NailTrim(name, procedureTime);
71+
}
72+
break;
73+
case "Adopt":
74+
{
75+
var name = args[1];
76+
var owner = args[2];
77+
output = animalCentre.Adopt(name, owner);
78+
}
79+
break;
80+
case "History":
81+
{
82+
var type = args[1];
83+
84+
output = animalCentre.History(type);
85+
}
86+
break;
87+
default:
88+
break;
89+
}
90+
}
91+
catch (InvalidOperationException IOE)
92+
{
93+
94+
output = "InvalidOperationException: " + IOE.Message;
95+
}
96+
catch (ArgumentException AE)
97+
{
98+
output = "ArgumentException: " + AE.Message;
99+
}
100+
Console.WriteLine(output);
101+
args = Console.ReadLine().Split();
102+
}
103+
104+
foreach (var owner in animalCentre.Adopted.OrderBy(x => x.Key))
105+
{
106+
Console.WriteLine($"--Owner: {owner.Key}");
107+
Console.WriteLine($" - Adopted animals: {string.Join(' ', owner.Value)}");
108+
}
109+
}
110+
}
111+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using AnimalCentre.Models.Contracts;
2+
using System;
3+
4+
namespace AnimalCentre.Entities.Animals
5+
{
6+
public abstract class Animal : IAnimal
7+
{
8+
private int happiness;
9+
private int energy;
10+
11+
protected Animal(string name, int energy, int happiness, int procedureTime)
12+
{
13+
Name = name;
14+
Energy = energy;
15+
Happiness = happiness;
16+
ProcedureTime = procedureTime;
17+
Owner = "Centre";
18+
IsAdopt = false;
19+
IsChipped = false;
20+
IsVaccinated = false;
21+
}
22+
23+
public string Name { get; set; }
24+
public int ProcedureTime { get; set; }
25+
public string Owner { get; set; }
26+
public bool IsAdopt { get; set; }
27+
public bool IsChipped { get; set; }
28+
public bool IsVaccinated { get; set; }
29+
30+
public int Energy
31+
{
32+
get { return energy; }
33+
set
34+
{
35+
if (value > 100 || value < 0)
36+
{
37+
throw new ArgumentException("Invalid energy");
38+
}
39+
energy = value;
40+
}
41+
}
42+
43+
public int Happiness
44+
{
45+
get { return happiness; }
46+
set
47+
{
48+
if (value > 100 || value < 0)
49+
{
50+
throw new ArgumentException("Invalid happiness");
51+
}
52+
happiness = value;
53+
}
54+
}
55+
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace AnimalCentre.Entities.Animals
2+
{
3+
public class Cat : Animal
4+
{
5+
public Cat(string name, int energy, int happiness, int procedureTime)
6+
: base(name, energy, happiness, procedureTime)
7+
{
8+
}
9+
public override string ToString()
10+
{
11+
return $" Animal type: {GetType().Name} - {Name} - Happiness: {Happiness} - Energy: {Energy}";
12+
}
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace AnimalCentre.Entities.Animals
2+
{
3+
public class Dog : Animal
4+
{
5+
public Dog(string name, int energy, int happiness, int procedureTime) : base(name, energy, happiness, procedureTime)
6+
{
7+
}
8+
public override string ToString()
9+
{
10+
return $" Animal type: {GetType().Name} - {Name} - Happiness: {Happiness} - Energy: {Energy}";
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)