所有
下面的代码可以正常使用ArrayList。你可以帮我解决一下如何获取name性别和amountSpent(数组大小[4])的用户输入,然后用空格分割,以便它有String,String和double。
此外,如何仅显示花费较高的客户的结果,然后显示其他客户的结果。
提前谢谢你!
此致
Viku
import java.util.Comparator;
public class Customer implements Comparable <Customer>{
public String name,gender;
public double amountSpent;
public Customer(String name, String gender, double amountSpent) {
super();
this.name = name;
this.gender = gender;
this.amountSpent = amountSpent;
}
public String getCustomername() {
return name;
}
public void setCoustomername(String name) {
this.name = name;
}
public String getgender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public double getamountSpent() {
return amountSpent;
}
public void setamountSpent(double amountSpent) {
this.amountSpent = amountSpent;
}
public static Comparator <Customer> CustomerNameComparator = new Comparator<Customer>() {
public int compare(Customer c1, Customer c2) {
String custName1 = c1.getCustomername().toUpperCase();
String custName2 = c2.getCustomername().toUpperCase();
//ascending order
//return custName1.compareTo(custName2);
//descending order
return custName2.compareTo(custName1);
}
};
public static Comparator <Customer> CustomerAmountSpentComparator = new Comparator<Customer>() {
public int compare(Customer aS1, Customer aS2) {
int custamtspent1 = (int) aS1.getamountSpent();
int custamtSpent2 = (int) aS2.getamountSpent();
//ascending order sort
// return custamtspent1 - custamtSpent2;
//descending order sort
return custamtSpent2 - custamtspent1;
}
};
@Override
public int compareTo(Customer o) {
return 0;
}
@Override
public String toString() {
return " Customer Name : " + name + ", Gender : " + gender + ", Amount Spent : " + amountSpent + "";
}
}
和主要计划:
import java.util.ArrayList;
import java.util.Collections;
public class MainProg {
public static void main(String args[]){
String nL = System.lineSeparator();
try {
ArrayList<Customer> arraylist = new ArrayList<Customer> ();
arraylist.add(new Customer ("Louis","Male", 4567.76));
arraylist.add(new Customer ("Daniela","Female", 7653.67));
arraylist.add(new Customer ("Jenny","Female", 3476.98));
arraylist.add(new Customer ("Arijit","Male", 9876.44));
System.out.println("Customer Name Decending Sort: " + nL);
Collections.sort(arraylist, Customer.CustomerNameComparator);
for (Customer str: arraylist) {
System.out.println(str);
}
System.out.println(nL + "Custmer Amount Spent [Hight to Low] Sorting: " + nL);
Collections.sort(arraylist, Customer.CustomerAmountSpentComparator);
for (Customer str: arraylist){
System.out.println(str);
}
System.out.println(nL + "Highest Amount Spent Custmer Detail: " + nL);
}
catch (Exception e){
System.out.println("Error: " + e);
}
finally {
System.out.println(nL + "Report Completed!");
}
}
}
答案 0 :(得分:0)
对于您的第一个问题(如果我已理解正确),您希望将用户输入作为字符串:
戴夫男123.45
然后将其解析为两个字符串和一个双精度数。你提到的扫描仪是一个很好的开始,然后尝试
String [] parts = input.split(“”);
double value = Double.parseDouble(parts [2]);
这会将输入拆分为大小为3的String数组,并将第三个元素转换为double对象,这样就可以创建Customers。
对于第二个问题,您可以通过迭代ArrayList中的所有Customers来使用一种简单的方法,并以最高的金额存储当前客户。如果您找到更大的消费者,请更换客户。
答案 1 :(得分:0)
Viku,尝试移动
arraylist.add(新客户(name,gender,amountSpent));
到
之内do{
...
arraylist.add(new Customer(name,gender,amountSpent));
} while(choice.equalsIgnoreCase("Yes"));
现在代码是,arrayList.add只在循环后执行 - &gt;只有最后一次进入。
答案 2 :(得分:0)
选项1(建议):
如果用户要输入数据,为什么需要将其拆分?请按以下步骤操作:
System.out.println("Name:");
name = scn.nextLine();
System.out.println("Gender:");
gender = scn.nextLine():
System.out.println("Amt:");
amt = scn.nextDouble();
Customer c1 = new Customer (name,gender, amt);
选项2:
或者,如果您希望用户在一行中输入所有内容(以空格分隔),请执行以下操作:
System.out.println("Input name, gender, amt:");
name = scn.next();
gender = scn.next():
amt = scn.next();
Customer c1 = new Customer (name,gender, amt);
节目输出:输入姓名,性别,amt: John Male 33.50
选项3(由您提出要求):
最后,如果您仍然坚持按空格进行拆分,并且您希望接受以空格分隔的一个字符串中的用户输入:
System.out.println("Input name, gender, amt:");
input = scn.nextLine();
String[] token = input.split(" ");
String name = token[0];
String gender = token[1];
double amt = Double.parseDouble(token[2]);
Customer c1 = new Customer (name,gender, amt);
节目输出:输入姓名,性别,amt: John Male 33.50