Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Elevator Sim
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; class Rextester { public static void main(String args[]) { System.out.println("Hello, World!"); //create object ElevatorOperations obj = new ElevatorOperations("mode1"); //set mode //getRequests obj.getRequests(); //process requests obj.processRequests(); obj.publishResults(); } } class Elevator{ public int floors_supported; private int position; public String direction; public int floors_travelled; public int getPosition(){ return position; } public void setPosition(int position){ this.position = position; } //returns distance (in floors) from current elevator position public int getFloorDistance(int floor){ return Math.abs(position-floor); } } class Passenger{ private int start_floor; private int end_floor; private int wait_time; private int travel_time; public Passenger(int start_floor,int end_floor){ this.start_floor = start_floor; this.end_floor = end_floor; } public int getStartFloor(){ return start_floor; } public void setStartFloor(int start_floor){ this.start_floor = start_floor; } public int getEndFloor(){ return end_floor; } public void setEndFloor(int end_floor){ this.end_floor = end_floor; } public int getWaitTime(){ return end_floor; } public int getTravelTime(){ return end_floor; } } class ElevatorOperations{ private String mode; List<Passenger> passengers; Elevator elevator; public ElevatorOperations(String mode){ this.mode = mode; passengers = new ArrayList<>(); elevator = new Elevator(); } public String getMode(){ return this.mode; } public void setMode(String mode){ this.mode = mode; } public void getRequests(){ //Read requests from STDIN/FileSystem String requests = "10:8-3;4-6;5-9;5-8"; //add elevator's initial floor elevator.setPosition(Integer.parseInt(requests.substring(0,requests.indexOf(":")))); System.out.println("Initial Pos: "+elevator.getPosition()); //get requests List<String> req_lst = new ArrayList<>(Arrays.asList(requests.substring(requests.indexOf(":")+1,requests.length()).split(";"))); //add passengers to list for(String entry:req_lst){ int start_floor = Integer.parseInt(entry.substring(0,entry.indexOf("-"))); int end_floor = Integer.parseInt(entry.substring(entry.indexOf("-")+1,entry.length())); passengers.add(new Passenger(start_floor,end_floor)); } } public void processRequests(){ int floor_count = 0; for(Passenger p:passengers){ floor_count+=elevator.getFloorDistance(p.getStartFloor()); elevator.setPosition(p.getStartFloor()); System.out.println("Elevator Pos: "+elevator.getPosition()); floor_count+=elevator.getFloorDistance(p.getEndFloor()); elevator.setPosition(p.getEndFloor()); System.out.println("Elevator Pos: "+elevator.getPosition()); } elevator.floors_travelled = floor_count; } public void publishResults(){ System.out.println("Mode "+mode); System.out.println("Floors travelled="+elevator.floors_travelled); } }
run
|
edit
|
history
|
help
0
sq-buzz
classwork
Compare Version Numbers
Java
Java Object structure dumper (iterative)
Alllocate and release memory
Central Inteligence Agency
// Java Coding Challenge - 08: Reversing a Number using String
Opr
Convert string array to lowercase.