public ICollection GetAllBooks() { DataTable dataTable = new DataTable(); DataRow dataRow; DataColumn dataColumnIndex = new DataColumn(); dataColumnIndex.DataType = typeof(String); dataColumnIndex.AutoIncrement = true; dataColumnIndex.AutoIncrementSeed = 1; dataColumnIndex.AutoIncrementStep = 1; dataColumnIndex.ColumnName = "Num"; dataTable.Columns.Add(dataColumnIndex); dataTable.Columns.Add(new DataColumn("ID", typeof(String))); dataTable.Columns.Add(new DataColumn("Name", typeof(String))); dataTable.Columns.Add(new DataColumn("Author", typeof(String))); dataTable.Columns.Add(new DataColumn("Year", typeof(int))); dataTable.Columns.Add(new DataColumn("Price ($)", typeof(String))); dataTable.Columns.Add(new DataColumn("Stock", typeof(int))); String[] readerBooks = ReadLines().ToArray(); Debug.WriteLine(readerBooks); String[] delimiters = { ",", "\r\n" }; for (int i = 0; i < readerBooks.GetLength(0); i++) { String[] arrayBooks = readerBooks[i].Split(delimiters, StringSplitOptions.RemoveEmptyEntries) .Select(Book => Book.Trim()) .ToArray(); Debug.WriteLine(arrayBooks); dataRow = dataTable.NewRow(); for (int j = 0; j < arrayBooks.GetLength(0); j++) { dataRow[j + 1] = arrayBooks[j]; // j+1 because index is pre-defined. } dataTable.Rows.Add(dataRow); } DataView dataView = new DataView(dataTable); return(dataView); }
public Boolean addBook(String[] newBook) { Book thisBook = createBook(newBook, 0); bool isUpdatedBookStock = true; try { String[] readerBooks = ReadLines().ToArray(); String[] delimiters = { ",", "\r\n" }; for (int i = 0; i < readerBooks.GetLength(0); i++) { String[] arrayBooks = readerBooks[i].Split(delimiters, StringSplitOptions.RemoveEmptyEntries) .Select(Book => Book.Trim()) .ToArray(); if (arrayBooks[0] == thisBook.ID) { int newStock = thisBook.stock; int oldStock = int.Parse(arrayBooks[5]); int updatedStock = oldStock + newStock; updateRecord(thisBook, updatedStock); isUpdatedBookStock = true; return(true); } } } catch (Exception Ex) { throw new FaultException <Exception>(new Exception(Ex.Message)); } if (isUpdatedBookStock) { AddNewRecord(thisBook); return(true); } return(false); }
public ICollection searchBook(string type, string input) { DataTable dataTable = new DataTable(); DataRow dataRow; DataColumn dataColumnIndex = new DataColumn(); dataColumnIndex.DataType = typeof(String); dataColumnIndex.AutoIncrement = true; dataColumnIndex.AutoIncrementSeed = 1; dataColumnIndex.AutoIncrementStep = 1; dataColumnIndex.ColumnName = "Num"; dataTable.Columns.Add(dataColumnIndex); dataTable.Columns.Add(new DataColumn("ID", typeof(String))); dataTable.Columns.Add(new DataColumn("Name", typeof(String))); dataTable.Columns.Add(new DataColumn("Author", typeof(String))); dataTable.Columns.Add(new DataColumn("Year", typeof(int))); dataTable.Columns.Add(new DataColumn("Price ($)", typeof(String))); dataTable.Columns.Add(new DataColumn("Stock", typeof(int))); String[] readerBooks = ReadLines().ToArray(); String[] delimiters = { ",", "\r\n" }; for (int i = 0; i < readerBooks.GetLength(0); i++) { String[] arrayBooks = readerBooks[i].Split(delimiters, StringSplitOptions.RemoveEmptyEntries) .Select(Book => Book.Trim()) .ToArray(); if (String.IsNullOrWhiteSpace(input)) { throw new NullReferenceException("Input is empty."); } else if (type.Equals("Year") && !IsYear(input)) { throw new ArgumentException("Input is not a valid year."); } else if (type.Equals("Year") && !IsPositive(int.Parse(input))) { throw new FormatException("Year must be a valid positive integer. Input: " + input); } else { switch (type) { case "ID": try { if (arrayBooks[0].Equals(input)) { dataRow = dataTable.NewRow(); for (int j = 0; j < arrayBooks.GetLength(0); j++) { dataRow[j + 1] = arrayBooks[j]; // j+1 because index is pre-defined. } dataTable.Rows.Add(dataRow); } } catch (Exception Ex) { throw new FaultException <Exception>(new Exception(Ex.Message)); } break; case "Name": try { if (arrayBooks[1].ToLower().Contains(input.ToLower())) { dataRow = dataTable.NewRow(); for (int j = 0; j < arrayBooks.GetLength(0); j++) { dataRow[j + 1] = arrayBooks[j]; // j+1 because index is pre-defined. } dataTable.Rows.Add(dataRow); } } catch (Exception Ex) { throw new FaultException <Exception>(new Exception(Ex.Message)); } break; case "Author": try { if (arrayBooks[2].ToLower().Contains(input.ToLower())) { dataRow = dataTable.NewRow(); for (int j = 0; j < arrayBooks.GetLength(0); j++) { dataRow[j + 1] = arrayBooks[j]; // j+1 because index is pre-defined. } dataTable.Rows.Add(dataRow); } } catch (Exception Ex) { throw new FaultException <Exception>(new Exception(Ex.Message)); } break; case "Year": try { if (int.Parse(arrayBooks[3]).Equals(int.Parse(input))) { dataRow = dataTable.NewRow(); for (int j = 0; j < arrayBooks.GetLength(0); j++) { dataRow[j + 1] = arrayBooks[j]; // j+1 because index is pre-defined. } dataTable.Rows.Add(dataRow); } } catch (Exception Ex) { throw new FaultException <Exception>(new Exception(Ex.Message)); } break; default: break; } } } DataView dataView = new DataView(dataTable); return(dataView); }