2016-03-07 83 views
0
public class Saleitem { 
    public Product product = null; 
    public int numberofproduct = 0; 

    static ArrayList<Saleitem> Saleitemarray = new ArrayList<Saleitem>();  
    static ArrayList<Integer[]> total = new ArrayList<Integer[]>(); 
//read the sales data 
public static void salesData() { 
    String SalesDataCSV = "SalesData.csv"; 
    BufferedReader br = null; 
    String line = ""; 
    String cvsSplitBy = ","; 
    System.out.println("\nThe Sales Data file has been opened\n"); 

    try { 
     int currentcustomer = 1; 
     int lastcustomer = 1; 
     double sum = 0; 

     br = new BufferedReader(new FileReader(SalesDataCSV)); 
     line = br.readLine(); 

     System.out.println("-----------------------------------------------"); 
     System.out.println("Sales Data File"); 
     System.out.println("Customer ID, Product ID, Number of Units"); 
     System.out.println("-----------------------------------------------"); 
     while ((line = br.readLine()) != null) { 
      String field[] = line.split(cvsSplitBy);   
      if(field.length>1) { 
       String currentcustomerID = field[0]; 
       String currentproductID = field[1]; 
       String currentunitnumber = field[2]; 
       Product currentproduct = null;        

       currentcustomer = Integer.parseInt(currentcustomerID); 
       int currentproductid = Integer.parseInt(currentproductID); 
       int currentproductunit = Integer.parseInt(currentunitnumber); 

       //------------------------------------- 
       // START OF PRODUCT/SALE ITEM PROCESSING 
       //------------------------------------- 
       System.out.println(currentcustomer + " , " + currentproductid + " , " + currentproductunit); 


       //////////////////// 
       if (lastcustomer == currentcustomer) { 

        Saleitem salesItemObject = new Saleitem(currentproductid, currentproductunit, 
          Product.getUnitPrice(currentproductid)); 
        Saleitemarray.add(salesItemObject); 

       } else { 

        // sale receipt date, time, etc. 
        Salereceipt salesReceiptObject = new Salereceipt(lastcustomer, lastcustomer, 
        sum, "2/20/16", (int) (Math.random() * 2000)); 

        Salereceipt.receipt.add(salesReceiptObject); 
        lastcustomer = currentcustomer; 

        Saleitemarray.clear(); 
        sum = 0; 
       } 
       /////////////////////////// 

       //Find the correct product that the customer ordered 
       for (int i = 0; i < Product.productData.size(); i++){ 
        if (((Product.productData).get(i)).productID == currentproductid){ 
         currentproduct = Product.productData.get(i); 
        } 
       } 
       Saleitem salesItemObject = new Saleitem(currentproduct, currentproductunit); 

       Saleitemarray.add(salesItemObject); 
       boolean found = false; 
       //update total 
       for (int i = 0; i < total.size(); i++){ 

        //total is an array of arrays =) 

        //in the array, index 0 is the productID 
        // index 1 is the total sold of that product 
        //Find the correct product total 
        if ((total.get(i))[0] == salesItemObject.product.productID){ 
         //if we found it then we will mark found 
         //so that we can add in the item if it doesnt exist 
         //in our total array 
         found = true; 
         //increment the total number of prodcuts sold 
         (total.get(i))[1] += salesItemObject.numberofproduct; 
        }      
       } 

       if (found == false){ 
        Integer[] array = new Integer[2]; 

        // index 0 = product id 
        // index 1 = total number of products sold 

        array[0] = salesItemObject.product.productID; 
        array[1] = salesItemObject.numberofproduct; 

        total.add(array); 
       } 

       //------------------------------------- 
       // END OF PRODUCT/SALE ITEM PROCESSING 
       //-------------------------------------  
       //this is done inside of the constructor 

       if (currentcustomer == lastcustomer){ 
        sum += currentproduct.productPrice * currentproductunit;      
       }      
      } 
     } 

销售数据由具有CUSTOMER_ID文件导入[0],PRODUCT_ID [1],Units_ordered [2] 我想到ArrayList总由PRODUCT_ID以升序排序。什么是最好的方式来做到这一点。 Im新的Java,所以我不知道很多的语法。排序数组列表

+2

Collections.sort,可能与自定义比较 – MadProgrammer

+2

可能的复制[如何使用JAVA Collections.sort()? (具体情况)](http://stackoverflow.com/questions/16425127/how-to-use-collections-sort-in-java-specific-situation) –

+0

谢谢我认为,将工作 –

回答

-1
total.sort((item1, item2) -> item1.getProductId() - item2.getProductId()); 
+1

谢谢!这是有效的 –

-1

您可以使用Collections#sort,如下所示。

添加一个getter为ProductId就大功告成了

Collections.sort(total, new Comparator<Saleitem>(){ 
    @Override 
    public int compare(Saleitem s1, Saleitem s2) { 
     return s1.getProductId() - s2.getProductId(); 
    } 
});