首页 > 技术文章 > 结对项目电梯调度--项目实施

FakerWang 2014-03-16 20:43 原文

小组成员:汪洋,辛垧

在项目的需求分析做好的3天后,终于开始准备实现这些功能了。因为是面向对象的思路,不慌不忙地先将elevatro类和control两个类的属性写好,但其中的方法确实还要好好琢磨一下。现在想想,我们之前写的需求分析很水,只是将一些属性包括了,一些很重要的方法没有写到,甚至没有想到(run() 以及 stop() 两个方法。不过如果没有之前的需求分析把问题想更的全面,现在就跪了,所以需求分析一定要具体一点,详细一点,在此特别感谢小组伙伴小垧同学的辛勤的努力和付出。

好,现在回到主题。在写好类后,发现一个很棘手的问题,四部电梯,四个线程。而现在看多线程却很头疼的问题,所以偷了一下懒,改用timer这个控件来实现并发。宏观上是并发的,微观上还是串行的(当然即便是多线程在单处理器下也是这样),利用四个timer分别控制四部电梯。

首先生成电梯的四个对象,初始化四部电梯。

timer的enabled属性为true,intervel=2000,保证timer每隔2秒执行。

电梯的run()方法执行。

判断是否需要在电梯此时所在的楼层停留。

因为考虑到每层的上下人数,进行判断此时是否符合电梯运行的要求。

在此pose上run方法

 1 public void run()//run方法
 2         {
 3            
 4             if (El_statues)
 5             {
 6                 if (El_go_diraction == 1)
 7                 {
 8                     if (El_stay_floor < maxCount())
 9                     {
10                         El_stay_floor++;
11                     }
12                     else
13                     {
14                         El_go_diraction = -1;
15                     }
16                 }
17                 else if (El_go_diraction == -1)
18                 {
19                     if (El_stay_floor > 0)
20                     {
21                         El_stay_floor--;
22                     }
23                     else
24                     {
25                         El_go_diraction = 1;
26                     }
27                 }
28             }
29            
30  }
View Code

stop方法(判断在这一层电梯是否需要停下来)

 1 public Boolean stop()
 2         {
 3             if (El_run_count[El_stay_floor] == 1||Pe_req_count[El_stay_floor] ==1)
 4             {
 5                 El_run_count[El_stay_floor] = 0;
 6                 Pe_req_count[El_stay_floor] = 0;
 7                 return false; }
 8             else
 9                 return true;
10         }
View Code

同时一项很重要的判断方法,选择四部电梯此时到外部请求楼层时间最短的函数time

 1 public int Time(int Pe_stay_floor )
 2         {
 3             int i,count=0;
 4             int floor_gap=0;
 5             if (El_go_diraction == 1)
 6             {
 7                 if (Pe_stay_floor >= El_stay_floor)
 8                 {
 9                     floor_gap = Pe_stay_floor - El_stay_floor;
10                     //the floor which the elevator possibly stopped
11                     for (i = El_stay_floor; i <= Pe_stay_floor; i++)
12                     {
13                         if (El_run_count[i] == 1)
14                         {
15                             count++;
16                         }
17                     }
18                    
19                 }
20                 else
21                 {
22                     floor_gap = 2*maxCount() - El_stay_floor - Pe_stay_floor;
23                     //abc
24                     for (i = Pe_stay_floor; i <= 20; i++)
25                     {
26                         if (El_run_count[i] == 1)
27                             count++;
28                     }
29                     
30                 }
31             }
32             else if (El_go_diraction == -1)
33             {
34                 if (Pe_stay_floor <= El_stay_floor)
35                 {
36                     floor_gap = El_stay_floor - Pe_stay_floor;
37                     for (i = Pe_stay_floor; i <= El_stay_floor; i++)
38                     {
39                         if (El_run_count[i] == 1)
40                             count++;
41                     }
42               
43                 }
44                 else
45                 {
46                     floor_gap = El_stay_floor + Pe_stay_floor;
47                     for (i = 0; i <= Pe_stay_floor; i++)
48                     {
49                         if (El_run_count[i] == 1)
50                             count++;
51                     }
52                 }
53             }
54             El_alltime = floor_gap * El_run_time_one + count * El_stay_time;
55             return El_alltime;
56         }
57     }
View Code

同时在timer.tick()方法中是timer自动循环调用的部分:

 1 private void timer3_Tick(object sender, EventArgs e)
 2         {
 3             if (eq3.El_statues)
 4             {
 5                 eq3.run();
 6                 int temp = 0;
 7                 timer3.Enabled = (eq3.stop() && timer3.Enabled);
 8                 if (timer3.Enabled == false)
 9                 {
10                     label3.BackColor = Color.Red;
11                     //eq1.check2(Convert.ToInt32(listBox1.SelectedItem));
12                     temp = Convert.ToInt32(listBox3.SelectedItem);
13                     eq3.El_people = eq3.El_people + temp;
14                     eq3.El_weight = eq3.El_weight + temp * 60;
15                     if (eq3.El_people > eq3.El_st_people || eq3.El_weight > eq3.El_st_weight || eq3.El_people < 0)
16                     {
17                         timer3.Enabled = false;
18                         eq3.El_people = eq3.El_people - temp;
19                         eq3.El_weight = eq3.El_weight - temp * 60;
20                         textBox10.Text = eq3.El_people.ToString();
21                         textBox11.Text = eq3.El_weight.ToString();
22                         MessageBox.Show("3号电梯处于非正常状态!");
23                     }
24                     else
25                     {
26 
27                     }
28 
29                 }
30                 else
31                 { label3.BackColor = Color.Green; }
32 
33                 textBox3.Text = eq3.El_stay_floor.ToString();
34                 if (eq3.El_go_diraction == 1 && eq3.El_stay_floor != 0)
35                     textBox3.AppendText("  向上");
36                 else if (eq3.El_go_diraction == -1 && eq3.El_stay_floor != 0)
37                     textBox3.AppendText("  向下");
38                 else
39                     textBox3.AppendText("  静止");
40                 textBox10.Text = eq3.El_people.ToString();
41                 textBox11.Text = eq3.El_weight.ToString();
42             }
43         }

这只是一部电梯的部分,其余的与这一样一样的。

以上是我们小组项目电梯实现的核心代码。

现在pose界面设计的局部图片:

附录时间表:

时间 3.12 3.12 3.13 3.14上午 3.14下午 3.15上午 3.16上午 3.16下午
汪洋 对方法实现的构思 写好elevator类 学习多线程 换掉多线程使用timer控件 对控制stop,run的修改 讨论电梯的控制,并提出修改   完成alpha版
辛垧 写好control类 多线程编程 界面的设计与修改 界面的完善

推荐阅读