我们需要上两个课: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
来存储它们,而不是ArrayList
ATreeSet
是始终排序的元素集合(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
实现hashcode
和GirlScoutCheck
,只使用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>
方法。。。一切都会好起来的;)