This repository has been archived by the owner on Sep 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Pull Request for Varga Vajk #4
Open
VajkVarga
wants to merge
4
commits into
CsharptutorialHungary:main
Choose a base branch
from
VajkVarga:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
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(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.