2015年5月13日 星期三

[2015][Quiz][Week11]Quiz05 - Team04

Form1.cs為視窗設計
Shape3D.cs為基礎類別
Ball.cs、Cube.cs、Cylinder.cs、Pyramid.cs為衍生類別
Constant.cs為紀錄運算需要用到的常數,例如pi
Form1.cs
using quiz5;
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;

namespace _2015OOP_Quiz5_win
{
    public partial class Form1 : Form
    {
        private Shape3D[] shapeArr=new Shape3D[100];
        private static double[] densityArr = { 2.7, 7.87, 11.3};

        public Form1()
        {
            InitializeComponent();     
            lblPara1.Visible = false;
            txtPara1.Visible = false;  
            lblPara2.Visible = false;
            txtPara2.Visible = false;  
        }

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

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int amount = Shape3D.Amount;
            double density = densityArr[cboxMaterialSet.SelectedIndex];
            switch (cboxShapeSet.SelectedIndex)
            {
                case 0:
                    shapeArr[amount] = new Ball(double.Parse(txtPara1.Text),density);
                    break;
                case 1:
                    shapeArr[amount] = new Cube(double.Parse(txtPara1.Text), density);
                    break;
                case 2:
                    shapeArr[amount] = new Cylinder(double.Parse(txtPara1.Text), double.Parse(txtPara2.Text), density);
                    break;
                case 3:
                    shapeArr[amount] = new Pyramid(double.Parse(txtPara1.Text), double.Parse(txtPara2.Text), density);
                    break;
                default:
                    break;
            }
            txtShapeNum.Text = Shape3D.Amount.ToString();
            txtAdd.AppendText(shapeArr[amount].ShowProperty() + Environment.NewLine);
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            txtShowSolution.Clear();
            for (int i=0;i < Shape3D.Amount;i++)
            {
                txtShowSolution.AppendText(shapeArr[i].ShowResults() + Environment.NewLine);
            }
        }
    }
}

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

namespace quiz5
{
    class Shape3D
    {
        protected double density;
        private static int _amount = 0;

        protected Shape3D()
        {
            Density = 0;
            _amount++;
        }

        protected Shape3D(double d)
        {
            Density = d;
            _amount++;
        }

        public double Density
        {
            get { return density; }
            set
            {
                if (density<0)
                {
                    density = 0;
                }
                else
                {
                    density = value;
                }
            }
        }
        public static int Amount
        {
            get { return _amount; }
        }

        protected virtual double Volume()
        {
            return 0;
        }
        
        private double Weight()
        {
            return density * Volume();
        }
        
        public virtual string ShowProperty()
        {
            return "";
        }

        public string ShowResults()
        {
            string results = ShowProperty();
            results+='\t';
            results += string.Format("{0,8:F2}", density);
            results += '\t';
            results += string.Format("{0,8:F2}", Volume());
            results += '\t';
            results += string.Format("{0,8:F2}", Weight());
            return results;

        }
    }
}

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

namespace quiz5
{
    class Ball:Shape3D
    {
        private double _radius;

        public Ball (double _radius, double density)
            :base(density)
        {
            Radius = _radius;
        }

        public double Radius
        {
            get { return _radius; }
            set
            {
                if (_radius<0)
                {
                    _radius = 0;
                }
                else
                {
                    _radius = value;
                }
            }
        }

        protected override double Volume()
        {
            return 4.0 / 3 * Constant.pi * _radius * _radius * _radius;
        }

        public override string ShowProperty()
        {
            string str = "Ball";
            str += '\t';
            str += string.Format("{0,8:F2}", _radius);
            str += '\t';
            str += "";
            return str;
        }
    }
}

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

namespace quiz5
{
    class Cube:Shape3D
    {
        private double _length;

        public Cube(double _length, double density)
            : base(density)
        {
            Length = _length;
        }

        public double Length
        {
            get { return _length; }
            set
            {
                if (_length < 0)
                {
                    _length = 0;
                }
                else
                {
                    _length = value;
                }
            }
        }

        protected override double Volume()
        {
            return _length * _length * _length;
        }

        public override string ShowProperty()
        {
            string str = "Cube";
            str += '\t';
            str += string.Format("{0,8:F2}", _length);
            str += '\t';
            str += "";
            return str;
        }
    }
}

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

namespace quiz5
{
    class Cylinder:Shape3D
    {
        private double _radius;
        private double _height;

        public Cylinder(double _radius, double _height, double density)
            :base(density)
        {
            Radius = _radius;
            Height = _height;
        }

        public double Radius
        {
            get { return _radius; }
            set
            {
                if (_radius<0)
                {
                    _radius = 0;
                }
                else
                {
                    _radius = value;
                }
            }
        }

        public double Height
        {
            get { return _height; }
            set
            {
                if (_height < 0)
                {
                    _height = 0;
                }
                else
                {
                    _height = value;
                }
            }
        }

        protected override double Volume()
        {
            return Constant.pi * _radius * _radius * _height;
        }

        public override string ShowProperty()
        {
            string str = "Cylinder";
            str += '\t';
            str += string.Format("{0,8:F2}", _radius);
            str += '\t';
            str += string.Format("{0,8:F2}", _height);
            return str;
        }
    }
}

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

namespace quiz5
{
    class Pyramid:Shape3D
    {
        private double _length;
        private double _height;

        public Pyramid(double _length, double _height, double density)
            :base(density)
        {
            Length = _length;
            Height = _height;
        }

        public double Length
        { 
            get { return _length; }
            set
            {
                if (_length < 0)
                {
                    _length = 0;
                }
                else
                {
                    _length = value;
                }
            }
        }

        public double Height
        {
            get { return _height; }
            set
            {
                if (_height < 0)
                {
                    _height = 0;
                }
                else
                {
                    _height = value;
                }
            }
        }

        protected override double Volume()
        {
            return 1.0/3*_length*_length*_height;
        }

        public override string ShowProperty()
        {
            string str = "Pyramid";
            str += '\t';
            str += string.Format("{0,8:F2}", _length);
            str += '\t';
            str += string.Format("{0,8:F2}", _height);
            return str;
        }
    }
}

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

namespace quiz5
{
    static class Constant
    {
        public static double pi = 3.1415926;
    }
}

沒有留言:

張貼留言