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 购物_工厂模式_
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//3.开始写事件了
private void button1_Click(object sender, EventArgs e)
{
try
{
double t1 = Convert.ToDouble(textBox1.Text);
double t2 = Convert.ToDouble(textBox2.Text);
label7.Text = (t1 * t2).ToString();
}
catch
{
MessageBox.Show("请输入数量或单价");
}
MyClass mc = GongChang.Createclass(comboBox1.SelectedItem.ToString());
label4.Text = mc.at(Convert.ToDouble(label7.Text)).ToString();
}
}
//2.建一个工厂类:
//用这个类 调用其他类中的函数
public class GongChang
{
public static MyClass Createclass(string c)
{
switch (c)
{
case "正常收费":
return new MyClass1();
case "打8折":
return new MyClass2(0.8);
case "满300送50":
return new MyClass3(300, 50);
default:
return null;
}
}
}
//1.首先三种付款方式的类
//创建个抽象类,使用抽象方法传入没有付款方式时的价钱
//之后重写抽象方法,更改价钱(三种付款方式)
public abstract class MyClass
{
public abstract double at(double money);
}
//三种方式:
//正常收费
public class MyClass1 : MyClass
{
public override double at(double money)
{
return money;
}
}
//打折
public class MyClass2 : MyClass
{
private double dazhe = 1;
public MyClass2(double dze)
{
dazhe = dze;
}
public override double at(double money)
{
return money * dazhe;
}
} //满300免50
public class MyClass3 : MyClass
{
private double x = 0;
private double y = 0;
public MyClass3(double _x, double _y)
{
x = _x;
y = _y;
}
public override double at(double money)
{
if (money > x)
{
return money - Math.Floor(money / x) * y;//300的倍数*50
}
else
return money;
}
}
}