root/Sterling/Transaction.cs

Revision 3ba7222aca8a81d3c0d6ca16fce55a30ce7edd1f, 3.7 kB (checked in by Hans Petter Jansson <hpj@linux-rqhg.site>, 1 year ago)

Enumerate properties.

  • Property mode set to 100644
Line 
1 // -*- Mode: csharp; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2
3 using System;
4 using System.IO;
5 using System.Reflection;
6 using System.Runtime.InteropServices;
7 using System.Collections.Generic;
8
9 namespace Sterling
10 {
11     public class TagRef
12     {
13         private string  name;
14         private bool    negate;
15
16         public TagRef (string name, bool negate)
17         {
18             this.name   = name.ToLower ();
19             this.negate = negate;
20         }
21
22         public string Name
23         {
24             get
25             {
26                 return name;
27             }
28         }
29
30         public bool Negate
31         {
32             get
33             {
34                 return negate;
35             }
36         }
37     }
38
39     public class TagBag : System.Collections.IEnumerable
40     {
41         private Dictionary<string, TagRef> dict;
42
43         public TagBag ()
44         {
45             dict = new Dictionary<string, TagRef> ();
46         }
47
48         public TagBag (string words)
49         {
50             dict = new Dictionary<string, TagRef> ();
51             AddMany (words);
52         }
53        
54         public System.Collections.IEnumerator GetEnumerator ()
55         {
56             foreach (string tag_name in dict.Keys)
57             {
58                 yield return tag_name;
59             }
60         }
61
62         public void Add (TagRef tagref)
63         {
64             // Always replace.
65             if (Contains (tagref.Name))
66                 Remove (tagref.Name);
67
68             dict.Add (tagref.Name, tagref);
69         }
70
71         public void Add (string name, bool negate)
72         {
73             Add (new TagRef (name, negate));
74         }
75
76         public void Add (string name)
77         {
78             Add (new TagRef (name, false));
79         }
80
81         public void AddMany (string words)
82         {
83             char[]   split_chars = { ',', ' ' };
84             string[] tag_names   = words.Split (split_chars, System.StringSplitOptions.RemoveEmptyEntries);
85
86             foreach (string tag_name in tag_names)
87             {
88                 if (tag_name [0] == '-')
89                     Add (tag_name.Substring (1), true);
90                 else
91                     Add (tag_name);
92             }
93         }
94
95         // FIXME: Should be IConvertible, ToString () or something.
96         public string GenString ()
97         {
98             string words = "";
99
100             foreach (TagRef tagref in dict.Values)
101             {
102                 if (tagref.Negate)
103                     words = String.Concat (words, "-");
104
105                 words = String.Concat (words, tagref.Name);
106                 words = String.Concat (words, " ");
107             }
108
109             return words.TrimEnd (' ');
110         }
111
112         public bool Remove (string name)
113         {
114             return dict.Remove (name);
115         }
116
117         public bool Contains (string name)
118         {
119             return dict.ContainsKey (name);
120         }
121
122         public bool Contains (TagRef tagref)
123         {
124             return dict.ContainsKey (tagref.Name);
125         }
126
127         public TagRef Get (string name)
128         {
129             return dict [name];
130         }
131
132         public TagRef Get (TagRef tagref)
133         {
134             return dict [tagref.Name];
135         }
136     }
137
138     public class Transaction
139     {
140         public string    id = "";
141         public string    description = "";
142         public DateTime  transaction_time;
143         public DateTime  creation_time = DateTime.Now;
144         public double    amount = 0.0;
145         public string    currency = "";
146         public TagBag    tags;
147
148         public enum Property : int
149         {
150             Description,
151             Tags,
152             Amount,
153             TransactionTime,
154             CreationTime
155         };
156
157         public Transaction ()
158         {
159             tags = new TagBag ();
160         }
161     }
162 }
Note: See TracBrowser for help on using the browser.