/* * CPP245 - Final Project * Spring 2008 * By: Joey Burgett * Instructor: Bruce Weston * 4/6/2008 */ using System; using System.IO; using System.Text; using System.Windows.Forms; namespace CPP245_Spring08_FinalProject { public partial class frmMain : Form { // Declarations - Global to our application StreamReader ReadFile = null; StreamWriter WriteFile = null; string Record_IN, Record_OUT, FileName_OUT; int Transactions; double Sales, Gallons_Regular, Gallons_Ethenol; public struct GasRecord { public string totalsale; public string stockcode; public string price; public string quantity; public string year; public string month_day; public string license; public string office_designator; public string account_number; } public struct NewRecord { public string year; public string office_code; public string account_code; public string license_number; public string stock_code; public string date; public string dollaramount; public string price; public string qty; public string desc; } public frmMain() { InitializeComponent(); // Wireup KeyDown event this.txtDateCreated.KeyDown += new System.Windows.Forms.KeyEventHandler(txtDateCreated_OnKeyDown); } // Handle keydown event private void txtDateCreated_OnKeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { // Detect if the Enter key is pressed // Note: Visual Studio 2008 the key is 'Return' // Visual Studio 2005 the key is 'Enter' if (e.KeyCode.ToString() == "Return") { switch (IsDate(txtDateCreated.Text)) { case false: MessageBox.Show("Invalid Date!","ATTENTION!"); txtDateCreated.Clear(); break; default: // Tokenize the contents of the textbox by splitting the string // into a string array at each instance of a "/". string[] tokens = txtDateCreated.Text.Split(new char[] { '/' }); FileName_OUT = String.Format("{0:0#}", Convert.ToInt32(tokens[0])) + String.Format("{0:0#}", Convert.ToInt32(tokens[1])); // Begin processing input file ReadRecord(); break; } } } // Determine if value is a valid date public bool IsDate(string StringDate) { DateTime DateTime; bool Test = true; try { DateTime = DateTime.Parse(StringDate); } catch { Test = false; } return Test; } // Process RAWTRANS.DAT file public void ReadRecord() { try { ReadFile = new StreamReader("RAWTRANS.DAT"); // Build out output filename based off date entered WriteFile = new StreamWriter("Feed" + FileName_OUT + ".DAT"); // Initialize variables Transactions = 0; Sales = 0D; Gallons_Ethenol = 0; Gallons_Regular = 0; // Loop while theres unread data to process. while (ReadFile.Peek() >= 0) { // Create structure instance GasRecord gr = new GasRecord(); // Read file string record = ReadFile.ReadLine(); // Assign field values for record gr.totalsale = record.Substring(11, 6); gr.stockcode = record.Substring(19, 2); gr.price = record.Substring(21, 4); gr.quantity = record.Substring(25, 8); gr.year = record.Substring(46, 2); gr.month_day = record.Substring(48, 4); gr.license = record.Substring(72, 5); gr.office_designator = record.Substring(77, 1); gr.account_number = record.Substring(78, 10); // Build our read string for processing Record_IN = gr.totalsale + " " + gr.stockcode + " " + gr.price + " " + gr.quantity + " " + gr.year + " " + gr.month_day + " " + gr.license + " " + gr.office_designator + " " + gr.account_number; double SaleTally = Convert.ToDouble(gr.totalsale.Substring(0, gr.totalsale.Length - 2) + "." + gr.totalsale.Substring(gr.totalsale.Length - 2)); // This is for debug purposes listBox1.Items.Add(SaleTally.ToString("c")); // Total the amount of sales Sales += SaleTally; // Write our record WriteRecord(gr); } } catch (Exception ex) { MessageBox.Show("Error reading input file! Error: " + ex.Message); } finally { // Update values on our form UpdateForm(); // Close both file streams if (ReadFile != null) ReadFile.Close(); if (WriteFile != null) WriteFile.Close(); } } // Update form controls private void UpdateForm() { txtTotalTransactions.Text = Transactions.ToString(); txtSalesTotal.Text = Sales.ToString("c"); // N0 = Numeric formatting with no decimal places but commas. txtGallonsEthanol.Text = Gallons_Ethenol.ToString("N3"); txtGallonsRegular.Text = Gallons_Regular.ToString("N3"); } // Replace last character in supplied string // prefix = "+" or "+00" or "+000" private string Recode(string original, string prefix) { // Multi-dimensional array of translation characters string[,] enc_dec = new string[,] { { "{", "0" }, { "A", "1" }, { "B", "2" }, { "C", "3" }, { "D", "4" }, { "E", "5" }, { "F", "6" }, { "G", "7" }, { "H", "8" }, { "I", "9" } }; // Retrieve the last character in the string and convert it to an integer int LastChar = Convert.ToInt32(original.Substring(original.Length - 1, 1)); // Remove that last character string Mod_String = original.Remove(original.Length - 1, 1); // Return the modified string with the translated character return (prefix + Mod_String + enc_dec[LastChar, 0].ToString()); } // Write new record format to new file private void WriteRecord(GasRecord data) { try { // Create new instance of record structure NewRecord nr = new NewRecord(); // Count # of transactions Transactions++; switch (data.office_designator) { case "1": nr.office_code = "CC"; break; default: nr.office_code = "JC"; break; } // Test if stockcode = "01" or "02" (else) switch (data.stockcode) { case "01": nr.stock_code = "0810 "; nr.desc = "GASOLINE, ETHENOL "; // This is for debug purposes this.listBox2.Items.Add(Convert.ToDouble(data.quantity.Substring(0, data.quantity.Length - 3) + "." + data.quantity.Substring(data.quantity.Length - 3)).ToString()); // Format our string Gallons_Ethenol += Convert.ToDouble(data.quantity.Substring(0,data.quantity.Length-3) + "." + data.quantity.Substring(data.quantity.Length-3)); break; default: nr.stock_code = "0811 "; nr.desc = "GASOLINE, UNLEADED "; // This is for debug purposes this.listBox3.Items.Add(Convert.ToDouble(data.quantity.Substring(0, data.quantity.Length - 3) + "." + data.quantity.Substring(data.quantity.Length - 3)).ToString()); // Format our string Gallons_Regular += Convert.ToDouble(data.quantity.Substring(0, data.quantity.Length - 3) + "." + data.quantity.Substring(data.quantity.Length - 3)); break; } // price, dollaramount and qty require the last character to be changed. // Pass Recode([Structure_Name].[Member_Name],[String Prefix Value]) nr.price = Recode(data.price, "+000"); nr.dollaramount = Recode(data.totalsale, "+00"); nr.qty = Recode(data.quantity, "+"); nr.account_code = data.account_number + "0"; nr.license_number = data.license + "S"; nr.year = data.year; nr.date = data.month_day + data.year; nr.license_number = data.license + "S"; // Build the final string/record to be written to file Record_OUT = nr.year + nr.office_code + nr.account_code + nr.license_number + nr.stock_code + nr.date + nr.qty + nr.price + nr.dollaramount + "GAL " + nr.desc + nr.office_code + nr.license_number + " 0000"; WriteFile.WriteLine(Record_OUT); } catch (Exception ex) { MessageBox.Show("Error writing to file! Error: " + ex.Message); } } } }