2014年4月13日 星期日

[Code Review] Team 10 - Hw06


下載執行檔
Form1.cs
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 OOP_HW6
{
    public partial class Form1 : Form
    {
        Shape3D[] ShapeArray = new Shape3D[10];

        public Form1()
        {
            InitializeComponent();            
        }

        public void ShapeShow(string text)
        {
            string picPath = text + ".png";
            //picBox_3DShape.SizeMode = PictureBoxSizeMode.AutoSize;
            picBox_3DShape.Image = Image.FromFile(picPath);
        }

        public void InputShow(string text)
        {
            panel1.Hide(); panel2.Hide(); panel3.Hide();
            switch (text)
            {
                case "Cube":
                    panel1.Show();
                    lbl_length.Text = "長度";
                    break;
                case "Cuboid":
                    panel1.Show(); panel2.Show(); panel3.Show();
                    lbl_length.Text = "長度";
                    break;
                case "Sphere":
                    panel1.Show();
                    lbl_length.Text = "半徑";
                    break;
                case "Cylinder":
                    panel1.Show(); panel2.Show();
                    lbl_length.Text = "半徑";
                    break;
                case"Pyramid":
                    panel1.Show(); panel2.Show();
                    lbl_length.Text = "長度";
                    break;
                case "Corn":
                    panel1.Show(); panel2.Show();
                    lbl_length.Text = "半徑";
                    break;
                default:
                    break;
            }
        }

        private void cBox_shape_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShapeShow(cBox_shape.Text);
            InputShow(cBox_shape.Text);
        }
        private void cBox_material_SelectedIndexChanged(object sender, EventArgs e)
        {
            label_materialShow.Text = cBox_material.Text.Substring(0,2);
        }

        private void btn_selected_Click(object sender, EventArgs e)
        {
            int amount = Shape3D.Amount;
            if (amount >= 10)
            {
                MessageBox.Show("加入額滿!");
                return;
            }
            switch(cBox_shape.Text)
            {
                case "Cube":
                    if (txt_length.Text == "")
                    {
                        MessageBox.Show("請輸入參數!");
                        return;
                    }
                    Cube cube = Cube.Create(double.Parse(txt_length.Text));
                    if (cube == null)
                    {
                        MessageBox.Show("加入失敗!");
                        return;
                    }
                    ShapeArray[amount] = cube;
                    break;
                case "Cuboid":
                    if (txt_length.Text == "" || txt_width.Text == "" || txt_height.Text == "")
                    {
                        MessageBox.Show("請輸入參數!");
                        return;
                    }
                    Cuboid cuboid = Cuboid.Create(double.Parse(txt_length.Text), 
                        double.Parse(txt_width.Text), double.Parse(txt_height.Text));
                    if (cuboid == null)
                    {
                        MessageBox.Show("加入失敗!");
                        return;
                    }
                    ShapeArray[amount] = cuboid;
                    break;
                case "Sphere":
                    if (txt_length.Text == "")
                    {
                        MessageBox.Show("請輸入參數!");
                        return;
                    }
                    Sphere sphere = Sphere.Create(double.Parse(txt_length.Text));
                    if (sphere == null)
                    {
                        MessageBox.Show("加入失敗!");
                        return;
                    }
                    ShapeArray[amount] = sphere;
                    break;
                case "Cylinder":
                    if (txt_length.Text == "" || txt_height.Text == "")
                    {
                        MessageBox.Show("請輸入參數!");
                        return;
                    }
                    Cylinder cylinder = Cylinder.Create(double.Parse(txt_length.Text), 
                        double.Parse(txt_height.Text));
                    if (cylinder == null)
                    {
                        MessageBox.Show("加入失敗!");
                        return;
                    }
                    ShapeArray[amount] = cylinder;
                    break;
                case "Pyramid":
                    if (txt_length.Text == "" || txt_height.Text == "")
                    {
                        MessageBox.Show("請輸入參數!");
                        return;
                    }
                    Pyramid pyramid = Pyramid.Create(double.Parse(txt_length.Text), 
                        double.Parse(txt_height.Text));
                    if (pyramid == null)
                    {
                        MessageBox.Show("加入失敗!");
                        return;
                    }
                    ShapeArray[amount] = pyramid;
                    break;
                case "Corn":
                    if (txt_length.Text == "" || txt_height.Text == "")
                    {
                        MessageBox.Show("請輸入參數!");
                        return;
                    }
                    Corn corn = Corn.Create(double.Parse(txt_length.Text),
                        double.Parse(txt_height.Text));
                    if (corn == null)
                    {
                        MessageBox.Show("加入失敗!");
                        return;
                    }
                    ShapeArray[amount] = corn;
                    break;
                default:
                    MessageBox.Show("請選擇形狀!");
                    return;                   
            }
            lbl_ashow.Text = Shape3D.Amount.ToString();
            ShapeArray[amount].Density(cBox_material.SelectedIndex);
            txt_vShow.Text = ShapeArray[amount].Volume().ToString("F");
            txt_wSHow.Text = ShapeArray[amount].Weight().ToString("F");            
            txt_select.AppendText(ShapeArray[amount].Name() + "\n");
        }

        private void btn_calaulate_Click(object sender, EventArgs e)
        {
            txt_select.Text = "";   //textBox Clear
            string str = "";
            for (int i = 0; i < Shape3D.Amount; i++)
            {
                str += ShapeArray[i].Name() + "\t"
                + ShapeArray[i].Density(cBox_material.SelectedIndex) + "\t"
                + ShapeArray[i].Volume().ToString("F") + "\t"
                + ShapeArray[i].Weight().ToString("F") + "\n";
            }
            str += "======================================================" + "\n";
            txt_caculate.AppendText(str);
            //歸零清空
            Shape3D.Amount = 0;
            lbl_ashow.Text = Shape3D.Amount.ToString();
        }
    }
}
Shape3D.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OOP_HW6
{
    class Shape3D
    {
        //Field
        private double density = 1;
        protected double length;
        protected double height;
        protected double width;
        protected static double Pi = 3.14159;
        public static double[] Material_density = { 19.3, 10.49, 8.96, 7.9, 2.7, 4.5, -1 };
        //
        protected static int amount = 0;
        //Constructors
        public Shape3D()
        {
            this.length = 0;
            this.width = 0;
            this.height = 0;
        }
        public Shape3D(double length)
        {
            this.length = length;
            amount++;
        }
        public Shape3D(double length, double height)
        {
            this.length = length;
            this.height = height;
            amount++;
        }
        public Shape3D(double length, double width, double height)
        {
            this.length = length;
            this.width = width;
            this.height = height;
            amount++;
        }
        //ReadOnly Attribute
        public static int Amount
        {
            get { return amount; }
            set { amount = value; }
        }        
        //Virtual Methods
        public virtual double Volume()
        {
            return -1;
        }
        public virtual string Name()
        {
            return "";
        }
        //Methods
        public double Density(int select)
        {
            if (select < 0) //都沒選擇材料的情況
                this.density = 1;
            else
                density = Material_density[select];
            return density;
        }
        public double Weight()
        {
            return Volume() * density;
        }
    }
}
Cube.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OOP_HW6
{
    class Cube : Shape3D
    {
        //Fields
        new private static int amount = 0;
        //Constructors
        public Cube()
            : base()
        {
            amount++;
        }
        public Cube(double length)
            : base(length)
        {
            amount++;
        }
        //繼承隱藏 hide inherited attribute
        new public static int Amount
        {
            get { return amount; }
        }
        //override Methods
        public override double Volume()
        {
            return length * length * length;
        }
        public override string Name()
        {
            return "Cube" + "\t" + length.ToString("F") + "\t" + "\t";
        }
        public static Cube Create(double length)
        {
            Cube cube = null;
            if (length < 0)
                return cube;
            else
            {
                cube = new Cube(length);
                return cube;
            }
        }
    }
}

6 則留言:

  1. 1. 介面很漂亮。
    2. length, width, height這些特徵是不是寫在子類別會比較好呢? 這樣感覺好像是父類別要包含所有子類別的欄位呢!
    3. 我覺得有針對輸入做保護是很好的想法。

    回覆刪除
  2. ==第七組==
    程式架構很完整!但是當尚未輸入參數時點選計算會出現分隔線,若這裡做個限制會更完美!

    回覆刪除
  3. [第六組]
    介面寫得很讚喔!
    Form1.cs: 81行的switch裡面,有很多程式碼是在檢查輸入的資料是否正確. 雖然這個步驟不可避免地要加入,不過我覺得可以參考第八組的作法. 在他們的Form1.cs:94行那邊,把這些檢查步驟分割出來成為一個CheckData(). 這樣在看switch處理資料的時候會感覺比較簡潔.
    Shape3D.cs: 21行的建構子內部,其實可以不用加入this.

    回覆刪除
  4. 林高遠:
    優:
    1.有把圖畫出來。
    2.有做使用者防呆檢查。

    缺:
    1. Shape3D的數目限制不能runtime決定,彈性太死。
    2.使用者防呆檢查做得不夠好,只能防止空白,數字部份我輸入非數字,程式就會當掉。
    2. lbl_length.Text內容卻可以是"半徑"或"長度",這個物件名稱取得不夠好。
    3. 寫Create( )似乎畫蛇添足,為什麼不把這功能做在建構子?

    疑問:
    1. ToString("F")是啥? 為什麼要string to string ?

    回覆刪除
    回覆
    1. ToString("F")是將輸出字串顯示至小數點以下兩位數。舉例:4→4.00

      刪除
  5. 第八組
    介面強大 !!
    cBox_shape.Text 是字串 但可以考慮用 cBox_shape.SelectedIndex (int)來做判斷
    而一些需要兩個的參數跟兩個參數的可以考慮一起判斷 (球和正方體or金字塔跟長方體)之類的
    然後加入失敗的狀況可以寫在外面
    if(ShapeArray[amount] == null) ...
    另外輸出視窗可以嘗試使用ListView就可以TextBox省去對齊的麻煩

    回覆刪除