fixed csv pasring by using a free csv file parsing lib

This commit is contained in:
2011-01-15 13:40:36 +00:00
parent 4dea95b9bf
commit 4752b05418
11 changed files with 86 additions and 339 deletions

View File

@@ -1,9 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<classpath> <classpath>
<classpathentry kind="src" path="src"/> <classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="version infomation"/>
<classpathentry kind="src" path="images"/> <classpathentry kind="src" path="images"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/jcalendar-1.3.3.jar"/> <classpathentry kind="lib" path="lib/jcalendar-1.3.3.jar"/>
<classpathentry kind="lib" path="lib/sqlitejdbc-v056.jar"/> <classpathentry kind="lib" path="lib/sqlitejdbc-v056.jar"/>
<classpathentry kind="lib" path="lib/ostermillerutils_1_07_00.jar"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

Binary file not shown.

View File

@@ -9,9 +9,8 @@ package com.TwentyCodes.java.OrderProcessor.DB;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.LineNumberReader;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.sql.Connection; import java.sql.Connection;
@@ -23,8 +22,8 @@ import java.sql.Statement;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Locale; import java.util.Locale;
import java.util.Scanner;
import com.Ostermiller.util.CSVParser;
import com.TwentyCodes.java.OrderProcessor.Date; import com.TwentyCodes.java.OrderProcessor.Date;
import com.TwentyCodes.java.OrderProcessor.InvalidDateFormatException; import com.TwentyCodes.java.OrderProcessor.InvalidDateFormatException;
import com.TwentyCodes.java.OrderProcessor.Main; import com.TwentyCodes.java.OrderProcessor.Main;
@@ -72,18 +71,21 @@ public class OrderDB {
} }
/** /**
* counts the number of lines in a file * counts the number of lines in a csv file
* @param file to be read * @param file to be read
* @return number of lines in a file * @return number of lines in a file
* @throws IOException * @throws IOException
* @author ricky barrette * @author ricky barrette
*/ */
public int countLines(File file) throws IOException { public int countLines(File file) throws IOException {
LineNumberReader reader = new LineNumberReader(new FileReader(file)); // LineNumberReader reader = new LineNumberReader(new FileReader(file));
int cnt = 0; int cnt = 1;
while ((reader.readLine()) != null) ; // while ((reader.readLine()) != null) ;
cnt = reader.getLineNumber(); // cnt = reader.getLineNumber();
reader.close(); // reader.close();
CSVParser csvParser = new CSVParser(new FileInputStream(file));
while(csvParser.getLine() != null)
cnt++;
return cnt; return cnt;
} }
@@ -177,7 +179,11 @@ public class OrderDB {
* @throws OrderExistsException * @throws OrderExistsException
*/ */
public void load(File file) throws SQLException, IOException, ClassNotFoundException{ public void load(File file) throws SQLException, IOException, ClassNotFoundException{
Scanner scan = new Scanner(file);
// Scanner scan = new Scanner(file);
CSVParser csvParser = new CSVParser(new FileInputStream(file));
int line = 1; int line = 1;
int lineCount = countLines(file); int lineCount = countLines(file);
isLoadingFile = true; isLoadingFile = true;
@@ -186,7 +192,7 @@ public class OrderDB {
prep = conn.prepareStatement(INSERT); prep = conn.prepareStatement(INSERT);
conn.setAutoCommit(true); conn.setAutoCommit(true);
while(scan.hasNextLine()){ while(csvParser.lastLineNumber() < lineCount){
if(mListener != null) if(mListener != null)
mListener.onProgressUpdate(line++, lineCount); mListener.onProgressUpdate(line++, lineCount);
@@ -195,7 +201,7 @@ public class OrderDB {
System.out.println("\non line: "+ line); System.out.println("\non line: "+ line);
try { try {
addOrder(new Order(scan.nextLine())); addOrder(new Order(csvParser.getLine()));
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
e.printStackTrace(); e.printStackTrace();
} catch (InvalidDateFormatException e) { } catch (InvalidDateFormatException e) {

View File

@@ -15,7 +15,7 @@ import com.TwentyCodes.java.OrderProcessor.UI.MainWindow;
public class Main { public class Main {
public static final boolean DEBUG = false; public static final boolean DEBUG = true;
/** /**
* called when the application first starts * called when the application first starts

View File

@@ -6,7 +6,11 @@
package com.TwentyCodes.java.OrderProcessor; package com.TwentyCodes.java.OrderProcessor;
import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
import java.io.StringReader;
import com.Ostermiller.util.CSVParser;
/** /**
@@ -34,48 +38,76 @@ public class Order implements Comparable<Order>, Serializable{
* @param order * @param order
* @author ricky barrette * @author ricky barrette
* @throws InvalidDateFormatException * @throws InvalidDateFormatException
* @throws IOException
*/ */
public Order(String order) throws NumberFormatException, InvalidDateFormatException{ public Order(String order) throws NumberFormatException, InvalidDateFormatException, IOException{
if(Main.DEBUG) if(Main.DEBUG)
System.out.println("parsing in order: "+ order); System.out.println("parsing in order: "+ order);
String [] lineParts; CSVParser parsedOrder = new CSVParser(new StringReader(order));
lineParts = order.split(","); String[] lineParts = parsedOrder.getLine();
parsedOrder.close();
this.mGoogleOrderNumber = Long.parseLong(removeLastChar(lineParts[0].split(" ")[1])); this.mGoogleOrderNumber = Long.parseLong(lineParts[0].split(" ")[1]);
this.mOrderCreationDate = new Date(lineParts[2].substring(1, (lineParts[2].length() -1) )); this.mOrderCreationDate = new Date(lineParts[2]);
this.mOrderAmount = Float.parseFloat(lineParts[4]); this.mOrderAmount = Float.parseFloat(lineParts[4]);
this.mAmountCharged = Float.parseFloat(lineParts[5]); this.mAmountCharged = Float.parseFloat(lineParts[5]);
this.mFinancialStatus = Status.parseStatus(lineParts[6]); this.mFinancialStatus = Status.parseStatus(lineParts[6]);
this.mFulfillmentStatus = Status.parseStatus(lineParts[7]); this.mFulfillmentStatus = Status.parseStatus(lineParts[7]);
this.mCustomerEmail = removeFistAndLastChar(lineParts[16]); this.mCustomerEmail = lineParts[16];
this.mCustomerName = removeFistAndLastChar(lineParts[17]); this.mCustomerName = lineParts[17];
this.mItemName = removeFistAndLastChar(lineParts[27]); this.mItemName = lineParts[27];
// String [] lineParts;
// lineParts = order.split(",");
// System.out.println(lineParts[0].split(" ")[1]);
// this.mGoogleOrderNumber = Long.parseLong(lineParts[0].split(" ")[1]);
// this.mOrderCreationDate = new Date(lineParts[2].substring(1));
// this.mOrderAmount = Float.parseFloat(lineParts[4]);
// this.mAmountCharged = Float.parseFloat(lineParts[5]);
// this.mFinancialStatus = Status.parseStatus(lineParts[6]);
// this.mFulfillmentStatus = Status.parseStatus(lineParts[7]);
// this.mCustomerEmail = removeFistAndLastChar(lineParts[16]);
// this.mCustomerName = removeFistAndLastChar(lineParts[17]);
// this.mItemName = removeFistAndLastChar(lineParts[27]);
if (Main.DEBUG) if (Main.DEBUG)
System.out.println(this.toString()); System.out.println(this.toString());
} }
/** // /**
* removes the last char from a string // * removes the last char from a string
* @param s // * @param s
* @return // * @return
* @author ricky barrette // * @author ricky barrette
*/ // */
private String removeLastChar(String s) { // private String removeLastChar(String s) {
return s.substring(0, s.length() - 1); // return s.substring(0, s.length() - 1);
} // }
//
// /**
// * removes the first and last chars from a string
// * @param s
// * @return
// * @author ricky barrette
// */
// private String removeFistAndLastChar(String s){
// return s.substring(1, s.length()-1);
// }
/**
* removes the first and last chars from a string
* @param s
* @return
* @author ricky barrette
*/
private String removeFistAndLastChar(String s){
return s.substring(1, s.length()-1);
}
public Order(String[] lineParts) throws InvalidDateFormatException, NumberFormatException {
this.mGoogleOrderNumber = Long.parseLong(lineParts[0].split(" ")[1]);
this.mOrderCreationDate = new Date(lineParts[2]);
this.mOrderAmount = Float.parseFloat(lineParts[4]);
this.mAmountCharged = Float.parseFloat(lineParts[5]);
this.mFinancialStatus = Status.parseStatus(lineParts[6]);
this.mFulfillmentStatus = Status.parseStatus(lineParts[7]);
this.mCustomerEmail = lineParts[16];
this.mCustomerName = lineParts[17];
this.mItemName = lineParts[27];
}
/** /**
* returns a human readable string that represnets this object * returns a human readable string that represnets this object
* (non-Javadoc) * (non-Javadoc)

View File

@@ -50,19 +50,19 @@ public enum Status {
if(Main.DEBUG) if(Main.DEBUG)
System.out.println("parsing status: "+ s); System.out.println("parsing status: "+ s);
if(s.equals("\""+ CHARGED.toString()+"\"")) if(s.equals(CHARGED.toString()))
return CHARGED; return CHARGED;
if(s.equals("\""+ CANCELLED.toString()+"\"")) if(s.equals(CANCELLED.toString()))
return CANCELLED; return CANCELLED;
if(s.equals("\""+ CANCELLED_BY_GOOGLE.toString()+"\"")) if(s.equals(CANCELLED_BY_GOOGLE.toString()))
return CANCELLED_BY_GOOGLE; return CANCELLED_BY_GOOGLE;
if(s.equals("\""+ DELIVERED.toString()+"\"")) if(s.equals(DELIVERED.toString()))
return DELIVERED; return DELIVERED;
if(s.equals("\""+ WILL_NOT_DELIVER.toString()+"\"")) if(s.equals(WILL_NOT_DELIVER.toString()))
return WILL_NOT_DELIVER; return WILL_NOT_DELIVER;
return null; return null;

View File

@@ -1,17 +0,0 @@
/**
* @author Twenty Codes, LLC
* @author ricky barrette
* @date Dec 22, 2010
*/
package com.TwentyCodes.java.OrderProcessor;
/**
* this enum will represent trantions types
* @author ricky barrette
*/
public enum TransactionType {
DEPOSIT, WITHDRAW, CHARGE_FEE
}

View File

@@ -1,277 +0,0 @@
/**
* GetStatsDialog.java
* @date Jan 2, 2010
* @author ricky barrette
* @author Twenty Codes, LLC
*/
package com.TwentyCodes.java.OrderProcessor.UI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import com.TwentyCodes.java.OrderProcessor.Date;
import com.TwentyCodes.java.OrderProcessor.Months;
/**
* this will be a dialog for getting information about the stats the user is looking for
* @author ricky barrette
*/
public class GetStatsDialog extends JFrame implements ActionListener {
private static final long serialVersionUID = -8681149469926405790L;
private JPanel contentPane;
private JRadioButton mJan;
private JRadioButton mFeb;
private JRadioButton mMar;
private JRadioButton mApr;
private JRadioButton mJun;
private JRadioButton mJul;
private JRadioButton mAug;
private JRadioButton mSep;
private JRadioButton mNov;
private JRadioButton mDec;
private JRadioButton mYear;
private JRadioButton mMay;
private JRadioButton mOct;
private JTextField textField;
private JButton mOkButton;
@SuppressWarnings("unused")
private Months mTimeFrame = Months.JAN;
@SuppressWarnings("unused")
private Date mStartDate;
@SuppressWarnings("unused")
private Date mEndDate;
/**
* Create the frame.
*/
public GetStatsDialog() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 260, 371);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
ButtonGroup group = new ButtonGroup();
mJan = new JRadioButton("Jan");
mFeb = new JRadioButton("Feb");
mMar = new JRadioButton("Mar");
mApr = new JRadioButton("Apr");
mMay = new JRadioButton("May");
mJun = new JRadioButton("Jun");
mJul = new JRadioButton("Jul");
mAug = new JRadioButton("Aug");
mSep = new JRadioButton("Sep");
mOct = new JRadioButton("Oct");
mNov = new JRadioButton("Nov");
mDec = new JRadioButton("Dec");
mYear = new JRadioButton("Year");
group.add(mJan);
group.add(mFeb);
group.add(mMar);
group.add(mApr);
group.add(mMay);
group.add(mJun);
group.add(mJul);
group.add(mAug);
group.add(mSep);
group.add(mOct);
group.add(mNov);
group.add(mDec);
group.add(mYear);
mJan.setSelected(true);
//Put the radio buttons in a column in a panel.
JPanel radioPanel = new JPanel();
mJan.addActionListener(this);
mFeb.addActionListener(this);
mMar.addActionListener(this);
mApr.addActionListener(this);
mMay.addActionListener(this);
mJun.addActionListener(this);
mJul.addActionListener(this);
mAug.addActionListener(this);
mSep.addActionListener(this);
mOct.addActionListener(this);
mNov.addActionListener(this);
mDec.addActionListener(this);
mYear.addActionListener(this);
setContentPane(contentPane);
JLabel lblTimeFrame = new JLabel("Time Frame");
JLabel lblApplicationName = new JLabel("Application Name");
textField = new JTextField();
textField.setColumns(10);
mOkButton = new JButton("Ok");
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(mOkButton, GroupLayout.PREFERRED_SIZE, 135, GroupLayout.PREFERRED_SIZE)
.addComponent(lblApplicationName))
.addPreferredGap(ComponentPlacement.RELATED))
.addComponent(textField))
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(lblTimeFrame)
.addComponent(radioPanel, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE))
.addGap(164))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(lblTimeFrame)
.addComponent(lblApplicationName))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
.addComponent(radioPanel, GroupLayout.PREFERRED_SIZE, 306, GroupLayout.PREFERRED_SIZE)
.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(mOkButton)))
.addContainerGap())
);
GroupLayout gl_radioPanel = new GroupLayout(radioPanel);
gl_radioPanel.setHorizontalGroup(
gl_radioPanel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_radioPanel.createSequentialGroup()
.addContainerGap()
.addComponent(mDec)
.addContainerGap(45, Short.MAX_VALUE))
.addGroup(gl_radioPanel.createSequentialGroup()
.addGroup(gl_radioPanel.createParallelGroup(Alignment.TRAILING)
.addGroup(Alignment.LEADING, gl_radioPanel.createSequentialGroup()
.addContainerGap()
.addGroup(gl_radioPanel.createParallelGroup(Alignment.LEADING)
.addComponent(mJan)
.addComponent(mFeb)
.addComponent(mMar)
.addComponent(mApr)
.addComponent(mMay)
.addComponent(mJun)
.addComponent(mJul)
.addComponent(mAug)
.addComponent(mSep)
.addComponent(mOct)
.addComponent(mNov, GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)))
.addGroup(Alignment.LEADING, gl_radioPanel.createSequentialGroup()
.addContainerGap()
.addComponent(mYear)))
.addGap(39))
);
gl_radioPanel.setVerticalGroup(
gl_radioPanel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_radioPanel.createSequentialGroup()
.addComponent(mJan)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(mFeb)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(mMar)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(mApr)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(mMay)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(mJun)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(mJul)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(mAug)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(mSep)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(mOct)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(mNov)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(mDec)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(mYear)
.addGap(167))
);
radioPanel.setLayout(gl_radioPanel);
contentPane.setLayout(gl_contentPane);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == mJan){
mTimeFrame = Months.JAN;
}
if(e.getSource() == mFeb){
mTimeFrame = Months.FEB;
}
if(e.getSource() == mMar){
mTimeFrame = Months.MAR;
}
if(e.getSource() == mApr){
mTimeFrame = Months.APR;
}
if(e.getSource() == mMay){
mTimeFrame = Months.MAY;
}
if(e.getSource() == mJun){
mTimeFrame = Months.JUN;
}
if(e.getSource() == mJul){
mTimeFrame = Months.JUL;
}
if(e.getSource() == mAug){
mTimeFrame = Months.AUG;
}
if(e.getSource() == mSep){
mTimeFrame = Months.SEP;
}
if(e.getSource() == mOct){
mTimeFrame = Months.OCT;
}
if(e.getSource() == mNov){
mTimeFrame = Months.NOV;
}
if(e.getSource() == mDec){
mTimeFrame = Months.DEC;
}
if(e.getSource() == mYear){
mTimeFrame = Months.YEAR;
}
if(e.getSource() == mOkButton){
}
}
}

View File

@@ -151,7 +151,7 @@ public class MainWindow extends JFrame implements ActionListener, ProgressListen
*/ */
@Override @Override
public void onProgressUpdate(int progress, int max) { public void onProgressUpdate(int progress, int max) {
progress++; // progress++;
mProgressBar.setMaximum(max); mProgressBar.setMaximum(max);
mProgressBar.setValue(progress); mProgressBar.setValue(progress);
mProgressBar.setString(" "+mCurrentFile + " ~ Parsing Order: "+ progress +" of "+mProgressBar.getMaximum()+" "); mProgressBar.setString(" "+mCurrentFile + " ~ Parsing Order: "+ progress +" of "+mProgressBar.getMaximum()+" ");

View File

@@ -0,0 +1 @@
0.12.0 b 27