-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBookAction.cs
229 lines (194 loc) · 6.9 KB
/
BookAction.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
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
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace Library
{
class BookAction
{
public static void Add(List<Book> books)
{
var newBook = NewBook(books);
var newEntry = string.Join(",",
newBook.BookId,
newBook.Title,
newBook.Author,
newBook.AvarageRating,
newBook.Isbn,
newBook.Isbn13,
newBook.Language,
newBook.NumberOfPages,
newBook.RatingsCount,
newBook.TextReviewsCount,
newBook.PublicationDate.ToShortDateString(),
newBook.Publisher);
Console.WriteLine(newEntry);
File.AppendAllText("books.csv", newEntry + Environment.NewLine);
}
private static Book NewBook(List<Book> books)
{
var id = NewId(books);
var title = NewText("Enter the book title");
var author = NewText("Enter the author name");
var avarageRating = NewRating();
var isbn = NewText("Enter the ISBN");
var isbn13 = NewIsbn13();
var language = NewText("Enter the book language");
var numberOfPages = NewNumber("Enter the number of pages");
var ratingsCount = NewNumber("Enter the ratings count");
var textReviewsCount = NewNumber("Enter the text review count");
var publicationDate = NewDate();
var publisher = NewText("Enter the publisher name");
var book = NewBookObject(id,
title,
author,
avarageRating,
isbn,
isbn13,
language,
numberOfPages,
ratingsCount,
textReviewsCount,
publicationDate,
publisher);
return book;
}
private static Book NewBookObject(int id,
string title,
string author,
decimal avarageRating,
string isbn,
long isbn13,
string language,
int numberOfPages,
int ratingsCount,
int textReviewsCount,
DateTime publicationDate,
string publisher)
{
var book = new Book
{
BookId = id,
Title = title,
Author = author,
AvarageRating = avarageRating,
Isbn = isbn,
Isbn13 = isbn13,
Language = language,
NumberOfPages = numberOfPages,
RatingsCount = ratingsCount,
TextReviewsCount = textReviewsCount,
PublicationDate = publicationDate,
Publisher = publisher
};
return book;
}
private static int NewId(List<Book> books)
{
var lastId = BookQuery.GetLastBookId(books);
return ++lastId;
}
private static int NewNumber(string message)
{
int validNumber;
var valid = false;
do
{
Console.WriteLine($"\n\t{message}");
var number = Console.ReadLine();
if (int.TryParse(number, out validNumber))
{
valid = true;
}
else
{
Console.WriteLine($"\n\tInvalide entry: {number}");
}
} while (!valid);
return validNumber;
}
private static long NewIsbn13()
{
long validIsbn13;
var valid = false;
do
{
Console.WriteLine("\n\tEnter the ISBN13");
var isbn13 = Console.ReadLine();
if (long.TryParse(isbn13, out validIsbn13))
{
if (isbn13.Length == 13)
{
valid = true;
}
else
{
Console.WriteLine("The ISBN13 must be 13 characters long");
}
}
else
{
Console.WriteLine($"Invalide entry: {isbn13}");
}
} while (!valid);
return validIsbn13;
}
private static decimal NewRating()
{
decimal validRating;
var valid = false;
do
{
Console.WriteLine("\n\tEnter the avarage rating of the book (1.00 - 5.00)");
var rating = Console.ReadLine();
if (decimal.TryParse(rating, out validRating))
{
valid = true;
}
else
{
Console.WriteLine($"Invalid entry: {rating}");
}
} while (!valid);
return validRating;
}
private static DateTime NewDate()
{
DateTime validDate;
var valid = false;
do
{
Console.WriteLine("\n\tEnter the publication date (mm/dd/yyyy)");
var date = Console.ReadLine();
if (DateTime.TryParse(date, out validDate))
{
valid = true;
}
} while (!valid);
return validDate;
}
private static string NewText(string message)
{
Console.WriteLine($"\n\t{message}");
return Console.ReadLine();
}
public static void Upload()
{
Console.WriteLine("Upload file");
// validate if file is csv
// validate if csv schema is correct (correct # of tables)
// validate if each table contains the expected data type
}
public static void Remove()
{
Console.WriteLine("Remove a single book");
// get book id
// Remove line with book id
}
public static void RemoveMenu()
{
Console.WriteLine("Remove book by specifying the index number after a query");
}
}
}