Example #1
0
        private void enterButton_Click(object sender, EventArgs e)
        {
            // store TextBox values string array
            string[] values = GetTextBoxValues();

            // Record containing TextBox values to serialize
            Record record = new Record();

            // determine whether TextBox account field is empty
            if (values[(int)TextBoxIndices.ACCOUNT] != "")
            {
                // store TextBox values in Record and serialize Record
                try
                {
                    // get account number value from TextBox
                    int accountNumber = Int32.Parse(
                    values[(int)TextBoxIndices.ACCOUNT]);

                    // determine whether accountNumber is valid
                    if (accountNumber > 0)
                    {
                        // store TextBox fields in Record
                        record.Account = accountNumber;
                        record.FirstName = values[(int)TextBoxIndices.FIRST];
                        record.LastName = values[(int)TextBoxIndices.LAST];
                        record.Balance = Decimal.Parse(
                        values[(int)TextBoxIndices.BALANCE]);

                        // write Record to file, fields separated by commas
                        fileWriter.WriteLine(
                        record.Account + "," + record.FirstName + "," +
                        record.LastName + "," + record.Balance);
                    } // end if
                    else
                    {
                        // notify user if invalid account number
                        MessageBox.Show("Invalid Account Number", "Error",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                    } // end else
                } // end try
                // notify user if error occurs in serialization
                catch (IOException)
                {
                    MessageBox.Show("Error Writing to File", "Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                } // end catch
                // notify user if error occurs regarding parameter format
                catch (FormatException)
                {
                    MessageBox.Show("Invalid Format", "Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                } // end catch
            }
        }
Example #2
0
        // invoked when user clicks credit balances,
        // debit balances or zero balances button
        private void getBalances_Click( object sender, System.EventArgs e )
        {
            // convert sender explicitly to object of type button
             Button senderButton = ( Button ) sender;

             // get text from clicked Button, which stores account type
             string accountType = senderButton.Text;

             // read and display file information
             try
             {
            // go back to the beginning of the file
            input.Seek( 0, SeekOrigin.Begin );

            displayTextBox.Text = "The accounts are:\r\n";

            // traverse file until end of file
            while ( true )
            {
               string[] inputFields; // stores individual pieces of data
               Record record; // store each Record as file is read
               decimal balance; // store each Record's balance

               // get next Record available in file
               string inputRecord = fileReader.ReadLine();

               // when at the end of file, exit method
               if ( inputRecord == null )
                  return;

               inputFields = inputRecord.Split( ',' ); // parse input

               // create Record from input
               record = new Record(
                  Convert.ToInt32( inputFields[ 0 ] ), inputFields[ 1 ],
                  inputFields[ 2 ], Convert.ToDecimal( inputFields[ 3 ] ) );

               // store record's last field in balance
               balance = record.Balance;

               // determine whether to display balance
               if ( ShouldDisplay( balance, accountType ) )
               {
                  // display record
                  string output = record.Account + "\t" +
                     record.FirstName + "\t" + record.LastName + "\t";

                  // display balance with correct monetary format
                  output += String.Format( "{0:F}", balance ) + "\r\n";

                  // copy output to screen
                  displayTextBox.AppendText( output );
               } // end if
            } // end while
             } // end try
             // handle exception when file cannot be read
             catch ( IOException )
             {
            MessageBox.Show( "Cannot Read File", "Error",
               MessageBoxButtons.OK, MessageBoxIcon.Error );
             } // end catch
        }
        /// <summary>
        /// Cycles through all the records in the file and closes all open file
        /// handles once the end has been reached.
        /// </summary>
        /// <param name="sender">The object sending the event</param>
        /// <param name="e">The arguments being sent by the event</param>
        private void nextButton_Click(object sender, EventArgs e)
        {
            try
            {
                // Get the next record
                string inputRecord = fileReader.ReadLine();
                string[] inputFields; // Stores each field of data

                if (inputRecord != null)
                {
                    // Split the string into records
                    inputFields = inputRecord.Split(',');

                    Record record = new Record(
                        Convert.ToInt32(inputFields[0]),
                        inputFields[1],
                        inputFields[2],
                        Convert.ToDecimal(inputFields[3]));

                    // Copy string array values to TextBox values
                    SetTextBoxValues(inputFields);
                }
                else
                {
                    // Close StreamReader
                    fileReader.Close();

                    // Close FileStream if no Records in file
                    input.Close();

                    // Enable Open File button
                    openButton.Enabled = true;

                    // Disable Next Record button
                    nextButton.Enabled = false;

                    // Clear All TextBox Controls
                    this.ClearTextBoxes();

                    // Notify user if no Records in file
                    MessageBox.Show("There are no more records left in the file.", "End of Records",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (IOException err)
            {
                MessageBox.Show("Error Reading from File: " + err.Message, "Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);

                // Enable Open File button
                openButton.Enabled = true;

                // Disable Next Record button
                nextButton.Enabled = false;
            }
        }