CatalogueData.cs

Go to the documentation of this file.
00001 using System;
00002 using System.Collections.Generic;
00003 using System.Collections;
00004 using System.Text;
00005 using System.Runtime.Serialization;
00006 using System.IO;
00007 using System.Runtime.Serialization.Formatters.Binary;
00008 using System.Reflection;
00009 
00010 namespace HouseOver.THARM
00011 {
00016     [Serializable]
00017     public class CatalogueData : ICollection<Customer>
00018     {
00019         private List<Customer> customers;
00020         private Dictionary<Customer, List<Profile>> customerProfiles;
00021         private Dictionary<Customer, List<Sale>> customerSales;
00022 
00026         public CatalogueData()
00027         {
00028             this.customers = new List<Customer>();
00029             this.customerProfiles = new Dictionary<Customer, List<Profile>>();
00030             this.customerSales = new Dictionary<Customer, List<Sale>>();
00031         }
00032 
00037         public CatalogueData(string fromFile)
00038         {
00039             this.customers = new List<Customer>();
00040             this.customerProfiles = new Dictionary<Customer, List<Profile>>();
00041             this.customerSales = new Dictionary<Customer, List<Sale>>();
00042 
00043             this.Deserialize(fromFile);
00044         }
00045 
00050         public void Serialize(string fileName)
00051         {
00052             IFormatter f = new BinaryFormatter();
00053 
00054             FileStream fs = new FileStream(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
00055                 + Path.DirectorySeparatorChar + Path.DirectorySeparatorChar + fileName, FileMode.Create);
00056 
00057             f.Serialize(fs, this);
00058             fs.Dispose();
00059         }
00060 
00065         private void Deserialize(string fileName)
00066         {
00067             IFormatter f = new BinaryFormatter();
00068 
00069             FileStream fs = new FileStream(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
00070                 + Path.DirectorySeparatorChar + Path.DirectorySeparatorChar + fileName, FileMode.Open);
00071 
00072             CatalogueData temp = (CatalogueData)f.Deserialize(fs);
00073 
00074             this.customers = temp.customers;
00075             this.customerSales = temp.customerSales;
00076             this.customerProfiles = temp.customerProfiles;
00077 
00078             fs.Close();
00079         }
00080         
00086         public Customer GetCustomer(int id)
00087         {
00088             foreach (Customer c in customers)
00089             {
00090                 if (c.ID == id) return c; 
00091             }
00092 
00093             return null;
00094         }
00095         
00099         public void Clear()
00100         {
00101             customerProfiles.Clear();
00102             customers.Clear();
00103             customerSales.Clear();
00104         }
00105 
00111         public bool Contains(Customer item)
00112         {
00113             return customers.Contains(item);
00114         }
00115 
00122         public void CopyTo(Customer[] array, int arrayIndex)
00123         {
00124             customers.CopyTo(array, arrayIndex);
00125         }
00126 
00130         public int  Count
00131         {
00132             get { return customers.Count; }
00133         }
00134 
00138         public bool IsReadOnly
00139         {
00140             get { return false; }
00141         }
00142 
00148         public bool Remove(Customer c)
00149         {
00150             return this.RemoveCustomer(c);
00151         }
00152 
00157         public void Add(Customer item)
00158         {
00159             customers.Add(item);
00160         }
00161 
00167         private bool RemoveCustomer(Customer c)
00168         {
00169             customerSales.Remove(c);
00170             customerProfiles.Remove(c);
00171             return customers.Remove(c);
00172         }
00173         
00179         public List<Profile> GetProfiles(Customer c)
00180         {
00181             if (!customerProfiles.ContainsKey(c))
00182                 return new List<Profile>();
00183             return customerProfiles[c];
00184         }
00185 
00191         public List<Sale> GetSales(Customer c)
00192         {
00193             if (!customerSales.ContainsKey(c))
00194                 return new List<Sale>();
00195             return customerSales[c];
00196         }
00197 
00202         public void Add(Profile p)
00203         {
00204             if (!customerProfiles.ContainsKey(p.Owner))
00205                 customerProfiles.Add(p.Owner, new List<Profile>());
00206 
00207             customerProfiles[p.Owner].Add(p);
00208         }
00209 
00214         public void Remove(Profile p)
00215         {
00216             if (customerProfiles.ContainsKey(p.Owner))
00217             {
00218                 customerProfiles[p.Owner].Remove(p);
00219                 if (customerProfiles[p.Owner].Count == 0)
00220                     customerProfiles.Remove(p.Owner);
00221             }
00222         }
00228         public void Add(Sale s)
00229         {
00230             if (!customerSales.ContainsKey(s.Seller))
00231                 customerSales.Add(s.Seller, new List<Sale>());
00232 
00233             customerSales[s.Seller].Add(s);
00234         }
00239         public void Remove(Sale s)
00240         {
00241             if (customerSales.ContainsKey(s.Seller))
00242             {
00243                 customerSales[s.Seller].Remove(s);
00244                 if (customerSales[s.Seller].Count == 0)
00245                     customerSales.Remove(s.Seller);
00246             }
00247         }
00248 
00252         public CustomerInformationWrapper<Sale> Sales
00253         {
00254             get { return new CustomerInformationWrapper<Sale>(customerSales.Values); }
00255         }
00256 
00260         public CustomerInformationWrapper<Profile> Profiles
00261         {
00262             get { return new CustomerInformationWrapper<Profile>(customerProfiles.Values); }
00263         }
00264 
00268         public List<Customer> CustomersWithProfiles
00269         {
00270             get { return new List<Customer>(customerProfiles.Keys); } 
00271         }
00272 
00276         public List<Customer> CustomersWithSales
00277         {
00278             get { return new List<Customer>(customerSales.Keys); }
00279         }
00280         
00285         public IEnumerator<Sale> GetSaleEnumerator()
00286         {
00287             return new CustomerStuffEnumerator<Sale>(customerSales.Values);
00288         }
00289 
00294         public IEnumerator<Customer> GetEnumerator()
00295         {
00296             return customers.GetEnumerator();
00297         }
00298 
00303         IEnumerator IEnumerable.GetEnumerator()
00304         {
00305             return customers.GetEnumerator();
00306         }
00307 
00312         private class CustomerStuffEnumerator<T> : IEnumerator<T>
00313         {
00314 
00315             IEnumerator<T> currentList;
00316             IEnumerator<List<T>> currentPos;
00317 
00322             public CustomerStuffEnumerator(Dictionary<Customer, List<T>>.ValueCollection source)
00323             {
00324                 currentPos = source.GetEnumerator();
00325             }
00326 
00330             public T Current
00331             {
00332                 get {
00333                     if (currentList == null)
00334                         return default(T);
00335                     else
00336                         return currentList.Current;
00337                 }
00338             }
00339             
00343             public void Dispose()
00344             {
00345                     
00346             }
00347 
00351             object IEnumerator.Current
00352             {
00353                 get {
00354                     if (currentList == null)
00355                         return null;
00356                     else
00357                         return currentList.Current;
00358                 }
00359             }
00360 
00365             public bool MoveNext()
00366             {
00367                 while(currentList == null || !currentList.MoveNext()) 
00368                 {
00369                     if (currentPos.MoveNext())
00370                         currentList = currentPos.Current.GetEnumerator();
00371                     else
00372                         return false;
00373                 }
00374 
00375                 return true;
00376             }
00377 
00381             public void Reset()
00382             {
00383                 currentPos.Reset();
00384                 currentList = null;
00385             }
00386                         
00387         }
00388 
00393         public class CustomerInformationWrapper<T> : IEnumerable<T>
00394         {
00395             private Dictionary<Customer, List<T>>.ValueCollection collection;
00396 
00401             public CustomerInformationWrapper(Dictionary<Customer, List<T>>.ValueCollection customerSales)
00402             {
00403                 collection = customerSales;
00404             }
00405             
00410             public IEnumerator<T> GetEnumerator()
00411             {
00412                 return new CustomerStuffEnumerator<T>(collection);
00413             }
00414                 
00419             IEnumerator IEnumerable.GetEnumerator()
00420             {
00421                 return new CustomerStuffEnumerator<T>(collection);
00422             }
00423                         
00424         }
00425 
00426                
00427     }
00428 }

Generated on Wed Jan 10 03:42:41 2007 for THARM by  doxygen 1.5.1-p1