2015-05-05 70 views
0

這是我已經完成的問題的唯一部分,它只從用戶讀取一個值列表,然後打印所有並指向最大的投入。我很困惑如何將特定客戶的數組列表與他們的具體姓名組合在一起。 實際問題:此方法應返回銷售量最大的客戶的名稱。實現一個方法public static String nameOfBestCustomer(ArrayList <Double> sales,ArrayList <String <customers)

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
    ArrayList<Double> values = new ArrayList<Double>(); 

    System.out.println("Enter the customers purchase amount, Q to quit"); 
    Scanner in = new Scanner(System.in); 
    while(in.hasNextDouble()){ 
     values.add(in.nextDouble()); 
    } 

    double largest = values.get(0); 
    for (int i =1; i< values.size(); i++){ 
     if (values.get(i) > largest){ 
      largest = values.get(i); 
     } 

    } 
    for (double element : values){ 
     System.out.print(element); 
     if (element == largest){ 
      System.out.println(" <===largest value"); 


     } 
     System.out.println(); 
    } 
} 
+1

一個理想的解決方案將包括在名爲(例如)Customer的類中定義'customerName'和'periodSales'作爲實例字段。如果'Customer'實例具有自然順序,那麼你也應該爲它們定義一個compator。然後,可以很容易地在'List sales = new ArrayList ();'集合中操縱'Customer'對象。您提供的代碼可能會受益於更大的面向對象。 – scottb

回答

0

1:我們不知道如何在程序中設置客戶名稱;無論是用戶輸入還是以其他方式創建。

2:如果客戶可以容納多個輸入,解決了這個創造客戶類,並讓它保持一個字符串名稱和輸入

class Customer{ 
String name; 
List<Integer> inputs=new ArrayList<Integer>(); 
Integer max; 
public Customer(String name){ 
this.name=name 
} 
public void sort(){ 
    ///sort code here 
    //set variable max to the max value in the inputs array 
} 
} 

那麼您可以在創建客戶列表的最簡單方法您的主要類如下:

List<Customer> list=new ArrayList<Customer>(); 
Customer customer1=new Customer("Brian"); 
Customer customer2=new Customer("Asura") 

and then add order amount to any customer depending how you want. 
customer1.inputs.add(999) 
customer2.inputs.add(23123); 

然後您需要比較所有客戶;調用每個客戶的排序方法

for(Customer aCustomer:list) 
    aCustomer.sort(); 

您現在已經爲每個客戶對象設置了最大變量;現在比較它們;最簡單的方法是使用可比較性,但對於初學者來說可能過於先進,所以繼承人的方式不同。

Customer result; //will hold the object with highest input 
int maximum=0; 
for(Customer aCustomer:list) 
    if(aCustomer.max>maximum){ 
     result=aCustomer; 
     maximum=aCustomer.max 
} 

,我們現在有一個包含最大

System.out.println(result.name+" maximum:"+result.max); 
+0

這就是我遇到的問題。我可以創建另一個類似於此的代碼,但只需要輸入客戶。我還沒有創建客戶部分。我很困惑如何分配哪個客戶名稱與他們的價格相匹配。我的技術水平是非常適中的初學者。我想我會創建兩個陣列列表。指定數組中哪個空間的最大值爲,然後使用字符串arraylist中的相同空格匹配該空間。有可能是一個更簡單的方法來做到這一點,但我卡住了。 –

0

客戶對象。如果我理解你的問題正確,你希望將一些用戶名與雙打的列表關聯。這可以使用Map<String, List<Double>>完成。所以,你可以聲明它想:

Map<String, List<Double>> user2values = new HashMap<>(); 

,並使用以下方法添加值:

void addValue(String user, double value) { 
    List<Double> values = user2values.get(user); 
    if(values == null) { 
     values = new ArrayList<>(); 
     user2values.put(user, values); 
    } 
    values.add(value); 
} 

或者更短,如果你使用的是Java 8:

void addValue(String user, double value) { 
    user2values.computeIfAbsent(user, k -> new ArrayList<>()).add(value); 
} 

之後,你會具有按用戶分組的值。如果您使用的是Java 8

String getMaxValueUser() { 
    String bestUser = null; 
    double maxValue = Double.NEGATIVE_INFINITY; 
    for(Map.Entry<String, List<Double>> entry : user2values.entrySet()) { 
     for(Double value : entry.getValue()) { 
      if(value > maxValue) { 
       value = maxValue; 
       bestUser = entry.getKey(); 
      } 
     } 
    } 
    return bestUser; 
} 

或更短:

String getMaxValueUser() { 
    return user2values.entrySet().stream() 
      .max(Comparator.comparingDouble(entry -> entry.getValue() 
        .stream().mapToDouble(x -> x).max() 
        .orElse(Double.NEGATIVE_INFINITY))).map(Entry::getKey) 
      .orElse(null); 
} 
0

試試這個:

公共靜態字符串如果你要掃描它最大的價值,你可以做以下nameOfBestCustomer(ArrayList的銷售,ArrayList的客戶){

Double largestSale = 0.0; 
int largestIndex = 0; 

for(Double s: sales) { 
    largestSale = Math.max(largestSale, s); 
} 

largestIndex = sales.indexOf(largestSale); 

return customers.get(largestIndex); 

}

+0

這個答案只適用於假設客戶的名稱與其銷售相關的索引相同。 如: sales.get(0)用於customers.get(0) – Eight

相關問題