/// <summary> /// CSVデータ読み込み /// </summary> public override void ReadTransactions() { skipToFirstLine(); TransactionList transactions = new TransactionList(); string line; bool hasFormatError = false; while ((line = mSr.ReadLine()) != null) { // CSV カラム分割 string[] row = SplitCsv(line); if (row.Length <= 1) continue; // ad hoc... // パース try { Transaction t = mRule.parse(row); transactions.Add(t); } catch (FormatException ex) { // ignore transaction // MessageBox.Show(ex.Message, Properties.Resources.Error); hasFormatError = true; } } if (transactions.Count == 0 && hasFormatError) { // フォーマットエラー例外をスロー throw new CsvReadException(Properties.Resources.CsvFormatError); } // ソート処理 switch (mRule.sortOrder) { default: case CsvRule.SortOrder.Ascent: break; case CsvRule.SortOrder.Descent: transactions.Reverse(); break; case CsvRule.SortOrder.Auto: transactions.list.Sort(compareByDate); break; } mTransactions = transactions; }
// 残高から金額を計算する private void CalcValueFromBalance(TransactionList transactions) { int prevBalance = 0; foreach (Transaction t in transactions.list) { t.value = t.balance - prevBalance; prevBalance = t.balance; } transactions.list.RemoveAt(0); // 最古のエントリは捨てる }
/// <summary> /// 後処理 /// </summary> /// <param name="list"></param> protected virtual void PostProcess(TransactionList transactions) { }
/// <summary> /// カード読み込み /// Note: 本来はこのメソッドは private で良いが、UnitTest 用に public にしてある。 /// </summary> /// <param name="f"></param> /// <returns></returns> public TransactionList ReadTransactions(IFelica f) { TransactionList transactions = new TransactionList(); f.Polling(mSystemCode); if (!analyzeCardId(f)) { throw new Exception(Properties.Resources.CantReadCardNo); } for (int i = 0; i < mMaxTransactions; i++) { byte[] data = new byte[16 * mBlocksPerTransaction]; byte[] block = null; for (int j = 0; j < mBlocksPerTransaction; j++) { block = f.ReadWithoutEncryption(mServiceCode, i * mBlocksPerTransaction + j); if (block == null) { break; } block.CopyTo(data, j * 16); } if (block == null) { break; } Transaction t = new Transaction(); // データが全0かどうかチェック int x = 0; foreach (int xx in data) { x |= xx; } if (x == 0) { // データが全0なら無視(空エントリ) t.Invalidate(); } // トランザクション解析 else if (!analyzeTransaction(t, data)) { t.Invalidate(); } transactions.Add(t); } if (mNeedReverse) { transactions.Reverse(); } if (mNeedCalcValue) { CalcValueFromBalance(transactions); } PostProcess(transactions); return transactions; }
// 履歴連番でソートする protected override void PostProcess(TransactionList transactions) { transactions.list.Sort(compareById); }