2017-08-02 17 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 + 
       '}'; 
    } 
} 

私は、これはスパゲッティコードですけど、これが私の最後の手段です。どんな助けでも謝罪されるでしょう!

+0

あなたの例を明確にしてください、私はあなたの質問が妙に何を意味するのか分かりませんか? –

+0

私のプログラムはFoodオブジェクトのリンクリストを作成します(単独でリンクされています)。私は、リンクされたリスト内のオブジェクトを操作する方法を知りたいだけです。それらが配列リストに格納されていれば、for-eachループを使用します。リンクされたリストは反復可能ではないことを理解しています。だから、私は別の解決策が必要です。 –

+0

リンクされたリストを繰り返し処理しますか?次の要素の権利に? –

答えて

1

オブジェクトを操作するには、カスタムIteratorを作成する必要があります。私はここに何も犯罪者ではないと考えていますLinkedListソースを開いて、それがどのように動作するか見てください。

1

このような何かが、あなたは

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

がここに1つである、オンライン多くのリソースを見つけることができます。

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コアツールを使ってロジックを推薦しようとしますLinkedListArrayList ...)。ロジックは構造的な解決策に変換する必要があります。たとえば、

  • 入力ポイントは、指定された入力ストリームを処理するためにstream serviceを作成して呼び出します。
  • stream handlerは、builderを操作する必要があります。
  • builder結果はcompositeに集められなければならない。
  • のように...

あなたはもっと構造的な方法であなたのロジックを提供する場合は、問題を指して、より明確な質問をするでしょう。また、私はあなたの質問がこの準備の後に消えると信じています。 、builderfactory methodcompositestrategy


また、私は次のGoF patternsに慣れるためにあなたをお勧めします。