我们需要上两个课:girlscoutcheck和cookieanalyzercheck。我们需要得到用户的输入-要求他们输入每个女孩的名字,直到他们输入逃跑。然后我们问他们每个女孩卖的饼干盒的数量,然后分别加上每个女孩的饼干盒,再乘以3.50。这是总收入然后根据总收入按降序排序。我几乎什么都做了。我只需要帮助排序和打印排序的信息。有谁能帮我完成这件事吗?谢谢您。

package Pcs;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class CookieAnalyzerCheck {

public static void main(String args[]) {
    NumberFormat nf = NumberFormat.getCurrencyInstance();
    String name = "";
    ArrayList al = new ArrayList();

    do {
        Scanner kb = new Scanner(System.in);
        System.out.println("Please enter the name of each"
                + " girl Scout, or \"RUNAWAY\" "
                + "to exit.");
        name = kb.nextLine();
        if (!name.equalsIgnoreCase("runaway")) {
            System.out.println("Enter the number of Thin Mints sold");
            int ThinMints = kb.nextInt();
            System.out.println("Enter the number of Caramel Delights sold");
            int CaramelDelights = kb.nextInt();
            System.out.println("Enter the number of Lemonades sold");
            int Lemonades = kb.nextInt();
            System.out.println("Enter the number of ThanksALot sold");
            int ThanksALot = kb.nextInt();
            System.out.println("Enter the number of MangoCremes sold");
            int MangoCremes = kb.nextInt();
            GirlScoutCheck ba = new GirlScoutCheck(name, ThinMints, CaramelDelights, Lemonades, ThanksALot, MangoCremes);
            al.add(ba);
            System.out.println(""); //makes output easier to read
            System.out.println();
        }
    } while (!name.equalsIgnoreCase("runaway"));


    GirlScoutCheck thisBA = (GirlScoutCheck) al.get(0);

    double maxBalance = thisBA.totalIncome;
    String maxName = thisBA.name;

    for (int i = 1; i < al.size(); i++) {
        thisBA = (GirlScoutCheck) al.get(i);
        if (thisBA.totalIncome > maxBalance) {
            //we have a new winner, so save the..
            maxBalance = thisBA.totalIncome;
            maxName = thisBA.name;

          int a= al.size();




            }
        }
    }


}



package Pcs;

import java.util.Collections;



public class GirlScoutCheck implements Comparable {
public String name;
public int ThinMints;
public int CaramelDelights;
public int Lemonades;
public int ThanksALot;
public int MangoCremes;
public int totalCookies;
public double totalIncome;

public GirlScoutCheck(String nm, int tm,int cd, int lem, int tal, int mc){
    name = nm;
    ThinMints = tm;
    CaramelDelights = cd;
    Lemonades = lem;
    ThanksALot = tal;
    MangoCremes = mc;
    totalCookies = ThinMints+CaramelDelights+Lemonades+ThanksALot+MangoCremes;
    totalIncome = totalCookies*3.50;
}

public int totalCookies(){
    return totalCookies;

}

public double totalIncome(){
    return totalIncome;

}
  public int compareTo(Object done){
   GirlScoutCheck b= (GirlScoutCheck)done;
   if (totalIncome>b.totalIncome){
       return 1;
   }
       else if(totalIncome<b.totalIncome)  {
           return -1;

       }
       else{
           return 0;
   }


}


public String getName(){
        String nameParts[] = name.split(" ");
    String first = nameParts[0];
    String last = nameParts[1];

    return String.format(last + "," + first);
}








@Override
public String toString() {
    return "girlScoutCheck [name=" + getName() + ", ThinMints=" + ThinMints
            + ", CaramelDelights=" + CaramelDelights + ", Lemonades="
            + Lemonades + ", ThanksALot=" + ThanksALot + ", MangoCremes="
            + MangoCremes + ", totalCookies=" + totalCookies + "]"+ System.lineSeparator()+ " (total income for Thin Mints is $" +
                    ThinMints*3.50+ ") ( total income for Caramel Delights is $" +
                    CaramelDelights*3.50+ ") total income for Lemonades is $" +
                    Lemonades*3.50+ ")( total income for MangoCremes is $" +
                    MangoCremes*3.50+ ") (total income for ThanksALot is $" +
                    ThanksALot*3.50+ ")"+System.lineSeparator() ;

}


}

最佳答案

我认为你在main方法中遗漏了一些东西:
您的变量nf从未使用过
既然您想对GirlScoutCheck进行排序,我建议您使用TreeSet来存储它们,而不是ArrayListATreeSet是始终排序的元素集合(here is its javadoc entry)。
理想情况下,您应该声明并初始化该容器,如下所示:

final Set<GirlScoutCheck> al = new TreeSet<>();

通常,您应该始终关闭流和任何外部资源。id est:在离开Scanner方法之前关闭main
您可以简单地使用Try-With-Resource语法,如下所示:
try (final Scanner kb = new Scanner(System.in)) {
    do {
        // your stuff
        kb.nextLine();
    } while (!name.equalsIgnoreCase("runaway"));
}

顺便说一下,您可能需要在读取另一个女孩的名字之前使用最后一个换行符:我建议您在重新进入kb.nextLine();循环之前直接调用do/while(就像我在上面的try with resource示例中所做的那样)。
基本上,你的主要方法可以是:
final Set<GirlScoutCheck> al = new TreeSet<>();

try (final Scanner kb = new Scanner(System.in)) {
    do {
        // your stuff
        kb.nextLine();
    } while (!name.equalsIgnoreCase("runaway"));
}

// from now on, al will contain all the GirlScoutCheck's ordered "as you want"

for (final GirlScoutCheck gsc : al) {
    System.out.println(gsc);
}

还有就这样!
现在,实际上,要实现这个“自动排序”,我们需要声明如何比较您的GirlScoutCheck。您做的是正确的,使GirlScoutCheck实现Comparable:)
但是,您应该指定您正在实现Comparable<GirlScoutCheck>。因此,您要重写的compareTo方法的签名变成:
public int compareTo(final GirlScoutCheck other)

而不是:
public int compareTo(final Object other);

您也可以这样简化:
@Override
public int compareTo(final GirlScoutCheck o) {
    return (int) (o.totalIncome - this.totalIncome);
}

最后一点:
为了确保已排序的集合(如equals)表现良好,需要与TreeSet保持一致。
来源:Implementing compareTo - JavaPractices
因此,您还应该为您的equals实现hashcodeGirlScoutCheck,只使用totalIncome字段(与您的compareTo实现保持一致)。别担心,你的ide可能会帮你:)
无论如何,我也可以。所以这里是:
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    long temp;
    temp = Double.doubleToLongBits(this.totalIncome);
    result = (prime * result) + (int) (temp ^ (temp >>> 32));
    return result;
}

@Override
public boolean equals(final Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final GirlScoutCheck other = (GirlScoutCheck) obj;
    if (Double.doubleToLongBits(this.totalIncome) != Double.doubleToLongBits(other.totalIncome)) {
        return false;
    }
    return true;
}

TL;博士
只是…
把代码塞进你的课堂,
用我给你的方法替换它的方法,
一定要让它实现GirlScoutCheck,而不仅仅是compareTo
按照我的建议修改Comparable<GirlScoutCheck>方法。。。
一切都会好起来的;)

08-06 04:18