2015年6月17日 星期三

[2015][Quiz][Week11]Quiz05 - Team02


主程式Form
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 CopQuiz;
using WindowsFormsApplication1;

namespace Team2_Quiz5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        ObjectType[] objectArr = new ObjectType[10];
        double[] densityArr = { 2.7, 7.87, 11.3 };
        
        private void btnAdd_Click(object sender, EventArgs e)
        {
            int amount = ObjectType.Amount;
            if (amount >= 10)
                return;
            if (cboxMaterial.Text == "")
            {
                MessageBox.Show("請選擇一種材料");
                return;
            }
            if (cboxShape.Text == "")
            {
                MessageBox.Show("請選擇一種形狀");
                return;
            }
            switch (cboxShape.SelectedIndex)
            {
                case 0:
                    objectArr[amount] = new Ball(Convert.ToDouble(txtParameter.Text));
                    objectArr[amount].density = densityArr[cboxMaterial.SelectedIndex];
                    break;
                case 1:
                    objectArr[amount] = new Cube(Convert.ToDouble(txtParameter.Text));
                    objectArr[amount].density = densityArr[cboxMaterial.SelectedIndex];
                    break;
                case 2:
                    objectArr[amount] = new Cylinder(Convert.ToDouble(txtParameter.Text), Convert.ToDouble(txtHeight.Text));
                    objectArr[amount].density = densityArr[cboxMaterial.SelectedIndex];
                    break;
                case 3:
                    objectArr[amount] = new Pyramid(Convert.ToDouble(txtParameter.Text), Convert.ToDouble(txtHeight.Text));
                    objectArr[amount].density = densityArr[cboxMaterial.SelectedIndex];
                    break;
            }
            txtConstant.AppendText(objectArr[amount].StringShape()+"\r\n");
            txtAmount.Text = ObjectType.Amount.ToString();
        }

        private void cboxShape_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cboxShape.SelectedIndex)
            { 
                case 0:
                    label3.Text = "半徑";
                    label4.Visible = false;
                    txtHeight.Visible = false;
                    break;
                case 1:
                    label3.Text = "邊長";
                    label4.Visible = false;
                    txtHeight.Visible = false;
                    break;
                case 2:
                    label3.Text = "半徑";
                    label4.Text = "高";
                    label4.Visible = true;
                    txtHeight.Visible = true;
                    break;
                case 3:
                    label3.Text = "底邊";
                    label4.Text = "高";
                    label4.Visible = true;
                    txtHeight.Visible = true;
                    break;
            }
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            txtResult.Clear();
            for (int i = 0; i < ObjectType.Amount; i++)
            {
                txtResult.AppendText(objectArr[i].StringProperties()+"\r\n");
            }
        }

        private void txtParameter_TextChanged(object sender, EventArgs e)
        {

            if (cboxShape.SelectedIndex == 2 || cboxShape.SelectedIndex == 3)
            {
                if (txtParameter.Text != "" && txtHeight.Text != "")
                    btnAdd.Enabled = true;
                else
                    btnAdd.Enabled = false;
            }
            else
            {
                if (txtParameter.Text != "")
                    btnAdd.Enabled = true;
                else
                    btnAdd.Enabled = false;
            }

        }

        private void txtHeight_TextChanged(object sender, EventArgs e)
        {
            

                if (cboxShape.SelectedIndex == 2 || cboxShape.SelectedIndex == 3)
                {
                    if (txtParameter.Text != "" && txtHeight.Text != "")
                        btnAdd.Enabled = true;
                    else
                        btnAdd.Enabled = false;
                }

        }
        
    }
}

以下皆為類別:ObjectType,Ball,Cube,Cylinder,Pyramid,Constant
ObjectType為父類別
class ObjectType
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 CopQuiz
{
    class ObjectType
    {
        private static int amount=0;
        public ObjectType()
        {
            amount++;
        }
        public static int Amount
        {
            get{return amount;}
            set
            {  }
        }
        public double density;
        public virtual double Volume()
        {
            return 0;
        }
        public double Mass()
        {
            return Volume() * density;
        }
        public virtual string StringShape()
        {
            return "";
        }
        public string StringProperties()
        {
            string str = "";
            str += StringShape();
            str += "\t";
            str += density.ToString();
            str += "\t";
            str += string.Format("{0,8:F2}", Volume());
            str += "\t";
            str += string.Format("{0,8:F2}", Mass());
            return str;
        }
    }
}

class ball
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CopQuiz;
namespace WindowsFormsApplication1
{
    class Ball:ObjectType
    { 
        private double radius;
        public Ball(double s):base()
        {
            this.radius = s;
        }
        public double Radius
        {
            get { return radius; }
            set
            {
                if(radius < 0)
                    radius = 0;
                else
                    radius = value;
            }
        }
        public override double Volume()
        {
            
            return 4.0/3*Constant.pi*radius*radius*radius;
        }
        public override string StringShape()
        {
            string str = "";
            str += string.Format("{0,8}","Ball");
            str += "\t";
            str += string.Format("{0,8:F2}", radius);
            str += "\t";
            return str;
        }
    }
}

class Cube
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CopQuiz;
namespace WindowsFormsApplication1
{
    class Cube : ObjectType
    {
        private double side;
        public Cube(double s)
            : base()
        {
            this.side = s;
        }
        public double Side
        {
            get { return side; }
            set
            {
                if(side < 0)
                    side = 0;
                else
                    side = value;
            }
        }
        public override double Volume()
        {
            return side*side*side;
        }
        public override string StringShape()
        {
            string str = "";
            str += string.Format("{0,8}","Cube");
            str += "\t";
            str += string.Format("{0,8:F2}", side);
            str += "\t";
            return str;
        }
    }
}
class Cylinder
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CopQuiz;
namespace WindowsFormsApplication1
{
    class Cylinder : ObjectType
    { 
        private double radius;
        public double height;
        public Cylinder(double s, double h)
            : base()
        {
            this.radius = s;
            height = h;
        }
        public double Radius
        {
            get { return radius; }
            set
            {
                if(radius < 0)
                    radius = 0;
                else
                    radius = value;
            }
        }
        public override double Volume()
        {
            
            return Constant.pi*radius*radius*height;
        }
        public override string StringShape()
        {
            string str = "";
            str += string.Format("{0,8}","Cylinder");
            str += "\t";
            str += string.Format("{0,8:F2}", radius);
            str += "\t";
            str += string.Format("{0,8:F2}", height);
            return str;
        }
    }
}
class Pyramid
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CopQuiz;
namespace WindowsFormsApplication1
{
    class Pyramid : ObjectType
    {
          private double side;
        public double height;
        public Pyramid(double s, double h)
            : base()
        {
            this.side = s;
            height = h;
        }
        public double Side
        {
            get { return side; }
            set
            {
                if(side < 0)
                    side = 0;
                else
                    side = value;
            }
        }
        public override double Volume()
        {
            return side*side*height/3;
        }
        public override string StringShape()
        {
            string str = "";
            str += string.Format("{0,8}","Pyramid");
            str += "\t";
            str += string.Format("{0,8:F2}", side);
            str += "\t";
            str += string.Format("{0,8:F2}", height);
            return str;
        }
    }
}
static class Constant
using System;

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

沒有留言:

張貼留言