2015年5月7日 星期四

[2015][Homework]Team01 - Hw07

Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HW7
{
    public partial class Form1 : Form
    {
        private Shape3D[] shapeArr = new Shape3D[100];
        private static double[] densityArr = { 2.7, 7.87, 11.3 };
        public Form1()
        {
            InitializeComponent();
            cbox_ShapeSelect.SelectedIndex = 0;
            cbox_MeterialSelect.SelectedIndex = 0;
        }
        private void cbox_ShapeSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cbox_ShapeSelect.SelectedIndex)
            {
                case 0:
                    lbl_Para1.Text = "半徑";
                    lbl_Para2.Visible = false;
                    txt_Para2.Visible = false;
                    break;
                case 1:
                    lbl_Para1.Text = "邊長";
                    lbl_Para2.Visible = false;
                    txt_Para2.Visible = false;
                    break;
                case 2:
                    lbl_Para1.Text = "半徑";
                    lbl_Para2.Text = "高";
                    lbl_Para2.Visible = true;
                    txt_Para2.Visible = true;
                    break;
                case 3:
                    lbl_Para1.Text = "邊長";
                    lbl_Para2.Text = "高";
                    lbl_Para2.Visible = true;
                    txt_Para2.Visible = true;
                    break;
                default:
                    break;
            }
        }
        private void btn_Add_Click(object sender, EventArgs e)
        {
            int amount = Shape3D.Amount;
            double density = densityArr[cbox_MeterialSelect.SelectedIndex];
            switch (cbox_ShapeSelect.SelectedIndex)
            {
                case 0:
                    shapeArr[amount] = new Ball(double.Parse(txt_Para1.Text), density);
                    break;
                case 1:
                    shapeArr[amount] = new Cube(double.Parse(txt_Para1.Text), density);
                    break;
                case 2:
                    shapeArr[amount] = new Cylinder(double.Parse(txt_Para1.Text), double.Parse(txt_Para2.Text), density);
                    break;
                case 3:
                    shapeArr[amount] = new Pyramid(double.Parse(txt_Para1.Text), double.Parse(txt_Para2.Text), density);
                    break;
                default:
                    break;
            }
            txt_Message.AppendText(shapeArr[amount].ShapeProperty() + Environment.NewLine);
            txt_AmountOfShape.Text = Shape3D.Amount.ToString();
        }

        private void btn_Calculate_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < Shape3D.Amount; i++)
            {
                string str = (shapeArr[i].ShowVolumeWeight() + Environment.NewLine);
                txt_Display.AppendText(str);
            }
        }
    }
}
Shape3D.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HW7
{
    class Shape3D
    {
        //Fields
        protected double _density;
        private static int _amount = 0;
        //Constructors
        public Shape3D()
        {
            _density = 0;
            _amount++;
        }
        public Shape3D(double d)
        {
            _density = d;
            _amount++;
        }
        //Attributes
        public double Density
        {
            get { return _density; }
            set
            {
                if (_density < 0)
                {
                    _density = 0;
                }
                else
                {
                    _density = value;
                }
            }
        }
        public static int Amount
        {
            get { return _amount; }
        }
        //Methods
        public double Weight()
        {
            return Density * Volume();
        }
        public string ShowVolumeWeight()
        {
            string str = ShapeProperty();
            str += '\t';
            str += string.Format("{0,8:F2}", Density);
            str += '\t';
            str += string.Format("{0,8:F2}", Volume());
            str += '\t';
            str += string.Format("{0,8:F2}", Weight());
            return str;
        }
        //Virtual Methods
        public virtual double Volume()
        {
            return 0;
        }
        public virtual string ShapeProperty()
        {
            return "";
        }
    }
}
Ball.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HW7
{
    class Ball : Shape3D
    {
        //Fields
        private double _radius;
        //Constructors
        public Ball(double radius, double density)
            : base(density)
        {
            Radius = radius;
        }
        //Attributes
        public double Radius
        {
            get { return _radius; }
            set
            {
                if (_radius < 0)
                {
                    _radius = 0;
                }
                else
                {
                    _radius = value;
                }
            }
        }
        //Methods
        public override double Volume()
        {
            return 4.0 / 3 * GeoConstant.pi * _radius * _radius * _radius;
        }
        public override string ToString()
        {
            string str = string.Format("{0,8}", "球");
            str += '\t';
            str += string.Format("{0,10:F2}", Volume());
            str += '\t';
            str += string.Format("{0,8:F2}", Weight());
            return str;
        }
        public override string ShapeProperty()
        {
            string str = string.Format("{0,8}", "Ball");
            str += '\t';
            str += string.Format("{0,8:F2}", Radius);
            str += "\t\t";
            str += string.Format("{0,8}", "");
            return str;
        }
    }
}
Cube.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HW7
{
    class Cube : Shape3D
    {
        //Fields
        private double _side;
        //Constructors
        public Cube(double side, double density)
            : base(density)
        {
            Side = side;
        }
        //Attributes
        public double Side
        {
            get { return _side; }
            set
            {
                if (_side < 0)
                {
                    _side = 0;
                }
                else
                {
                    _side = value;
                }
            }
        }
        //Methods
        public override double Volume()
        {
            return _side * _side * _side;
        }
        public override string ToString()
        {
            string str = string.Format("{0,8}", "正方體");
            str += '\t';
            str += string.Format("{0,10:F2}", Volume());
            str += '\t';
            str += string.Format("{0,8:F2}", Weight());
            return str;
        }
        public override string ShapeProperty()
        {
            string str = string.Format("{0,8}", "Cube");
            str += '\t';
            str += string.Format("{0,8:F2}", Side);
            str += "\t\t";
            str += string.Format("{0,8}", "");
            return str;
        }
    }
}
Cylinder.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HW7
{
    class Cylinder : Shape3D
    {
        //Fields
        private double _radius;
        private double _height;
        //Constructors
        public Cylinder(double radius, double height, double density)
            : base(density)
        {
            Radius = radius;
            Height = height;
        }
        //Attributes
        public double Height
        {
            get { return _height; }
            set
            {
                if (_height < 0)
                {
                    _height = 0;
                }
                else
                {
                    _height = value;
                }
            }
        }
        public double Radius
        {
            get { return _radius; }
            set
            {
                if (_radius < 0)
                {
                    _radius = 0;
                }
                else
                {
                    _radius = value;
                }
            }
        }
        //Methods
        public override double Volume()
        {
            return GeoConstant.pi * _radius * _radius * _height;
        }
        public override string ToString()
        {
            string str = string.Format("{0,8}", "圓柱體");
            str += '\t';
            str += string.Format("{0,10:F2}", Volume());
            str += '\t';
            str += string.Format("{0,8:F2}", Weight());
            return str;
        }
        public override string ShapeProperty()
        {
            string str = string.Format("{0,8}", "Cylinder");
            str += '\t';
            str += string.Format("{0,8:F2}", Radius);
            str += '\t';
            str += string.Format("{0,8:F2}", Height);
            str += '\t';
            str += string.Format("{0,8}", "");
            return str;
        }
    }
}
Pyramid.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HW7
{
    class Pyramid : Shape3D
    {
        //Fields
        private double _side;
        private double _height;
        //Constructors
        public Pyramid(double side, double height, double density)
            : base(density)
        {
            Side = side;
            Height = height;
        }
        //Attributes
        public double Height
        {
            get { return _height; }
            set
            {
                if (_height < 0)
                {
                    _height = 0;
                }
                else
                {
                    _height = value;
                }
            }
        }
        public double Side
        {
            get { return _side; }
            set
            {
                if (_side < 0)
                {
                    _side = 0;
                }
                else
                {
                    _side = value;
                }
            }
        }
        //Methods
        public override double Volume()
        {
            return 1.0 / 3 * _side * _side * _height;
        }
        public override string ToString()
        {
            string str = string.Format("{0,8}", "金字塔");
            str += '\t';
            str += string.Format("{0,10:F2}", Volume());
            str += '\t';
            str += string.Format("{0,8:F2}", Weight());
            return str;
        }
        public override string ShapeProperty()
        {
            string str = string.Format("{0,8}", "Pyramid");
            str += '\t';
            str += string.Format("{0,8:F2}", Side);
            str += '\t';
            str += string.Format("{0,8:F2}", Height);
            str += '\t';
            str += string.Format("{0,8}", "");
            return str;
        }
    }
}
GeoConstant.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HW7
{
    class GeoConstant
    {
        public static double pi = 3.1415926;
    }
}

Form1.Designer.cs(按我顯示)

沒有留言:

張貼留言