2015年5月13日 星期三

[2015][Quiz][Week11]Quiz05 - Team03


public class Shape3D
    {
        //欄位
        protected double _density;
        public static int Amount = 0;

        //建構子
        public Shape3D(double density)//有密度
        {
            _density = density;
            Amount++;
        }
        public Shape3D()//沒密度
        {
            _density = 0;
            Amount++;
        }

        //屬性
        public double  Density
        {
            set 
            {
                if (value > 0)
                    _density = value;
                else
                    return ;
            }
            get { return _density; }
        }

        //方法
        public virtual double Volume()
        {
            return 0.0;
        }
        public double Weight()
        {
            return Volume() * _density;
        }
        public virtual string ShowProperty()
        {
            return "";
        }
        public string ShowResult()
        {
            string output = ShowProperty();
            output += string.Format("{0,8:F2}", Density) + "\t"
                + string.Format("{0,8:F2}", Volume()) + "\t"
                + string.Format("{0,8:F2}", Weight());
            return output;
        }
    }
class Ball:Shape3D     {          //欄位         private double _radius;          //建構子         public Ball(double density, double radius)             : base(density)         {             _radius = radius;         }         public Ball()             : base()         {             _radius = 0;         }          //屬性         public double Radius         {             //set { _radius = value; }             get { return _radius; }         }          //方法         public override double Volume()//計算體積         {             return 3.0/4.0 *Constant.Pi*_radius * _radius * _radius;         }         public override string ShowProperty()//秀屬性         {             string output="";             output += string.Format("{0:8}","Ball")+"\t" + string.Format("{0,8:F2}", _radius) + "\t" + string.Format("{0,8}","") + "\t";             return output;         }      } 
class Cube:Shape3D
    {
           //欄位
        private double _side;

        //建構子
        public Cube(double density, double radius)
            : base(density)
        {
            _side = radius;
        }
        public Cube()
            : base()
        {
            _side = 0;
        }

        //屬性
        public double Side
        {
            //set { _radius = value; }
            get { return _side; }
        }

        //方法
        public override double Volume()//計算體積
        {
            return _side * _side * _side;
        }
        public override string ShowProperty()//秀屬性
        {
            string output = "";
            output += string.Format("{0:8}", "Cube") + "\t" + string.Format("{0,8:F2}", _side) + "\t" + string.Format("{0,8}", "") + "\t";
            return output;
        }
   
    }
class Cylinder:Shape3D
    {
        //欄位
        private double _radius;
        private double _hight;

        //建構子
        public Cylinder(double density, double radius, double hight)
            : base(density)
        {
            _radius = radius;
            _hight = hight;
        }
        public Cylinder()
            : base()
        {
            _radius = 0;
            _hight = 0;
        }

        //屬性
        public double Radius
        {
            //set { _radius = value; }
            get { return _radius; }
        }
        public double Hight
        {
            //set { _hight = value; }
            get { return _hight; }
        }

        //方法
        public override double Volume()//計算體積
        {
            return Constant.Pi*_radius * _radius * _hight;
        }
        public override string ShowProperty()//秀屬性
        {
            string output="";
            output += string.Format("{0:8}","Cylinder")+"\t"+ string.Format("{0,8:F2}", _radius) +"\t" + string.Format("{0,8:F2}", _hight) + "\t";
            return output;
        }
    }
class Pyramid:Shape3D
    {
         //欄位
        private double _side;
        private double _hight;

        //建構子
        public Pyramid(double density, double side, double hight)
            : base(density)
        {
            _side = side;
            _hight = hight;
        }
        public Pyramid()
            : base()
        {
            _side = 0;
            _hight = 0;
        }

        //屬性
        public double Side
        {
            //set { _radius = value; }
            get { return _side; }
        }
        public double Hight
        {
            //set { _hight = value; }
            get { return _hight; }
        }

        //方法
        public override double Volume()//計算體積
        {
            return (1.0/3.0) * _side * _side * _hight;
        }
        public override string ShowProperty()//秀屬性
        {
            string output="";
            output += string.Format("{0:8}", "Pyramid") + "\t" + string.Format("{0,8:F2}", _side) + "\t" + string.Format("{0,8:F2}", _hight) + "\t";
            return output;
        }
  
    }
public partial class Form1 : Form
    {
        static Shape3D[] myshape = new Shape3D[100];
        public double[] densityInfo = { 2.7, 7.87, 11.3 };
        public Form1()
        {
            InitializeComponent();
            comboBoxMaterial.SelectedIndex = 0;
            comboBoxShape.SelectedIndex = 0;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            double density=densityInfo[comboBoxMaterial.SelectedIndex];
            switch(comboBoxShape.SelectedIndex)
            {
                case 0:
                    myshape[Shape3D.Amount] = new Ball(density,Convert.ToDouble(txtPara1.Text));
                    break;
                case 1:
                    myshape[Shape3D.Amount] = new Cube(density, Convert.ToDouble(txtPara1.Text));
                    break;
                case 2:
                    myshape[Shape3D.Amount] = new Cylinder(density, Convert.ToDouble(txtPara1.Text), Convert.ToDouble(txtPara2.Text));
                    break;
                default:
                    myshape[Shape3D.Amount] = new Pyramid(density, Convert.ToDouble(txtPara1.Text), Convert.ToDouble(txtPara2.Text));
                    break;
            }

            string msg = myshape[Shape3D.Amount-1].ShowProperty();
            txtProperty.AppendText( msg + Environment.NewLine);
            txtAmount.Text= Shape3D.Amount.ToString();
        }

        private void btnCompute_Click(object sender, EventArgs e)
        {
            txtDisplay.Clear();
            for(int i=0; i < Shape3D.Amount; i++)
            {
                txtDisplay.AppendText( myshape[i].ShowResult()+Environment.NewLine);
            }
        }

        private void comboBoxShape_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBoxShape.SelectedIndex)
            {
                case 0:
                    lblPara1.Text = "半徑";
                    lblPara2.Visible = false;
                    txtPara2.Visible = false;
                    break;
                case 1:
                    lblPara1.Text = "邊長";
                    lblPara2.Visible = false;
                    txtPara2.Visible = false;
                    break;
                case 2:
                    lblPara1.Text = "半徑";
                    lblPara2.Text = "高";
                    lblPara2.Visible = true;
                    txtPara2.Visible = true;
                    break;
                case 3:
                    lblPara1.Text = "底邊";
                    lblPara2.Text = "高";
                    lblPara2.Visible = true;
                    txtPara2.Visible = true;
                    break;

            }
        }
    }

沒有留言:

張貼留言