2013-09-25 23 views
0

我很確定我必須使用集合,但我如何實現它真的超級失去了!我的任務需要我我很難找到正確的方法來按價格排序

「允許用戶查看按價格(從最低到最高),科學名稱(按字母順序排列)或通用名稱(按第一個字母的首字母排序)的植物。」

這是我的代碼,我不知道在哪裏究竟把分揀以及如何將其寫在代碼:(

package plant.nursery.program; 

import java.util.Scanner; 
import java.util.ArrayList; 

public class PlantNurseryProgram { 

private double maxHeightInFt; 
private String commonName; 
private String scientificName; 
private double price; 
boolean isFragile; 

    public PlantNurseryProgram(String commonName, 
     String scientificName, double maxHeightInFt, double price, 
     boolean isFragile) { 
     this.maxHeightInFt = maxHeightInFt; 
     this.commonName = commonName; 
     this.scientificName = scientificName; 
     this.price = price; 
     this.isFragile = isFragile; 
    } 


    @Override 
    public String toString() { 
     return commonName + " (" + scientificName + ") " + "is " + 
      maxHeightInFt + " ft tall " + "it costs $" + price + " and " 
      + "isFragile = " + isFragile; 
    } 

    public String setName(String newName){ 
     commonName = newName; 
     return newName; 
    } 

    public String setSName(String newSName) 
    { 
     scientificName = newSName; 
     return scientificName; 
    } 

    public double setHeight(double newHeight){ 
    maxHeightInFt = newHeight; 
    return maxHeightInFt; 
    } 

    public double setPrice(double newPrice){ 
     price = newPrice; 
     return price; 
    } 

    public boolean setFragile(boolean newFragile){ 
     isFragile = newFragile; 
     return isFragile; 
    } 

public static void main(String[] args) {   
    Scanner plant = new Scanner(System.in); 
    boolean toContinue = true; 
    ArrayList<PlantNurseryProgram> plantNurseryAL = new ArrayList<>(); 

    do 
    { 
    System.out.println("What's the name of your plant"); 
    String commonName = plant.next(); 
    System.out.println("What's the scientific name for your plant?"); 
    String scientificName = plant.next(); 
    System.out.println("How tall is your plant?"); 
    double maxHeightInFt = plant.nextDouble(); 
    System.out.println("What's the price of your plant?"); 
    double price = plant.nextDouble(); 
    System.out.println("Is the plant fragile? If yes type(true), if no " 
      + "type (false)"); 
    boolean isFragile = plant.nextBoolean(); 
    System.out.println("Do you wish to continue?"); 
     toContinue = plant.nextBoolean(); 

    PlantNurseryProgram userPlant = new PlantNurseryProgram( 
      commonName, scientificName, maxHeightInFt, price, isFragile); 

    plantNurseryAL.add(userPlant); 

    System.out.println("Is all the information you entered correct?"); 
    boolean corrections = plant.nextBoolean(); 
    if(corrections == false) 
    { 
     System.out.println("What would you like to correct?" 
       + " 1. Name, 2. Scientific name, 3. Height, 4. Price" 
       + "5. Fragility?"); 
     int userChoice = plant.nextInt(); 
     if(userChoice == 1) 
     { 
      System.out.println("Please enter the correct name"); 
      String newName = plant.next(); 
      userPlant.setName(newName); 
      System.out.println(userPlant); 
     } 
     else if(userChoice == 2) 
     { 
      System.out.println("Please enter the correct scientific name"); 
      String newSName = plant.next(); 
      userPlant.setSName(newSName); 
      System.out.println(userPlant); 
     } 
     else if(userChoice == 3) 
     { 
      System.out.println("Please enter the correct height"); 
      double newHeight = plant.nextDouble(); 
      userPlant.setHeight(newHeight); 
      System.out.println(userPlant); 
     } 
     else if(userChoice == 4) 
     { 
      System.out.println("Please enter the correct price"); 
      double newPrice = plant.nextDouble(); 
      userPlant.setPrice(newPrice); 
      System.out.println(userPlant); 
     } 
     else if(userChoice == 5) 
     { 
      System.out.println("Please enter if the plant is fragile or not"); 
      boolean newFragile = plant.nextBoolean(); 
      userPlant.setFragile(newFragile); 
      System.out.println(userPlant); 
     }  
    } 
} while(toContinue == true); 

    for (PlantNurseryProgram plantNurseryProgram : plantNurseryAL) { 
      System.out.println(plantNurseryProgram); 
    } // end of for loop 
} //end of main 
} // end of class 

回答

1

你張貼太多在這裏。爲了弄清楚該怎麼做,你並不需要一個main方法或用戶界面。順便說,按照慣例,制定者是void方法。

你應該熟悉的java.util包集合可用的方法。或者你可以使用索引並查找sort。您會看到類Collections兩種sort方法。一個是時的情形類本質上是Comparable

public class TemperatureGauge implements Comparable<TemperatureGauge> {...} 

另一種是當一個類可以進行排序許多不同的方式,所以沒有定義一個標準的比較自然的方式。然後,您創建一個Comparator

public class CommonNameComparator implements Comparator<PlantNurseryProgram> { 
    public int compare(PlantNurseryProgram left, PlantNurseryProgram right) { 
     // Do something with left.getCommonName() and right.getCommonName(). 
     // It must return a negative, zero, or positive depending on whether 
     // left should come before, in the same place, or after right. 
     return /*anInteger*/; 
    } 
} 

重複該過程的科學名稱和價格。

要獲得額外榮譽,請爲每個比較器和排序過程編寫JUnit測試。

0

您可以使用集合類的set接口。在此之前,您應該在您的代碼中重寫equals和hashcode方法,並根據您希望縮短的字段。 在此之後,您可以創建PlantNurseryProgram的對象,並在set接口的一個子類中創建該對象。根據您的要求,您可以使用Treeset類。 Reemember還有其他幾種方法來做到這一點。使用設置你不能複製項目。

如果您的目的是在數據結構中插入數據之後進行排序,則使用java collection api中提供的使用排序方法。

請參閱下面的鏈接。

http://www.roseindia.net/java/jdk6/set-interface.shtml