使用Linked List Java解决空中交通管制计划问题

时间:2012-07-30 17:02:00

标签: java linked-list implementation

我已经在这个项目上工作了几个星期,这是我在Java编程课程中的最后一个项目,它给了我(以及很多其他学生)一些非常好的头痛。 我们需要创建一个程序,允许用户输入,显示和删除新飞机以及飞机的速度,高度和飞机类型。 让主要类与其他类进行通信时,我遇到的问题最多。因此,我不知道我的LinkedList是否能够正常工作,或者根本不知道。我担心列表不会将所有字段正确存储在一起,并且节点没有正确编码。 我真的可以使用你能提供的任何帮助或建议。代码如下。我愿意接受任何建议。代码不必保持与当前所在的完全相同的类。如果某些东西在其他地方更好用,我会很乐意尝试它。

主类。这是用户与程序交互的地方。我一直很难让其他类的方法在这个类中工作。我确信这是我想念的简单事。

package airTraffic;

import java.util.*;

public class Main {

static Scanner in = new Scanner(System.in);

public static void main(String[] args) {

    do {
        try {
            System.out.println("Please enter command to proceed: ");
            System.out.println("Enter new aircraft = e");
            System.out.println("Display all aircraft = d");
            System.out.println("Show specific flight = s");
            System.out.println("Remove specific flight = r");
            String command = in.next();
            in.next(command);

            if ( (in.next(command)).equals("e") ) {
                ATControl.addToList();  // need to somehow "start" this class 

            } else if ( (in.next(command)).equals("d") ) {
                ATControl.displayAll();

            } else if ( (in.next(command)).equals("s") ){
                ATControl.showFlight();

            } else if ( (in.next(command)).equals("r") ) {
                ATControl.removeFlight();

            } else if ( (in.next(command)).equals(null) ) {
            }
        } catch (InputMismatchException exc) {
            System.out.println("Wrong entry, please try again:");
        }
    } while (true);
}
}

链接列表和节点 - 我称之为飞机。我认为这是存储和创建列表的地方。对列表的操作发生在下一个类(ATControl)中,或者至少我认为它会。

package airTraffic;

import java.util.LinkedList;

public class Aircraft  {

// stores data
private static final int INITIAL_ALLOCATION = 20;
private int size = INITIAL_ALLOCATION; 

//declare LinkedList and node names
static LinkedList <String> list = new LinkedList <String> ();
private Aircraft head = new Aircraft ();
private Aircraft tail = new Aircraft ();

// tells list to add nodes
public  void addNodes (int n, LinkedList<String> s) {
    s = list;
    head.next = tail;
    tail.next = tail;
    size = n;
    Aircraft temp = head;
    for (int i= 0; i < size; ++i) {
        temp.next = new Aircraft ();
        temp = temp.next;
    }
    temp.next = tail;
}

private String value;
Aircraft craft;

public Aircraft (String v) {
    value = v;
}

public Aircraft () {

}

public String get () {
    return value;
}

public void set (String v) {
    value = v;
}

public Aircraft next = null;

//auto generated method from ATControl 
public static void add(String flight) {
    // a for or while loop might be needed here. Seems to easy to just have an empty add class

}
//auto generated method from ATControl
public static void remove() {

}
 }

ATControl类。这是(我认为)列表被操纵的地方,允许用户添加,删除和显示航班。

package airTraffic;

import java.util.*;

public class ATControl{

// implement Aircraft class (node) - empty argument list?? 
Aircraft aircraft = new Aircraft ();

static Scanner in = new Scanner (System.in);

// list of planes 
static String [] planeList = {"Wide-body Airliner = w", "Regional Airliner = r", "Private Plane = p", 
        "Military = m", "Cargo only: c", "Unknown = u"};

//add plane and details
public static void addToList () {
    System.out.printf("Enter flight number: ");
    String flight = in.nextLine();
    Aircraft.add(flight);

    //type of plane
    System.out.printf("Enter type of plane, ", "Choose from: " + planeList);
    String type = in.nextLine();
    try {
    if (type == "w") {
         System.out.println("Wide-body Airliner");
    }else if (type == "r") {
             System.out.println("Regional Airliner");
    }else if (type == "p") {
         System.out.println("Private Plane");
    }else if (type == "m") {
         System.out.println("Military");
    }else if (type == "c") {
         System.out.println("Cargo only");
    }else if (type == "u") {
         System.out.println("Unknown");
    } else type = null;
        }
    catch (InputMismatchException i) {
        System.out.println("You must enter valid command: " + planeList);
    }
    Aircraft.add(type);

    //plane speed
    System.out.printf("Enter current speed: ");
    String speed = in.nextLine();
    Aircraft.add(speed);

    //add Altitude 
    System.out.printf("Enter current altitude: ");
    String alt = in.nextLine();
    Aircraft.add(alt);
}

//show flight
public static void showFlight () {
    System.out.printf("Enter flight number for details: ");
    in.nextLine();
    Aircraft.get(Aircraft, index);
}

// display all flights
public static void displayAll () {
    System.out.printf("All flights: " );

}

//remove flight
public static void removeFlight () {
    System.out.printf("Enter flight number to be removed: ");
    in.nextLine();
    Aircraft.remove();
}   
}

有什么想法吗?谢谢!

3 个答案:

答案 0 :(得分:2)

要“启动”ATControl,您需要创建一个新实例:

ATControl control = new ATControl();
control.addToList();

与您的Aircraft相同。您需要使用new创建新实例,然后在其上调用Add()等。

您可能还希望将Scanner从main传递到新的ATControl并使用它来阅读输入,而不是使用全新的Scanner

答案 1 :(得分:0)

  • 您的一般设计似乎忽略了面向对象的原则。在我看来,飞机应该将其类型,速度和高度保持在一个物体中。

  • 为什么重新发明轮子?已经在JRE中构建了大量预制(且经过良好测试)的集合类(例如,ArrayList,LinkedList)。利用它们来保存您的飞机实例。

这两项更改应该会大幅减少您需要编写/维护的代码量,并且应该大大降低程序的整体复杂性。

答案 2 :(得分:0)

使用面向对象设计时,请将对象视为实际对象的表示。您的飞机等级应代表实际的飞机。飞机应该跟踪与之相关的事物。因此,航班号,速度,高度等等应该是该类的属性。

public class Aircraft{
    private int speed;
    private int altitude;
    private int flightNum;
    //
    //Regional Airliner, Military, Private Plane, etc.
    private String type;

    public void setSpeed(int speed){
        this.speed = speed;
    }
    public int getSpeed(){
        return speed;
    }
    //
    //TODO: Getters and Setters for the rest of the aircraft properties

}

现在几乎所有其他东西都应由你的ATControl类处理。在这种情况下,使用Aircraft类型的数组列表似乎更合乎逻辑。

public class ATControl{
    private ArrayList<Aircraft> currentFlights;

    //
    //Constructor gets called on initialization
    public ATControl(){
        currentFlights = new ArrayList<Aircraft>();
    }

    //
    //User input should be handled in main class and passed into this
    public void addFlight(int flightNum, int speed, int altitude){
        Aircraft newCraft = new Aircraft();

        //
        //assign the properties we just got from the user to our new aircraft
        newCraft.setFlightNumber(flightNum);
        newCraft.setSpeed(speed);
        //
        //Now add our new flight to the list of current flights
        currentFlights.add(newCraft);

    }


}

你的主要课程可以保持不变。不过,我会在那里处理所有用户输入。

public class Main {

    static Scanner in = new Scanner(System.in);


    public static void main(String[] args) {
       ATControl denverTrafficControl = new ATControl();
       //
       //Handle user input here: get the speed, altitude, flightNum, etc..

       denverTrafficControl.addFlight(flightNum, speed, altitude);

    }
}

这是应该怎么做的。希望这有助于您更好地掌握有针对性的导向设计。让主类处理I / O,而其他实际的“对象”类处理数据。祝你好运。