Monday, October 13, 2014

Inheritance_Rectangle Shapes

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



namespace Zau_Lab_Inheritance_Rectangle
{
    [Serializable]
    public class Rectangle
    {
        internal protected double _length;
        internal protected double _width;

        //Constructor
        public Rectangle(double length, double width)
        {
            _length = length;
            _width = width;
        }

        //Default Constructor
        public Rectangle()
        {
            _length = 5;
            _width = 5;
        }

        //Property
        public double Length
        { get { return _length; } }

        public double Width
        { get { return _width; } }

        //Methods
        public double Perimeter()
        {
            return 2 * (_length + _width);
        }

        public double Area()
        {
            return _length * _width;
        }
    }
}
//===========================================================
namespace Zau_Lab_Inheritance_Rectangle
{
    [Serializable]
    public class Box : Rectangle
    {
        internal protected double _height;

        public Box(double length, double width, double height):base (length, width)
        {
           _height = height;
        }

        public Box(double height)
        {
            _height = 6;
        }

        public double Height
        { get { return _height; } }

        new public double Perimeter()
        {
           // P = 4L+4W+4H 
            return (4 * Length) + (4 * Width) + (4 * Height);
        }

        new public double Area()
        {
            return 2 * (Length * Height) + 2 * (Length * Width) + 2 * (Width * Height);
        }

        public double Volume()
        {
            return Length * Width * Height;
        }
    }
}
//===========================================================
namespace Zau_Lab_Inheritance_Rectangle
{
    [Serializable]
    public class Pyramid : Rectangle
    {
        private double _height;
        public double Height
        { get { return _height; } }

        public Pyramid(double length, double width, double height): base(length, width)
        {
            _height = height;
        }

        new public double Area()
        {
            return Length * Width + (Length * Math.Sqrt (Math.Pow((Width/2), 2) +
                Math.Pow (Height, 2)) + Width * Math.Sqrt (Math.Pow((Length/2), 2) + 
                Math.Pow(Height, 2)));
        }

        public double Volume()
        {
            return 1f / 3f * (Length * Width) * Height;
        }
    }
}
//===========================================================
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; //added
using System.Runtime.Serialization;//added
using System.Runtime.Serialization.Formatters.Binary;//added


namespace Zau_Lab_Inheritance_Rectangle
{
    public partial class Form1 : Form
    {
        List<Rectangle> rectList = new List<Rectangle>();
        string fileLocation = "RectList.dat";
        Random rand = new Random();

        public Form1()
        {
            InitializeComponent();
        }
        
        private void btnCreateRectangle_Click(object sender, EventArgs e)
        {
            double length = rand.Next(5, 50);
            double width = rand.Next(10, 100);
            Rectangle r = new Rectangle(length, width);
            rectList.Add(r);
            MessageBox.Show("Your shape has been added");
            Display(r);
        }

        private void btnCreateBox_Click(object sender, EventArgs e)
        {
            double length = rand.Next(10, 100);
            double width = rand.Next(5, 50);
            double height = rand.Next(5, 50);
            Box b = new Box(length, width, height);
            rectList.Add(b);
            Display(b);
            MessageBox.Show("Your box has been added");
        }

        private void btnCreatePyramid_Click(object sender, EventArgs e)
        {
            double length = rand.Next(10, 100);
            double width = rand.Next(5, 50);
            double height = rand.Next(5, 50);
            Pyramid p = new Pyramid(length, width, height);
            rectList.Add(p);
            MessageBox.Show("Your shape has been added");
            Display(p);
        }

        private void Display (Rectangle r)
        {
            if(r is Box)
            {
                Box b = (Box)r;
                richTextBox1.AppendText(String.Format(
                "{0} Length: {1} Width: {2:f} Height: {3:f} Area: {4:f} Volume: {5:f}\n\n",
                b.GetType().Name, b.Length, b.Width, b.Height, b.Area(), b.Volume()));
            }
            else if (r is Pyramid)
            {
                //cast r back Pyramid type
                Pyramid p = (Pyramid)r;
                richTextBox1.AppendText(String.Format(
                    "{0} Length: {1} Width: {2:f} Height: {3:f} Area: {4:f} Volume: {5:f} \n\n",
                    p.GetType().Name, p.Length, p.Width, p.Height, p.Area(), p.Volume()));
            }
            else
            {
                richTextBox1.AppendText(String.Format(
                "{0} Length: {1} Width: {2:f} Peri: {3:f} Area: {4:f}\n\n",
                r.GetType().Name, r.Length, r.Width, r.Perimeter(), r.Area()));
            }
        }

        private void btnDisplayAllObjects_Click(object sender, EventArgs e)
        {
            Display();
        }

        private void Display()
        {

            listView1.Items.Clear();//clear the contents
            ListViewItem lstvitem;
            foreach (Rectangle r in rectList)
            {
                if (r is Box)
                {
                    //cast r from Rectangle to Box
                    Box bo = (Box)r;
                    lstvitem = new ListViewItem(bo.GetType().Name.ToString());
                    lstvitem.SubItems.Add(bo.Length.ToString("f"));
                    lstvitem.SubItems.Add(bo.Width.ToString("f"));
                    lstvitem.SubItems.Add(bo.Height.ToString("f"));
                    lstvitem.SubItems.Add(bo.Perimeter().ToString("f"));
                    lstvitem.SubItems.Add(bo.Area().ToString("f"));
                    lstvitem.SubItems.Add(bo.Volume().ToString("f"));

                    listView1.Items.Add(lstvitem);
                }
                else if (r is Pyramid)
                {
                    //cast r from Rectangle to Pyramid
                    Pyramid py = (Pyramid)r;
                    lstvitem = new ListViewItem(py.GetType().Name.ToString());
                    lstvitem.SubItems.Add(py.Length.ToString("f"));
                    lstvitem.SubItems.Add(py.Width.ToString("f"));
                    lstvitem.SubItems.Add(py.Height.ToString("f"));
                    lstvitem.SubItems.Add(""); //no perimeter method
                    lstvitem.SubItems.Add(py.Area().ToString("f"));
                    lstvitem.SubItems.Add(py.Volume().ToString("f"));

                    listView1.Items.Add(lstvitem);
                }
                else
                {
                    lstvitem = new ListViewItem(r.GetType().Name.ToString());
                    lstvitem.SubItems.Add(r.Length.ToString("f"));
                    lstvitem.SubItems.Add(r.Width.ToString("f"));
                    lstvitem.SubItems.Add("");
                    lstvitem.SubItems.Add(r.Perimeter().ToString("f"));
                    lstvitem.SubItems.Add(r.Area().ToString("f"));
                    lstvitem.SubItems.Add("");

                    listView1.Items.Add(lstvitem);
                }
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dr = MessageBox.Show("Would you like to save any changes before exiting the application?", "Save Data?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                IFormatter formatter = null;
                Stream stream = null;

                try
                {
                    formatter = new BinaryFormatter();
                    stream = new FileStream(fileLocation, FileMode.Append, FileAccess.Write, FileShare.None);

                    formatter.Serialize(stream, rectList);
                }
                catch (SerializationException se)
                {
                    MessageBox.Show(se.Message);
                }
                finally
                {
                    if (stream != null)
                    {
                        stream.Close();
                    }
                }

                MessageBox.Show("All data saved successfully", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //deserialize peopleList
            BinaryFormatter formatter = new BinaryFormatter();
            Stream s = null;
            try
            {
                s = new FileStream(fileLocation,
                                           FileMode.Open,
                                           FileAccess.ReadWrite, FileShare.None);
                //3.Deserialize (reconstruct the list of rect
                while (s.Position != s.Length)
                {
                    rectList = (List<Rectangle>)formatter.Deserialize(s);
                }
                
                MessageBox.Show("Rectangle list has been deserialized");
            }
            catch (IOException ioe)
            {
                MessageBox.Show(ioe.Message);
            }
            catch (SerializationException se)
            {
                MessageBox.Show(se.Message);
            }
            finally
            {
                if (s != null) s.Close();
            }
        }
    }
}

No comments:

Post a Comment