using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
/// <summary>
/// Summary description for Book
/// </summary>
[XmlRoot("BookStore")]
public class BookStore
{
List<Book> booksList;
public BookStore()
{
booksList = new List<Book>();
}
public void LoadFromXML(string path)
// מטודה מקבלת קובץ קסמל לטעון ממנו את הנתונים
// לדוגמא: LoadFromXML(Server.MapPath("list.xml"));
{
XmlSerializer s = new XmlSerializer(typeof(BookStore));
TextReader r = new StreamReader(path);
booksList = ((BookStore)s.Deserialize(r)).BooksList;
r.Close();
}
public void SaveToXML(string path)
// מטודה שומרת נתונים לקובץ קסמל
// לדוגמא: SaveToXML(Server.MapPath("list.xml"));
{
XmlSerializer s = new XmlSerializer(typeof(BookStore));
TextWriter w = new StreamWriter(path);
s.Serialize(w, this);
w.Close();
}
public void AddBook(Book book)
{
booksList.Add(book);
}
public void RemoveBook(int i)
{
booksList.RemoveAt(i);
}
public void RemoveBook(string name)
{
for (int i = 0; i < booksList.Count; i++)
if (booksList[i].Name == name)
{
booksList.RemoveAt(i);
break;
}
}
[XmlElement("Book")]
public List<Book> BooksList
{
get { return booksList; }
set { booksList = value; }
}
}
public class Book
{
[XmlAttribute("name")]
private string name;
[XmlAttribute("imageurl")]
private string imageurl;
[XmlAttribute("subject")]
private string subject;
[XmlAttribute("author")]
private string author;
[XmlAttribute("level")]
private int level;
[XmlAttribute("year")]
private int year;
[XmlAttribute("rating")]
private int rating;
[XmlAttribute("booksellers")]
private List<string> booksellers;
[XmlAttribute("bookprices")]
private List<double> bookprices;
public Book()
{
booksellers = new List<string>();
bookprices = new List<double>();
}
public Book(string name, string imageurl, string subject, string author, int level, int year, int rating)
{
this.name = name;
this.imageurl = imageurl;
this.subject = subject;
this.author = author;
this.level = level;
this.year = year;
this.rating = rating;
booksellers = new List<string>();
bookprices = new List<double>();
}
public void AddSeller(string seller, double price)
{
booksellers.Add(seller);
bookprices.Add(price);
}
public void RemoveSeller(string seller)
{
int i = booksellers.IndexOf(seller);
booksellers.RemoveAt(i);
bookprices.RemoveAt(i);
}
public string Name
{
get { return name; }
set { name = value; }
}
public string Imageurl
{
get { return imageurl; }
set { imageurl = value; }
}
public string Subject
{
get { return subject; }
set { subject = value; }
}
public string Author
{
get { return author; }
set { author = value; }
}
public int Level
{
get { return level; }
set { if (value > 0) level = value; }
}
public int Year
{
get { return year; }
set { if ((value > 1900) && (value <= DateTime.Now.Year)) year = value; }
}
public int Rating
{
get { return rating; }
set { if ((rating >= 1) && (rating <= 5)) rating = value; }
}
public List<string> BookSellers
{
get { return booksellers; }
set { booksellers = value; }
}
public List<double> BookPrices
{
get { return bookprices; }
set { bookprices = value; }
}
}