2014-03-28 61 views
0

在我編寫的這個程序中,我必須打印出在商店中花費最多的客戶的名稱。我需要幫助搜索花費最多的客戶的數組列表。查找數組列表中的最大值

package bestcustomer; 
import java.util.*; 
/** 
* 
* @author muf15 
*/ 
public class BestCustomer { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     Scanner in = new Scanner(System.in); 
     ArrayList<Double> sales = new ArrayList<Double>(); 
     ArrayList<String> names = new ArrayList<String>(); 
     double salesAmount; 
     System.out.println("Enter the sales for first customer: "); 
     salesAmount = in.nextDouble(); 
     while(salesAmount !=0) 
     { 
      sales.add(salesAmount); 
      System.out.println("Enter customers name"); 
      names.add(in.next()); 
      System.out.println("Enter the next sales amount, 0 to exit: "); 
      salesAmount = in.nextDouble(); 
     } 
     String bestCustomer = nameOfBestCustomer(sales, names); 

    } 
    public static String nameOfBestCustomer(ArrayList<Double> sales, 
      ArrayList<String> customers) 
    { 
     String name = ""; 
     double maxSales; 



     return name; 
    } 


} 

回答

1

你應該在一個大概叫Customer類包裝這兩個字段,然後

使用Collections.max();

Collections.max(yourCollection, customComparator); 
1

你應該考慮客戶的一類,但是這將找到的名字你當前數據結構:

public static String nameOfBestCustomer(ArrayList<Double> sales, 
     ArrayList<String> customers) 
{ 
    String name = ""; 
    double maxSales = 0; 
    int index = -1; 

    for(int i = 0; i < sales.size(); i++) { 
     if(sales.get(i) > maxSales) { 
     index = i; 
     maxSales = sales.get(i); 
     } 
    } 

    if(index == -1) { 
     return null; //lists are empty 
    } 

    return customers.get(index); 
} 
0

我可能有點遲了.. 我認爲,如果你創建一個客戶類有兩個字段,namesale這將是更好的設計,如其他答案所述。 然後在BestCustomer中,您可以遍歷客戶列表,找到最高銷售額並返回名稱。 像這樣的BestCustomer

private ArrayList<Customer> customers = new ArrayList<Customer>(); 

public BestCustomer(){ 
    Scanner in = new Scanner(System.in); 
    double salesAmount; 
    System.out.println("Enter the sales for first customer: "); 
    salesAmount = in.nextDouble(); 
    while(salesAmount !=0) 
    { 
     System.out.println("Enter customers name"); 
     String name = in.next(); 
     customers.add(new Customer(name, salesAmount)); 
     System.out.println("Enter the next sales amount, 0 to exit: "); 
     salesAmount = in.nextDouble(); 
    } 
    String bestCustomer = nameOfBestCustomer(); 
    System.out.print(bestCustomer); 
} 

private double highestSale(){ 
    double highestSale = 0; 
    for(Customer c: customers) 
     if (c.getSales() > highestSale) 
      highestSale = c.getSales(); 

    return highestSale; 
} 

public String nameOfBestCustomer(){ 
    for (Customer c: customers) 
     if(c.matchSale(highestSale())) 
      return c.getName(); 
    return null; 
} 

}

,這是Customer

public class Customer { 
private String name; 
private double sales; 



public Customer(String name, double salesAmount) { 
    this.name = name; 
    sales = salesAmount; 
} 

public boolean matchSale(double sales){ 
    return this.sales == sales; 
} 

public double getSales(){ 
    return sales; 
} 

public String getName(){ 
    return name; 
} 

} 

我是初學者,所以我敢肯定有一個更有效的方式來做到這一點。另外我使用兩個getters,據我瞭解,它不是更好的設計..

+1

nameOfBestCustomer()應該只使用與highestSale()具有完全相同的代碼,除了您將存儲客戶,而不僅僅是銷售數字。然後返回客戶的姓名。實際上,如果客戶列表變得非常大,那麼您的nameOfBestCustomer方法將執行得非常差:對於列表中的每個客戶,您都在highestSale內遍歷整個列表。在名單上有100萬用戶的情況下,如果您只需要購買100萬美元,那麼您最終可以進行1萬億次(1億次)的比較! –