-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBookImporter.cs
65 lines (56 loc) · 2.23 KB
/
BookImporter.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BookTips
{
internal static class BookImporter
{
public static List<Book> GetBooks()
{
List<Book> bookList = new List<Book>();
try
{
var filePath = Path.Combine(@"C:\Users\moham", @"Downloads\tips_texter.txt");
if (File.Exists(filePath))
{
using (StreamReader reader = new StreamReader(filePath, Encoding.Default, true))
{
while (reader.ReadLine() is string item)
{
string[] bookParts = item.Split("###", StringSplitOptions.RemoveEmptyEntries);
if (bookParts.Length != 4) continue;
string title = bookParts[0];
string author = bookParts[1];
string type = bookParts[2];
bool.TryParse(bookParts[3], out bool availability);
Book tempBook = ConvertToBook(type: type, Author: author, Title: title, available: availability);
bookList.Add(tempBook);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return bookList;
}
// This method is converting from a string line to a book object and specifies the type of book
public static Book ConvertToBook(string type, string Title, string Author, bool available)
{
switch (type)
{
case "Roman": // Roman
return new Novel(Title, Author, available);
case "Tidskrift": // Tidskrift
return new Magazine(Title, Author, available);
case "Novellsamling":
return new ShortStoryCollection(Title, Author, available);
default:
throw new ArgumentOutOfRangeException("Invalid book type!");
}
}
}
}