Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Common;
- using System.ServiceModel;
- using System.IO;
- namespace Server
- {
- public class ApotekaService : IApoteka
- {
- private static StreamWriter logWriter;
- static ApotekaService()
- {
- logWriter = new StreamWriter("log.txt", true, System.Text.Encoding.UTF8);
- }
- public List<Lek> dobaviSveLekove()
- {
- List<Lek> ret = new List<Lek>();
- foreach(Lek lek in DataBase.DataBaseOfLek.Values)
- {
- ret.Add(lek);
- }
- return ret;
- }
- public bool DodajLek(Lek lek)
- {
- if (DataBase.DataBaseOfLek.ContainsKey(lek.Id))
- {
- Dogadjaj($"Neuspesno dodavanje, postoji lek sa ID={lek.Id}");
- return false;
- }
- else
- {
- DataBase.DataBaseOfLek.Add(lek.Id, lek);
- Dogadjaj($"Uspesno dodavanje leka ID={lek.Id}");
- return true;
- }
- }
- public void dodajSastojak(int id, string sastojak)
- {
- Lek lek;
- if (DataBase.DataBaseOfLek.TryGetValue(id, out lek))
- {
- throw new FaultException<CustomException>(new CustomException("Nemoguce dodati sastojak jer ne postoji taj lek"));
- }
- else
- {
- if (lek.Sastojci.Contains(sastojak))
- {
- throw new FaultException<CustomException>(new CustomException("Nemoguce dodati sastojak jer vec postoji"));
- }
- else
- {
- lek.Sastojci.Add(sastojak);
- Dogadjaj($"Uspesno dodat sastojak");
- }
- }
- }
- public void ObrisiLek(int id)
- {
- if (DataBase.DataBaseOfLek.ContainsKey((int)id))
- {
- DataBase.DataBaseOfLek.Remove(id);
- Dogadjaj($"Uspesno obrisan lek");
- }
- else
- {
- throw new FaultException<CustomException>(new CustomException($"Neuspesno brisanje leka"));
- }
- }
- public void PromeniKolicinuLek(int id, int kolicina, bool povecaj)
- {
- Lek lek;
- if(DataBase.DataBaseOfLek.TryGetValue(id, out lek))
- {
- if(lek.Kolicina - kolicina < 0 && !povecaj)
- {
- throw new FaultException<CustomException>(new CustomException($"Neuspesna promena kolicine"));
- }
- else
- {
- if (!povecaj)
- lek.Kolicina = lek.Kolicina - kolicina;
- else lek.Kolicina += kolicina;
- Dogadjaj($"Promena izvrsena");
- }
- }
- }
- public List<Lek> PronadjiLekNaOsnovuSastojka(string sastojak)
- {
- List<Lek> retV = new List<Lek> ();
- foreach(Lek lek in DataBase.DataBaseOfLek.Values)
- {
- if(lek.Sastojci.Contains(sastojak))
- {
- retV.Add(lek);
- }
- }
- if (retV.Count > 0)
- Dogadjaj($"Uspesno pronadjen lek");
- return retV;
- }
- private static void Dogadjaj(string tekst)
- {
- logWriter.WriteLine(tekst);
- logWriter.Flush();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement