-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
635 lines (553 loc) · 18.5 KB
/
Program.cs
File metadata and controls
635 lines (553 loc) · 18.5 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
// Define the IBorrowable interface
interface IBorrowable
{
void Borrow(Borrower borrower);
void Return();
}
class Media : IBorrowable
{
public string ID { get; protected set; }
public string Title { get; set; }
public bool IsBorrowed { get; set; } = false;
public Borrower Borrower { get; set; }
public Media(string title)
{
Title = title;
ID = GenerateID("BO");
}
private string GenerateID(string prefix)
{
return prefix + DateTime.Now.ToString("yyyyMMddHHmmss"); ;
}
public virtual void Borrow(Borrower borrower)
{
IsBorrowed = true;
Borrower = borrower;
}
public virtual void Return()
{
IsBorrowed = false;
Borrower = null;
}
}
class Book : Media
{
public string Author { get; set; }
public string Genre { get; set; }
public Book(string title, string author, string genre) : base(title)
{
Author = author;
Genre = genre;
}
public override void Borrow(Borrower borrower)
{
base.Borrow(borrower);
Console.WriteLine($"Book '{Title}' borrowed by {borrower.Name}.");
}
public override void Return()
{
Console.WriteLine($"Book '{Title}' returned.");
base.Return();
}
}
class DVD : Media
{
public string Director { get; set; }
public int Runtime { get; set; }
public DVD(string title, string director, int runtime) : base(title)
{
Director = director;
Runtime = runtime;
}
public override void Borrow(Borrower borrower)
{
base.Borrow(borrower);
Console.WriteLine($"DVD '{Title}' borrowed by {borrower.Name}.");
}
public override void Return()
{
Console.WriteLine($"DVD '{Title}' returned.");
base.Return();
}
}
class Borrower
{
public string ID { get; private set; }
public string Name { get; set; }
public string Address { get; set; }
public string ContactNumber { get; set; }
public string Email { get; set; }
public Borrower(string name, string address, string contactNumber, string email)
{
ID = GenerateID("BR");
Name = name;
Address = address;
ContactNumber = contactNumber;
Email = email;
}
public static string GenerateID(string prefix)
{
return prefix + DateTime.Now.ToString("yyyyMMddHHmmss");
}
}
class GenericLibrary<T> : IEnumerable<T> where T : Media
{
private List<T> mediaItems = new List<T>();
public void AddMedia(T media)
{
mediaItems.Add(media);
}
public void RemoveMedia(string mediaId)
{
T mediaToRemove = mediaItems.Find(media => media.ID == mediaId);
if (mediaToRemove != null)
{
mediaItems.Remove(mediaToRemove);
Console.WriteLine("Media item removed successfully.");
}
else
{
Console.WriteLine("Media item not found.");
}
}
public T GetMediaById(string mediaId)
{
return mediaItems.Find(media => media.ID == mediaId);
}
// Implementation of IEnumerable interface
public IEnumerator<T> GetEnumerator()
{
return mediaItems.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
class Library
{
public GenericLibrary<Book> BookLibrary { get; } = new GenericLibrary<Book>();
public GenericLibrary<DVD> DVDLibrary { get; } = new GenericLibrary<DVD>();
private List<Borrower> borrowers = new List<Borrower>();
public void AddBorrower(Borrower borrower)
{
borrowers.Add(borrower);
}
public void RemoveBorrower(Borrower borrower)
{
borrowers.Remove(borrower);
}
public void BorrowMedia(Media media, Borrower borrower)
{
media.Borrow(borrower);
}
public void ReturnMedia(Media media)
{
media.Return();
}
public void DisplayMediaInventory()
{
Console.WriteLine("Books:");
foreach (var book in BookLibrary)
{
string availability = book.IsBorrowed ? "Not Available" : "Available";
Console.WriteLine($"ID: {book.ID}, Title: {book.Title}, Author: {book.Author}, Genre: {book.Genre}, Availability: {availability}");
}
Console.WriteLine("\nDVDs:");
foreach (var dvd in DVDLibrary)
{
string availability = dvd.IsBorrowed ? "Not Available" : "Available";
Console.WriteLine($"ID: {dvd.ID}, Title: {dvd.Title}, Director: {dvd.Director}, Runtime: {dvd.Runtime} minutes, Availability: {availability}");
}
}
// Method to access borrowers list
public List<Borrower> GetBorrowers()
{
return borrowers;
}
public List<Media> SearchMedia(string keyword)
{
var results = BookLibrary.Concat<Media>(DVDLibrary)
.Where(media => media.Title.Contains(keyword) ||
(media is Book && ((Book)media).Author.Contains(keyword)) ||
(media is Book && ((Book)media).Genre.Contains(keyword)) ||
(media is DVD && ((DVD)media).Director.Contains(keyword)))
.ToList();
return results;
}
public List<Media> GetBorrowedMedia()
{
var borrowedMedia = BookLibrary.Concat<Media>(DVDLibrary)
.Where(media => media.IsBorrowed)
.ToList();
return borrowedMedia;
}
public List<Media> GetAvailableMedia()
{
var availableMedia = BookLibrary.Concat<Media>(DVDLibrary)
.Where(media => !media.IsBorrowed)
.ToList();
return availableMedia;
}
public List<Media> GetMediaBorrowedByBorrower(string borrowerID)
{
var borrowedByBorrower = BookLibrary.Concat<Media>(DVDLibrary)
.Where(media => media.IsBorrowed && media.Borrower.ID == borrowerID)
.ToList();
return borrowedByBorrower;
}
public List<Media> GetMediaWithTitleContainingKeyword(string keyword)
{
var mediaWithTitleContainingKeyword = BookLibrary.Concat<Media>(DVDLibrary)
.Where(media => media.Title.Contains(keyword))
.ToList();
return mediaWithTitleContainingKeyword;
}
public List<Media> GetMediaBorrowedInLast7Days()
{
DateTime startDate = DateTime.Now.AddDays(-7);
var borrowedInLast7Days = BookLibrary.Concat<Media>(DVDLibrary)
.Where(media => media.IsBorrowed && media.Borrower != null && media.Borrower.ID.Contains("BR") &&
DateTime.ParseExact(media.Borrower.ID.Substring(2), "yyyyMMddHHmmss", null) >= startDate)
.ToList();
return borrowedInLast7Days;
}
}
class LibraryApp
{
static void Main(string[] args)
{
Library library = new Library();
while (true)
{
Console.WriteLine("1. Add Book");
Console.WriteLine("2. Add DVD");
Console.WriteLine("3. Add Borrower");
Console.WriteLine("4. Remove Book");
Console.WriteLine("5. Remove DVD");
Console.WriteLine("6. Remove Borrower");
Console.WriteLine("7. Borrow Book/DVD");
Console.WriteLine("8. Return Book/DVD");
Console.WriteLine("9. Search Media");
Console.WriteLine("10. Display Inventory");
Console.WriteLine("11. Display Borrowers");
Console.WriteLine("12. Display Borrowed Media");
Console.WriteLine("13. Display Available Media");
Console.WriteLine("14. Display Media Borrowed by a Borrower");
Console.WriteLine("15. Display Media with Title Containing Keyword");
Console.WriteLine("16. Display Media Borrowed in Last 7 Days");
Console.WriteLine("17. Exit");
Console.Write("Enter your choice: ");
string choice = Console.ReadLine();
switch (choice)
{
case "1":
AddBook(library);
break;
case "2":
AddDVD(library);
break;
case "3":
AddBorrower(library);
break;
case "4":
RemoveBook(library);
break;
case "5":
RemoveDVD(library);
break;
case "6":
RemoveBorrower(library);
break;
case "7":
BorrowMedia(library);
break;
case "8":
ReturnMedia(library);
break;
case "9":
SearchMedia(library);
break;
case "10":
DisplayInventory(library);
break;
case "11":
DisplayBorrowers(library);
break;
case "12":
DisplayBorrowedMedia(library);
break;
case "13":
DisplayAvailableMedia(library);
break;
case "14":
DisplayMediaBorrowedByBorrower(library);
break;
case "15":
DisplayMediaWithTitleContainingKeyword(library);
break;
case "16":
DisplayMediaBorrowedInLast7Days(library);
break;
case "17":
return;
default:
Console.WriteLine("Invalid choice. Please try again.");
break;
}
}
}
static void AddBook(Library library)
{
Console.Write("Enter book title: ");
string title = Console.ReadLine();
Console.Write("Enter author: ");
string author = Console.ReadLine();
Console.Write("Enter genre: ");
string genre = Console.ReadLine();
Book book = new Book(title, author, genre);
library.BookLibrary.AddMedia(book);
Console.WriteLine("Book added successfully.");
}
static void AddDVD(Library library)
{
Console.Write("Enter DVD title: ");
string title = Console.ReadLine();
Console.Write("Enter director: ");
string director = Console.ReadLine();
Console.Write("Enter runtime (in minutes): ");
int runtime;
while (!int.TryParse(Console.ReadLine(), out runtime) || runtime <= 0)
{
Console.Write("Invalid input. Enter a valid runtime (in minutes): ");
}
DVD dvd = new DVD(title, director, runtime);
library.DVDLibrary.AddMedia(dvd);
Console.WriteLine("DVD added successfully.");
}
static void AddBorrower(Library library)
{
Console.Write("Enter borrower name: ");
string name = Console.ReadLine();
Console.Write("Enter address: ");
string address = Console.ReadLine();
Console.Write("Enter contact number: ");
string contactNumber = Console.ReadLine();
Console.Write("Enter email: ");
string email = Console.ReadLine();
Borrower borrower = new Borrower(name, address, contactNumber, email);
library.AddBorrower(borrower);
Console.WriteLine($"Borrower added successfully. ID: {borrower.ID}");
}
static void RemoveBook(Library library)
{
Console.Write("Enter ID of the book to remove: ");
string id = Console.ReadLine();
library.BookLibrary.RemoveMedia(id);
}
static void RemoveDVD(Library library)
{
Console.Write("Enter ID of the DVD to remove: ");
string id = Console.ReadLine();
library.DVDLibrary.RemoveMedia(id);
}
static void RemoveBorrower(Library library)
{
Console.Write("Enter ID of the borrower to remove: ");
string id = Console.ReadLine();
Borrower borrowerToRemove = library.GetBorrowers().Find(b => b.ID == id);
if (borrowerToRemove != null)
{
library.RemoveBorrower(borrowerToRemove);
Console.WriteLine("Borrower removed successfully.");
}
else
{
Console.WriteLine("Borrower not found.");
}
}
static void BorrowMedia(Library library)
{
Console.Write("Enter ID of the media item to borrow: ");
string id = Console.ReadLine();
Media media = library.BookLibrary.GetMediaById(id);
if (media == null)
{
media = library.DVDLibrary.GetMediaById(id);
}
if (media != null)
{
Console.Write("Enter ID of the borrower: ");
string borrowerID = Console.ReadLine();
Borrower borrower = library.GetBorrowers().Find(b => b.ID == borrowerID);
if (borrower != null)
{
library.BorrowMedia(media, borrower);
}
else
{
Console.WriteLine("Borrower not found.");
}
}
else
{
Console.WriteLine("Media item not found.");
}
}
static void ReturnMedia(Library library)
{
Console.Write("Enter ID of the media item to return: ");
string id = Console.ReadLine();
Media media = library.BookLibrary.GetMediaById(id);
if (media == null)
{
media = library.DVDLibrary.GetMediaById(id);
}
if (media != null)
{
library.ReturnMedia(media);
}
else
{
Console.WriteLine("Media item not found.");
}
}
static void SearchMedia(Library library)
{
Console.Write("Enter search keyword: ");
string keyword = Console.ReadLine();
List<Media> results = library.SearchMedia(keyword);
if (results.Count == 0)
{
Console.WriteLine("No media found matching the search criteria.");
}
else
{
Console.WriteLine("Search results:");
foreach (var media in results)
{
if (media is Book)
{
Book book = (Book)media;
Console.WriteLine($"Book: {book.Title} by {book.Author}");
}
else if (media is DVD)
{
DVD dvd = (DVD)media;
Console.WriteLine($"DVD: {dvd.Title} directed by {dvd.Director}");
}
}
}
}
static void DisplayInventory(Library library)
{
library.DisplayMediaInventory();
}
static void DisplayBorrowers(Library library)
{
List<Borrower> borrowers = library.GetBorrowers();
Console.WriteLine("Borrowers:");
foreach (var borrower in borrowers)
{
Console.WriteLine($"ID: {borrower.ID}, Name: {borrower.Name}, Address: {borrower.Address}, Contact: {borrower.ContactNumber}, Email: {borrower.Email}");
}
}
static void DisplayBorrowedMedia(Library library)
{
List<Media> borrowedMedia = library.GetBorrowedMedia();
Console.WriteLine("Borrowed Media:");
foreach (var media in borrowedMedia)
{
if (media is Book)
{
Book book = (Book)media;
Console.WriteLine($"Book: {book.Title}");
}
else if (media is DVD)
{
DVD dvd = (DVD)media;
Console.WriteLine($"DVD: {dvd.Title}");
}
}
}
static void DisplayAvailableMedia(Library library)
{
List<Media> availableMedia = library.GetAvailableMedia();
Console.WriteLine("Available Media:");
foreach (var media in availableMedia)
{
if (media is Book)
{
Book book = (Book)media;
Console.WriteLine($"Book: {book.Title}");
}
else if (media is DVD)
{
DVD dvd = (DVD)media;
Console.WriteLine($"DVD: {dvd.Title}");
}
}
}
static void DisplayMediaBorrowedByBorrower(Library library)
{
Console.Write("Enter ID of the borrower: ");
string borrowerID = Console.ReadLine();
List<Media> borrowedByBorrower = library.GetMediaBorrowedByBorrower(borrowerID);
Console.WriteLine($"Media Borrowed by Borrower (ID: {borrowerID}):");
foreach (var media in borrowedByBorrower)
{
if (media is Book)
{
Book book = (Book)media;
Console.WriteLine($"Book: {book.Title}");
}
else if (media is DVD)
{
DVD dvd = (DVD)media;
Console.WriteLine($"DVD: {dvd.Title}");
}
}
}
static void DisplayMediaWithTitleContainingKeyword(Library library)
{
Console.Write("Enter keyword to search titles: ");
string keyword = Console.ReadLine();
List<Media> mediaWithTitleContainingKeyword = library.GetMediaWithTitleContainingKeyword(keyword);
Console.WriteLine($"Media with Titles Containing Keyword \"{keyword}\":");
foreach (var media in mediaWithTitleContainingKeyword)
{
if (media is Book)
{
Book book = (Book)media;
Console.WriteLine($"Book: {book.Title}");
}
else if (media is DVD)
{
DVD dvd = (DVD)media;
Console.WriteLine($"DVD: {dvd.Title}");
}
}
}
static void DisplayMediaBorrowedInLast7Days(Library library)
{
List<Media> borrowedInLast7Days = library.GetMediaBorrowedInLast7Days();
Console.WriteLine("Media Borrowed in Last 7 Days:");
foreach (var media in borrowedInLast7Days)
{
if (media is Book)
{
Book book = (Book)media;
Console.WriteLine($"Book: {book.Title}");
}
else if (media is DVD)
{
DVD dvd = (DVD)media;
Console.WriteLine($"DVD: {dvd.Title}");
}
}
}
}