2015年5月20日 星期三

[2015][Quiz][Week12]Quiz06 - Team06

程式分為七個部分
Form1 為視窗主程式.進行排序
ConstantTable 為抽象類別
    定義圓周率.金屬密度.建立密度體積重量輸出方法.虛擬計算體積方法.虛擬形狀以及參數輸出方法
Shape3D 為父類別
    產生靜態數量.形狀.材質
Ball.Cube.Cylinder.Pyramid 為子類別(繼承Shape)
    定義各形狀之參數.建立體積計算方法.形狀以及參數輸出方法

Form1
using System;
using System;
using System.Collections;
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 Quiz6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            cmbBoxMaterial.SelectedIndex = 0;
            cmbBoxShape.SelectedIndex = 0;
            cmbBoxSortMethod.SelectedIndex = 0;
            txtBoxShapeMessage.Text += "形狀\t\t半徑or邊長\t高";
            txtBoxShapeMessage.Text += Environment.NewLine;
            txtBoxMessage.Text += "形狀\t\t半徑or邊長\t高\t密度\t體積\t重量";
            txtBoxMessage.Text += Environment.NewLine;
        }

        ArrayList shapeArray = new ArrayList(); 

        private void cmbBoxShape_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cmbBoxShape.SelectedIndex)
            {
                case 0:
                    lblParameter1.Text = "半徑";
                    CleanParameterInput();
                    ShowParameter2(false);
                    break;
                case 1:
                    lblParameter1.Text = "邊長";
                    CleanParameterInput();
                    ShowParameter2(false);
                    break;
                case 2:
                    lblParameter1.Text = "半徑";
                    lblParameter2.Text = "高";
                    CleanParameterInput();
                    ShowParameter2(true);
                    break;
                case 3:
                    lblParameter1.Text = "邊長";
                    lblParameter2.Text = "高";
                    CleanParameterInput();
                    ShowParameter2(true);
                    break;
                default:
                    break;
            }
        }

        private void btnAddShape_Click(object sender, EventArgs e)
        {
            switch(cmbBoxShape.SelectedItem.ToString())
            {
                case "球":
                    shapeArray.Add(new Ball(cmbBoxMaterial.Text, Convert.ToDouble(txtBoxParameter1.Text)));
                    break;
                case "正方體":
                    shapeArray.Add(new Cube(cmbBoxMaterial.Text, Convert.ToDouble(txtBoxParameter1.Text)));
                    break;
                case "圓柱體":
                    shapeArray.Add(new Cylinder(cmbBoxMaterial.Text, Convert.ToDouble(txtBoxParameter1.Text), Convert.ToDouble(txtBoxParameter2.Text)));
                    break;
                case "金字塔":
                    shapeArray.Add(new Pyramid(cmbBoxMaterial.Text, Convert.ToDouble(txtBoxParameter1.Text), Convert.ToDouble(txtBoxParameter2.Text)));
                    break;
                default:
                    break;
            }
            txtBoxAmount.Text = shapeArray.Count.ToString();
            txtBoxShapeMessage.Text += ((Shape3D)shapeArray[shapeArray.Count - 1]).ShowShapeAndParameters();
            txtBoxShapeMessage.Text += Environment.NewLine;
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            txtBoxMessage.Text = null;
            txtBoxMessage.Text += "形狀\t\t半徑or邊長\t高\t密度\t體積\t重量";
            txtBoxMessage.Text += Environment.NewLine;
            SortCheck();
            for (int i = 0; i < shapeArray.Count; i++)
            {
                txtBoxMessage.Text += ((Shape3D)shapeArray[i]).ShowShapeAndParameters() + ((Shape3D)shapeArray[i]).ShowDensityVolumeAndWeight();
            }
        }

        public void CleanParameterInput()
        {
            txtBoxParameter1.Text = "0";
            txtBoxParameter2.Text = "0";
        }
        public void ShowParameter2(bool check)
        {
            if(check)
            {
                lblParameter2.Visible = true;
                txtBoxParameter2.Visible = true;
            }
            else
            {
                lblParameter2.Visible = false;
                txtBoxParameter2.Visible = false;
            }
        }

        private void btnSort_Click(object sender, EventArgs e)
        {
            switch(cmbBoxSortMethod.SelectedItem.ToString())
            {
                case "體積":
                    if (radBtnAscend.Checked)
                    {
                        BSort(shapeArray, CompareByVolumeAscend);
                    }
                    else if (radBtnDescend.Checked)
                    {
                        BSort(shapeArray, CompareByVolumeDescend);
                    }
                    break;
                case "重量":
                    if (radBtnAscend.Checked)
                    {
                        BSort(shapeArray, CompareByWeightAscend);
                    }
                    else if (radBtnDescend.Checked)
                    {
                        BSort(shapeArray, CompareByWeightDescend);
                    }
                    break;
                default:
                    break;
            }
        }

        private delegate bool CompareFun(Shape3D shape1, Shape3D shape2);

        private static bool CompareByVolumeAscend(Shape3D shape1, Shape3D shape2)
        {
            return shape1.GetVolume() > shape2.GetVolume();
        }
        private static bool CompareByVolumeDescend(Shape3D shape1, Shape3D shape2)
        {
            return shape1.GetVolume() < shape2.GetVolume();
        }
        private static bool CompareByWeightAscend(Shape3D shape1, Shape3D shape2)
        {
            return shape1.GetWeight() > shape2.GetWeight();
        }
        private static bool CompareByWeightDescend(Shape3D shape1, Shape3D shape2)
        {
            return shape1.GetWeight() < shape2.GetWeight();
        }

        private static void BSort(ArrayList shapeArray, CompareFun cmp)
        {
            Shape3D temp;
            for (int i = 0; i < shapeArray.Count -1; i++)
            {
                for (int j = 0; j < shapeArray.Count -1 ; j++)
                {
                    if (cmp((Shape3D)shapeArray[j], (Shape3D)shapeArray[j+1]))
                    {
                        temp = (Shape3D)shapeArray[j];
                        shapeArray[j] = shapeArray[j + 1];
                        shapeArray[j + 1] = temp;
                    }
                }
            }
        }

        public void SortCheck()
        {
            switch (cmbBoxSortMethod.SelectedItem.ToString())
            {
                case "體積":
                    if (radBtnAscend.Checked)
                    {
                        BSort(shapeArray, CompareByVolumeAscend);
                    }
                    else if (radBtnDescend.Checked)
                    {
                        BSort(shapeArray, CompareByVolumeDescend);
                    }
                    break;
                case "重量":
                    if (radBtnAscend.Checked)
                    {
                        BSort(shapeArray, CompareByWeightAscend);
                    }
                    else if (radBtnDescend.Checked)
                    {
                        BSort(shapeArray, CompareByWeightDescend);
                    }
                    break;
                default:
                    break;
            }
        }
             
    }
}

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

namespace Quiz5
{
    abstract class ConstantTable
    {
        public static double pi = 3.141592653589793238462433;

        public static double GetDensity(string material)
        {
            double density;
            switch (material)
            {
                case "鋁":
                    density = 2.7;
                    break;
                case "鐵":
                    density = 7.87;
                    break;
                case "鉛":
                    density = 11.3;
                    break;
                default:
                    density = 0;
                    break;
            }
            return density;
        }
    }
}

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

namespace Quiz6
{
    abstract class Shape3D
    {
        private string shape;
        private string material;
        public string Shape
        {
            get { return shape; }
            set 
            {
                shape = value;
            }
        }
        public string Material
        {
            get { return material; }
            set
            {
                material = value;
            }
        }

        public Shape3D()
        {
            Material = "";
        }
        public Shape3D(string material)
        {
            Material = material;
        }

        public abstract double GetVolume();
        public double GetWeight()
        {
            return  Math.Round(ConstantTable.GetDensity(Material) * GetVolume(), 2);
        }
        public string ShowDensityVolumeAndWeight()
        {
            string message = "";
            message += ConstantTable.GetDensity(Material) + "\t" + GetVolume() + "\t" + GetWeight();
            message += Environment.NewLine;
            return message;
        }
        public abstract string ShowShapeAndParameters();
    }
}

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

namespace Quiz6
{
    class Ball : Shape3D
    {
        private double radius;

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

        public Ball()
        {
            Shape = "Ball";
            Radius = 0;
        }
        public Ball(string material, double radius) 
            : base(material)
        {
            Shape = "Ball";
            Radius = radius;
        }

        public override double GetVolume()
        {
            return Math.Round(4.0 / 3 * ConstantTable.pi * Math.Pow(radius, 3), 2);
        }
        public override string ShowShapeAndParameters()
        {
            string message = "";
            message += Shape + "\t\t" + Radius + "\t\t\t";
            return message;
        }
    }
}

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

namespace Quiz6
{
    class Cube : Shape3D
    {
        private double sideLength;

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

        public Cube()
        {
            Shape = "Cube";
            SideLength = 0;
        }
        public Cube(string material, double sideLength)
            : base(material)
        {
            Shape = "Cube";
            SideLength = sideLength;
        }
        public override double GetVolume()
        {
            return Math.Round(Math.Pow(sideLength, 3), 2);
        }
        public override string ShowShapeAndParameters()
        {
            string message = "";
            message += Shape + "\t\t" + SideLength + "\t\t\t";
            return message;
        }
    }
}

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

namespace Quiz6
{
    class Cylinder : Shape3D
    {
        private double radius;
        private double height;

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

        public Cylinder()
        {
            Shape = "Cylinder";
            Radius = 0;
            Height = 0;
        }
        public Cylinder(string material, double radius, double height)
            : base(material)
        {
            Shape = "Cylinder";
            Radius = radius;
            Height = height;
        }
        public override double GetVolume()
        {
            return Math.Round(Height * ConstantTable.pi * Math.Pow(Radius, 2), 2);
        }
        public override string ShowShapeAndParameters()
        {
            string message = "";
            message += Shape + "\t\t" + Radius + "\t\t" + Height + "\t";
            return message;
        }
    }
}

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

namespace Quiz6
{
    class Pyramid : Shape3D
    {
        private double sideLength;
        private double height;

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

        public Pyramid()
        {
            Shape = "Pyramid";
            SideLength = 0;
            Height = 0;
        }
        public Pyramid(string material, double sideLength, double height)
            : base(material)
        {
            Shape = "Pyramid";
            SideLength = sideLength;
            Height = height;
        }
        public override double GetVolume()
        {
            return Math.Round(1.0 / 3 * height * Math.Pow(sideLength, 2), 2);
        }
        public override string ShowShapeAndParameters()
        {
            string message = "";
            message += Shape + "\t\t" + SideLength + "\t\t" + Height + "\t";
            return message;
        }
    }
}

沒有留言:

張貼留言