-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
194 lines (168 loc) · 6.61 KB
/
Copy pathProgram.cs
File metadata and controls
194 lines (168 loc) · 6.61 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System;
using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
namespace BookLibrary
{
internal class Program
{
static readonly List<IBook> books = []; // List to store all books
private static void Main(string[] args)
{
int option;
do
{
Console.WriteLine("0 - Exit");
Console.WriteLine("1 - Add a new book");
Console.WriteLine("2 - Find a book");
Console.WriteLine("3 - Borrow a book");
Console.WriteLine("4 - Return a book");
Console.Write("Enter your choice: ");
string? input = Console.ReadLine();
if (input == null)
{
Console.WriteLine("Invalid input. Please try again.");
return;
}
else
{
option = int.Parse(input);
}
switch (option)
{
case 0:
Console.WriteLine("Exiting...");
break;
case 1:
AddBook();
break;
case 2:
FindBook();
break;
case 3:
BorrowBook();
break;
case 4:
ReturnBook();
break;
default:
Console.WriteLine("This operation is not supported, please try again.");
break;
}
} while (option != 0);
}
private static void AddBook()
{
// Prompt user for the book type
Console.WriteLine("Enter book type (Ebook, HardCover, AudioBook): ");
string? type = Console.ReadLine() ?? "";
// Prompt user for the book title
Console.WriteLine("Enter book title: ");
string? title = Console.ReadLine() ?? "";
// Create a new book instance based on the type entered
IBook book = type.ToLower() switch
{
"ebook" => new Ebook { Title = title },
"hardcover" => new HardCover { Title = title },
"audiobook" => new AudioBook { Title = title },
_ => throw new ArgumentException("Invalid book type. Please try again.")
};
// Check if the book type is valid and add to list if it is
if (book != null)
{
book.Title = title; // Set the title of the book
// Set initial properties for the new book
if (book is HardCover hardCoverBook)
{
hardCoverBook.Location = "Library";
}
books.Add(book); // Add the book to the list
Console.WriteLine($"Added {type} '{title}' to the library.");
}
else
{
Console.WriteLine("Invalid book type. Please try again.");
}
}
private static void FindBook()
{
// Prompt user to enter the book title they are looking for
Console.WriteLine("Enter the title of the book to find: ");
string? title = Console.ReadLine() ?? "";
// Search the list for a book with the given title
IBook? foundBook = books.Find(book => book.Title.Equals(title, StringComparison.OrdinalIgnoreCase));
// Check if the book was found
if (foundBook != null)
{
string status = foundBook.IsBorrowed ? "borrowed" : "available";
Console.WriteLine($"The book '{title}' is {status}.");
}
else
{
Console.WriteLine($"The book '{title}' does not exist in the library.");
}
}
private static void BorrowBook()
{
// Prompt user to enter the title of the book they want to borrow
Console.WriteLine("Enter the title of the book to borrow: ");
string? title = Console.ReadLine() ?? "";
// Search for the book in the list
IBook? foundBook = books.Find(book => book.Title.Equals(title, StringComparison.OrdinalIgnoreCase));
// If the book exists and is available, mark it as borrowed
if (foundBook != null)
{
if (!foundBook.IsBorrowed)
{
foundBook.MarkAsBorrowed(); // Mark book as borrowed
// If the book is a hardcover, change its location
if (foundBook is HardCover hardCoverBook)
{
hardCoverBook.Location = "Client";
}
Console.WriteLine($"You have borrowed '{title}'.");
}
else
{
Console.WriteLine($"The book '{title}' is already borrowed.");
}
}
else
{
Console.WriteLine($"The book '{title}' does not exist in the library.");
}
}
private static void ReturnBook()
{
// Prompt user to enter the title of the book they want to return
Console.WriteLine("Enter the title of the book to return: ");
string? title = Console.ReadLine() ?? "";
// Search for the book in the list
IBook? foundBook = books.Find(book => book.Title.Equals(title, StringComparison.OrdinalIgnoreCase));
// If the book is found and currently borrowed, mark it as returned
if (foundBook != null)
{
if (foundBook.IsBorrowed)
{
foundBook.MarkAsReturned(); // Mark book as returned
// If the book is a hardcover, set its location back to Library
if (foundBook is HardCover hardCoverBook)
{
hardCoverBook.Location = "Library";
}
Console.WriteLine($"You have returned '{title}'.");
}
else
{
Console.WriteLine($"The book '{title}' was not borrowed.");
}
}
else
{
Console.WriteLine($"The book '{title}' does not exist in the library.");
}
}
}
}