我很确定我必须使用集合,但老实说,我对如何实现它感到非常迷茫!我的任务需要我
“允许用户查看按价格(从最低到最高)、学名(按属字母排序)或通用名称(按第一个单词的第一个字母排序)排序的植物。”
这是我的代码,我不知道在哪里准确放置排序以及如何在代码中编写它:(
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