-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBooksController.cs
More file actions
91 lines (76 loc) · 2.97 KB
/
Copy pathBooksController.cs
File metadata and controls
91 lines (76 loc) · 2.97 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
using Spectre.Console;
using TCSA.OOP.LibraryManagementSystem.Models;
namespace TCSA.OOP.LibraryManagementSystem;
internal class BooksController
{
internal void ViewBooks()
{
var table = new Table();
table.Border(TableBorder.Rounded);
table.AddColumn("[yellow]ID[/]");
table.AddColumn("[yellow]Title[/]");
table.AddColumn("[yellow]Author[/]");
table.AddColumn("[yellow]Category[/]");
table.AddColumn("[yellow]Location[/]");
table.AddColumn("[yellow]Pages[/]");
//Filtering only items of the book type
var books = MockDatabase.LibraryItems.OfType<Book>();
foreach (var book in books)
{
table.AddRow(
book.Id.ToString(),
$"[cyan]{book.Name}[/]",
$"[cyan]{book.Author}[/]",
$"[green]{book.Category}[/]",
$"[blue]{book.Location}[/]",
book.Pages.ToString()
);
}
AnsiConsole.Write(table);
AnsiConsole.MarkupLine("Press Any Key to Continue.");
Console.ReadKey();
}
internal void AddBook()
{
var title = AnsiConsole.Ask<string>("Enter the [green]title[/] of the book to add:");
var author = AnsiConsole.Ask<string>("Enter the [green]author[/] of the book:");
var category = AnsiConsole.Ask<string>("Enter the [green]category[/] of the book:");
var location = AnsiConsole.Ask<string>("Enter the [green]location[/] of the book:");
var pages = AnsiConsole.Ask<int>("Enter the [green]number of pages[/] in the book:");
if (MockDatabase.LibraryItems.OfType<Book>().Any(b => b.Name.Equals(title, StringComparison.OrdinalIgnoreCase)))
{
AnsiConsole.MarkupLine("[red]This book already exists.[/]");
}
else
{
var newBook = new Book(MockDatabase.LibraryItems.Count + 1,title, author, category, location, pages);
MockDatabase.LibraryItems.Add(newBook);
AnsiConsole.MarkupLine("[green]Book added successfully![/]");
}
AnsiConsole.MarkupLine("Press Any Key to Continue");
Console.ReadKey();
}
internal void DeleteBook()
{
if (MockDatabase.LibraryItems.Count == 0)
{
AnsiConsole.MarkupLine("[red]No books available to delete.[/]");
Console.ReadKey();
return;
}
var bookToDelete = AnsiConsole.Prompt(
new SelectionPrompt<LibraryItem>().Title("Select a [red] book[/] to delete:")
.UseConverter(b => $"{b.Name}")
.AddChoices(MockDatabase.LibraryItems));
if (MockDatabase.LibraryItems.Remove(bookToDelete))
{
AnsiConsole.MarkupLine("[red]Book deleted successfully![/]");
}
else
{
AnsiConsole.MarkupLine("[red]Book not found.[/]");
}
AnsiConsole.MarkupLine("Press Any Key to Continue.");
Console.ReadKey();
}
}