Adding the rest tasks
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
using System.Diagnostics;
|
||||||
|
using System.Reflection.Metadata;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
namespace DependencyInjections
|
namespace DependencyInjections
|
||||||
@@ -33,12 +35,32 @@ namespace DependencyInjections
|
|||||||
|
|
||||||
serviceCollection.AddTransient<NotificationService>();
|
serviceCollection.AddTransient<NotificationService>();
|
||||||
|
|
||||||
|
bool UseSQL = false;
|
||||||
|
|
||||||
|
if (UseSQL)
|
||||||
|
{
|
||||||
|
serviceCollection.AddTransient<IProductRepository, SqlProductRepository>();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
serviceCollection.AddSingleton<IProductRepository, InMemoryProductReposity>();
|
||||||
|
}
|
||||||
|
|
||||||
|
serviceCollection.AddTransient<ProductService>();
|
||||||
|
|
||||||
|
serviceCollection.AddTransient<IGameEngine, GuessNumberGame>();
|
||||||
|
serviceCollection.AddTransient<IGameEngine, RockPaperScissorsGame>();
|
||||||
|
|
||||||
|
serviceCollection.AddTransient<GameManager>();
|
||||||
|
|
||||||
var sp = serviceCollection.BuildServiceProvider();
|
var sp = serviceCollection.BuildServiceProvider();
|
||||||
|
|
||||||
sp.GetRequiredService<UserService>().Log();
|
sp.GetRequiredService<UserService>().Log();
|
||||||
sp.GetRequiredService<CheckoutService>().process();
|
sp.GetRequiredService<CheckoutService>().process();
|
||||||
sp.GetRequiredService<NotificationService>().SendNotification("Hello, There");
|
sp.GetRequiredService<NotificationService>().SendNotification("Hello, There");
|
||||||
|
sp.GetRequiredService<ProductService>().SeedProducts();
|
||||||
|
sp.GetRequiredService<ProductService>().DisplayAllProducts();
|
||||||
|
sp.GetRequiredService<GameManager>().Start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,4 +184,179 @@ namespace DependencyInjections
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface IProductRepository
|
||||||
|
{
|
||||||
|
List<string> GetAllProducts();
|
||||||
|
void AddProduct(string name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class InMemoryProductReposity : IProductRepository
|
||||||
|
{
|
||||||
|
List<string> products = new List<string>();
|
||||||
|
|
||||||
|
public void AddProduct(string name)
|
||||||
|
{
|
||||||
|
products.Add(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<string> GetAllProducts()
|
||||||
|
{
|
||||||
|
return products;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class SqlProductRepository : IProductRepository
|
||||||
|
{
|
||||||
|
public void AddProduct(string name)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Wrote {name} to SQL Database");
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<string> GetAllProducts()
|
||||||
|
{
|
||||||
|
Console.WriteLine("Got all products from SQL Database");
|
||||||
|
return new List<string>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ProductService
|
||||||
|
{
|
||||||
|
private readonly IProductRepository productRepository;
|
||||||
|
|
||||||
|
public ProductService(IProductRepository productRepository)
|
||||||
|
{
|
||||||
|
this.productRepository = productRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SeedProducts()
|
||||||
|
{
|
||||||
|
productRepository.AddProduct("Skovl");
|
||||||
|
productRepository.AddProduct("Spade");
|
||||||
|
productRepository.AddProduct("Spand");
|
||||||
|
productRepository.AddProduct("Teslacoil");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DisplayAllProducts()
|
||||||
|
{
|
||||||
|
foreach (var prod in productRepository.GetAllProducts())
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Got: {prod}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IGameEngine
|
||||||
|
{
|
||||||
|
void Play();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class GuessNumberGame : IGameEngine
|
||||||
|
{
|
||||||
|
public void Play()
|
||||||
|
{
|
||||||
|
int rightNumber = new Random().Next(1, 11);
|
||||||
|
|
||||||
|
Console.WriteLine("Guess number between 1 and 10");
|
||||||
|
|
||||||
|
var readline = Console.ReadLine();
|
||||||
|
|
||||||
|
if (int.TryParse(readline, out var number))
|
||||||
|
{
|
||||||
|
if (number.Equals(rightNumber))
|
||||||
|
{
|
||||||
|
Console.WriteLine("You won.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine("WRONG");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine("Is that even a number?");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class RockPaperScissorsGame : IGameEngine
|
||||||
|
{
|
||||||
|
public void Play()
|
||||||
|
{
|
||||||
|
var posibilities = new List<string>()
|
||||||
|
{
|
||||||
|
"scissor",
|
||||||
|
"paper",
|
||||||
|
"rock",
|
||||||
|
};
|
||||||
|
|
||||||
|
Console.WriteLine("Choose between");
|
||||||
|
|
||||||
|
for (int i = 0; i < posibilities.Count; i++)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"{i}: {posibilities[i]}");
|
||||||
|
}
|
||||||
|
|
||||||
|
var readline = Console.ReadLine();
|
||||||
|
|
||||||
|
if (int.TryParse(readline, out var number))
|
||||||
|
{
|
||||||
|
if (number > posibilities.Count)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Must choose inside the range");
|
||||||
|
}
|
||||||
|
|
||||||
|
var computerPlay = new Random().Next(0, 3);
|
||||||
|
|
||||||
|
Console.WriteLine($"Computer choose: {posibilities[computerPlay]}");
|
||||||
|
|
||||||
|
if (computerPlay > number || (computerPlay == 0 && number == 2))
|
||||||
|
{
|
||||||
|
Console.WriteLine("Computer won...");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine("You won");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine("Is that even a number?");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class GameManager
|
||||||
|
{
|
||||||
|
private readonly IEnumerable<IGameEngine> gameEngines;
|
||||||
|
|
||||||
|
public GameManager(IEnumerable<IGameEngine> gameEngines)
|
||||||
|
{
|
||||||
|
this.gameEngines = gameEngines;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Start()
|
||||||
|
{
|
||||||
|
Console.WriteLine("Choose game");
|
||||||
|
|
||||||
|
for (int i = 0; i < gameEngines.Count(); i++)
|
||||||
|
{
|
||||||
|
Console.WriteLine(i + ": " + gameEngines.ToList()[i].GetType().Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
var readline = Console.ReadLine();
|
||||||
|
|
||||||
|
if (int.TryParse(readline, out var number))
|
||||||
|
{
|
||||||
|
if (number > gameEngines.Count())
|
||||||
|
{
|
||||||
|
Console.WriteLine("Must be in range");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
gameEngines.ToList()[number].Play();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user