import java.awt.Font;
import java.io.File;
import java.io.PrintWriter;
import java.text.Format;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/*
 * PriceDataUsingMaps.java
 *
 * Created on November 7, 2006, 1:56 PM
 *
 * COP 6007 Programming Concepts
 * Examples
 */

/**
 *
 * @author Bill Kraynek
 */
public class PriceDataUsingMaps {
         
    /** Creates a new instance of PriceDataUsingMaps */
        public PriceDataUsingMaps() throws Exception {
            Format currency = NumberFormat.getCurrencyInstance();
            TreeMap<String,Double> priceData = loadPriceData("prices.data");
            String fromDate = "01/02/2004";
            String toDate = "01/29/2004";
            ArrayList<Double> prices = getPrices(fromDate,toDate,priceData);
            double averagePrice = averagePrice(fromDate,toDate,priceData);
            double maxPrice = maxPrice(fromDate,toDate,priceData);
            String out = "The prices of SPX between " + fromDate + " to but not including " + toDate + " were\n";
            for( double price : prices ) out += currency.format(price) + "\n";
            out += "The average price of SPX between " + fromDate + " to " + toDate + " was " + currency.format(averagePrice) + "\n";
            out += "The max price of SPX between " + fromDate + " to " + toDate + " was " + currency.format(maxPrice) + "\n";
            JTextArea outArea = new JTextArea(out,25,80);
            outArea.setFont(new Font("Courier",Font.BOLD,15));
            JOptionPane.showMessageDialog(null,new JScrollPane(outArea));
            PrintWriter pOut = new PrintWriter("results.txt");
            pOut.println(out);
            pOut.close();
            Map<String,Double> priceOrder = getPriceOrder(fromDate,toDate,priceData);
            out = "The unique prices of SPX between " + fromDate + " to " + toDate + " in order were\nDate\t\tPrice\n";
            for( String date : priceOrder.keySet()) out += date + "\t" + priceOrder.get(date) + "\n";
            outArea.setText(out);
            JOptionPane.showMessageDialog(null,new JScrollPane(outArea));
        }
        
        TreeMap<String,Double> loadPriceData(String fileName) throws Exception {
            TreeMap<String,Double> prices = new TreeMap<String,Double>();
            Scanner fileScanner = new Scanner(new File(fileName));
            fileScanner.useDelimiter("[,\n]+");
            while( fileScanner.hasNext() ) {
                prices.put(fileScanner.next(),fileScanner.nextDouble());
            }// end while
            return prices;
        }// end loadPriceData
        
        ArrayList<Double> getPrices(String fromDate, String toDate, TreeMap<String,Double> priceData) {
            Map<String,Double> priceDataSlice = priceData.subMap(fromDate,toDate);
            ArrayList<Double> prices = new ArrayList<Double>();
            for( String date : priceDataSlice.keySet() ) {
                prices.add(priceDataSlice.get(date));
            }// end for
            return prices;
        }// end getPrices
        
        double averagePrice(String fromDate, String toDate, TreeMap<String,Double> priceData) {
            Map<String,Double> priceDataSlice = priceData.subMap(fromDate,toDate);
            double priceSum = 0.0;
            for( String date : priceDataSlice.keySet() ) {
                priceSum += priceDataSlice.get(date);
            }// end for
            return priceSum/priceDataSlice.keySet().size();
        }//end averagePrice
        
        double maxPrice(String fromDate, String toDate, TreeMap<String,Double> priceData) {
            Map<String,Double> priceDataSlice = priceData.subMap(fromDate,toDate);
            double priceMax = 0.0;
            for( String date : priceDataSlice.keySet() ) {
                if( priceMax < priceDataSlice.get(date) ) priceMax = priceDataSlice.get(date);
            }// end for
            return priceMax;
        }// end maxPrice
        
        Map<String,Double> getPriceOrder(String fromDate, String toDate, TreeMap<String,Double> priceData) {
            Map<String,Double> priceDataSlice = priceData.subMap(fromDate,toDate);
            Map<String,Double> slice = new TreeMap<String,Double>(new PriceOrderComparator(priceData));
            slice.putAll(priceDataSlice);
            return slice;
        }// end getPriceData
        
        class PriceOrderComparator implements Comparator<String> {
            private Map<String,Double> map;
            PriceOrderComparator(Map<String,Double> map) {
                this.map = map;
            }
            public int compare(String left, String right) {
                return (int)(100*map.get(left) - 100*map.get(right));
            }// end compare
        }// end PriceOrderComparator
        
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        new PriceDataUsingMaps();
    }
    
}

