Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Pull Request for Varga Vajk #4

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions VajkVarga/Library/Library.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.1.32414.318
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Library", "Library\Library.csproj", "{45BC14A8-30C2-4E49-992D-1688590AC968}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{45BC14A8-30C2-4E49-992D-1688590AC968}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{45BC14A8-30C2-4E49-992D-1688590AC968}.Debug|Any CPU.Build.0 = Debug|Any CPU
{45BC14A8-30C2-4E49-992D-1688590AC968}.Release|Any CPU.ActiveCfg = Release|Any CPU
{45BC14A8-30C2-4E49-992D-1688590AC968}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4EDD2B31-371D-4F65-951A-348A410832A9}
EndGlobalSection
EndGlobal
64 changes: 64 additions & 0 deletions VajkVarga/Library/Library/BookManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Library.Entities;

namespace Library
{
internal class BookManager
{
private readonly List<Book> Books;

public BookManager(List<Book> books)
{
Books = books.Distinct().ToList();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HashSet is lehetne éppen, akkor a Distinct és a ToList megspórolható, illetve későbbi bővítésnél is Distinct maradna.

}

internal void StartConsoleLoop()
{
while (true)
{
Console.WriteLine("Choose from the following options or type \"exit\" to quit:");
Console.WriteLine("1 - Get all book data");
Console.WriteLine("2 - Get all books published before 1991");
var userInput = Console.ReadLine();
switch (userInput)
{
case "1":
ShowAllBook();
break;
case "2":
ShowBooksPublishedBefore(1991);
break;
case "exit":
return;
default:
Console.WriteLine("Invalid line, type \"1\", \"2\" or \"exit\"");
break;
}
}
}

internal void ShowAllBook()
{
ShowBooks(Books);
}

internal void ShowBooksPublishedBefore(int year)
{
ShowBooks(Books.Where(b => b.PublishYear < year));
}

private void ShowBooks(IEnumerable<Book> books)
{
foreach (var book in books)
{
Console.WriteLine($"Title: {book.Title}");
Console.WriteLine($"Author: {book.Author}");
Console.WriteLine($"PublishYear: {book.PublishYear}");
Console.WriteLine($"ReceivedDate: {book.ReceivedDate.ToString("yyyy-MM-dd")}");
Console.WriteLine();
}
}
}
}
17 changes: 17 additions & 0 deletions VajkVarga/Library/Library/Entities/Book.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace Library.Entities
{
internal record Book
{
public Guid Id { get; set; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Az ID-t biztos állítani kell létrehozás után is ?


public string Title { get; init; }

public string Author { get; init; }

public int PublishYear { get; init; }

public DateTime ReceivedDate { get; init; }
}
}
71 changes: 71 additions & 0 deletions VajkVarga/Library/Library/FileReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Library.Entities;
using Library.Serializer;

namespace Library
{
internal static class FileReader
{
public static List<Book> ReadAllBooks()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ezt a metódust pl több kisebbre lehetne bontani szerintem.

{
Console.WriteLine("Reading all books...");
var now = DateTime.Now;

var directory = @"../../../Resources";
if (!Directory.Exists(directory))
{
Console.WriteLine($"Error: Cannot find directory {directory}.");
return new();
}

var files = Directory.GetFiles(directory, "*.json", SearchOption.AllDirectories);

var fileContents = new List<string>();
var tasks = new List<Task<string>>();
foreach (var file in files)
{
var task = Task.Run(() => ReadFileContent(file));
tasks.Add(task);
}
Task.WaitAll(tasks.ToArray());
fileContents.AddRange(tasks.Select(task => task.Result));

var books = new List<Book>();
foreach (var fileContent in fileContents)
{
try
{
books.AddRange(JsonSerializer.Deserialize<List<Book>>(fileContent, new JsonSerializerOptions
{
WriteIndented = true,
Converters =
{
new CustomDateJsonConverter()
}
}));
}
catch (Exception)
{
Console.WriteLine($"Error: Couldn't deserialize content.");
return new();
}
}

books = books.Where(b => b.Id != Guid.Empty && b.ReceivedDate.Year >= b.PublishYear &&
!String.IsNullOrEmpty(b.Title) && !String.IsNullOrEmpty(b.Author)).ToList();

Console.WriteLine($"Successfully read {books.Count}({books.Distinct().Count()} distinct) books in {(DateTime.Now - now).TotalSeconds} seconds.");
return books.Distinct().ToList();
}

private static Task<string> ReadFileContent(string file)
{
return File.ReadAllTextAsync(file);
}
}
}
8 changes: 8 additions & 0 deletions VajkVarga/Library/Library/Library.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

</Project>
23 changes: 23 additions & 0 deletions VajkVarga/Library/Library/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Linq;

namespace Library
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to my library!");

var books = FileReader.ReadAllBooks();
if (!books.Any())
{
Console.WriteLine("No books found, exiting from program.");
return;
}

var bookManager = new BookManager(books);
bookManager.StartConsoleLoop();
}
}
}
Loading