Skip to content

Commit a7e09c4

Browse files
committed
Add JS and C# samples
1 parent b9d566c commit a7e09c4

File tree

16 files changed

+804
-0
lines changed

16 files changed

+804
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ __pycache__/
4444
*.pyc
4545
*.pyo
4646

47+
# .NET
48+
bin/
49+
obj/
50+
*.user
51+
*.suo
52+
4753
# Testing
4854
coverage/
4955
.nyc_output/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<DefaultItemExcludes>$(DefaultItemExcludes);Tests/**</DefaultItemExcludes>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<None Update="data.json">
13+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
14+
</None>
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace BookApp.Models;
2+
3+
public class Book
4+
{
5+
public string Title { get; set; } = string.Empty;
6+
public string Author { get; set; } = string.Empty;
7+
public int Year { get; set; }
8+
public bool Read { get; set; }
9+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using BookApp.Models;
2+
using BookApp.Services;
3+
4+
var collection = new BookCollection();
5+
6+
void ShowBooks(List<Book> books)
7+
{
8+
if (books.Count == 0)
9+
{
10+
Console.WriteLine("No books found.");
11+
return;
12+
}
13+
14+
Console.WriteLine("\nYour Book Collection:\n");
15+
16+
for (int i = 0; i < books.Count; i++)
17+
{
18+
var book = books[i];
19+
var status = book.Read ? "✓" : " ";
20+
Console.WriteLine($"{i + 1}. [{status}] {book.Title} by {book.Author} ({book.Year})");
21+
}
22+
23+
Console.WriteLine();
24+
}
25+
26+
void HandleList()
27+
{
28+
var books = collection.ListBooks();
29+
ShowBooks(books);
30+
}
31+
32+
void HandleAdd()
33+
{
34+
Console.WriteLine("\nAdd a New Book\n");
35+
36+
Console.Write("Title: ");
37+
var title = Console.ReadLine()?.Trim() ?? "";
38+
39+
Console.Write("Author: ");
40+
var author = Console.ReadLine()?.Trim() ?? "";
41+
42+
Console.Write("Year: ");
43+
var yearStr = Console.ReadLine()?.Trim() ?? "";
44+
45+
if (int.TryParse(yearStr, out var year))
46+
{
47+
collection.AddBook(title, author, year);
48+
Console.WriteLine("\nBook added successfully.\n");
49+
}
50+
else
51+
{
52+
Console.WriteLine($"\nError: '{yearStr}' is not a valid year.\n");
53+
}
54+
}
55+
56+
void HandleRemove()
57+
{
58+
Console.WriteLine("\nRemove a Book\n");
59+
60+
Console.Write("Enter the title of the book to remove: ");
61+
var title = Console.ReadLine()?.Trim() ?? "";
62+
collection.RemoveBook(title);
63+
64+
Console.WriteLine("\nBook removed if it existed.\n");
65+
}
66+
67+
void HandleFind()
68+
{
69+
Console.WriteLine("\nFind Books by Author\n");
70+
71+
Console.Write("Author name: ");
72+
var author = Console.ReadLine()?.Trim() ?? "";
73+
var books = collection.FindByAuthor(author);
74+
75+
ShowBooks(books);
76+
}
77+
78+
void ShowHelp()
79+
{
80+
Console.WriteLine("""
81+
82+
Book Collection Helper
83+
84+
Commands:
85+
list - Show all books
86+
add - Add a new book
87+
remove - Remove a book by title
88+
find - Find books by author
89+
help - Show this help message
90+
""");
91+
}
92+
93+
if (args.Length == 0)
94+
{
95+
ShowHelp();
96+
return;
97+
}
98+
99+
var command = args[0].ToLower();
100+
101+
switch (command)
102+
{
103+
case "list":
104+
HandleList();
105+
break;
106+
case "add":
107+
HandleAdd();
108+
break;
109+
case "remove":
110+
HandleRemove();
111+
break;
112+
case "find":
113+
HandleFind();
114+
break;
115+
case "help":
116+
ShowHelp();
117+
break;
118+
default:
119+
Console.WriteLine("Unknown command.\n");
120+
ShowHelp();
121+
break;
122+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Book Collection App
2+
3+
*(This README is intentionally rough so you can improve it with GitHub Copilot CLI)*
4+
5+
A C# console app for managing books you have or want to read.
6+
It can add, remove, and list books. Also mark them as read.
7+
8+
---
9+
10+
## Current Features
11+
12+
* Reads books from a JSON file (our database)
13+
* Input checking is weak in some areas
14+
* Some tests exist but probably not enough
15+
16+
---
17+
18+
## Files
19+
20+
* `Program.cs` - Main CLI entry point
21+
* `Models/Book.cs` - Book model class
22+
* `Services/BookCollection.cs` - BookCollection class with data logic
23+
* `data.json` - Sample book data
24+
* `Tests/BookCollectionTests.cs` - xUnit tests
25+
26+
---
27+
28+
## Running the App
29+
30+
```bash
31+
dotnet run -- list
32+
dotnet run -- add
33+
dotnet run -- find
34+
dotnet run -- remove
35+
dotnet run -- help
36+
```
37+
38+
## Running Tests
39+
40+
```bash
41+
cd Tests
42+
dotnet test
43+
```
44+
45+
---
46+
47+
## Notes
48+
49+
* Not production-ready (obviously)
50+
* Some code could be improved
51+
* Could add more commands later
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System.Text.Json;
2+
using BookApp.Models;
3+
4+
namespace BookApp.Services;
5+
6+
public class BookCollection
7+
{
8+
private readonly string _dataFile;
9+
private List<Book> _books = [];
10+
11+
private static readonly JsonSerializerOptions JsonOptions = new()
12+
{
13+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
14+
WriteIndented = true
15+
};
16+
17+
public BookCollection(string? dataFile = null)
18+
{
19+
_dataFile = dataFile ?? Path.Combine(AppContext.BaseDirectory, "data.json");
20+
LoadBooks();
21+
}
22+
23+
public IReadOnlyList<Book> Books => _books;
24+
25+
private void LoadBooks()
26+
{
27+
try
28+
{
29+
var json = File.ReadAllText(_dataFile);
30+
_books = JsonSerializer.Deserialize<List<Book>>(json, JsonOptions) ?? [];
31+
}
32+
catch (FileNotFoundException)
33+
{
34+
_books = [];
35+
}
36+
catch (JsonException)
37+
{
38+
Console.WriteLine("Warning: data.json is corrupted. Starting with empty collection.");
39+
_books = [];
40+
}
41+
}
42+
43+
private void SaveBooks()
44+
{
45+
var json = JsonSerializer.Serialize(_books, JsonOptions);
46+
File.WriteAllText(_dataFile, json);
47+
}
48+
49+
public Book AddBook(string title, string author, int year)
50+
{
51+
var book = new Book { Title = title, Author = author, Year = year };
52+
_books.Add(book);
53+
SaveBooks();
54+
return book;
55+
}
56+
57+
public List<Book> ListBooks() => _books;
58+
59+
public Book? FindBookByTitle(string title)
60+
{
61+
return _books.Find(b => b.Title.Equals(title, StringComparison.OrdinalIgnoreCase));
62+
}
63+
64+
public bool MarkAsRead(string title)
65+
{
66+
var book = FindBookByTitle(title);
67+
if (book is null) return false;
68+
book.Read = true;
69+
SaveBooks();
70+
return true;
71+
}
72+
73+
public bool RemoveBook(string title)
74+
{
75+
var book = FindBookByTitle(title);
76+
if (book is null) return false;
77+
_books.Remove(book);
78+
SaveBooks();
79+
return true;
80+
}
81+
82+
public List<Book> FindByAuthor(string author)
83+
{
84+
return _books
85+
.Where(b => b.Author.Equals(author, StringComparison.OrdinalIgnoreCase))
86+
.ToList();
87+
}
88+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="coverlet.collector" Version="6.0.4" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
13+
<PackageReference Include="xunit" Version="2.9.3" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<Using Include="Xunit" />
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<ProjectReference Include="..\BookApp.csproj" />
23+
</ItemGroup>
24+
25+
</Project>

0 commit comments

Comments
 (0)