2017-07-17 29 views
-4
public static void main(String[] args) { 

/*The following is the code to a Class containing two elements, name and boardFootage. I'll get the data from a Scanner, store it in an ArrayList in my Furniture object, then print out the items of the ArrayList in ascending order. 
*/ 

    class Furniture implements Comparable<Furniture> { 
     public String toString() { 
      return getName() + ": " + getBoardFootage() + "\n"; 
     } 

     private String name; 
     private double boardFootage; 

     Furniture() { 
      name = ""; 
      boardFootage = 0.0; 
     } 

     Furniture(String nameInput, double boardFootageInput) { 
      name = nameInput; 
      boardFootage = boardFootageInput; 
     } 


     public String getName() 
     { 
      Scanner keyboard = new Scanner(System.in); 
      String name = ""; 
      while (!name.equalsIgnoreCase("quit")) 
      { 
       name = keyboard.next(); 
       if (name == "quit") { 
        System.out.println("Project Summary"); 
        // PRINT LIST SORTED BY PROJECT SIZE FROM SMALLEST 
        // TO LARGEST USING Collections.sort METHOD 
       } 
      } 
      return name; 
     } 


     public void setName(String name) { 
      Scanner keyboard = new Scanner(System.in); 
      this.name = keyboard.next(); 
     } 


     public double getBoardFootage() { 
      Scanner keyboard = new Scanner(System.in); 
      double boardFootage = 0.0; 
      boardFootage = keyboard.nextDouble(); 
      return boardFootage; 
     } 


     public void setBoardFootage(double boardFottage) { 
      Scanner keyboard = new Scanner(System.in); 
      this.boardFootage = keyboard.nextDouble(); 
     } 

     public int compareTo(Furniture o) { 
      if (this.boardFootage < o.boardFootage) { 
       return -1; 
      } else if (this.boardFootage > o.boardFootage) { 
       return 1; 
      } 
      return this.name.compareTo(o.name); 
     } 
    } 



    Furniture furniture = new Furniture(); 

    ArrayList<Furniture> furnitureList = new ArrayList<Furniture>(); 
+2

あなたはまだ何かを試してみましたか? –

+0

@TimBiegeleisenはい、ご質問: – Tavo

+0

[Javaで文字列を比較するには?](https://stackoverflow.com/q/513832/1553851) – shmosel

答えて

1

あなたはいつでも好きですか?

//furnitureRef is a reference to a member of the list 
for(Furniture furnitureRef : furnitureList) { 
    //Do stuff with furnitureRef 
} 

これは宿題の質問のように思えるので、私はあなたが、少なくともいくつかを自分でやろうとすることなく、より多くの助けを与えることに慎重です。

また、一般に受け入れられている方法は、定義したすべてのオブジェクトに対してtoStringメソッドを作成することです。このToString関数のような、シリアライズされ、あなたのオブジェクトの文字列を返す必要があります:

class Car { 
    String make; 
    String model; 

    public String toString() { 
     return "{ make: " + make + " model: "+model+" }"; 
    } 
} 

これは、あなたが書くことができるようになる:

//furnitureRef is a reference to a member of the list 
for(Furniture furnitureRef : furnitureList) { 
    System.out.println(furnitureRef.toString()); 
} 
+3

単純なインデックスでない限り、変数に「i」という名前を付けないでください。 – shmosel

+0

@shmoselええ、十分に私が推測する。通常、私は彼らが実際に学生であれば、適切な例を設定し、例を怠ってはいけないと思う:^) – DavidBittner

+0

あなたは明らかにそこにあるにもかかわらず、 ':-) – paxdiablo

関連する問題