I am trying to create a simple banking system that reads in data from a text file.

Attached is a copy of the text file and another text file explaining the layout.

How would i read this information in. I need a class for customers, accounts and transactions and an arraylist to store the information.

What i know is that the customer class will have 14 items in it. However what confuses me is that a customer could have 2,3 or more accounts with a handful of transactions on each account. How would i read this information in.

Attached is the Text files.

Attachments
1458879524
Mr
Patrick
J
McCabe
19/12/1968
243a
Beacon Park Road
Mannadon
Plymouth
Devon
PL237JP
icyfish9
1
56-85-87
4.14578E+11
ours + kids!!!
01/03/2008
1523.25
500
10
02/02/2010
D/D
Freshling Nursery
650
2173.25
02/02/2010
C/R
Child benefit
-20
2153.25
05/02/2010
OTR
Vets4Pets - insurance service
-44.55
2108.7
08/02/2010
D/D
Diet Coke shippers - trade only
-800
1308.7
12/02/2010
S/O
Lecky Recky Suppliers
-145
1163.7
17/02/2010
D/D
South Waste Water
-75
1088.7
22/02/2010
D/D
What a GAS supplier
-88.56
1000.14
02/03/2010
ATM
Morriston Swansea
-120
880.14
02/03/2010
BAC
UNiversity of Plymouth
4542.36
5422.5
02/03/2010
S/O
PCC - Council Tax
-95.67
5326.83
1425478594
Mrs
Gemma
K
Reynolds
02/12/1987
27
Flat Road
Mannamead
Plymouth
Devon
PL87TS
muppetsman1a
2
54-25-36
12455871265
jennies uni account !!!
02/12/1999
-25.36
1000
8
02/02/2010
D/D
Freshling Nursery
-650
-675.36
02/02/2010
C/R
Child benefit
20
-655.36
05/02/2010
OTR
Vets4Pets - insurance service
-44.55
-699.91
08/02/2010
D/D
Diet Coke shippers - trade only
-800
-1499.91
12/02/2010
S/O
Lecky Recky Suppliers
-145
-1644.91
17/02/2010
D/D
South Waste Water
-75
-1719.91
22/02/2010
D/D
What a GAS supplier
-88.56
-1808.47
02/03/2010
ATM
Morriston Swansea
-120
-1928.47
58-69-71
12455871265
jennies uni account !!!
02/12/1999
-25.36
1000
2
02/02/2010
D/D
Freshling Nursery
-650
-675.36
02/02/2010
C/R
Child benefit
20
-655.36
" 	Customer ID number --- this is the first customer 1

" 	Customer title

" 	Customer first name

" 	Customer initials  //not required - defaults to null

" 	Customer surname

" 	Customer date of birth

" 	Customer house name or number

" 	Customer street name

" 	Customer address area  //not required - defaults to null

" 	Customer city or town name

" 	Customer county

" 	Customer postcode

" 	Customer password *minimum size is 8 characters in length

" 	The number of accounts belonging to this customer ---  equals 2 in this example



	o	Account sort code --- this is the first account of customer 1

	o	Account Number

	o	Account Nick Name //not required   defaults to null

	o	Date the account was created

	o	Current account balance

	o	Account  overdraft limit

	o	Number of transactions available for this account---  equals 2 in this example 



			Transaction Date --- this is the first transaction of account 1

			Transaction Type

			Transaction Description

			Transaction Amount

			Transaction Balance after transaction has been applied to account



			Transaction Date --- this is the second transaction of account 1

			Transaction Type

			Transaction Description

			Transaction Amount

			Transaction Balance after transaction has been applied to account



	o	Account sort code --- this is the second account of customer 1

	o	Account Number

	o	Account Nick Name //not required   defaults to null

	o	Date the account was created

	o	Current account balance

	o	Account  overdraft limit

	o	Number of transactions available for this account---  equals 2 in this example

 

			Transaction Date --- this is the first transaction of account 2

			Transaction Type

			Transaction Description

			Transaction Amount

			Transaction Balance after transaction has been applied to account



			Transaction Date --- this is the second transaction of account 2

			Transaction Type

			Transaction Description

			Transaction Amount

			Transaction Balance after transaction has been applied to account





" 	Customer ID number --- this is the second customer 

" 	Customer title

" 	Customer first name

" 	Customer initials  //not required - defaults to null

" 	Customer surname

" 	...

" 	...

" 	...

This but i dont now how to read in whats after the customer details cause as i said a customer could have 1 or 2 acocunts with loads of transactions.

Heres the main form:

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();

        string inputDataFile = @"C:\e-Softies.txt";

        const int numCustItems = 14;

        public Form1()
        {
            InitializeComponent();
            setUpBank();
        }

        private void setUpBank()
        {
            readCustomer();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            showListsOfCustomers();
        }

        private void showListsOfCustomers()
        {
            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());
        }

        private bool fileOpenOk(string readFile, ref StreamReader dataIn)
        {
            try
            {
                dataIn = 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;
            }

        }

        private bool getNextCustomer(StreamReader inNext, string[] nextCustomerData)
        {
            string nextLine;
            int numDataItems = nextCustomerData.Count();

            for (int i = 0; i < numDataItems; i++)
            {
                try
                {
                    nextLine = inNext.ReadLine();
                    if (nextLine != null)
                        nextCustomerData[i] = nextLine;
                    else
                        return false;
                }

                catch (Exception Exception)
                {
                    MessageBox.Show("There was an ERROR " + Exception.Message);
                    return false;
                }
            }
            return true;
        }

        private void readCustomer()
        {
            StreamReader inCustomer = null;
            Customer tempCustomer;
            bool anyMoreCustomers = false;
            string[] customerData = new string[numCustItems];

            if (fileOpenOk(inputDataFile, ref inCustomer))
            {
                anyMoreCustomers = getNextCustomer(inCustomer, customerData);

                {
                    tempCustomer = 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]);

                    customerDetails.Add(tempCustomer);

                    anyMoreCustomers = getNextCustomer(inCustomer, customerData);
                }
            }

            if (inCustomer != null)
                inCustomer.Close();
        }
    }
}

Heres the customer class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace e_SOFT_Banking
{
    class Customer
    {
        private string customerNumber;
        private string customerTitle;
        private string firstName;
        private string initials;  //not required - defaults to null
        private string surname;
        private string dateOfBirth;
        private string houseNameNumber;
        private string streetName;
        private string area; //not required - defaults to null
        private string cityTown;
        private string county;
        private string postcode;
        private string password;
        private int numberAccounts;

        public Customer(string theCustomerNumber, string theCustomerTitle, string theFirstName, string theInitials, string theSurname, string theDateOfBirth, string theHouseNameNumber, string theStreetName, string theArea, string theCityTown, string theCounty, string thePostcode, string thePassword, string theNumberAccounts)
        {
            customerNumber = theCustomerNumber;
            customerTitle = theCustomerTitle;
            firstName = theFirstName;
            initials = theInitials;
            surname = theSurname;
            dateOfBirth = theDateOfBirth;
            houseNameNumber = theHouseNameNumber;
            streetName = theStreetName;
            area = theArea;
            cityTown = theCityTown;
            county = theCounty;
            postcode = thePostcode;
            password = thePassword;
            setNumberAccounts(theNumberAccounts); 

        }

        public string getCustomerNumber()
        {
            return customerNumber;
        }

        public string getCustomerTitle()
        {
            return customerTitle;
        }

        public string getFirstName()
        {
            return firstName;
        }

        public string getInitials()
        {
            return initials;
        }

        public string getSurname()
        {
            return surname;
        }

        public string getDateOfBirth()
        {
            return dateOfBirth;
        }

        public string getHouseNameNumber()
        {
            return houseNameNumber;
        }

        public string getStreetName()
        {
            return streetName;
        }

        public string getArea()
        {
            return area;
        }

        public string getCityTown()
        {
            return cityTown;
        }

        public string getCounty()
        {
            return county;
        }

        public string getPostcode()
        {
            return postcode;
        }

        public string getPassword()
        {
            return password;
        }

        public int getNumberAccounts()
        {
            return numberAccounts;
        }


        public void setCustomerNumber(string inCustomerNumber)
        {
            customerNumber = inCustomerNumber;
        }

        public void setCustomerTitle(string inCustomerTitle)
        {
            customerTitle = inCustomerTitle;
        }

        public void setFirstName(string inFirstName)
        {
            firstName = inFirstName;
        }

        public void setInitials(string inInitials)
        {
            initials = inInitials;
        }

        public void setSurname(string inSurname)
        {
            surname = inSurname;
        }

        public void setDateOfBirth(string inDateOfBirth)
        {
            dateOfBirth = inDateOfBirth;
        }

        public void setHouseNameNumber(string inHouseNameNumber)
        {
            houseNameNumber = inHouseNameNumber;
        }

        public void setStreetName(string inStreetName)
        {
            streetName = inStreetName;
        }

        public void setArea(string inArea)
        {
            area = inArea;
        }

        public void setCityTown(string inCityTown)
        {
            cityTown = inCityTown;
        }

        public void setCounty(string inCounty)
        {
            county = inCounty;
        }

        public void setPostcode(string inPostcode)
        {
            postcode = inPostcode;
        }

        public void setPassword(string inPassword)
        {
            password = inPassword;
        }

        public void setNumberAccounts(string inNumberAccounts)
        {
            try
            {
                numberAccounts = Convert.ToInt32(inNumberAccounts);
            }
            catch (FormatException invalidInput)
            {
                System.Windows.Forms.MessageBox.Show("ERROR" + invalidInput.Message + "Please enter a valid number");
            }
        }

    }
}

First create classes for each of the objects you need: Customer, Account, Transaction.
Then you'll need a loop to read the file.
First you'll read the customer information noting that the last line of the customer information is how many accounts they have.
So, you'll start a second, nested, loop to read account information, noting, again, that the last line of the account information is how many transactions for this account.
Again, you'll start another, nested, loop to read transaction information.

That should give you all the information from the file in objects. Customer has a List of accounts. Accounts has a List of transactions.

You should also learn to use Properties, it will shorten your classes.

This is my loop so far, can anyone please have a look at it and see where i am going wrong.
Whenever i print out the contents of the account list into my listbox all i get is zeros. However the customer information prints out fine.

for (int i = 0; i < numCItems; i++)
            {
                    nextLine = inNext.ReadLine();
                    if (nextLine != null)
                    {
                        nextCustomerData[i] = nextLine;
                        if (i == 14)
                        {
                            for (i = 0; i < numAItems; i++)
                            {
                                nextLine = inNext.ReadLine();
                                if (nextLine != null)
                                    nextAccountData[i] = nextLine;
                            }
                        }
                    }
                    else
                        return false;
            }
            return true;
        }

Thanks again.

I have got this now. However the transaction output seems to output only the last transaction for the customer. I think it goes through to the end and overwriting the previous transactions. How do i stop it from over writing and returning each transaction loop.

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)
                    {
                        for (int a = 0; a < numAItems; a++)
                        {
                            nextLine = inNext.ReadLine();
                            nextAccountData[a] = nextLine;
                            if (a == 6)
                            {
                                for (int l = 0; l < Convert.ToInt32(nextAccountData[6]); l++)
                                {
                                    for (int t = 0; t < numTItems; t++)
                                    {
                                        nextLine = inNext.ReadLine();
                                        nextTransactionData[t] = nextLine;
                                    }
                                }
                            }
                        }
                    }                    
                }
                    else
                        return false;
            }
            return true;
        }

In your first listing(in this thread that is) I see this definition, which is fine!

const int numCustItems = 14;

Is this related to the hard coded values 14 and 13 you have in your two last listings?

Yes they are. The second one is correct (13), it is 13 as opposed to 14 as the arrays start at 0 so 13 would be the correct number to use to get item 14.

Why not just save the information in xml format rather than txt?? Much easier to read as System.Xml.XmlTextWriter(this would be the write method) and System.Xml.XmlDocument(this would be the read method) Would Save You Alot of unnecessary coding.

Why not just save the information in xml format rather than txt?? Much easier to read as System.Xml.XmlTextWriter(this would be the write method) and System.Xml.XmlDocument(this would be the read method) Would Save You Alot of unnecessary coding.

Because homework problems you don't get to choose the file format :)

Comments
Why did I not find that out?

Hi, sorry about the late reply.

Anyway, i have had another go at getting the data, Firstly i have successfully got all of the Customer information. However i seem to be only getting the last 20 Accounts and Transactions. I think this has something to do with the Customer Information as there are 20 Customers. I dunno but would it be possible to use a single arraylist to store all of the information in the order it appears in the document? and be able to access the arraylist at the right points to add, edit and delete any customer, account or transaction. OR would it be best to continue with the three seperate arraylists?

Again, you help is MUCH appreciated :-).

Here is my code now:

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 bankDetails = new ArrayList();
        public static ArrayList accDetails = new ArrayList();
        public static ArrayList tranDetails = 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)
        {
            showListsOfCust();
        }

        private void setUpBank()
        {
            readData();
        }

        private void showListsOfCust()
        {
            listBox1.Items.Clear();

            foreach (Customer c in bankDetails)
                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 accDetails)
                listBox1.Items.Add(a.getAccSort() + " " + a.getAccNumber() + " " + a.getAccNick() + " " + a.getAccDate()
                                   + " " + a.getAccCurBal() + " " + a.getAccOverDraft() + " " + a.getAccNumTrans());

            foreach (Transaction t in tranDetails)
                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]);

                    bankDetails.Add(curCust);
                    accDetails.Add(curAcc);
                    tranDetails.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;
            }
        }
    }
}

There are ofcourse the classes for the customers, accounts and transaction as well.

Instead of using so many ArrayList, you can use Dictionary:

public partial class Form1 : Form
    {
        Dictionary<string, ArrayList> dic;
        public Form1()
        {
            InitializeComponent();
            comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
            CreateMain();
        }

        private void CreateMain()
        {
            dic = new Dictionary<string, ArrayList>();
            string[] array = { "bankDetails", "accDetails", "transDetails" };
            foreach (string item in array)
            {
                dic.Add(item, PopulateArrays(item));
                comboBox1.Items.Add(item);
            }
        }

        private ArrayList PopulateArrays(string value)
        {
            ArrayList aList = new ArrayList();
            if (value == "bankDetails")
            {
                aList.AddRange(new string[] { "a1", "a2", "a3" });
            }
            else if (value == "accDetails")
            {
                aList.AddRange(new string[] { "b1", "b2", "b3" });
            }
            else if (value == "transDetails")
            {
                aList.AddRange(new string[] { "c1", "c2", "c3" });
            }
            return aList;
        }

        private void comboBox1_SelectedIndexChanged(object obj, EventArgs e)
        {
            string item = comboBox1.SelectedItem.ToString();
            listBox1.Items.Clear();
            //this:
            var varItems = from d in dic
                           where d.Key == item
                           select d.Value;
            //or this:
            //var varItems = dic.Where(a => a.Key == item).Select(a => a.Value).ToList();
            foreach (ArrayList list in varItems)
                foreach (string str in list)
                    listBox1.Items.Add(str);
        }
    }

Im afraid it has to be an arraylist to store the information. Would it be possible to store all of the information in one single arraylist, in the order it is in the text file, so:

customer
account1
trans1
account2
trans2
customer
account1
...

I have tried changing it to one arraylist but when i click the button it says there was an invalid cast at the point where it says:

foreach (Customer c in bankDetails)
...
foreach (Account a in accDetails)
...

No. Its not possible. Ok, it is, but then you run out of every order. So, no, better stay away of putting all into one ArrayList.

About my code, all is saved into arrayLists, just that all are gethered in the Dictionary( i.e: at index 0 there are bankDetails, at index 1 there are accDetails...).

But I guess you do not understand what I`m talking about dont you?
You will (later).
Mitja

Instead of using so many ArrayList, you can use Dictionary:

public partial class Form1 : Form
    {
        Dictionary<string, ArrayList> dic;
        public Form1()
        {
            InitializeComponent();
            comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
            CreateMain();
        }

        private void CreateMain()
        {
            dic = new Dictionary<string, ArrayList>();
            string[] array = { "bankDetails", "accDetails", "transDetails" };
            foreach (string item in array)
            {
                dic.Add(item, PopulateArrays(item));
                comboBox1.Items.Add(item);
            }
        }

        private ArrayList PopulateArrays(string value)
        {
            ArrayList aList = new ArrayList();
            if (value == "bankDetails")
            {
                aList.AddRange(new string[] { "a1", "a2", "a3" });
            }
            else if (value == "accDetails")
            {
                aList.AddRange(new string[] { "b1", "b2", "b3" });
            }
            else if (value == "transDetails")
            {
                aList.AddRange(new string[] { "c1", "c2", "c3" });
            }
            return aList;
        }

        private void comboBox1_SelectedIndexChanged(object obj, EventArgs e)
        {
            string item = comboBox1.SelectedItem.ToString();
            listBox1.Items.Clear();
            //this:
            var varItems = from d in dic
                           where d.Key == item
                           select d.Value;
            //or this:
            //var varItems = dic.Where(a => a.Key == item).Select(a => a.Value).ToList();
            foreach (ArrayList list in varItems)
                foreach (string str in list)
                    listBox1.Items.Add(str);
        }
    }

Great code A suggestion create yourself a folder called snippets copy this code paste in notepad and save in the snippets folder. Any code that you think you may want to reference back to for future references. Have been coding some time and still find this useful to recall methods that i have written to perform certain tasks. It will also help as you progress in your degree. And again Mitja great job on explaining how to use dictionary if he heeds my advice this will defiantly same him much time and headache later

But to use Dictionary collection and especially the queries (using Linq) on it takes time (quite a lot of it). I am still rearing, but the progress is noticeable.
Dictionary is a very powerful "tool", which can come right in many occasions.

Mitja

This article has been dead for over six months. Start a new discussion instead.