ThoughtWorks思特沃克2018校園招聘之春招家庭作業 - 無人機Java實現

題目要求
如上題:本人用時2小時,單次通過(無任何調試)。
主類:

package com.wang.test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Homework {
    public static final String NORMAL = "正常";
    public static final String ABNORMAL = "故障";

    public static List<Fly> readFile(String path) {
        List<Fly> flys = new ArrayList<>(10);
        try {
            BufferedReader in = new BufferedReader(new FileReader(path));
            String line = null;
            String[] info = null;
            Fly fly = null;
            Fly lastFly = null;
            int[] xyz = null;
            int[] offsetXYZ = null;
            while ((line = in.readLine()) != null) {
                info = line.split(" ");
                fly = new Fly();
                fly.setId(info[0]);
                try {
                    xyz = new int[]{Integer.parseInt(info[1]), Integer.parseInt(info[2]), Integer.parseInt(info[3])};
                    fly.setXyz(xyz);
                    if (flys.size() != 0) {
                        offsetXYZ = new int[]{Integer.parseInt(info[4]), Integer.parseInt(info[5]), Integer.parseInt(info[6])};
                        fly.setOffsetXYZ(offsetXYZ);
                        String status = checkNormal(lastFly, fly);
                        fly.setStatus(status);
                    } else {
                        fly.setStatus(NORMAL);
                    }
                } catch (Exception e) {
                    fly.setStatus(ABNORMAL);
                }
                flys.add(fly);
                lastFly = fly;
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flys;
    }

    private static String checkNormal(Fly lastFly, Fly fly) {
        if (lastFly == null || fly == null) {
            return ABNORMAL;
        }
        if (ABNORMAL.equals(lastFly.getStatus())) {
            return ABNORMAL;
        }
        if (lastFly.getOffsetXYZ() == null) {
            return equalXYZ(lastFly.getXyz(), fly.getXyz());
        } else {
            return equalXYZ(lastFly.getXyz(), lastFly.getOffsetXYZ(), fly.getXyz());
        }
    }

    private static String equalXYZ(int[] xyz, int[] offsetXYZ, int[] xyz1) {
        for (int i = 0; i < xyz.length; i++) {
            if (xyz[i] + offsetXYZ[i] != xyz1[i]) {
                return ABNORMAL;
            }
        }
        return NORMAL;
    }

    private static String equalXYZ(int[] xyz, int[] xyz1) {
        for (int i = 0; i < xyz.length; i++) {
            if (xyz[i] != xyz1[i]) {
                return ABNORMAL;
            }
        }
        return NORMAL;
    }


    public static void main(String[] args) {
        int[] msgID = new int[]{2, 4, 100};
//        獲取系統路徑
        String filePath = Homework.class.getResource("/data.txt").getPath();
        List<Fly> flys = readFile(filePath);
        for (int i : msgID) {
            String result = getResult(i,flys);
            System.out.println(result);
        }
    }

    private static String getResult(int index, List<Fly> flys) {
        if (index > flys.size()) {
            return "Cannot find " + index;
        }
        Fly fly = flys.get(index);
        if (ABNORMAL.equals(fly.getStatus())) {
            return "Error :" + index;
        } else {
            StringBuilder sb = new StringBuilder();
            int[] xyz = fly.getXyz();
            int[] offsetXYZ = fly.getOffsetXYZ();
            for (int i = 0; i < xyz.length; i++) {
                sb.append(" ").append(xyz[i] + offsetXYZ[i]);
            }
            return fly.getId() + " " + index + sb.toString();
        }

    }

}

bean類

package com.wang.test;

public class Fly {
    private String id;
    private String status;
    private int[] xyz;
    private int[] offsetXYZ;

    public Fly(String id, int[] xyz, int[] offsetXYZ) {
        this.id = id;
        this.xyz = xyz;
        this.offsetXYZ = offsetXYZ;
    }

    @Override
    public String toString() {
        return id + " " + arrToString(xyz) + arrToString(offsetXYZ) + " " + status;
    }

    private static String arrToString(int[] arr) {
        if (arr == null) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            sb.append(arr[i]).append(" ");
        }
        return sb.toString();
    }


    public Fly() {

    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }


    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public int[] getXyz() {
        return xyz;
    }

    public void setXyz(int[] xyz) {
        this.xyz = xyz;
    }

    public int[] getOffsetXYZ() {
        return offsetXYZ;
    }

    public void setOffsetXYZ(int[] offsetXYZ) {
        this.offsetXYZ = offsetXYZ;
    }
}


測試數據data.txt
plane1 1 1 1
plane1 1 1 1 1 2 3
plane1 2 3 4 1 1 1
plane1 3 4 5
plane1 1 1 1 1 2 3
項目目錄:
在這裏插入圖片描述
通過截圖:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章