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
4

2 回答 2

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 测试。

于 2013-09-25T04:03:34.573 回答
0

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

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

请参阅以下链接。

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

于 2013-09-25T04:10:40.263 回答