2012-07-30 33 views
0

我已經在這個程序上工作了幾個星期了,這是我的Java編程課程的最後一個項目,它一直給我(和很多其他學生)一些相當好的頭痛。 我們需要創建一個程序,允許用戶輸入,顯示和刪除新飛機以及飛機的速度,高度和飛機類型。 我一直在獲取主類與其他類進行通信時遇到的最多問題。正因爲如此,我不知道我的LinkedList是否能正常工作,或者根本沒有問題。我擔心列表不會正確存儲所有字段,並且節點編碼不正確。 我真的可以使用任何幫助或建議,你可以提供。代碼如下。我願意接受任何和所有建議。代碼不必停留在與它當前所在的類完全相同的類中。如果某件事情在其他地方能夠更好地發揮作用,我很樂意嘗試它。使用LinkedList的空中交通管制程序的麻煩Java

主類。這是用戶將與該程序進行交互的地方。我一直很難從其他班級的方法在這個班級工作。我相信這是一件簡單的事,我錯過了。

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(); 
} 
} 

任何想法?謝謝!

+0

的第一條建議是進行單元測試的單獨的塊。直到您確信它是穩定的,在您嘗試將所有內容一次性合併之前運行代碼爲止,您自己與LinkedList一起工作。設計一些複雜的情景,添加,刪除等一堆航班,然後檢查列表仍然是正確的。 – mellamokb 2012-07-30 17:04:28

+0

ATControl應該是靜態的使用它以這樣一種方式在你的主 – Alfabravo 2012-07-30 17:07:10

+0

我給你的一般技巧...... 1)使用ArrayList 2)平面類型可以是枚舉3)不要使用兩臺掃描儀讀取System.in 4)你從in.next()中創建的字符串 - 使用它而不是調用幾十個in.next()調用...你的程序將阻塞每個人,期待輸入。 – Shark 2012-07-30 17:20:21

回答

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類進行處理。在這種情況下,飛機類型的數組列表似乎更符合邏輯。

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和其他實際的「對象」類來處理數據。祝你好運。

+0

非常感謝,這確實讓我更有意義了。我希望我們可以像使用數組列表或鏈接列表一樣簡單,但部分任務是使用節點創建我們自己的鏈接列表。我最初有一個鏈接列表,但教授告訴我要擺脫它。 他傾向於使我們的功課比它需要更復雜 – mossy367 2012-07-31 20:35:47

2

「開始」 ATControl,你需要創建一個新的實例:

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

同樣的,你的Aircraft。你將需要創建新的實例new,然後調用Add()等。

你也可能會想從主通過你的Scanner到新ATControl,並用它來讀取輸入,而不是使用新Scanner

+0

感謝您的建議 - 迄今爲止,這一切都很有幫助。有一件事情讓我有點失落,但是我怎樣才能將掃描儀從Main傳遞到ATControl?我明白你在說什麼,但編碼是一回事。再次感謝! – mossy367 2012-07-31 16:28:58

+0

你應該改變功能INSITE ATControl: 公共靜態無效addToList(掃描儀中){ ... in.nextLine(); ... } 和你的主要稱之爲如下里面: control.addToList(中); – nolegs 2012-07-31 17:12:50

0
  • 你一般設計一個品牌似乎忽略了面向對象的原則。正如我所看到的,飛機應該把它的類型,速度和高度放在一起。

  • 爲什麼要重新發明輪子? JRE中已經內置了大量的預製(以及測試過的)集合類(例如ArrayList,LinkedList)。利用它們來保存你的飛機實例。

這兩個變化應該會大大減少您需要編寫/維護的代碼量,並且程序的整體複雜度應該大大降低。

+0

感謝指針,如果它忽略了面向對象的原則,那是因爲我對此很陌生,並且誠實地真的不知道我在做什麼。 當你說「飛機應該保持其類型」時 - 你究竟是什麼意思?我遇到麻煩的一件事就是弄清楚如何將所有東西放在一起。我們的教材沒有給出像我們目前的項目這樣的複雜例子的非常詳細的例子。 再次感謝您的反饋 – mossy367 2012-07-31 16:10:27