Thursday, October 2, 2014

BinaryReadWrite_LabAssignment


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



namespace BinaryRWPractice
{
    public class Expense
    {
        private decimal _amount;
        private DateTime _date;
        private ExpenseCategory _category;
        private string _description;

        //Constructor
        public Expense (decimal amount, DateTime date, ExpenseCategory category, string description)
        {
            this._amount = amount;
            this._date = date;
            this._category = category;
            this._description = description;
        }

        //Properties
        public decimal Amount
        { get { return this._amount;} }
        public DateTime Date
        { get { return this._date; } }
        public ExpenseCategory Category
        { get { return this._category; } }
        public string Description
        { get { return this._description; } }
    }
}
//==============================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace BinaryRWPractice
{
    public enum ExpenseCategory
    {
        Groceries, Clothing, Entertainment, House_Bill, Phone, Car
    };

    public partial class Zau_Expense : Form
    {
        string filepath = "expense.dat";

        public Zau_Expense()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] categories =
                Enum.GetNames(typeof(ExpenseCategory));
            //load/populate the combobox with this array
            cboCategory.Items.AddRange(categories);
            cboCategory.SelectedIndex = 0;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            ///Need a FileStream object to
            ///open/create file
            ///BinaryWriter to write to a binary file
            ///BinaryReader to read a binary file
            FileStream fs = null;
            BinaryWriter bw = null;
            try
            {
                //1. open file for writing or appending
                fs = new FileStream(filepath,
                                    FileMode.Append,
                                    FileAccess.Write);
                bw = new BinaryWriter(fs);
                decimal amount = decimal.Parse(txtAmount.Text);
                DateTime dt = dt1.Value; 
                string categories = cboCategory.Text; 
                string description = txtDescription.Text;

                //save
                bw.Write(amount);
                bw.Write(dt.ToShortDateString());
                bw.Write(categories);
                bw.Write(description);

                
                MessageBox.Show("Data has been saved", "Expense",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
            }

            catch (FormatException fe)
            {
                MessageBox.Show(fe.Message);
            }
            catch (ArgumentException ae)
            {
                MessageBox.Show(ae.Message);
            }
            catch (IOException ioe)
            {
                MessageBox.Show(ioe.Message);
            }
            finally
            {
                //3. close file
                if (bw != null) bw.Close();
                if (fs != null) fs.Close();
            }
        }

        private void btnReadExpense_Click(object sender, EventArgs e)
        {
            //read binary file
            //Need to use FileStream to open the file for reading
            //Need to use BinaryReader to read binary data from file
            FileStream fs = null;
            BinaryReader br = null;

            try
            {
                fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
                br = new BinaryReader(fs);

                listView1.Items.Clear();

                while (br.PeekChar() != -1)
                {
                    decimal amount = br.ReadDecimal();
                    DateTime dt = Convert.ToDateTime(br.ReadString());
                    ExpenseCategory ec = (ExpenseCategory)Enum.Parse(typeof(ExpenseCategory), br.ReadString());
                    string description = br.ReadString();

                    //save to a listview
                    //create a listviewitem
                    ListViewItem lvitem = new ListViewItem(amount.ToString("c"));
                    lvitem.SubItems.Add(dt.ToShortDateString());
                    lvitem.SubItems.Add(ec.ToString());
                    lvitem.SubItems.Add(description);
                    //add lvitem to the listview
                    listView1.Items.Add(lvitem);
                }
            }
            catch (FormatException fe)
            {
                MessageBox.Show(fe.Message);
            }
            catch (ArgumentException ae)
            {
                MessageBox.Show(ae.Message);
            }
            catch (IOException ioe)
            {
                MessageBox.Show(ioe.Message);
            }

            finally
            {
                if (fs != null) fs.Close();
                if (br != null) br.Close();
            }
        }
    }
}

//www.nullskull.com/q/10150785/reading-and-writing-datetime-to-binary.aspx
//p://msdn.microsoft.com/en-us/library/cc165448.aspx

No comments:

Post a Comment