
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/*
 * $Id: WebBillReader.java,v 1.1 2005/01/06 00:48:09 tag Exp $
 * Created on 06.01.2005
 * Last commit on $Date: 2005/01/06 00:48:09 $ by $Author: tag $
 * Cologne, Germany
 * 
 * @author Juan Antonio Agudo
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 */

/**
 * @author tag Juan Antonio Agudo
 * 
 */
public class WebBillReader {
    private List<Connection> data = new ArrayList<Connection>();

    private List<String> numbers = new ArrayList<String>();

    public static void main(String[] args) {
        if (args.length == 1) {
            WebBillReader w = new WebBillReader(new File(args[0]));
        } else {
            System.out.println("Usage: java WebBillReader bill.csv");
        }
    }

    public WebBillReader(File bill) {
        BufferedReader in = null;

        try {
            in = new BufferedReader(new FileReader(bill));
            String line = null;
            in.skip(143); // Skip the first line which is exactly 143 bytes
                          // (including the "\n" character)
            while ((line = in.readLine()) != null) {
                line = line.replaceAll("\"", ""); // entferne
                                                  // Anführungszeichen
                String[] cols = line.split(";");

                Connection conn = new Connection();
                conn.setAnschluss(cols[0]);
                conn.setDatum(cols[1]);
                conn.setZeit(cols[2]);
                conn.setZielrufnr(cols[3]);
                conn.setOrt(cols[4]);
                conn.setTarifzone(cols[5]);
                conn.setZeitzone(cols[7]);
                conn.setMenge(cols[9]);
                conn.setEinheit(cols[10]);
                conn.setBetrag(new BigDecimal(cols[11].replace(',','.')));

                data.add(conn);

                if (!numbers.contains(conn.getAnschluss())) {
                    numbers.add(conn.getAnschluss());
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("File not found.");
        }catch (IOException e1) {
            e1.printStackTrace();
            System.out.println("Error while reading file.");
        }

        BigDecimal total = new BigDecimal("0.0");
        for (String number : numbers) {
            BigDecimal sum = new BigDecimal("0.0");
            for (Connection con : data) {
                if (con.getAnschluss().equals(number)) {
                    sum = sum.add(con.getBetrag());
                }
            }
            total = total.add(sum);
            System.out.println("Number " + number + " has a bill of: " + sum + "€");
        }
        System.out.println("Total: " + total + "€");
    }

    private class Connection {
        private String Anschluss;

        private String Datum;

        private String Zeit;

        private String Zielrufnr;

        private String Ort;

        private String Tarifzone;

        private String Zeitzone;

        private String Menge;

        private String Einheit;

        private BigDecimal Betrag;

        private String Anbieter;

        public BigDecimal getBetrag() {
            return Betrag;
        }

        public void setBetrag(BigDecimal betrag) {
            Betrag = betrag;
        }

        public String getAnbieter() {
            return Anbieter;
        }

        public void setAnbieter(String anbieter) {
            Anbieter = anbieter;
        }

        public String getAnschluss() {
            return Anschluss;
        }

        public void setAnschluss(String anschluss) {
            Anschluss = anschluss;
        }

        public String getDatum() {
            return Datum;
        }

        public void setDatum(String datum) {
            Datum = datum;
        }

        public String getEinheit() {
            return Einheit;
        }

        public void setEinheit(String einheit) {
            Einheit = einheit;
        }

        public String getMenge() {
            return Menge;
        }

        public void setMenge(String menge) {
            Menge = menge;
        }

        public String getOrt() {
            return Ort;
        }

        public void setOrt(String ort) {
            Ort = ort;
        }

        public String getTarifzone() {
            return Tarifzone;
        }

        public void setTarifzone(String tarifzone) {
            Tarifzone = tarifzone;
        }

        public String getZeit() {
            return Zeit;
        }

        public void setZeit(String zeit) {
            Zeit = zeit;
        }

        public String getZeitzone() {
            return Zeitzone;
        }

        public void setZeitzone(String zeitzone) {
            Zeitzone = zeitzone;
        }

        public String getZielrufnr() {
            return Zielrufnr;
        }

        public void setZielrufnr(String zielrufnr) {
            Zielrufnr = zielrufnr;
        }
    }

}

