The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
public class Service1 : IService1
{
public List books = new List();//All book information
public static string path = @"C:UsersHPDesktop7204assignmentassignment3books.txt";//path of book.txt
public List GetAllBooks()
//This operation reads all books information from file books.txt (Download from Blackboard) and returns a list of books.
{
books = new List();
FileStream file1 = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
StreamReader a = new StreamReader(file1);
String line = "";
int i = 0;
a = new StreamReader(file1);
i = 0;
Book last = new Book();
String[] str = new String[6];
try
{
while ((line = a.ReadLine()) != null)//read txt
{
str = new String[6];
str = line.Split(',');
last = new Book();
last.id = str[0];
last.Name = str[1];
last.Author = str[2];
last.Year = int.Parse(str[3]);
last.Price = float.Parse(str[4].Substring(1));
last.Stock = int.Parse(str[5]);
books.Add(last);
i++;
}
}
catch (Exception)
{
}
a.Close();
file1.Close();
return books;
}
public Boolean AddBook(Book a)
///This operation receives the information of a book and creates an instance of class
///Book, then add this book into existing book list and save into file books.txt. If the
///operation succeeds to add a book into the book list and save to file, it returns true.
///Otherwise it returns false.
{
int n;
n = books.FindIndex(s => s.id == a.id);
if ((n == -1) & (a.Author != "") & (a.id != "") & (a.Name != "") & (a.Year > 1970) & (a.Year 0) & (a.Stock > 0))
{
books.Add(a);
FileStream file2 = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
StreamWriter b = new StreamWriter(file2);
for (int i = 0; i {
string str = books[i].id + "," + books[i].Name + "," + books[i].Author + "," + books[i].Year.ToString() + ",$" + books[i].Price.ToString() + "," + books[i].Stock.ToString();
b.WriteLine(str);
}
b.Close();
file2.Close();
return true;
}
else
{
return false;
}
}
i dont understand what the 'S' is, in "s.id=a.id”, it desnot claim a "s" before , right? so how itcan judge s.id=a.id ?. and what the "n==-1" means? could someone explain this code for me?
-----------