2017-08-02 41 views
2

我成功創建了一個鏈表,但現在我遇到了麻煩。我需要將哪些方法添加到我的FoodList類中才能處理我的對象?例如,我需要讓用戶能夠選擇手動添加食物對象,以便打印膳食。另外,我不能使用java API中的任何集合類。這一切都必須定製。如何處理我的自定義鏈接列表中的對象?

public static void main(String[] args) { 
    FoodList list = new FoodList(); 
    boolean keepGoing = true; 
    int scanResultInt; 
    try 
    { 
     //I/O stream 
    FileReader fr = new FileReader("foodlist.txt"); 
    BufferedReader br = new BufferedReader(fr); 
    Scanner scan = new Scanner(br); 

     Food hold = new Food(); 
     while(scan.hasNext()){ 
      list.add(hold = new Food()); 
      String str = scan.next(); 
      //str = scan.next(); 
      hold.setName(str); 
      str = scan.next(); 
      hold.setGroup(str); 
      int cal = scan.nextInt(); 
      hold.setNumCal(cal); 
      double percent = scan.nextDouble(); 
      hold.setPercentDV(percent); 
      list.add(hold); 
     } 
     //System.out.println("" + list.toString()); 

     br.close(); //close I/O stream 
    } 
    catch(IOException e){ 
     System.err.println("I/O EXCEPTION: " + e.getMessage()); 
    } 
    Scanner scan2 = new Scanner(System.in); 
do { 
    System.out.println("---------------------------------------------------------"); 
    System.out.println("   Welcome to the Parkland Meal Selector"  ); 
    System.out.println("---------------------------------------------------------"); 
    System.out.println("Enter the number of the menu option you would like to select:"); 
    System.out.println("  1) List food database"); 
    System.out.println("  2) Create meal by manual selection"); 
    System.out.println("  3) Create meal by random selection"); 
    System.out.println("  4) Remove foods high in calories"); 
    System.out.println("  5) Exit"); 

    scanResultInt = scan2.nextInt(); 
    switch(scanResultInt) { 
     case 1: { 
      System.out.println("" + list.toString()); 
      break; 
     } 
     case 2: { 
      System.out.println("Create-A-Meal Menu\n"); 
      System.out.println("Enter the name of a food you would like to add:\n"); 
      String foodWanted = scan2.next(); 

      /*while(!= null){ 
       if(foodWanted.equals()); 
      }*/ 
      /*Food tmp; 
      for(tmp = head; tmp != null; tmp = tmp.next) 
      { 
       result += tmp.f; 
      } 
      return result;*/ 
     } 
     case 3: { 
      System.out.println("Create meal by random selection: \n"); 
      break; 
     } 
     case 4: { 
      System.out.println("Remove Food High In Calories: \n"); 
      break; 
     } 
     case 5: { 
      keepGoing = false; 
      break; 
     } 
    } 
} 
while(keepGoing); 
} 

這裏是我的鏈接列表:

public class FoodList { 

    // Class fields 
    private FoodNode head; 
    private int listCount; 

    // Private inner class 
    private class FoodNode 
    { 
     public Food f; 
     public FoodNode next; 
     public FoodNode(Food f) 
     { 
      this.f = f; 
      this.next = null; 
     } 
    } 


    // Constructor for LinkedList 
    public FoodList() 
    { 
     // Initialize start of the list 
     head = null; 
     listCount = 0; 
    } 

    // Add method (adds a reservation to the linked list) 
    public void add(Food f) 
    { 
     // Create a new ReservationNode 
     FoodNode node = new FoodNode(f); 

     // If this is the first node 
     if(head == null) 
      head = node; 
     else 
     { 
      FoodNode tmp = head; 
      while(tmp.next != null) 
       tmp = tmp.next; 
      tmp.next = node; 
     } 
     listCount++ 
    } 
    /*public boolean hasThatFood(String food){ 
     boolean haveThat = false; 
     FoodNode tmp; 
     for(tmp = head; tmp != null; tmp = tmp.next) 
     { 
      if (food == f.getName()); 
       haveThat = true; 
     } 
     return haveThat; 
    }*/ 
    /*public boolean hasNext(){ 
     boolean hasNext = false; 
     if(head != null) { 
      hasNext = true; 
      return hasNext; 
     } 

    }*/ 

    @Override 
    public String toString() { 
     String result = "My Foods:" + '\n'; 

     // Loop through all the reservation nodes 
     FoodNode tmp; 
     for(tmp = head; tmp != null; tmp = tmp.next) 
     { 
      result += tmp.f; 
     } 
     return result; 
    } 
} 

我的食品類

 public class Food { 
    private String name; 
    private String group; 
    private int numCal; 
    private double percentDV; 

    public Food() {//String name, String group, int numCal, double percentDV 
     /*this.name = name; 
     this.group = group; 
     this.numCal = numCal; 
     this.percentDV = percentDV;*/ 
     name = ""; 
     group = ""; 
     numCal = 0; 
     percentDV = 0.0; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getGroup() { 
     return group; 
    } 

    public void setGroup(String group) { 
     this.group = group; 
    } 

    public int getNumCal() { 
     return numCal; 
    } 

    public void setNumCal(int numCal) { 
     this.numCal = numCal; 
    } 

    public double getPercentDV() { 
     return percentDV; 
    } 

    public void setPercentDV(double percentDV) { 
     this.percentDV = percentDV; 
    } 
    @Override 
    public String toString() { 
     return "Food{" + 
       "name: '" + name + '\'' + 
       ", Food Group: '" + group + '\'' + 
       ", Calories: " + numCal + 
       ", Daily Percent: " + percentDV + 
       '}'; 
    } 
} 

我知道這是意大利麪條的代碼,但是這是我最後的手段。任何幫助將appriciated!

+0

你能否澄清你的例子,我沒有得到什麼激動人心的問題意味着什麼。請提供更好的解釋 –

+0

我的程序創建一個Food對象的鏈表(單鏈)。我只想知道如何操作鏈接列表中的對象。如果它們存儲在數組列表中,我會使用for-each循環。我明白鏈表雖然不是可迭代的。所以,我需要一個不同的解決方案。 –

+0

所以你想遍歷鏈表?到下一個元素的權利? –

回答

1

要對物體進行操作,您必須編寫自定義Iterator。我想這裏沒有任何罪惡來打開LinkedList來源,看看它是如何工作的。

1

這樣的事情,你可以在網上找到很多資源,

https://crunchify.com/how-to-implement-a-linkedlist-class-from-scratch-in-java/

這是一個。

public Object getElement(int index) 
{ 
    if (index < 0) 
     return null; 
    Node Current = null; 
    if (head != null) { 
     Current = head.getNext(); 
     for (int i = 0; i < index; i++) { 
      if (Current.getNext() == null) 
       return null; 

      Current = Current.getNext(); 
     } 
     return Current.getData(); 
    } 
    return Current; 

} 
1

您已經實現了一些複雜的類。它的內在邏輯不像你的問題那麼清楚。所以幾乎任何答案都不會涵蓋你的需求。

如果我願意,我會嘗試推薦使用java核心工具的邏輯(不需要實現類,以最佳方式實現LinkedList,ArrayList ...)。邏輯應該轉換成一些結構性解決方案。例如:

  • 輸入點創建並調用您的stream service來處理提供的輸入流;
  • stream handler應該操縱builder;
  • builder結果必須收集到composite;
  • 等等...

如果您在多個結構方式,你會問指向問題更清楚的問題提供你的邏輯。此外,我相信你的問題將在此準備後消失。


此外,我建議你來熟悉下一個GoF patternsbuilderfactory methodcompositestrategy

相關問題