结对编程之电梯

电梯调度

结对伙伴:朱民福http://http://www.cnblogs.com/zhumf/

现有一新建办公大厦,共有21层,共有四部电梯,所有电梯基本参数如下表所示:

电梯编号

可服务楼层

最大乘客数量

最大载重量

1

全部楼层

10

800 kg

2

单层

10

800 kg

3

双层

20

1600 kg

4

全部楼层

20

2000 kg

      其使用规定如下:

      1、楼层号为0~20,其中0号为地下一层;

      2、有楼层限制的电梯不在响应楼层停靠,如单双层;

      3、所有电梯采用统一按钮控制

      请根据上述要求设计并实现一个电梯控制程序,如果有图形显示就更好了。

程序分析

首先运行,中间的textbox显示电梯的初始楼层,下边的button是用户所在楼层,最上边的是选择使用的电梯号。

假设人在15层,点击15,电梯是向上走的,置or为0。

public static class quanju
        {
            public static int flot = 0;//dianti suo zai louceng!
            public static int flag = 0;//运行状态
            public static int num_flot = 0;
            public static int or = 2;//0为上!1为下!2为静止
            public static int flot1 = 0;
            public static int flot2 = 0;
            public static int flot3 = 2;
            public static int flot4 = 0;
        }

比电梯所在楼层的楼层按钮低的不响应。

到达15层,开门!并且重置禁止响应的楼层按钮。

实现是通过在窗口中添加4个timer控件,在控件中分别控制对应的电梯运行,其中的语句在4个timer事件中差不多

private void timer1_Tick(object sender, EventArgs e)
        {
            if (quanju.flag == 0)//没到
            {
                int a = quanju.flot;
                quanju.num_flot = a;
                if (a > quanju.flot1 && quanju.flot1 < 21)
                {
                    quanju.or = 0;//正在向上
                    textBox1.Text = Convert.ToString(quanju.flot1);
                    quanju.flot1++;
                }
                else if (a < quanju.flot1 && quanju.flot1 > -1)
                {
                    quanju.or = 1;//正在向下
                    textBox1.Text = Convert.ToString(quanju.flot1);
                    quanju.flot1--;
                }
                else if (a == quanju.flot1)
                {
                    quanju.or = 2;//正在开门
                    textBox1.Text = Convert.ToString(quanju.flot1);
                    timer1.Enabled = false;//关闭计时器
                    quanju.flag = 1;
                }
            }
            if (quanju.flag == 1)//到达指定楼层
            {
                textBox1.Text = "正在开门! ";
                quanju.flag = 0;//可以运行
                quanju.or = 2;//静止
                button2.Enabled = true;
                button6.Enabled = true;
                button7.Enabled = true;
                button8.Enabled = true;
            }
        }

然后就是电梯如果是向上状态,应该禁止响应比电梯楼层低的楼层按钮。

private void button2_Click(object sender, EventArgs e)
        {
            if(quanju.or==0)   //shang
            {
                //比当前楼层低的按钮不响应
                if(int.Parse(button2.Text)<=quanju.flot1)
                {
                    button2.Enabled = false;
                }  
            }
            else if (quanju.or == 1)//xia
            {
                //比当前楼层高的按钮不响应
                if (int.Parse(button2.Text) >= quanju.flot1)
                {
                    button2.Enabled = false;
                }
            }
            else  //静止状态传送本按钮楼层
            { 
                quanju.flot = int.Parse(button2.Text); 
            }
        }

最后就是上边的选择要使用的电梯号。事件为启动timer控件。

public void button1_Click(object sender, EventArgs e)
        {
                timer1.Enabled = true;
        }

总结

在这次结队编程过程中,我深刻体会到了团队编程的重要性,集体的智慧是无穷大的,这次编程朱民福是主要的,我跟着学了不少知识。

原文地址:https://www.cnblogs.com/Liyutang/p/5371320.html