-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathProgram.cs
More file actions
122 lines (99 loc) · 2.4 KB
/
Copy pathProgram.cs
File metadata and controls
122 lines (99 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using BookApp.Models;
using BookApp.Services;
var collection = new BookCollection();
void ShowBooks(List<Book> books)
{
if (books.Count == 0)
{
Console.WriteLine("No books found.");
return;
}
Console.WriteLine("\nYour Book Collection:\n");
for (int i = 0; i < books.Count; i++)
{
var book = books[i];
var status = book.Read ? "✓" : " ";
Console.WriteLine($"{i + 1}. [{status}] {book.Title} by {book.Author} ({book.Year})");
}
Console.WriteLine();
}
void HandleList()
{
var books = collection.ListBooks();
ShowBooks(books);
}
void HandleAdd()
{
Console.WriteLine("\nAdd a New Book\n");
Console.Write("Title: ");
var title = Console.ReadLine()?.Trim() ?? "";
Console.Write("Author: ");
var author = Console.ReadLine()?.Trim() ?? "";
Console.Write("Year: ");
var yearStr = Console.ReadLine()?.Trim() ?? "";
if (int.TryParse(yearStr, out var year))
{
collection.AddBook(title, author, year);
Console.WriteLine("\nBook added successfully.\n");
}
else
{
Console.WriteLine($"\nError: '{yearStr}' is not a valid year.\n");
}
}
void HandleRemove()
{
Console.WriteLine("\nRemove a Book\n");
Console.Write("Enter the title of the book to remove: ");
var title = Console.ReadLine()?.Trim() ?? "";
collection.RemoveBook(title);
Console.WriteLine("\nBook removed if it existed.\n");
}
void HandleFind()
{
Console.WriteLine("\nFind Books by Author\n");
Console.Write("Author name: ");
var author = Console.ReadLine()?.Trim() ?? "";
var books = collection.FindByAuthor(author);
ShowBooks(books);
}
void ShowHelp()
{
Console.WriteLine("""
Book Collection Helper
Commands:
list - Show all books
add - Add a new book
remove - Remove a book by title
find - Find books by author
help - Show this help message
""");
}
if (args.Length == 0)
{
ShowHelp();
return;
}
var command = args[0].ToLower();
switch (command)
{
case "list":
HandleList();
break;
case "add":
HandleAdd();
break;
case "remove":
HandleRemove();
break;
case "find":
HandleFind();
break;
case "help":
ShowHelp();
break;
default:
Console.WriteLine("Unknown command.\n");
ShowHelp();
break;
}