Skip to content

Commit 9064fd4

Browse files
committed
Added last exercises
1 parent c8317fe commit 9064fd4

9 files changed

Lines changed: 283 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace PokemonTrainer
2+
{
3+
public class Pokemon
4+
{
5+
public string Name { get; set; }
6+
public string Element { get; set; }
7+
public int Health { get; set; }
8+
9+
public Pokemon(string name, string element, int health)
10+
{
11+
Name = name;
12+
Element = element;
13+
Health = health;
14+
}
15+
16+
}
17+
}
Lines changed: 8 additions & 0 deletions
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>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace PokemonTrainer
6+
{
7+
public class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
List<Trainer> trainers = new List<Trainer>();
12+
string command = Console.ReadLine();
13+
14+
while (command != "Tournament")
15+
{
16+
string[] operations = command.Split(" ", StringSplitOptions.RemoveEmptyEntries);
17+
string trainerName = operations[0];
18+
string pokeName = operations[1];
19+
string element = operations[2];
20+
int health = int.Parse(operations[3]);
21+
Trainer trainer = new Trainer(trainerName, 0, new List<Pokemon>());
22+
Pokemon pokemon = new Pokemon(pokeName, element, health);
23+
24+
if (!trainers.Any(x => x.Name == trainerName))
25+
{
26+
trainers.Add(trainer);
27+
}
28+
trainers.First(x => x.Name == trainerName).Pokemons.Add(pokemon);
29+
30+
command = Console.ReadLine();
31+
}
32+
33+
string elem = Console.ReadLine();
34+
35+
while (elem != "End")
36+
{
37+
foreach (Trainer trainer in trainers)
38+
{
39+
if (trainer.Pokemons.Any(p => p.Element == elem))
40+
{
41+
trainer.Badges++;
42+
}
43+
else
44+
{
45+
for (int i = 0; i < trainer.Pokemons.Count; i++)
46+
{
47+
trainer.Pokemons[i].Health -= 10;
48+
if (trainer.Pokemons[i].Health <= 0)
49+
{
50+
trainer.Pokemons.Remove(trainer.Pokemons[i]);
51+
i--;
52+
}
53+
}
54+
}
55+
}
56+
elem = Console.ReadLine();
57+
}
58+
59+
Console.WriteLine(string.Join(Environment.NewLine, trainers.OrderByDescending(t => t.Badges)));
60+
}
61+
}
62+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Collections.Generic;
2+
3+
namespace PokemonTrainer
4+
{
5+
public class Trainer
6+
{
7+
public string Name { get; set; }
8+
public int Badges { get; set; }
9+
public List<Pokemon> Pokemons { get; set; }
10+
11+
public Trainer(string name, int badges, List<Pokemon> pokemons)
12+
{
13+
Name = name;
14+
Badges = badges;
15+
Pokemons = pokemons;
16+
}
17+
public override string ToString()
18+
{
19+
return $"{this.Name} {this.Badges} {this.Pokemons.Count}";
20+
}
21+
}
22+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Text;
2+
3+
namespace SoftUniParking
4+
{
5+
public class Car
6+
{
7+
public string Make { get; set; }
8+
9+
public string Model { get; set; }
10+
11+
public int HorsePower { get; set; }
12+
13+
public string RegistrationNumber { get; set; }
14+
15+
public Car(string make, string model, int horsePower, string registrationNumber)
16+
{
17+
Make = make;
18+
Model = model;
19+
HorsePower = horsePower;
20+
RegistrationNumber = registrationNumber;
21+
}
22+
23+
public override string ToString()
24+
{
25+
StringBuilder builder = new StringBuilder();
26+
27+
builder
28+
.AppendLine($"Make: {Make}")
29+
.AppendLine($"Model: {Model}")
30+
.AppendLine($"HorsePower: {HorsePower}")
31+
.AppendLine($"RegistrationNumber: {RegistrationNumber}");
32+
33+
return builder.ToString().Trim();
34+
}
35+
36+
}
37+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace SoftUniParking
5+
{
6+
public class Parking
7+
{
8+
private List<Car> cars;
9+
private int capacity;
10+
public Parking(int capacity)
11+
{
12+
this.capacity = capacity;
13+
this.cars = new List<Car>();
14+
}
15+
16+
public int Count => this.cars.Count();
17+
18+
public string AddCar(Car car)
19+
{
20+
if (this.cars.Any(x => x.RegistrationNumber == car.RegistrationNumber))
21+
{
22+
return $"Car with that registration number, already exists!";
23+
}
24+
25+
else if (this.cars.Count >= this.capacity)
26+
{
27+
return $"Parking is full!";
28+
}
29+
else
30+
{
31+
this.cars.Add(car);
32+
return $"Successfully added new car {car.Make} {car.RegistrationNumber}";
33+
34+
}
35+
36+
}
37+
public string RemoveCar(string registrationNumber)
38+
{
39+
if (!this.cars.Any(x => x.RegistrationNumber == registrationNumber))
40+
{
41+
return $"Car with that registration number, doesn't exist!";
42+
}
43+
else
44+
{
45+
this.cars.Remove(this.cars.FirstOrDefault(x => x.RegistrationNumber == registrationNumber));
46+
return $"Successfully removed {registrationNumber}";
47+
48+
}
49+
}
50+
public Car GetCar(string registrationNumber)
51+
{
52+
return this.cars.FirstOrDefault(x => x.RegistrationNumber == registrationNumber);
53+
}
54+
55+
public void RemoveSetOfRegistrationNumber(List<string> registrationNumbers)
56+
{
57+
foreach (var currentNumber in registrationNumbers)
58+
{
59+
this.cars.RemoveAll(x => x.RegistrationNumber == currentNumber);
60+
}
61+
}
62+
}
63+
}
Lines changed: 8 additions & 0 deletions
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>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31624.102
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SoftUniParking", "SoftUniParking.csproj", "{C8693C0F-6BEE-437D-86FA-C1A244A45089}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{C8693C0F-6BEE-437D-86FA-C1A244A45089}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{C8693C0F-6BEE-437D-86FA-C1A244A45089}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{C8693C0F-6BEE-437D-86FA-C1A244A45089}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{C8693C0F-6BEE-437D-86FA-C1A244A45089}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {763A2D3C-9ADA-4F3E-8755-5EB1022FCC75}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
3+
namespace SoftUniParking
4+
{
5+
public class StartUp
6+
{
7+
public static void Main(string[] args)
8+
{
9+
var car = new Car("Skoda", "Fabia", 65, "CC1856BG");
10+
var car2 = new Car("Audi", "A3", 110, "EB8787MN");
11+
12+
Console.WriteLine(car.ToString());
13+
//Make: Skoda
14+
//Model: Fabia
15+
//HorsePower: 65
16+
//RegistrationNumber: CC1856BG
17+
18+
var parking = new Parking(5);
19+
Console.WriteLine(parking.AddCar(car));
20+
//Successfully added new car Skoda CC1856BG
21+
22+
Console.WriteLine(parking.AddCar(car));
23+
//Car with that registration number, already exists!
24+
25+
Console.WriteLine(parking.AddCar(car2));
26+
//Successfully added new car Audi EB8787MN
27+
28+
Console.WriteLine(parking.GetCar("EB8787MN").ToString());
29+
//Make: Audi
30+
//Model: A3
31+
//HorsePower: 110
32+
//RegistrationNumber: EB8787MN
33+
34+
Console.WriteLine(parking.RemoveCar("EB8787MN"));
35+
//Successfullyremoved EB8787MN
36+
37+
Console.WriteLine(parking.Count); //1
38+
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)