Splitting program into two classes

Item Object – Stores an instance of an item (Number, Description)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BasicDataStorageApp
{
    public class Item
    {
        private int _number;

        public int Number
        {
            get { return _number; }
            set { _number = value; }
        }

        private string _description;

        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        public Item(int number, string description)
            : this()
        {
            _number = number;
            _description = description;
        }

        public Item()
        {
        }

        public override string ToString()
        {
            return string.Format("Item number {0} Description {1}", _number, _description);
        }
    }
}

Model Object – Stores a collection of Items and includes methods to read and write to a file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BasicDataStorageApp
{
    public class Model
    {
        private Item[] _items;

        public Item[] Items
        {
            get { return _items; }
            set { _items = value; }
        }

        public bool WriteItems(string filename, bool append)
        {
            if (_items != null)
            {
                for (int i = 0; i < _items.Length; i++)
                {
                    string str = _items[i].ToString();
                    FileHelper.WriteLine(str, filename, append);
                }

                return true;
            }

            return false;
        }

        public IEnumerable<string> ReadItems(string filename)
        {
            return FileHelper.ReadLines(filename);
        }
    }
}

FileHelper – Provides read and write IO static methods.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BasicDataStorageApp
{
    public static class FileHelper
    {
        public static bool WriteLines(IEnumerable<string> lines, string filename, bool append)
        {
            try
            {
                using (StreamWriter writer = new StreamWriter(filename, append))
                {
                    foreach (var line in lines)
                    {
                        writer.WriteLine(line);
                    }
                }

                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static bool WriteLine(string line, string filename, bool append)
        {
            try
            {
                using (StreamWriter writer = new StreamWriter(filename, append))
                {
                    writer.WriteLine(line);
                }

                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static IEnumerable<string> ReadLines(string filename)
        {
            try
            {
                var lines = new List<string>();

                using (StreamReader reader = new StreamReader(filename))
                {
                    string line = null;
                    while ((line = reader.ReadLine()) != null)
                    {
                        lines.Add(line);
                    }
                }

                return lines;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

Program – Includes the described logic to get user input, write it to a file and read it back to the user

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BasicDataStorageApp
{
    class Program
    {
        static Model _model;
        const int _totalInput = 10;
        const string _filename = @"C:\temp\DataEntry.txt";

        static void Main(string[] args)
        {
            _model = new Model();
            _model.Items = new Item[_totalInput];

            Console.WriteLine("This program is designed to take input and hold data for 10 items");

            int i = 0;
            while (i < _totalInput)
            {
                int number = -1;

                Console.WriteLine("\nEnter number: ");
                string numberValue = Console.ReadLine();

                if (Int32.TryParse(numberValue, out number))
                {
                    _model.Items[i] = new Item(number, null);

                    Console.WriteLine("\nEnter description: ");
                    string descriptionValue = Console.ReadLine();

                    _model.Items[i].Description = descriptionValue;

                    i++;
                }
            }

            _model.WriteItems(_filename, true);

            var itemStrings = _model.ReadItems(_filename);
            foreach (var s in itemStrings)
            {
                Console.WriteLine(s);
            }

            Console.ReadLine();
        }
    }
}

Browse More Popular Posts

Leave a Comment