我在寻找按价格排序的正确方法时遇到了很多麻烦

时间:2013-09-25 03:46:23

标签: sorting collections arraylist

我很确定我必须使用收藏夹,但我真的很失落如何实现它!我的任务需要我

“允许用户查看按价格(从最低到最高)排序的植物,科学名称(按类别按字母顺序排列)或通用名称(按第一个单词的首字母按字母顺序排列)。”

这是我的代码,我不知道在哪里准确地进行排序以及如何在代码中编写它:(

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

2 个答案:

答案 0 :(得分:1)

你在这里发帖太多了。要弄清楚该怎么做,您不需要main方法或用户界面。顺便说一下,传统上,setter是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测试。

答案 1 :(得分:0)

您可以使用集合类的set接口。首先,您应该使用您想要缩短的字段覆盖代码中的equals和hashcode方法。 在此之后,您可以在set interface的子类之一中创建PlantNurseryProgram的对象。根据您的要求,您可以使用Treeset类。 再说一下有几种其他的ddo方法。使用set你不能重复项目。

如果您的目的是在数据结构中插入数据后进行排序,请使用java collection api中提供的sort方法。

请参阅以下链接。

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