第八次作业

代码:import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Elevator {
private int minFloor;
private int maxFloor;
private int currentFloor;
private String direction;
private int preFloor;
private int[] internalQueue;
private int internalIndex;
private String[] externalQueue;
private int externalIndex;

public Elevator(int minFloor, int maxFloor) {
this.minFloor = minFloor;
this.maxFloor = maxFloor;
this.currentFloor = 1;
this.preFloor = 1;
this.direction = "UP";
this.internalQueue = new int[100];
this.internalIndex = 0;
this.externalQueue = new String[100];
this.externalIndex = 0;
printStatus();
}

private void printStatus() {
System.out.println("Current Floor: " + currentFloor + " Direction: " + direction);
}

private boolean isEndCommand(String input) {
return input != null && input.equalsIgnoreCase("end");
}

private boolean isValidFormat(String input) {
return input.startsWith("<") && input.endsWith(">");
}

private boolean isUpDirection(String input) {
Pattern pattern = Pattern.compile("UP");
Matcher matcher = pattern.matcher(input);
return matcher.find();
}

public void handleRequests() {
Scanner scanner = new Scanner(System.in);
int min = scanner.nextInt();
int max = scanner.nextInt();
scanner.nextLine();

this.minFloor = min;
this.maxFloor = max;

String request;
while (true) {
request = scanner.nextLine();
if (isEndCommand(request)) {
break;
}
if (isValidFormat(request)) {
try {
String[] parts = request.substring(1, request.length() - 1).split(",");
int floor = Integer.parseInt(parts[0]);
if (floor < minFloor || floor > maxFloor) {
System.out.println("Invalid floor request. Please try again.");
continue;
}
if (parts.length == 1) {
addInternalRequest(floor);
} else {
addExternalRequest(request.substring(1, request.length() - 1));
}
} catch (NumberFormatException e) {
System.out.println("Invalid format. Please use correct syntax.");
}
} else {
System.out.println("Invalid format. Use or <floor,direction>.");
}
}

processRequests();
scanner.close();
}

private void addInternalRequest(int floor) {
internalQueue[internalIndex++] = floor;
}

private void addExternalRequest(String request) {
externalQueue[externalIndex++] = request;
}

private void processRequests() {
while (hasPendingRequests()) {
if (hasBothQueues()) {
handleMixedRequests();
} else if (hasInternalRequests()) {
handleInternalRequests();
} else if (hasExternalRequests()) {
handleExternalRequests();
}
}
}

private boolean hasPendingRequests() {
return internalIndex > 0 || externalIndex > 0;
}

private boolean hasBothQueues() {
return internalIndex > 0 && externalIndex > 0;
}

private boolean hasInternalRequests() {
return internalIndex > 0;
}

private boolean hasExternalRequests() {
return externalIndex > 0;
}

private void handleMixedRequests() {
int internalFirst = internalQueue[0];
String externalFirst = externalQueue[0];
int externalFloor = Integer.parseInt(externalFirst.split(",")[0]);
boolean externalIsUp = isUpDirection(externalFirst.split(",")[1]);

if (direction.equals("UP")) {
handleUpMixedRequests(internalFirst, externalFloor, externalIsUp);
} else {
handleDownMixedRequests(internalFirst, externalFloor, externalIsUp);
}
}

private void handleUpMixedRequests(int internalFloor, int externalFloor, boolean externalIsUp) {
if (internalFloor > currentFloor) { // 内部请求向上
if (externalIsUp && externalFloor < internalFloor) { // 外部同方向且楼层更低
processExternalRequest(externalFloor, "UP");
removeFirstExternalRequest();
} else { // 优先处理更高的内部请求
processInternalRequest(internalFloor);
removeFirstInternalRequest();
}
} else { // 内部请求向下(可能在反向时处理)
if (externalIsUp) { // 外部向上请求
processExternalRequest(externalFloor, "UP");
removeFirstExternalRequest();
} else { // 外部向下请求,比较楼层
if (externalFloor > internalFloor) {
processExternalRequest(externalFloor, "DOWN");
removeFirstExternalRequest();
} else {
processInternalRequest(internalFloor);
removeFirstInternalRequest();
}
}
}
}

private void handleDownMixedRequests(int internalFloor, int externalFloor, boolean externalIsUp) {
if (internalFloor < currentFloor) { // 内部请求向下
if (!externalIsUp && externalFloor > internalFloor) { // 外部同方向且楼层更高
processExternalRequest(externalFloor, "DOWN");
removeFirstExternalRequest();
} else { // 优先处理更低的内部请求
processInternalRequest(internalFloor);
removeFirstInternalRequest();
}
} else { // 内部请求向上(可能在反向时处理)
if (!externalIsUp) { // 外部向下请求
processExternalRequest(externalFloor, "DOWN");
removeFirstExternalRequest();
} else { // 外部向上请求,比较楼层
if (externalFloor < internalFloor) {
processExternalRequest(externalFloor, "UP");
removeFirstExternalRequest();
} else {
processInternalRequest(internalFloor);
removeFirstInternalRequest();
}
}
}
}

private void handleInternalRequests() {
int target = internalQueue[0];
processInternalRequest(target);
removeFirstInternalRequest();
}

private void handleExternalRequests() {
String request = externalQueue[0];
int floor = Integer.parseInt(request.split(",")[0]);
String dir = isUpDirection(request) ? "UP" : "DOWN";
processExternalRequest(floor, dir);
removeFirstExternalRequest();
}

private void processInternalRequest(int target) {
setDirection(target);
moveToTarget(target);
printArrival();
}

private void processExternalRequest(int target, String dir) {
setDirection(dir);
moveToTarget(target);
printArrival();
}

private void setDirection(int targetFloor) {
direction = targetFloor > currentFloor ? "UP" : "DOWN";
}

private void setDirection(String newDir) {
direction = newDir;
}

private void moveToTarget(int target) {
if (target > currentFloor) {
for (int i = currentFloor + 1; i <= target; i++) {
currentFloor = i;
printStatus();
}
} else {
for (int i = currentFloor - 1; i >= target; i--) {
currentFloor = i;
printStatus();
}
}
}

private void printArrival() {
System.out.println("Open Door # Floor " + currentFloor);
System.out.println("Close Door");
preFloor = currentFloor;
}

private void removeFirstInternalRequest() {
System.arraycopy(internalQueue, 1, internalQueue, 0, internalIndex - 1);
internalIndex--;
}

private void removeFirstExternalRequest() {
System.arraycopy(externalQueue, 1, externalQueue, 0, externalIndex - 1);
externalIndex--;
}
}

public class Main {
public static void main(String[] args) {
Elevator elevator = new Elevator(1, 10);
elevator.handleRequests();
}
}

分析:
1.初次编写大作业,编程意识较低,没有按照单一职责原则进行代码的编写,在电梯中设置了多个方法,而且在主要逻辑运算中频繁使用if else 语句,使代码的最大复杂度高于正常值,使最深层嵌套代码块数高达67,最复杂方法的行数达到47,方法结构太大没有遵循老师的要求!但庆幸的是只有两个方法的复杂度达到了9以上,如下图所示

无疑,这对于一个新手来说还是比较好的
2.在第一次大作业时,低估了作业难度没有按时开始,导致出现逻辑的些许错误使得没能通过测试点,代码可以正确输出PTA中的测试案例但是没能正确输出后续QQ群里所发测试样例,深究其原因,设置输出队列里的信息在每次到达内外部所输入的楼层后发现在通过已经通过的楼层(也就是第一次上升过的地方,第二次上升经过后会自动跳过,这也是在同一
个楼层关门后再请求开门但是没有开门的原因,比如<3,UP><3>在到达第三层后只会开关门一次)不会进行开关门,而且不会到达(比如<7,DOWN><3>在上升时应为第一次上升到
第十层已经经过了7,所以上升到第六层没有上升到第七层,而是直接到六后直接下到五)这是代码少了7楼开关门和和下到六层这三行输出使得样例没通过,但是,代码逻辑确实是
按照内外请求判断第一个而进行运行的。
3.正如第一个分析所说,编程意识较低在代码中尝试将操作全部集结到电梯类之中,这也是新手常犯的错误,虽然程序可以正常运行并对样例进行正确的作答(仅针对于PTA平台上的样例)但是没有考虑到代码的可读性和可修复性,后续代码出现的问题无法轻易地得出(即便没有再进行后续的迭代)。
4.注释问题。注释过少(加上大作业完成时间跨度大),导致代码出现逻辑错误后无法精准地找到问题所在即便知道了通过调试知晓了错误的逻辑(如第二点所说),但还是没能修正代码的逻辑,最后时刻头脑更是慌乱以至于最终放弃对待妈的修正。

posted @ 2025-04-20 19:53  樱花下了一整夜  阅读(44)  评论(0)    收藏  举报