U E D R , A S I H C RSS

데블스캠프2011/다섯째날/How To Write Code Well/강소현,구자경

기본

Elevator.java

public class Elevator {

	private int floorMax;
	private int floorMin;
	private int current;
	private int hopeFloor;
	
	public Elevator(int i, int j) {
		floorMax = i;
		floorMin = j;
		if(1<=floorMin && 1>=floorMax)
			current = 1;
		else
			current = floorMin;
	}


	public void pressButten(int i, int k) {
		goTo(i);//있는 장소로 이동
		current = i;
		setHopeFloor(k);
		
	}

	public void goTo(int currentFloor) {
		if(currentFloor<= floorMax && currentFloor >= floorMin)
			current = currentFloor;
	}


	public int floor() {
		// TODO Auto-generated method stub
		return current;
	}


	public int getHopeFloor() {
		return hopeFloor;
	}


	public void setHopeFloor(int hopeFloor) {
		this.hopeFloor = hopeFloor;
	}
	
	

}

app.java

import static org.junit.Assert.*;

import org.junit.Test;

public class app {
	@Test
	public void testElevator() {
		final Elevator el = new Elevator(20, -10);//최상층, 최하층

		assertEquals(1, el.floor());//기본 층 == 1층

		el.pressButten(5, 15);//현재 위치, 이동하고픈 위치
		assertEquals(5, el.floor());//현재 위치 == 엘레베이터 층
		el.goTo(el.getHopeFloor());
		assertEquals(15, el.floor());
		
		
		el.pressButten(20, 1);
		assertEquals(20, el.floor());//현재 위치 == 엘레베이터 층
		el.goTo(el.getHopeFloor());//이동하고픈 위치 이동
		assertEquals(1, el.floor());

	}


}

활용 시도했으나

Elevator.java

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;


public class Elevator {

	private int floorMax;
	private int floorMin;
	private int peopleMax;
	private int floor;
	private int people;
	private Timer timer;
	private boolean direction;
	
	public Elevator(int i, int j, int k) {
		floorMax = i;
		floorMin = j;
		peopleMax = k;
		timer = new Timer();
	}

	public int floor() {
		return floor;
	}

	public void pressButten(int i, int j, int k) {
		if(people == 0){
			goTo(i);
		}
		
	}

	public int getCurrentFloor() {
		// TODO Auto-generated method stub
		return floor;
	}

	public void goTo(int currentFloor) {
		timer.scheduleAtFixedRate(new TimerTask(){

			@Override
			public void run() {
				if(direction){
					floor++;
				}else{
					floor--;
				}
			}
			
		}, new Date(), 1000);
		if(floor == currentFloor){
			timer.cancel();
		}
	}
	
	public void in(int i) {
		// TODO Auto-generated method stub
		
	}

	public void out(int i) {
		// TODO Auto-generated method stub
		
	}

}

app.java

import static org.junit.Assert.*;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import org.junit.Test;

public class app {
	@Test
	public void testElevator() {
		final Elevator el = new Elevator(20, -10, 2);//최상층, 최하층, 수용인원
		Timer timer = new Timer();
		timer.scheduleAtFixedRate(new TimerTask(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				System.out.println(el.floor());
			}
			
		}, new Date(), 1000);
		assertEquals(1, el.floor());//기본 층 == 1층

		el.in(1);
		el.pressButten(5, 1, 15);//현재 위치, 1: up, 0: down, 이동하고픈 위치
		//el.goTo(el.getCurrentFloor());//현재 층으로 이동
		el.in(2);
		assertEquals(5, el.floor());//현재 위치 == 엘레베이터 층
		
		
		el.pressButten(8, 1, 13);//현재 위치, 1: up, 0: down, 이동하고픈 위치
		assertEquals(8, el.floor());
		el.out(2);
		assertEquals(13, el.floor());
		el.out(1);
		assertEquals(15, el.floor());
		//if(el.checkSameDir()){//만약에 같은 방향으로 이동하고, 처음에 누른 사람이 이동하고픈 위치보다 미달인 곳에 있으면
			//el.goTo(el.getCurrentFloor());//두번째 사람이 있는 층으로 이동
			//assertEquals(8, el.floor());
			//for(int i=0; i<el.numOfPeople();i++)//사람 수만큼 엘레베이터 이동
				//el.goTo(el.closerFloor());//가까운 위치 이동
		//}
		

		/*
		assertEquals(15, el.floor());
		
		
		el.pressButten(20, 0, 1);
		el.goTo(el.getCurrentFloor());//현재 층으로 이동
		assertEquals(20, el.floor());//현재 위치 == 엘레베이터 층
		el.goTo(el.closerFloor());//이동하고픈 위치 이동
		assertEquals(1, el.floor());
		*/
	}


}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2011-07-01 19:07:02
Processing time 0.0877 sec