Vous êtes sur la page 1sur 7

ELEVATOR PROGRAM

IN JAVA
import java.io.*;

class elevator
{
int currentfloor;
int headingfloor;
boolean movement;
boolean doors;

void goToFloor(int s)
{
headingfloor=s;
System.out.println("\nLift is heading to floor="+headingfloor);
}

void openDoors()
{
doors=true;
System.out.println("\nDoors are open\n");
}

void closeDoors()
{
doors=false;
System.out.println("\nDoors are close\n");
}

void goingUp()
{
movement=true;
}

void goingDown()
{
movement=false;
}

void print(int h)
{
currentfloor=h;
System.out.print("\n@@Heading please wait@@\n");
if(headingfloor>currentfloor)
{
System.out.print("\nMovement up\n");
}
else
{
System.out.print("\nMovement down\n");
}
}
}

class ElevatorProgram
{
public static void main(String args[]) throws IOException
{
elevator ob=new elevator();
elevator obdup=new elevator();
ob.openDoors();
System.out.print("\nPresent floor=");
BufferedReader stdin1 = new BufferedReader ( new InputStreamReader( System.in ) );
String h;
h=stdin1.readLine();
int a= Integer.parseInt(h);
System.out.print("\nDesired floor=");
BufferedReader stdin = new BufferedReader ( new InputStreamReader( System.in ) );
String l;
l=stdin.readLine();
int u= Integer.parseInt(l);
ob.goToFloor(u);

obdup.goingUp();
obdup.goingDown();
ob.print(a);
obdup.closeDoors();
}
}

01 public class Elevator {


02
03 @SuppressWarnings("unused")
04 private String Floors;
05 @SuppressWarnings("unused")
06 private String CurrentFloor;
07 private int floor = 1;
08
09 //constructor
10 fiveFloorBuilding( String floors, String currentfloor, int start )
11 {
12 Floors = floors ;
13 CurrentFloor = currentfloor ;
14 floor = start ;
15 }
16
17 changeFloor (String change, int change2)
18 {
19 ChangeFloor = change ;
20 Changefloorto = change2;
21
22 }
23
24 // methods
25 int getCurrentFloor()
26 {
27 return currentfloor ;
28 }
29
30 void changeFloor( int amount )
31 {
32 floor= amount ;
33 }
34
35 void changeFloor2( int amount )
36 {
37 floor = amount ;
38 }
39
40 void goToFloor (int i)
41 {
42 int i;
43 if (floor <= 5) {
44 floor =5;
45 else
46 floor =1;
47 }
48 }
49
50
51 public static void main(String[] args) {
52
53 fiveFloorBuilding floors = new fiveFloorBuilding(5);
54
55
56
57 // Check the current Floor
58 System.out.println("The elevator is currently on: " +
floors.getCurrentFloor());
59
60 // Change floor
61 floors.changeFloor(3);
62
63 // Check New Floor

64 System.out.println("The elevator is currently on: " +


floors.getCurrentFloor());
65
66 // Change to Floor 5
67
68 floors.changeFloor2(5);
69
70 // Check new Floor

71 System.out.println("The elevator is currently on: " +


floors.getCurrentFloor());
72
73 floors.changeFloor3(2);
74
75 //Check new floor

76 System.out.println("The elevator is currently on: " +


floors.getCurrentFloor());
77
78
79 CurrentFloor.floor = 5;

80 System.out.println("The elevator is currently on : " +


floors.floor);
81
82 }
83
84 }

1. public class elevator {


2. public static void main(String[] args) {
3. move one = new move();
4. System.out.println("Current floor is: " + one.getFloor());
5. System.out.println("Going up two floors.");
6. one.goUp();
7. one.goUp();
8. System.out.println("New floor is: " + one.getFloor());
9. }
10. }
11.
12. class move {
13. int floor;
14.
15. public void goDown(){
16. floor--;
17. }
18. public void goUp(){
19. floor++;
20. }
21. public int getFloor(){
22. return floor;
23. }
24. }

(Creating an elevator simulator) Write an applet that


simulates an elevator going up and down (see Figure
18.21).
Posted on May 23, 2012 by iJava2Admin

(Creating an elevator simulator) Write an applet that simulates an elevator going


up and down (see Figure 18.21). The buttons on the left indicate the floor where
the passenger is now located. The passenger must click a button on the left to
request that the elevator come to his or her floor. On entering the elevator, the
passenger clicks a button on the right to request that it go to the specified floor.
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Exercise18_13 extends JApplet
{
private JLabel jlblStatue = new JLabel("Status");
private Elevator elevator = new Elevator(this);

public void init()


{
ButtonPanel lb = new ButtonPanel(this.elevator, true);
ButtonPanel rb = new ButtonPanel(this.elevator, false);

setLayout(new BorderLayout());
add(this.jlblStatue, "North");
add(lb, "West");
add(this.elevator, "Center");
add(rb, "East");
}

public void setStatus(String status, Color color)


{
this.jlblStatue.setForeground(color);
this.jlblStatue.setText(status);
}
public static void main(String[] args)
{
JFrame frame = new JFrame("Exercise18_13: Elevator Simulation");

Exercise18_13 applet = new Exercise18_13();

frame.add(applet, "Center");

applet.init();
applet.start();

frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
class ButtonPanel extends JPanel
implements ActionListener
{
private Elevator elevator;
private boolean left;
private JButton[] b = new JButton[8];

ButtonPanel(Elevator elevator, boolean left)


{
this.elevator = elevator;
this.left = left;

setLayout(new GridLayout(8, 1, 0, 0));

setBackground(Color.blue);

for (int i = 8; i > 0; i--) {


add(this.b[(i - 1)] = = new JButton("F" + (char)(48 + i)));
}

for (int i = 0; i < 8; i++)


this.b[i].addActionListener(this);
}

public void actionPerformed(ActionEvent e)


{
String arg = e.getActionCommand();
if ((e.getSource() instanceof JButton))
this.elevator.move(arg.charAt(1) - '0', this.left);
}
}