Write a program that simulates the movement of an elevator. Create class called button that abstracts a push button located in the elevator. Use an numerated type to represent its state either pressed or not pressed. Write functions to change the state,to retrieve the state. If button is pressed that is already in the pressed state, then display error message. Write a class called elevator that contains the data members: An array of button objects, An integer representing which floor the elevator is currently on, A constant integer representing the top floor for the elevator. Its value comes from the one formal argument in the elevator constructor function. Assume that the button floor is always number 1.Write a member function for the elevator class that allows the user to press any number of valid buttons. Assume that button number means that it is time to close the elevator doors. When this happens, the elevator must move to a floor that has a corresponding button pressed.
package elevator;
class Button{
private int pressed=1;
private int state;
private int not_pressed=0;
public void setState(){
state = not_pressed;
}
public void changeState(){
if(state == pressed){
System.out.println("Error message. Wrong press");
}
state = pressed;
}
public int getState(){
return state;
}
}
public class Elevator {
private Button buttons[];
private int floor;
private int top_floor;
Elevator(int max){
top_floor = max;
floor = 1;
buttons = new Button[top_floor];
}
public void userPressButton(){
floor++;
Button fl = buttons[floor];
}
public static void main(String[] args) {
Button t = new Button();
}
}
Comments
Leave a comment