2015年5月6日 星期三

[2015][Homework]Team06 - Hw07

程式分為:
視窗進入點:Form
父類別:Shape3D
子類別:Ball,Cube,Cylinder,Pyramid

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;

namespace Homework7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            cboxMaterial.SelectedIndex = 0;
            cboxShape.SelectedIndex = 0;
        }
        private Shape3D[] shapeArray = new Shape3D[100];
        private void cboxShape_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch(cboxShape.SelectedIndex)
            {
                case 0:             //ball
                    lblParameter1.Text = "半徑";
                    lblParameter2.Visible = false;
                    txtParameter2.Visible = false;
                    break;
                case 1:             //cube
                    lblParameter1.Text = "邊長";
                    lblParameter2.Visible = false;
                    txtParameter2.Visible = false;
                    break;
                case 2:             //cylinder
                    lblParameter1.Text = "半徑";
                    lblParameter2.Text = "高";
                    lblParameter2.Visible = true;
                    txtParameter2.Visible = true;
                    break;
                case 3:             //pyramid
                    lblParameter1.Text = "邊長";
                    lblParameter2.Text = "高";
                    lblParameter2.Visible = true;
                    txtParameter2.Visible = true;
                    break;
                default:
                    break;
            }
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            switch(cboxShape.SelectedIndex)
            {
                case 0:
                    txtParameter2.Text = null;
                    shapeArray[Convert.ToInt16(txtAmount.Text)] = new Ball(cboxMaterial.SelectedItem.ToString(), Convert.ToInt16(txtParameter1.Text));
                    break;
                case 1:
                    txtParameter2.Text = null;
                    shapeArray[Convert.ToInt16(txtAmount.Text)] = new Cube(cboxMaterial.SelectedItem.ToString(), Convert.ToInt16(txtParameter1.Text));
                    break;
                case 2:
                    shapeArray[Convert.ToInt16(txtAmount.Text)] = new Cylinder(cboxMaterial.SelectedItem.ToString(), Convert.ToInt16(txtParameter1.Text), Convert.ToInt16(txtParameter2.Text));
                    break;
                case 3:
                    shapeArray[Convert.ToInt16(txtAmount.Text)] = new Pyramid(cboxMaterial.SelectedItem.ToString(), Convert.ToInt16(txtParameter1.Text), Convert.ToInt16(txtParameter2.Text));
                    break;
            }
            ShowHeadingMessage();
            txtAmount.Text = Shape3D.amount.ToString();
        }
        private void btnCalculate_Click(object sender, EventArgs e)
        {
            ShowSolutionMessage();
        }
        private void ShowHeadingMessage()
        {
            txtHeading.Text += shapeArray[Convert.ToInt16(txtAmount.Text)].shape.ToString() + "\t";
            txtHeading.Text += txtParameter1.Text + "\t" + txtParameter2.Text + "\t";
            txtHeading.Text += Environment.NewLine;
        }
        private void ShowSolutionMessage()
        {
            for (int i = 0; i < Convert.ToInt16(txtAmount.Text); i++ )
            {
                txtSolution.Text += shapeArray[i].shape.ToString() + "\t";
                txtSolution.Text += shapeArray[i].density.ToString() + "\t";
                txtSolution.Text += shapeArray[i].Volume().ToString() + "\t";
                txtSolution.Text += shapeArray[i].Weight().ToString() + "\t";
                txtSolution.Text += Environment.NewLine;
            }
        }
    }
}
Shape3D
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework7
{
    class Shape3D
    {
        public static int amount;
        public double density;
        public string shape;
        protected double volume;
        protected double weight;
        public Shape3D()
        {

        }
        public double MaterialDensity(string Material)
        {
            switch(Material)
            {
                case "鐵":
                    density = 7.87;
                    break;
                case "鋁":
                    density = 2.70;
                    break;
                case "鉛":
                    density = 11.30;
                    break;
                default:
                    break;
            }
            return density;
        }
        public virtual double Volume()
        {
            return volume;
        }
        public double Weight()
        {
            weight = volume * density;
            weight = Math.Round(weight, 2);
            return weight;
        }
    }
}
Ball
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework7
{
    class Ball : Shape3D
    {
        private double radius;
        private static double pi = 3.1415926;
        public Ball()
        {
            
        }
        public Ball(string material, double radius)
        {
            this.shape = "Ball";
            this.radius = radius;
            density = base.MaterialDensity(material);
            amount++;
        }
        public override double Volume()
        {
            base.volume = 4.0/3*pi*(Math.Pow(radius,3));
            base.volume = Math.Round(base.volume,2);
            return base.volume;
        }
    }
}
Cube
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework7
{
    class Cube : Shape3D
    {
        private double sideLength;
        public Cube()
        {
            
        }
        public Cube(string material, double sideLength)
        {
            this.shape = "Cube";
            this.sideLength = sideLength;
            density = base.MaterialDensity(material);
            amount++;
        }
        public override double Volume()
        {
            base.volume = Math.Pow(sideLength, 3);
            base.volume = Math.Round(base.volume,2);
            return base.volume;
        }
    }
}
Cylinder
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework7
{
    class Cylinder : Shape3D
    {
        private double radius;
        private double height;
        private static double pi = 3.1415926;
        public Cylinder()
        {
            
        }
        public Cylinder(string material, double radius, double height)
        {
            this.shape = "Cylinder";
            this.radius = radius;
            this.height = height;
            density = base.MaterialDensity(material);
            amount++;
        }
        public override double Volume()
        {
            base.volume = height*pi*(Math.Pow(radius,2));
            base.volume = Math.Round(base.volume,2);
            return base.volume;
        }
    }
}
Pyramid
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Homework7
{
    class Pyramid : Shape3D
    {
        private double sideLength;
        private double height;
        public Pyramid()
        {
            
        }
        public Pyramid(string material, double sideLength, double height)
        {
            this.shape = "Pyramid";
            this.sideLength = sideLength;
            this.height = height;
            density = base.MaterialDensity(material);
            amount++;
        }
        public override double Volume()
        {
            base.volume = height * Math.Pow(sideLength, 2) / 3;
            base.volume = Math.Round(base.volume,2);
            return base.volume;
        }
    }
}

沒有留言:

張貼留言