Pair programming Project--Elevator dispatching

Source: Internet
Author: User

Elevator Dispatch

Existing new office building, a total of 21 floors, a total of four elevators, all elevators basic parameters are shown in the following table:

Elevator number

Service Floors available

Maximum number of passengers

Maximum Load weight

1

All floors

10

Approx.

2

Single layer

10

Approx.

3

Double

20

Approx.

4

All floors

20

Kg

The terms of use are as follows:

1, the floor number is 0~20, of which No. 0 is the basement layer;

2. Elevators with floor restrictions are not docked in the response floor, such as a single double layer;

3, all elevators with unified button control

Please design and implement an elevator control program according to the above requirements, if there is a graphical display is better.

Partner Introduction and evaluation:

This pair programming project (Elevator dispatch) My partner is Liu Chengying, this is his blog link http://www.cnblogs.com/tiezhu.

After two people's consultations, the final decision division is: i Editor Cheng, his Master review. Here are some of the snippets from our work:

Although my knot to the partner base is somewhat weak (in fact, I do not), but in this project, I still clearly feel that the ability of the other side is worthy of affirmation, work attitude is very serious, patience, understanding is high, but also often can put forward some constructive views. Moreover, I would like to admit that the other side is more careful than I (my eyes really do not), often can point out some errors (I often cut into the Chinese input method, resulting in the text characters in the confusion and compile error). Finally also to say the other side of the shortcomings, because this time I use the Java language, the other side has not been contacted before, so the process of communication also encountered a lot of obstacles.

Requirements analysis and simple ideas:

The so-called elevator scheduling, I think is under limited conditions, to do their best to reduce the waiting time for passengers, while allowing more passengers to take the elevator. But if you think about it, there are still a lot of things to think about (I was a fool when I first saw the phrase "if there was a graphic display better").

1. Four elevators, and the information of the four elevators are different (number of passengers, load capacity). The more special point is the 2, 3rd, this has a floor limit elevator, mention the floor limit, I think of our school library elevator (1-6 floor nonstop), should or single double-decker more difficult. The initial idea here is to differentiate this information by numbering the different (1-4), feeling that this should be moot.

2. Scheduling problem: A total of 21 floors, 0 represents the ground floor. First of all, to consider the initial state, there is no problem to feel either the first 4 elevators are parked at the bottom of the ground (that is, the ground floor) or at the beginning are parked in the lobby (1 floor, after all, 1 floor people are often the most). Then I combined the particularity of the 4 elevators, think of an optimal initial position namely: Elevator No. 1th was originally parked on the 1 floor (the most common elevator), elevator 2nd was originally parked on the 7 floor (single-storey elevator), elevator 3rd was originally parked on the 14 floor (double-decker elevator), 4th elevators parked in the 0 floor (ground floor, the highest carrying capacity, suitable for transporting goods , here I guess the big weight of the goods should be in the basement ... Although I feel that this arrangement of 1, 7, 14, 0 not only play the advantages of the respective elevators and can guarantee the shortest start of the scheduling time. But later on, there seems to be no such arrangement in reality. Then the important thing is the elevator scheduling algorithm, about in this elevator scheduling algorithm, I think for a long time, and in the process also constantly changing their ideas, because I think it is complex, some problems are completely contradictory, difficult to choose. For example, 1th elevator in the 3 floor is idle, elevator 2nd is carrying people from the bottom up to the end of the 7 floor, when the 2nd elevator just to rise to the 3 floor, 5 floor someone pressed ↑ button. So this is the free elevator to the No. 1th? Or is it just on the way to just a single-storey, 2nd-stop elevator? And so on, this kind of tangled problem is endless. In the end I can only give up in the endless tangle, perhaps really I think too much. You should start with something simple. The floor that triggers the external key is the request floor, and the internal floor key is the target floor, and the elevator is currently on the floor. Here I think the most basic algorithm is to let (request floor-the current floor) the smallest to respond to the external button, the same priority (the destination floor-the current floor) the smallest to respond to internal keys (different people have different destination floor).

3. Interface problem: This is really very difficult ah, I have never done any interface before, the elevator is moving, if used to let the graphics "moving" is the biggest problem. In the end, I really don't understand. "All elevators Use Unified button control" what does that mean, a button how to control the internal request of 4 elevators, 21 floors of external requests.

implementation process:

The beginning is still relatively smooth, all kinds of class first out. But the problem that could not be solved later came ... The following is a short piece of code for the original failed work:

class elevatorinfo{int elevator_num; int Elevator_service;int elevator_maxpassenger;int elevator_maxweight;public elevatorinfo (int a,int b,int C,int d) { This. Elevator_num=a;this. elevator_service=b;//Service Floor: 0 for all, 1 for single layer, 2 for double layer. This. Elevator_maxpassenger=c;this. Elevator_maxweight=d;}}            Class Elevatorstatus{int Num;int currentpassengers;int currentweight;int currentfloor;int Status;            Elevator operation Status: 0 for Stop, 1 for up,-1 for down int Arrive; Whether the elevator reached the target floor: 1 for arrival, 0 for not arriving public elevatorstatus (int n) {switch (n) {case 1:this. Currentfloor=1;break;case 2:this. Currentfloor=7;break;case 3:this. Currentfloor=14;break;case 4:this. Currentfloor=0;break;} This. Num=n;this. Currentpassengers=0;this. Currentweight=0;this. Status=0;this. arrive=0;}} Class Requestinfo{int Requestfloor;int upordown;int passengers;int Weight;} 

Perhaps because I have just started to learn Java, the level is really too food, followed by http://www.cnblogs.com/vamei/archive/2013/03/31/2991531.html this site tutorial random writing, the more write more like C language, Later study for a long time, without multi-threading at all can not let 4 elevators simultaneously operation, however I do not, go online Baidu, also did not understand, and interface part is not proficient. It is not written out, and eventually this "first draft" will add up to 200 rows or so, there is no obvious Java appearance, can only realize the console input some passengers, weight, external request floor What then output a call elevator information. Later through my unremitting all kinds of Baidu, check the book. "Learn from" the various elevator procedures in the multi-threaded and interface parts. After two or three days of additions and deletions were finally completed. But the problem is still very large, multi-threaded or do not understand, there is no elevator single-layer problem, the elevator between the number of passengers and the capacity of the difference between the load (here I feel may need each thread is not the same, good trouble, have to compromise) but I think it doesn't matter, this project really let me learn a lot. So the focus is on the summary.

Summary and experience:

1.Java is a very obvious object-oriented feature.

2. After a slightly larger program, which requires a lot of variable names to be defined, carefully design their names, otherwise it is often easy to confuse. It is best to translate it into English.

3. In the interface, a variety of common such as frame, Panel, Button, textfiled and so on.

4. After creating a new window, remember to add setvisible (true) to make it appear. Instead, setvisible (false) can be used to hide it.

5.setBounds and SetLayout, the former since the positioning and definition of size setbounds (int x,int y,int width,int height), the latter defines the layout method, Common setlayout (NULL) and setlayout (New FlowLayout ())

6. Event monitoring mechanism and common mouse events, keyboard events. The following is attached to a program of code, which contains the window frame, text boxes, buttons, mouse events to implement the pop-up window.

Class Passengerandweightaction implements ActionListener {                                                               //Enter the number and weight of the private JFrame F1;            Private JFrame f2;private TextField tf1;private TextField tf2;private TextField tf3;private Button but;public void Actionp Erformed (ActionEvent e) {f1 = new JFrame ("Please enter the number of passengers and weight"); F2 = new JFrame ("error"); F1.setlayout (new FlowLayout ()); f1.setbounds (450,350,400,100); f1.setvisible (true); F1.setf2.setBounds (450,350,260,70); TF1 = new TextField; TF2 = new TextField TF3 = new TextField (+); Tf3.settext ("The number or weight exceeds the limit, please return to re-enter!"); but = New button ("Confirmation"); F1.add (But); F1.add (TF1); F1.add (TF2); F2.add (TF3); But.addmouselistener (new Mouseadapter () {  public void mouseclicked (MouseEvent e) {int p = integer.parseint (Tf1.gettext ()); int w = Integer.parseint (Tf2.gettext ()); if (p>20| | w>2000) {f2.setvisible (true);} else  {f1.dispose ();}}); } }

7. in Elipse, remember to use Ctrl+shift+o for one click Import, such as various java.awt and javax.swing

8. In some mouse and keyboard events because the arguments passed are of type string, if you want to get the int type information in the text box, you can use the Integer.parseint (Textfield.gettext ()) statement, for example:

But.addmouselistener (New Mouseadapter () {public void mouseclicked (MouseEvent e) {int p = integer.parseint (Tf1.gettext () ); int w = integer.parseint (Tf2.gettext ()); if (p>20| | w>2000) {f2.setvisible (true);} else  {f1.dispose ();}});

9.Java in package and interface, Public,private difference. Implements usage.

The inheritance extends in Java, and the inheritance of interfaces. Although multiple inheritance is not supported, it can be implemented with interfaces.

Class Upaction implements Actionlistener{private int floor;public upaction (int i) {floor = i;} public void actionperformed (ActionEvent e) {bup[floor].setforeground (color.red); bup[floor].setenabled (false); Foundthread.addtask (floor, True);}} Class Downaction implements Actionlistener{private int floor;public downaction (int i) {floor = i;} public void actionperformed (ActionEvent e) {bdown[floor].setforeground (color.red); bdown[floor].setenabled (false); Foundthread.addtask (floor, false);}}

11. Most common: constructors and method overloading, composition, class data, and class methods.

Class Elevator extends Thread {                                                      //elevator run main class private int id;  
public void SetState ()}

12. If the defined variable name is confused and needs to be changed, you can select the variable name you want to change in Eclipse press Alt+shift+r to change all of the variable names in the program.

13. The most common mistake is to keep an eye on the problem of importing Chinese into the law; () and (), "" and "" and so on. Remember that programming must be careful.

This pair of programming harvest quite abundant, first of all to thank my partner's patience and careful review, help me to point out the need for a lot of mistakes and improvements in the place. I feel that throughout the process, our procedures are improved step-by-step, until the final molding, without the exchange of discussions between each other, but also to both sides have learned a lot of new knowledge. Our cooperation in this pair of programming projects is very enjoyable. Although the final finished product does not fully meet all requirements, but this project is important to let a beginner Java I consolidated a lot of basic knowledge and also learned a lot of new knowledge, I believe this project to my Java learning has a great help. More importantly, let me know more about the end of the programming, the real experience of a pair of programming, learned how to better work with others to complete a project.

           

Pair programming Project--Elevator dispatching

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.