电梯系统程序

电梯程序

  • Test.java
  • Floor.java
  • Elevator.java
  • Input.java
  • RequestQueue.java
  • Request.java
  • Dispatcher.java

本学期写了一个电梯程序,写的不是很完善。

创建
电梯类(Elevator)楼层类(Floor)请求类(Request)请求队列类(RequestQueue)调度器类(Dispatcher)测试类(Test)

Test.java

package com.abc;

public class Test {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
          Dispatcher.dispatcher();
	}
}

Floor.java

package com.abc;

public class Floor {
	private Integer MaxFloor=10; //最高楼层
	private Integer MinFloor=0;  //最低楼层
	public Integer getMaxFloor() {
		return MaxFloor;
	}
	public void setMaxFloor(Integer MaxFloor) {
		this.MaxFloor = MaxFloor;
	}
	public Integer getMinFloor() {
		return MinFloor;
	}
	public void setiMinFloornumber(Integer MinFloor) {
		this.MinFloor = MinFloor;
	}
	
}

Elevator.java

package com.abc;

//电梯类
class Elevator {

	private static String Mode;
	private static double Sendtime;
	private static String Direction;
	private static Integer AimFloor;
	private static Integer NowFloor = 1;
	private static double RunningTime = 0;

	public static double getSendtime() {
		return Sendtime;
	}

	public static void setSendtime(double sendtime) {
		Sendtime = sendtime;
	}

	public String getMode() {
		return Mode;
	}

	public static void setMode(String mode) {
		Mode = mode;
	}

	public static String getDirection() {
		return Direction;
	}

	public static void setDirection(String direction) {
		Direction = direction;
	}

	public static Integer getAimFloor() {
		return AimFloor;
	}

	public static void setAimFloor(Integer aimFloor) {
		AimFloor = aimFloor;
	}

	public static Integer getNowFloor() {
		return NowFloor;
	}

	public static void setNowFloor(Integer nowFloor) {
		NowFloor = nowFloor;
	}

	public static double getRunningTime() {
		return RunningTime;
	}

	public static void setRunningTime(double runningTime) {
		RunningTime = runningTime;
	}

}

Input.java

package com.abc;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;

public class Input {


	String[] inputAcquisition(String s) {
	
		s = s.replaceAll("\\(|\\)", "");
		// 转换String字符串数组
		String[] s1 = s.split(",");
		return s1;
	}
	
	public LinkedList getList(){
		Scanner sc = new Scanner(System.in);
		LinkedList list = new LinkedList<>();
		
		while(true) {
	    String Command = sc.next();
		if(Command.equals("RUN")) {
			break;
		} else {
			list.add(Command);
		}

		}

		return list;
	}
	

}

RequestQueue.java

package com.abc;

import java.util.LinkedList;
import java.util.Queue;

//请求队列
public class RequestQueue {
	private static Queue queue = new LinkedList(); 
	public void addRequest(Elevator elevator) {
		queue.offer(elevator);
	}

	public Elevator peek() {
		return queue.peek();
	}
	public Elevator poll() {
		return queue.poll();
	}
}

Request.java

package com.abc;

public class Request {

	public static String FRFloorchange(String TargetDirection, Integer AimFloor, double Sendtime,Integer NowFloor,double RunningTime) {
		// Elevator elevator = new Elevator();
//		 = Elevator.getNowFloor();
//		 = Elevator.getRunningTime();
//		RequestQueue requestQueue = new RequestQueue();
//		
//          System.out.println(requestQueue.poll().getRunningTime());
//          System.out.println(requestQueue.poll().getAimFloor());
//          System.out.println(requestQueue.poll().getSendtime());
		if ((AimFloor == 1 && TargetDirection.equals("DOWN"))) {
			return "ERROR1";
		} else if ((AimFloor == 10 && TargetDirection.equals("UP"))) {
			return "ERROR2";
		} else if (RunningTime > Sendtime) {
			System.out.println(RunningTime + " " + Sendtime);
			return "ERROR3";
		} else {
			if (AimFloor == NowFloor) {
				Sendtime += 1f;
				RunningTime = Sendtime;
				Elevator.setRunningTime(RunningTime);
				Elevator.setNowFloor(NowFloor);
				return "STILL";
			}
			Sendtime += Math.abs(AimFloor - NowFloor) * 0.5;
			RunningTime = Sendtime;
			if (NowFloor < AimFloor) {
				NowFloor = AimFloor;
				Elevator.setNowFloor(NowFloor);
				Elevator.setRunningTime(RunningTime);
				return "UP";
			} else {
				NowFloor = AimFloor;
				Elevator.setNowFloor(NowFloor);
				Elevator.setRunningTime(RunningTime);
				return "DOWN";
			}
		}
	}

	public static String ERFloorchange(Integer AimFloor, double Sendtime) {
		// Elevator elevator = new Elevator();
		double RunningTime = Elevator.getRunningTime();
		Integer NowFloor = Elevator.getNowFloor();
		if (RunningTime > Sendtime) {
			return "ERROR3";
		} else if (AimFloor < 0 || AimFloor > 10) {
			return "ERROR4";
		}
		if (NowFloor == AimFloor) {
			Sendtime += 1f;
			RunningTime = Sendtime;
			Elevator.setNowFloor(NowFloor);
			Elevator.setRunningTime(RunningTime);
			return "STILL";
		} else {
			Sendtime += Math.abs(AimFloor - NowFloor) * 0.5;
			RunningTime = Sendtime;
			if (NowFloor > AimFloor) {
				NowFloor = AimFloor;
				Elevator.setNowFloor(NowFloor);
				Elevator.setRunningTime(RunningTime);
				return "DOWN";
			} else {
				NowFloor = AimFloor;
				Elevator.setNowFloor(NowFloor);
				Elevator.setRunningTime(RunningTime);
				return "UP";
			}
		}
	}

	public static void error() {
		RequestQueue requestQueue = new RequestQueue();
		while (true) {
			
			String sDirection;
			if (requestQueue.peek() == null) {
				break;
			} else if (requestQueue.peek().getMode().equals("FR")) {
//				System.out.println(requestQueue.peek().getNowFloor());
				requestQueue.poll();
				sDirection = Request.FRFloorchange(Elevator.getDirection(), Elevator.getAimFloor(),
						Elevator.getSendtime(),Elevator.getNowFloor(),Elevator.getRunningTime());
				if (sDirection.equals("ERROR1")) {
					System.out.println("ERROR");
					System.out.println("#1楼不能下。");
				} else if (sDirection.equals("ERROR2")) {
					System.out.println("ERROR");
					System.out.println("#顶楼不能上。");
				} else if (sDirection.equals("ERROR3")) {
					System.out.println("ERROR");
					System.out.println("#命令发送时间有误。");
				} else {
					System.out.println(
							"(" + Elevator.getNowFloor() + "," + sDirection + "," + Elevator.getRunningTime() + ")");
				}
				
			} else {
				 requestQueue.poll();
				sDirection = Request.ERFloorchange(Elevator.getAimFloor(), Elevator.getSendtime());
				if (sDirection.equals("ERROR3")) {
					System.out.println("ERROR");
					System.out.println("#命令发送时间有误。");
				} else if (sDirection.equals("ERROR4")) {
					System.out.println("ERROR");
					System.out.println("#命令目标楼层有误。");
				} else {
					System.out.println(
							"(" + Elevator.getNowFloor() + "," + sDirection + "," + Elevator.getRunningTime() + ")");
				}
			}
		}
	}
}

Dispatcher.java

package com.abc;

import java.util.LinkedList;

//调度器
class Dispatcher {
	 
	public static void dispatcher() {
		
		Input input = new Input();
		LinkedList list  = input.getList();
		RequestQueue requestQueue = new RequestQueue();
		while (true) {
			Elevator elevator = new Elevator();	
			 if(list.isEmpty()) {
				 list = input.getList();
			 }
			// System.out.println(list);
			// System.out.println("over");
			String Command[] = input.inputAcquisition(list.poll());
			// System.out.println(Command[0]);
			// 请求为外请求		
			if (Command[0].equals("FR")) {
				Elevator.setMode(Command[0]);
				Elevator.setAimFloor(Integer.valueOf(Command[1]));
				Elevator.setDirection(Command[2]);
				Elevator.setSendtime(Double.valueOf(Command[3]));
				requestQueue.addRequest(elevator);					
//				
			}
			// 请求为内请求
			else if (Command[0].equals("ER")) {
				Elevator.setMode(Command[0]);
				Elevator.setAimFloor(Integer.valueOf(Command[1]));
				Elevator.setSendtime(Double.valueOf(Command[2]));
				requestQueue.addRequest(elevator);
				
			} 
			Request.error();
		

		}
		
	}

	
}

你可能感兴趣的:(第一个)