Advertisement
Lauda

save-load-object-collection

Apr 29th, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using System.IO;
  7. using System.Runtime.Serialization.Formatters.Binary;
  8.  
  9. namespace HCI_Projekat.klase
  10. {
  11.     [Serializable]
  12.     class ZivotinjskeVrste
  13.     {
  14.         String oznaka;
  15.         String ime;
  16.         String opis;
  17.         String tip;
  18.         String status_ugrozenosti;
  19.         String ikona;
  20.         Boolean opasnaZaLjude;
  21.         Boolean IUCNLista;
  22.         Boolean naseljenoMjesto;
  23.         String turistickiStatus;
  24.         Int32 prihod;
  25.         DateTime datumOtkrivanja;
  26.  
  27.         public ZivotinjskeVrste() { }
  28.         public ZivotinjskeVrste(String oznaka, String ime, String opis, String tip, String status_ugrozenosti, String ikona, Boolean opasnaZaLjude, Boolean IUCNLista, Boolean naseljenoMjesto, String turistickiStatus, Int32 prihod, DateTime datumOtkrivanja)
  29.         {
  30.             this.oznaka = oznaka;
  31.             this.ime = ime;
  32.             this.opis = opis;
  33.             this.tip = tip;
  34.             this.status_ugrozenosti = status_ugrozenosti;
  35.             this.ikona = ikona;
  36.             this.opasnaZaLjude = opasnaZaLjude;
  37.             this.IUCNLista = IUCNLista;
  38.             this.naseljenoMjesto = naseljenoMjesto;
  39.             this.turistickiStatus = turistickiStatus;
  40.             this.prihod = prihod;
  41.             this.datumOtkrivanja = datumOtkrivanja;
  42.         }
  43.  
  44.         public String Oznaka
  45.         {
  46.             get { return oznaka; }
  47.             set { oznaka = value; }
  48.         }
  49.  
  50.         public String Ime
  51.         {
  52.             get { return ime; }
  53.             set { ime = value; }
  54.         }
  55.  
  56.         public String Opis
  57.         {
  58.             get { return opis; }
  59.             set { opis = value; }
  60.         }
  61.  
  62.         public String Tip
  63.         {
  64.             get { return tip; }
  65.             set { tip = value; }
  66.         }
  67.  
  68.         public String StatusUgrozenosti
  69.         {
  70.             get { return status_ugrozenosti; }
  71.             set { status_ugrozenosti = value; }
  72.         }
  73.  
  74.         public String Ikona
  75.         {
  76.             get { return ikona; }
  77.             set { ikona = value; }
  78.         }
  79.  
  80.         public Boolean OpasnaZaLjude
  81.         {
  82.             get { return opasnaZaLjude; }
  83.             set { opasnaZaLjude = value; }
  84.         }
  85.  
  86.         public Boolean IUCN
  87.         {
  88.             get { return IUCNLista; }
  89.             set { IUCNLista = value; }
  90.         }
  91.  
  92.         public Boolean NaseljenoMjesto
  93.         {
  94.             get { return naseljenoMjesto; }
  95.             set { naseljenoMjesto = value; }
  96.         }
  97.  
  98.         public String TuristickiStatus
  99.         {
  100.             get { return turistickiStatus; }
  101.             set { turistickiStatus = value; }
  102.         }
  103.  
  104.         public Int32 Prihod
  105.         {
  106.             get { return prihod; }
  107.             set { prihod = value; }
  108.         }
  109.  
  110.         public DateTime DatumOtkrivanja
  111.         {
  112.             get { return datumOtkrivanja; }
  113.             set { datumOtkrivanja = value; }
  114.         }
  115.     }
  116.  
  117.     class FileHandler
  118.     {
  119.         private Dictionary<String, ZivotinjskeVrste> _handler = new Dictionary<String, ZivotinjskeVrste>();
  120.         private readonly string _datoteka;
  121.  
  122.          public FileHandler() {
  123.             _datoteka = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "zivotinjskeVrste.dat");
  124.             LoadFile();
  125.         }
  126.  
  127.          public void Dodaj(ZivotinjskeVrste zv) {
  128.              if (String.IsNullOrEmpty(zv.Oznaka))
  129.                  // zv.Oznaka = "derp?";
  130.                  return;
  131.              if (!_handler.ContainsKey(zv.Oznaka))
  132.                  _handler.Add(zv.Oznaka, zv);
  133.              SaveFile();
  134.          }
  135.  
  136.          public void Delete(ZivotinjskeVrste zv) {
  137.              _handler.Remove(zv.Oznaka);
  138.              SaveFile();
  139.          }
  140.  
  141.          private void SaveFile() {
  142.              BinaryFormatter formatter = new BinaryFormatter();
  143.              FileStream stream = null;
  144.  
  145.              try {
  146.                  stream = File.Open(_datoteka, FileMode.OpenOrCreate);
  147.                  formatter.Serialize(stream, _handler);
  148.              }
  149.              catch
  150.              { }
  151.              finally {
  152.                  if (stream != null)
  153.                      stream.Dispose();
  154.              }
  155.          }
  156.  
  157.          private void LoadFile() {
  158.              BinaryFormatter formatter = new BinaryFormatter();
  159.              FileStream stream = null;
  160.  
  161.              if (File.Exists(_datoteka)) {
  162.                  try {
  163.                      stream = File.Open(_datoteka, FileMode.Open);
  164.                      _handler = (Dictionary<String, ZivotinjskeVrste>)formatter.Deserialize(stream);
  165.                  }
  166.                  catch
  167.                  { }
  168.                  finally {
  169.                      if (stream != null)
  170.                          stream.Dispose();
  171.                  }
  172.              }
  173.              else
  174.                  _handler = new Dictionary<String, ZivotinjskeVrste>();
  175.          }
  176.  
  177.          public Dictionary<String, ZivotinjskeVrste> getAll() {
  178.              return _handler;
  179.          }
  180.  
  181.          public ZivotinjskeVrste this[String oznaka] {
  182.              get { return _handler[oznaka]; }
  183.              set { _handler[oznaka] = value; }
  184.          }
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement