/
Library.cs
118 lines (95 loc) · 2.45 KB
/
Library.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
enum SearchOption { NameOfBook, Author }
enum DisplayOption { NameOfBook, Author }
class Library
{
string name;
List<Book> bookList;
public Library(string name, List<Book> bookList = null)
{
this.name = name;
if (bookList == null)
{
this.bookList = new List<Book>();
}
else
{
this.bookList = bookList;
}
}
public string Name
{
get
{
return Name;
}
set
{
this.name = value;
}
}
public void Add(Book someBook)
{
this.bookList.Add(someBook);
}
public int IndexOf(string name, int startIndex, SearchOption opt)
{
int foundIndx = -1;
name = name.Trim();
for (int i = startIndex; i < this.bookList.Count; i++)
{
if (opt == SearchOption.Author)
{
if (this.bookList[i].Author == name)
{
foundIndx = i;
}
}
else
{
if (this.bookList[i].Title == name)
{
foundIndx = i;
}
}
}
return foundIndx;
}
public void Display(string name, DisplayOption opt)
{
Console.WriteLine("{0,-3}|{1,-15}|{2,-15}", "N", "Title", "Author");
for (int i = 0; i < this.bookList.Count; i++)
{
if (opt == DisplayOption.Author)
{
if (this.bookList[i].Author == name)
{
Console.WriteLine("{0,-3}|{1,-15}|{2,-15}", i + 1, bookList[i].Title, bookList[i].Author);
}
}
else
{
if (this.bookList[i].Title == name)
{
Console.WriteLine("{0,-3}|{1,-15}|{2,-15}", i + 1, bookList[i].Title, bookList[i].Author);
}
}
}
}
public void DisplayAll()
{
int count = 1;
Console.WriteLine("{0,-3}|{1,-15}|{2,-15}","N","Title","Author");
foreach (Book elem in this.bookList)
{
Console.WriteLine("{0,-3}|{1,-15}|{2,-15}",count, elem.Title, elem.Author);
count++;
}
}
public void DeleteAt(int index)
{
this.bookList.RemoveAt(index);
}
}