using System; using System.Linq; namespace GCHR.Model.Uebersetzung { class KontoTyp { enum Typ { S, Q, M, V, D }; enum Buchung { HH, HS, SH, SS }; public KontoTyp(string typ) { if (typ.Length <= 0) return; _kontoTyp = TryParseKontoTyp(typ.Substring(0, 1)); _negativ = typ.Contains('-'); _alternativ = typ.Contains('A'); if (_kontoTyp == Typ.V) { if (typ.Contains("HH")) _buchung = Buchung.HH; if (typ.Contains("HS")) _buchung = Buchung.HS; if (typ.Contains("SH")) _buchung = Buchung.SH; if (typ.Contains("SS")) _buchung = Buchung.SS; } } private static Typ TryParseKontoTyp(string typ) { try { return (Typ)Enum.Parse(typeof(Typ), typ); } catch (Exception) { return Typ.S; } } private readonly Typ _kontoTyp = Typ.S; private readonly bool _negativ; private readonly bool _alternativ; private readonly Buchung _buchung = Buchung.SH; public decimal Faktor { get { return (_negativ) ? -1m : 1m; } } public override bool Equals(object obj) { var typ = obj as KontoTyp; if (typ != null) { return EqualsTyp(typ._kontoTyp); } if (obj is Typ) { return EqualsTyp((Typ)obj); } var str = obj as string; if (str != null) { try { return EqualsTyp((Typ)Enum.Parse(typeof(Typ), str.Substring(0, 1))); } catch (Exception) { return false; } } return false; } private bool EqualsTyp(Typ typ) { if (typ == _kontoTyp) return true; if (typ == Typ.D || typ == Typ.D) return true; if ((typ == Typ.V && _kontoTyp == Typ.S) || (typ == Typ.S && _kontoTyp == Typ.V)) return true; return false; } public override string ToString() { return _kontoTyp + ((_alternativ) ? "A" : "") + ((_kontoTyp == Typ.V) ? _buchung.ToString() : "") + ((_negativ) ? "-" : ""); } public override int GetHashCode() { return ToString().GetHashCode(); } } }