OOP_HW08.rar
Form1.cs
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace OOP_HW08
{
public partial class Form1 : Form
{
List ShapeList = new List();
bool IsClean = false;
public Form1()
{
InitializeComponent();
}
private void btn_AddShape3D_Click(object sender, EventArgs e)
{
if (IsClean == false)
{
ShowList("Clean");
ShapeList.Clear();
IsClean = true;
}
switch (cmb_Shape.Text)
{
case "Ball":
Ball ball = new Ball(cmb_Material.SelectedIndex, double.Parse(txt_Radius.Text));
ShapeList.Add(ball);
break;
case "Cube":
Cube cube = new Cube(cmb_Material.SelectedIndex, double.Parse(txt_Radius.Text));
ShapeList.Add(cube);
break;
case "Cylinder":
Cylinder cylinder = new Cylinder(cmb_Material.SelectedIndex, double.Parse(txt_Radius.Text), double.Parse(txt_Height.Text));
ShapeList.Add(cylinder);
break;
case "Pyramid":
Pyramid pyramid = new Pyramid(cmb_Material.SelectedIndex, double.Parse(txt_Radius.Text), double.Parse(txt_Height.Text));
ShapeList.Add(pyramid);
break;
default:
MessageBox.Show("請選擇形狀!");
return;
}
ShapeList[ShapeList.Count - 1].Order = ShapeList.Count; //加入順序
txt_Result.AppendText(ShapeList[ShapeList.Count - 1].ShowNameAndDensity() + "\n"); //更新各型狀之數量
UpdateShapesAmount();
}
private void comBox_shape_SelectedIndexChanged(object sender, EventArgs e)
{
switch (cmb_Shape.SelectedIndex)
{
case 0:
lbl_radius.Text = "半徑";
lbl_height.Visible = false;
txt_Height.Visible = false;
break;
case 1:
lbl_radius.Text = "長度";
lbl_height.Visible = false;
txt_Height.Visible = false;
break;
case 2:
lbl_radius.Text = "半徑";
lbl_height.Visible = true;
txt_Height.Visible = true;
break;
case 3:
lbl_radius.Text = "長度";
lbl_height.Visible = true;
txt_Height.Visible = true;
break;
}
}
private void btn_cal_Click(object sender, EventArgs e)
{
Sort();
ShowList("Calculate");
IsClean = false;
}
private void UpdateShapesAmount()
{
txt_BallAmount.Text = Ball.Amount.ToString();
txt_CubeAmount.Text = Cube.Amount.ToString();
txt_CylinderAmount.Text = Cylinder.Amount.ToString();
txt_PyramidAmount.Text = Pyramid.Amount.ToString();
txt_amount.Text = ShapeList.Count.ToString();
}
private void ShowList(string Case) {
string paraline = "種類" + "\t" + "參數 1" + "\t" + "參數 2" + "\t" + "密度" + "\t";
string newline = "\n" + "====================================================" + "\n";
switch (Case)
{
case "Clean":
txt_Result.Text = (paraline + newline);
break;
case "Calculate":
txt_Result.AppendText(newline + "===================" + cmb_Sort.Text + "======================" + "\n");
txt_Result.AppendText(paraline + "體積" + "\t" + "質量" + "\t" + "滾動" + newline);
foreach (var shape in ShapeList)
{
IRollable rollable = shape as IRollable;
if (rollable != null)
txt_Result.AppendText(shape.ShowAllData() + "\t" + rollable.RollDist() + "\n");
else
txt_Result.AppendText(shape.ShowAllData());
}
break;
case "Delete":
txt_Result.AppendText(newline + "===================" + "資料已更新" + "======================" + "\n");
txt_Result.AppendText(paraline + newline);
foreach (var shape in ShapeList)
{
txt_Result.AppendText(shape.ShowNameAndDensity() + "\n"); //更新各型狀之數量
}
break;
default:
break;
}
}
private void btn_DeleteShape_Click(object sender, EventArgs e)
{
int order = int.Parse(txt_DeleteNum.Text);
if (order >= ShapeList.Count)
return;
ShapeList[order] = null;
GC.Collect();
GC.WaitForPendingFinalizers();
ShapeList.RemoveAt(order);
UpdateShapesAmount();
ShowList("Delete");
}
private void Sort()
{
switch (cmb_Sort.Text)
{
case "依加入順序":
ShapeList.Sort(SortByOrder);
break;
case "依形狀類別":
ShapeList.Sort(SortByType);
break;
case "依材質類別":
ShapeList.Sort(SortByMaterial);
break;
case "依體積大小":
ShapeList.Sort(SortByVolume);
break;
case "依質量大小":
ShapeList.Sort(SortByWeight);
break;
case "依滾動距離":
ShapeList.Sort(SortByRollDist);
break;
default:
return;
}
}
//Sort Fun.
private int SortByOrder(Shape3D a, Shape3D b)
{
if (a.Order > b.Order)
return 1;
return -1;
}
private int SortByType(Shape3D a, Shape3D b)
{
if (a.ShapeType > b.ShapeType)
return 1;
return -1;
}
private int SortByMaterial(Shape3D a, Shape3D b)
{
if (a.Density > b.Density)
return 1;
return -1;
}
private int SortByVolume(Shape3D a, Shape3D b)
{
if (a.Volume() > b.Volume())
return 1;
return -1;
}
private int SortByWeight(Shape3D a, Shape3D b)
{
if (a.Weight() > b.Weight())
return 1;
return -1;
}
private int SortByRollDist(Shape3D a, Shape3D b)
{
double RollDist1 = 0, RollDist2 = 0;
IRollable Ra = a as IRollable;
if (Ra != null)
RollDist1 = Ra.RollDist();
IRollable Rb = b as IRollable;
if (Rb != null)
RollDist2 = Rb.RollDist();
if (RollDist1 > RollDist2)
return 1;
return -1;
}
}
}
Shape3D.cs
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OOP_HW08
{
abstract class Shape3D
{
//Field
protected double density;
protected int order;
protected Shape3DTypes.Types shapeType;
//Constructors
public Shape3D()
{
density = 0;
}
public Shape3D(int d)
{
density = Material.mDensity[d];
}
//Property
public int Order
{
get { return order; }
set { order = value; }
}
public int ShapeType
{
get { return (int)shapeType; }
}
public double Density
{
get { return density; }
}
public abstract double Volume();
public double Weight()
{
return density * Volume();
}
protected abstract string ShapeName();
public string ShowNameAndDensity()
{
string str = "";
str += ShapeName();
str += "\t";
str += density.ToString("F");
return str;
}
public string ShowAllData()
{
string str = "";
str += ShowNameAndDensity();
str += "\t";
str += Volume().ToString("F");
str += "\t";
str += Weight().ToString("F");
str += "\n";
return str;
}
}
static class Shape3DTypes
{
public enum Types { Unknown = 0, Ball, Cube, Cylinder, Pyramid };
}
static class Material
{
public static double[] mDensity = { 2.7, 7.87, 11.3 };
}
}
Ball.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OOP_HW08
{
class Ball : Shape3D, IRollable
{
private double radius;
private static int amount = 0;
public Ball(int density, double radius)
: base(density)
{
this.radius = radius;
shapeType = Shape3DTypes.Types.Ball;
amount++;
}
//解構子
~Ball()
{
amount--;
}
public static int Amount
{
get { return amount; }
}
public override double Volume()
{
return 4.0 / 3 * Math.PI * radius * radius * radius;
}
protected override string ShapeName()
{
string str = "";
str += "Ball";
str += "\t";
str += radius.ToString("F");
str += "\t";
return str;
}
public double RollDist()
{
return radius * radius;
}
}
}
Cube.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OOP_HW08
{
class Cube : Shape3D
{
private double side;
private static int amount = 0;
public Cube(int density, double side)
: base(density)
{
this.side = side;
shapeType = Shape3DTypes.Types.Cube;
amount++;
}
~Cube()
{
amount--;
}
public static int Amount
{
get { return amount; }
}
public override double Volume()
{
return side * side * side;
}
protected override string ShapeName()
{
string str = "";
str += "Cube";
str += "\t";
str += side.ToString("F");
str += "\t";
return str;
}
}
}
Cylinder.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OOP_HW08
{
class Cylinder : Shape3D, IRollable
{
private double radius, height;
private static int amount = 0;
public Cylinder(int density, double radius, double height)
: base(density)
{
this.radius = radius;
this.height = height;
shapeType = Shape3DTypes.Types.Cylinder;
amount++;
}
~Cylinder()
{
amount--;
}
public static int Amount
{
get { return amount; }
}
public override double Volume()
{
return radius * radius * Math.PI * height;
}
protected override string ShapeName()
{
string str = "";
str += "Cylinder";
str += "\t";
str += radius.ToString("F");
str += "\t";
str += height.ToString("F");
return str;
}
public double RollDist()
{
return 5 * radius;
}
}
}
Pyramid.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OOP_HW08
{
class Pyramid : Shape3D
{
private double side, height;
private static int amount = 0;
public Pyramid(int density, double side, double height)
: base(density)
{
this.side = side;
this.height = height;
shapeType = Shape3DTypes.Types.Pyramid;
amount++;
}
~Pyramid()
{
amount--;
}
public static int Amount
{
get { return amount; }
}
public override double Volume()
{
return side * side * Math.PI * height;
}
protected override string ShapeName()
{
string str = "";
str += "Pyramid";
str += "\t";
str += side.ToString("F");
str += "\t";
str += height.ToString("F");
return str;
}
}
}
IRollable.cs
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OOP_HW08
{
interface IRollable
{
double RollDist();
}
}

沒有留言:
張貼留言