CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Hybrid View

  1. #1
    Join Date
    Jan 2011
    Posts
    8

    Help reading in this file

    I am trying to create a simple banking app which reads in a text file. Attached is what i have got so far. But for some reason when i click the show data button nothing happens.

    Could anyone help decode and tell me what the problem is.

    I have attached my program so far with the text file.
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2011
    Posts
    8

    Re: Help reading in this file

    It currently reads in all of the customers which is 20; but only reads 20 of accounts and transactions. There are however more transactions and accounts. I do i get it to go grab the customer then the customer accounts then the transactions of each account.

    Here is what i got so far:

    Code:
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace e_SOFT_Banking
    {
        public partial class Form1 : Form
        {
            public static ArrayList customerDetails = new ArrayList();
            public static ArrayList accountDetails = new ArrayList();
            public static ArrayList transactionDetails = new ArrayList();
    
            string inputDataFile = @"C:\e-SOFT_v1.txt";
    
            const int numCustItems = 14;
            const int numAccItems = 7;
            const int numTransItems = 5;
    
            public Form1()
            {
                InitializeComponent();
                setUpBank();
            }
    
            private void btnShowData_Click_1(object sender, EventArgs e)
            {
                showListsOfData();
            }
    
            private void setUpBank()
            {
                readData();
            }
    
            private void showListsOfData()
            {
                listBox1.Items.Clear();
    
                foreach (Customer c in customerDetails)
                    listBox1.Items.Add(c.getCustomerNumber() + " " + c.getCustomerTitle() + " " + c.getFirstName()
                                       + " " + c.getInitials() + " " + c.getSurname() + " " + c.getDateOfBirth()
                                       + " " + c.getHouseNameNumber() + " " + c.getStreetName() + " " + c.getArea()
                                       + " " + c.getCityTown() + " " + c.getCounty() + " " + c.getPostcode()
                                       + " " + c.getPassword() + " " + c.getNumberAccounts());
    
                foreach (Account a in accountDetails)
                    listBox1.Items.Add(a.getAccSort() + " " + a.getAccNumber() + " " + a.getAccNick() + " " + a.getAccDate()
                                       + " " + a.getAccCurBal() + " " + a.getAccOverDraft() + " " + a.getAccNumTrans());
    
                foreach (Transaction t in transactionDetails)
                    listBox1.Items.Add(t.getDate() + " " + t.getType() + " " + t.getDescription() + " " + t.getAmount()
                                       + " " + t.getBalAfter());
    
            }
    
            private void readData()
            {
                StreamReader readerIn = null;
                Transaction curTrans;
                Account curAcc;
                Customer curCust;
                bool anyMoreData;
                string[] customerData = new string[numCustItems];
                string[] accountData = new string[numAccItems];
                string[] transactionData = new string[numTransItems];
    
                if (readOK(inputDataFile, ref readerIn))
                {
                    anyMoreData = getNextCustomer(readerIn, customerData, accountData, transactionData);
    
                    while (anyMoreData == true)
                    {
                        curCust = new Customer(customerData[0], customerData[1], customerData[2], customerData[3], customerData[4],
                                               customerData[5], customerData[6], customerData[7], customerData[8], customerData[9],
                                               customerData[10], customerData[11], customerData[12], customerData[13]);
    
                        curAcc = new Account(accountData[0], accountData[1], accountData[2], accountData[3], accountData[4],
                                             accountData[5], accountData[6]);
    
                        curTrans = new Transaction(transactionData[0], transactionData[1], transactionData[2], transactionData[3], 
                                                   transactionData[4]);
    
                        customerDetails.Add(curCust);
                        accountDetails.Add(curAcc);
                        transactionDetails.Add(curTrans);
    
                        anyMoreData = getNextCustomer(readerIn, customerData, accountData, transactionData);
                    }
    
                    if (readerIn != null)
                        readerIn.Close();
                }
            }
    
            private bool getNextCustomer(StreamReader inNext, string[] nextCustomerData, string[] nextAccountData, string[] nextTransactionData)
            {
                string nextLine;
                int numCItems = nextCustomerData.Count();
                int numAItems = nextAccountData.Count();
                int numTItems = nextTransactionData.Count();
    
                for (int i = 0; i < numCItems; i++)
                {
                    nextLine = inNext.ReadLine();
                    if (nextLine != null)
                    {
                        nextCustomerData[i] = nextLine;
                        if (i == 13)
                        {
                            int cItems = Convert.ToInt32(nextCustomerData[13]);
                            for (int q = 0; q < cItems; q++)
                            {
                                for (int a = 0; a < numAItems; a++)
                                {
                                    nextLine = inNext.ReadLine();
                                    nextAccountData[a] = nextLine;
                                    if (a == 6)
                                    {
                                        int aItems = Convert.ToInt32(nextAccountData[6]);
                                        for (int w = 0; w < aItems; w++)
                                        {
                                            for (int t = 0; t < numTItems; t++)
                                            {
                                                nextLine = inNext.ReadLine();
                                                nextTransactionData[t] = nextLine;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                        return false;
                }
                return true;
            }
    
            private bool readOK(string readFile, ref StreamReader readerIn)
            {
                try
                {
                    readerIn = new StreamReader(readFile);
                    return true;
                }
    
                catch (FileNotFoundException notFound)
                {
                    MessageBox.Show("ERROR Opening file (when reading data in)" + " - File could not be found.\n" + notFound.Message);
                    return false;
                }
    
                catch (Exception e)
                {
                    MessageBox.Show("ERROR Opening File (when reading data in)" + "- Operation failed.\n" + e.Message);
                    return false;
                }
            }
        }
    }

  3. #3
    Join Date
    Oct 2005
    Location
    Seattle, WA U.S.A.
    Posts
    353

    Re: Help reading in this file

    Good morning, Gunner.

    It's been so long since you posted this that I'm certain you've already resolved the issue, but to get on with it .....

    At first glance it would seem that you are overwriting the storage array(s) so that only the last transaction read in, in function 'getNextCustomer', is the one that is seen by function 'readData'.

    That is, you DO read in each of the transactions, but each new transaction read in overwrites the previous transaction. so no matter how many transactions an account has, only the last is seen by the rest of the program.

    The clue ? In 'readData', set a breakpoint at the line "while (anyMoreData == true)". When you reach that breakpoint, observe the contents of, say, transactionData. What you see there is the last of the 40 transactions for that account. I think you're overwriting the previous 39 (i.e. using the same storage for all 40 transactions).

    My suggestion ? 'getNextCustomer' should continue to read as it is, but each new set of transaction strings should be used to construct a class transaction, and that newly constructed transaction should be added to a List<transaction>.

    Then at the end 'getNextCustomer' the List<transaction> is converted to an array of transactions and returned to readData. (or if you prefer to work with strings instead of objects, use a List<string>, then at the end convert the List<string> to a string[] and return that).

    just a thought ...

    best wishes.


    OldFool

  4. #4
    Join Date
    Jan 2011
    Posts
    8

    Re: Help reading in this file

    Hi thanks for the reply,

    Well, i have implemented a class for each of Customer, Account and Transaction with there get and set methods. Each item is stored in a different arraylist; customer accounts and transactions. When i use the:

    Code:
             private void showListsOfData()
            {
                listBox1.Items.Clear();
    
                foreach (Customer c in customerDetails)
                    listBox1.Items.Add(c.getCustomerNumber() + " " + c.getCustomerTitle() + " " + c.getFirstName()
                                       + " " + c.getInitials() + " " + c.getSurname() + " " + c.getDateOfBirth()
                                       + " " + c.getHouseNameNumber() + " " + c.getStreetName() + " " + c.getArea()
                                       + " " + c.getCityTown() + " " + c.getCounty() + " " + c.getPostcode()
                                       + " " + c.getPassword() + " " + c.getNumberAccounts());
    
                foreach (Account a in accountDetails)
                    listBox1.Items.Add(a.getAccSort() + " " + a.getAccNumber() + " " + a.getAccNick() + " " + a.getAccDate()
                                       + " " + a.getAccCurBal() + " " + a.getAccOverDraft() + " " + a.getAccNumTrans());
    
                foreach (Transaction t in transactionDetails)
                    listBox1.Items.Add(t.getDate() + " " + t.getType() + " " + t.getDescription() + " " + t.getAmount()
                                       + " " + t.getBalAfter());
    
            }
    This prints out ALL of the Customers (Of which there are 20), However when it prints out the contents of the Accounts and Transactions it only shows the LAST 20 from the text document, I think this has something to do with the Customer information as there are 20.

    Also, a further note, when the information is printed out it does all of the customers, then accounts, and then transactions. Is there a way to list the infomration in the approariate order so each customer has there account then there transactions, then it moves onto the next customer.

    Sorry if this may seem simple, but im not too much of a C# guru :-).

    Again any help is MUCH eppreciated.

  5. #5
    Join Date
    Oct 2005
    Location
    Seattle, WA U.S.A.
    Posts
    353

    Re: Help reading in this file

    Hi Gunner.

    Yup !

    you're reading in the last transaction ... as I may have mentioned in my earlier post, I think that MAY be because each new transaction overwrites the previous one.

    This morning I made a few changes to the existing code and now I THINK everything is displayed ... that is, all transactions for each account, for each customer. But, I rewrote it in a manner that I am comfortable with.

    That is, I'm thinkin' that each client should read himself (or herself) in, including not only the client personal data (name, address, etc) but also each account, and each transaction in each of the client's accounts. Consequently, the Client, Account, and Transaction classes have a "readSelf()" function ... you can see this in action in "readData()".

    Now ... a disclaimer .... you will see precious little defensive coding in the project. That's because I was hungry and wanted to go to breakfast and I didn't want to delay breakfast thinking about what I'd do if the input file was corrupted. I KNEW from personal inspection that it was good, and I coded in complete confidence. And besides, it's just throw-away, demo code so it needn't withstand the rigors of corrupted files, unexpected operator input and all the rest.

    So, I didn't use safe-coding practices. (I'll take a pill in the morning). Also, this is one of the rare "nice" days in Seattle so I am very eager to take one of the toys and go play out on the freeway or some narrow, winding road. So, I'll bail out here after posting my version of the project. It's working on this machine (VS2010 on Windows XP).

    Best wishes.

    OldFool
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured