2015-09-03 89 views
2

我需要從ArrayList中找到最高價值(銷售額),並顯示哪家商店的每個季度銷售額最高。如何從ArrayList中獲取最高價值並顯示信息

 BufferedReader inputFile = null; 
     PrintWriter outputFile = null; 

     BookStore bs; 
     ArrayList <BookStore> data = new ArrayList(); 

     try 
     { 
      inputFile = new BufferedReader (new FileReader ("bookstoresales.txt")); 
      outputFile = new PrintWriter (new FileWriter ("bookstoreoutput.txt")); 

      String indata = null; 

      while ((indata = inputFile.readLine()) != null) 
      { 
       System.out.println (indata); 
       StringTokenizer st = new StringTokenizer (indata, ","); 

       String storeBranch = st.nextToken(); 
       String storeAddress = st.nextToken(); 
       double jan = Double.parseDouble (st.nextToken()); 
       double feb = Double.parseDouble (st.nextToken()); 
       double mac = Double.parseDouble (st.nextToken()); 
       double apr = Double.parseDouble (st.nextToken()); 
       double may = Double.parseDouble (st.nextToken()); 
       double jun = Double.parseDouble (st.nextToken()); 
       double jul = Double.parseDouble (st.nextToken()); 
       double aug = Double.parseDouble (st.nextToken()); 
       double sep = Double.parseDouble (st.nextToken()); 
       double oct = Double.parseDouble (st.nextToken()); 
       double nov = Double.parseDouble (st.nextToken()); 
       double dec = Double.parseDouble (st.nextToken()); 

       bs = new BookStore (storeBranch, storeAddress, jan, feb, mac, apr, may, jun, jul, aug, sep, oct, nov, dec); 

       data.add(bs); 
      } 

      outputFile.println ("\n\n=================================== The Highest Bookstore Sales Per Quarter ==================================="); 


      for (int i=0; i<data.size(); i++) 
      { 
       //This is where I got stucked. 
      } 

      outputFile.println ("\nQuarter 1 : The Highest sales is Bookstores : " + //getbranch name from the highest sales + " with value of + //the highest value"); 


      inputFile.close(); 
      outputFile.close(); 
     } 

     catch (FileNotFoundException fnf) 
     { 
      System.out.println ("File not found!"); 
     } 

     catch (IOException ioe) 
     { 
      System.out.println (ioe.getMessage()); 
     } 
    } 
} 

現在我需要獲得並顯示商店名稱以及最高的銷售價值。對於ArrayList,我感到很困惑。

+0

如果你正在做這個manu盟友,你會怎麼做?你會從頂部開始往下看清單,記住迄今爲止看到的最大價值。 – dimo414

+0

考慮使用一個數組''double [] months = new double [12]'而不是所有那些雙變量 – Sid

+0

首先確定如何遍歷ArrayList。 – mdnghtblue

回答

2

創建一個名爲max的變量。在循環中,如果列表中的項目高於max,請將max設置爲該項目。

BookStore max = data.get(0); 
for (BookStore bs : data) { 
    if (bs.sales > max.sales) { 
     max = bs; 
    } 
} 
+0

簡單而直截了當。 +1 – Catch44

+0

'BookStore.sales'不是每個季度的總數。這將給出每個月的最大值。 –

0

的另一種方式是使用Collections.max()

Collections.max(data, new Comparator<BookStore>() { 
    @Override 
    public int compare(BookStore a, BookSore b) { 
     return a.sales - b.sales; 
    } 
}); 

或者,如果BookStoreComparable,後來乾脆:

Collections.max(data); 
1

使用Java 8:

BookStore max = 
    data.stream() 
     .max(Comparator.comparingDouble(bs -> bs.getJan() + ... + bs.getDec())) 
     .get(); 
相關問題