Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I've been working on this elevator program for quite some time now and finally finished it and made it work and I'm pretty proud of myself, but I'd like to see ways how to optimize my code so I can learn for the future. By optimizing I mean, making the code look better, maybe by using less lines of codes.

Would be great if someone really took the code and gave a real example of how it could/should look like, thanks!

import java.awt.geom.*;

public class elevator
{

    static int floor = 0, choice1, person = 0;

    public static void main(String args[])
    {

        floor = ((int) (Math.random() * 10 + 1));

        System.out.println("The elevator is now on floor " +floor);
        System.out.print("Which floor are you at now (0-10) where 0 = basement: ");
        choice1 = Keyboard.readInt();

        if(floor == choice1)
        {
            System.out.println("Enter the elevator");
        }

        else if(floor > choice1)
        {
            ElevatorDown();
        }

        else if(floor < choice1)
        {
            ElevatorUp();
        }

        System.out.println("To which floor would you want to go (0-10) where 0 = basement");
        choice1 = Keyboard.readInt();

        if(floor > choice1)
        {
            ElevatorDown();
        }

        else if(floor < choice1)
        {
            ElevatorUp();
        }

    }

    public static void ElevatorUp()
    {
        System.out.println("The elevator is on it's way up...");

        for (person = choice1; choice1>=floor; floor++)

            System.out.println(floor);

        System.out.println("The elevator has arrived");
    }

    public static void ElevatorDown()
    {
        System.out.println("The elevator is on it's way down...");
        for (person = choice1; choice1<=floor; floor--)

            System.out.println(floor);

        System.out.println("The elevator has arrived");
    }
}
share|improve this question
1  
Maybe you could post the complete code of your class. – user10142 Jan 20 '12 at 10:06
1  
And please post Keyboard, too. – user10142 Jan 20 '12 at 10:07
I've posted my whole code now and I uploaded the Keyboard.class to speedyshare, not sure if there's a better place to upload it. speedy.sh/pGMx8/Keyboard.class – Lukas Jan 20 '12 at 10:14
Why not just use pastebin.com instead of some shitty warez spreading site? – user10142 Jan 20 '12 at 10:16
1  
Making the code look better (more readable) and using less lines of code are usually opposite aims in Java. – toto2 Jan 23 '12 at 19:23
show 6 more comments

2 Answers

import java.awt.geom.*;

public class elevator

Class names are typically capitalized

{

    static int floor = 0, choice1, person = 0;

Prefer local variables to statics like this. Person doesn't appear to be used.

    public static void main(String args[])
    {

        floor = ((int) (Math.random() * 10 + 1));

You've got an extra pair of parens that you don't need.

        System.out.println("The elevator is now on floor " +floor);
        System.out.print("Which floor are you at now (0-10) where 0 = basement: ");
        choice1 = Keyboard.readInt();

        if(floor == choice1)
        {
            System.out.println("Enter the elevator");
        }

        else if(floor > choice1)
        {
            ElevatorDown();
        }

        else if(floor < choice1)
        {
            ElevatorUp();
        }

Rather then keeping your choice in a static variable. Pass it as a parameter to your ElevatorUp()/ElevatorDown() Movement functions.

        System.out.println("To which floor would you want to go (0-10) where 0 = basement");
        choice1 = Keyboard.readInt();

        if(floor > choice1)
        {
            ElevatorDown();
        }

        else if(floor < choice1)
        {
            ElevatorUp();
        }

This is repeated from before. Write an ElevatorMove() function that decides whether to call ElevatorUp or ElevatorDown.

    }

    public static void ElevatorUp()
    {
        System.out.println("The elevator is on it's way up...");

        for (person = choice1; choice1>=floor; floor++)

Declare for loop variables in the loop not some other random place. And person? What does person have to do with it? In fact person=choice1 does nothing. I'd make this a while loop.

            System.out.println(floor);

        System.out.println("The elevator has arrived");
    }

    public static void ElevatorDown()
    {
        System.out.println("The elevator is on it's way down...");
        for (person = choice1; choice1<=floor; floor--)

            System.out.println(floor);

        System.out.println("The elevator has arrived");
    }

These two functions are similiar. They can be combined. }

My reworking of your code:

import java.awt.geom.*;

public class elevator
{

    static int floor;

    public static void main(String args[])
    {

        floor = (int) (Math.random() * 10 + 1);

        System.out.println("The elevator is now on floor " +floor);
        System.out.print("Which floor are you at now (0-10) where 0 = basement: ");
        int current_floor = Keyboard.readInt();

        if(floor == current_floor)
        {
            System.out.println("Enter the elevator");
        }
        else
        {
            MoveElevator(current_floor);
        }


        System.out.println("To which floor would you want to go (0-10) where 0 = basement");
        int target_floor = Keyboard.readInt();

        MoveElevator(target_floor);
    }

    public static void MoveElevator(int target_floor)
    {
        int direction;
        if( target_floor > floor )
        {
            System.out.println("The elevator is on it's way up...");
            direction = 1;
        }else{
            System.out.println("The elevator is on it's way down...");
            direction = -1;
        }

        while(target_floor != floor)
        {
            floor += direction;
            System.out.println(floor);
        }

        System.out.println("The elevator has arrived");
    }
}
share|improve this answer

In object oriented programming, you typically ask yourself: which objects do I operate with, what can I do with them, and what are their properties? In your case:

  • There is an elevator, and there could possibly be many elevators, which are all similar.
  • An elevator always has a current floor where it currently is.
  • That current floor can change when the elevator is in action.
  • One typical action is to move up.
  • Another typical action is to move down.
  • When a person wants to use the elevator, the elevator moves to the floor where the person is. In general, the elevator moves to a certain floor.

And here is the code, based on the description above:

public class Elevator {

  private int currentFloor;

  public void run() {
    currentFloor = 1 + ((int) (Math.random() * 10));

    System.out.println("The elevator is now on floor " + currentFloor);
    System.out.print("Which floor are you at now (0-10) where 0 = basement: ");
    int personFloor = Keyboard.readInt();
    moveTo(personFloor);
    System.out.println("Enter the elevator");

    System.out.println("To which floor do you want to go (0-10) where 0 = basement");
    int destinationFloor = Keyboard.readInt();
    moveTo(destinationFloor);
    System.out.println("Leave the elevator");
  }

  private void moveTo(int destinationFloor) {
    if (destinationFloor == currentFloor) {
      /* nothing to do */
    } else if (destinationFloor > currentFloor) {
      moveUpTo(destinationFloor);
    } else {
      moveDownTo(destinationFloor);
    }
  }

  private void moveUpTo(int destinationFloor) {
    System.out.println("The elevator is on its way up ...");
    while (currentFloor < destinationFloor) {
      currentFloor++;
      System.out.println(currentFloor);
    }
    System.out.println("The elevator has arrived");
  }

  private void moveDownTo(int destinationFloor) {
    System.out.println("The elevator is on its way down ...");
    while (currentFloor > destinationFloor) {
      currentFloor--;
      System.out.println(currentFloor);
    }
    System.out.println("The elevator has arrived");
  }

  public static void main(String[] args) {
    new Elevator().run();
  }

}

By the way, the elevator is on its way, not on it's way.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.