Exemplo n.º 1
0
        public List <ExpenseItem> LoadFromFile(string fileName)
        {
            List <ExpenseItem> expenseItems = new List <ExpenseItem>();

            if (string.IsNullOrWhiteSpace(fileName) || !File.Exists(fileName))
            {
                throw new Exception("file doesn't exist.");
            }
            XmlTextReader reader = new XmlTextReader(fileName);
            ExpenseItem   item   = null;

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (string.Equals(reader.Name, "Item", StringComparison.CurrentCultureIgnoreCase))
                    {
                        item = new ExpenseItem();
                    }
                    else if (item != null)
                    {
                        if (string.Equals(reader.Name, "ID", StringComparison.CurrentCultureIgnoreCase))
                        {
                            item.ID = int.Parse(reader.ReadElementString().Trim());
                        }

                        if (string.Equals(reader.Name, "Date", StringComparison.CurrentCultureIgnoreCase))
                        {
                            item.DateString = reader.ReadElementString().Trim();
                        }

                        if (string.Equals(reader.Name, "Amount", StringComparison.CurrentCultureIgnoreCase))
                        {
                            item.Amount = double.Parse(reader.ReadElementString().Trim());
                        }

                        if (string.Equals(reader.Name, "Tag", StringComparison.CurrentCultureIgnoreCase))
                        {
                            item.Tag = reader.ReadElementString().Trim();
                        }
                    }
                }

                if (reader.NodeType == XmlNodeType.EndElement && item != null)
                {
                    expenseItems.Add(item);
                    item = null;
                }
            }



            return(expenseItems);
        }
Exemplo n.º 2
0
        public List <ExpenseItem> LoadFromFile(string fileName)
        {
            List <ExpenseItem> expenseItems = new List <ExpenseItem>();

            if (string.IsNullOrWhiteSpace(fileName) || !File.Exists(fileName))
            {
                throw new Exception("file doesn't exist.");
            }

            string [] lines = File.ReadAllLines(fileName);
            foreach (string itemLine in lines)
            {
                string[] words = itemLine.Trim().Split(new char[] { ',' });
                if (words.Length != 4)
                {
                    continue;
                }
                ExpenseItem item = new ExpenseItem();
                int         id;
                if (int.TryParse(words[0].Trim(), out id))
                {
                    item.ID = id;
                }
                else
                {
                    continue;
                }

                string tmpString = words[1].Trim();
                item.DateString = tmpString;
                double amount;
                if (double.TryParse(words[2].Trim(), out amount))
                {
                    item.Amount = amount;
                }
                else
                {
                    continue;
                }

                item.Tag = words[3].Trim();
                expenseItems.Add(item);
            }

            return(expenseItems);
        }
Exemplo n.º 3
0
        public bool AddOneItem(int expID, string dateString, double amount, string tag)
        {
            if (expID < 0)
            {
                expID = GetNextID();
            }
            ExpenseItem item = new ExpenseItem(expID, dateString, amount, tag);

            if (!item.Validate())
            {
                return(false);
            }

            if (ContainsID(expID))
            {
                return(false);
            }
            _expenseItems.Add(item);
            return(true);
        }