public abstract class Lift {
//층수는 고정되어야 하니까 스테틱으로 사용
static int floor =0;
abstract void up();
abstract void down();
abstract void start(int choice);
abstract void stop();
abstract void go();
}
public class Elevator extends Lift{
final int maxFloor=10;
final int minFloor= -3;
@Override
void up() {
floor++;
}
@Override
void down() {
floor--;
}
@Override
void start(int choice) {
if(choice < floor) {
for (int i = 0; i < floor-choice+i; i++) {
if(floor != 0) {
System.out.println(floor+"층");
try {
Thread.sleep(1000);
}catch(InterruptedException e) {}
}
down();
}
stop();
}else if(choice != floor) {
for (int i = 0; i <= choice - floor+i; i++) {
if(floor!=0) {
System.out.println(floor+"층");
}
up();
}
stop();
}else {
System.out.println("같은 층 선택불가");
}
}
@Override
void stop() {
System.out.println("$$도착$$");
}
@Override
void go() {
int choice =0;
String msg="";
while(true){
msg="층수를 입력하세요(현재층 :" + floor + "층)";
System.out.println(msg);
choice=new Scanner(System.in).nextInt();
if(choice > maxFloor || choice < minFloor) {
System.out.println("지하3층 부터 지상 10층 까지 운행합니다.");
}else {
break;
}
}
start(choice);
}
}
public class Building {
public static void main(String[] args) {
Elevator e = new Elevator();
Random r = new Random();
int cnt;
int floor=0;
String check="";
int[] arElevator = new int[5];
cnt = r.nextInt(6);
for (int i = 0; i < cnt; i++) {
arElevator[i]=1;
}
while(true) {
//최대 :10층
//최저 :-3층
floor=r.nextInt(e.maxFloor+(e.minFloor*-1)+1)+e.minFloor;//14가 나와야함
if(floor!=0) break;
}
if(cnt==0) {
System.out.println("현재 탑승 인원 : 없음(최대5명)");
}else {
System.out.println("현재 탑승 인원 :" + cnt + "명(최대5명)");
}
System.out.println("Y : 타기 N: 보내기");
check = new Scanner(System.in).next().toUpperCase();//소문자를 입력해도 대문자로 변경해줌
if(check.equals("Y")) {
cnt++;
try {
arElevator[cnt-1] = 1;
Elevator.floor = floor;
e.go();
} catch (Exception e1) {
System.out.println("정원초과");
}
}
}
}