从文件中读取数据

时间:2011-03-19 16:09:12

标签: c# file text

如果您想下载应用程序,请输入以下链接:

Simple banking app

Text file with data to read

我正在尝试创建一个简单的银行应用程序,从文本文件中读取数据。到目前为止,我已经设法阅读了所有客户,其中有20个。但是,当阅读帐户和交易内容时,它只读取20但文本文件中还有更多内容。

这是我到目前为止所拥有的。我认为它与getNextCustomer方法中的嵌套for循环有关。

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;
            }
        }
    }
}

我还有三个类别用于客户,一个用于帐户,一个用于交易,按顺序排列。

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");
            }
        }

    }
}

账户:

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

namespace e_SOFT_Banking
{
    class Account
    {
        private string accSort;
        private Int64 accNumber;
        private string accNick;
        private string accDate;  //not required - defaults to null
        private double accCurBal;
        private double accOverDraft;
        private int accNumTrans;

        public Account(string theAccSort, string theAccNumber, string theAccNick, 
                       string theAccDate, string theAccCurBal, string theAccOverDraft, 
                       string theAccNumTrans)
        {
            accSort = theAccSort;
            setAccNumber(theAccNumber);
            accNick = theAccNick;
            accDate = theAccDate;
            setAccCurBal(theAccCurBal);
            setAccOverDraft(theAccOverDraft);
            setAccNumTrans(theAccNumTrans);
        }

        public string getAccSort()
        {
            return accSort;
        }

        public long getAccNumber()
        {
            return accNumber;
        }

        public string getAccNick()
        {
            return accNick;
        }

        public string getAccDate()
        {
            return accDate;
        }

        public double getAccCurBal()
        {
            return accCurBal;
        }

        public double getAccOverDraft()
        {
            return accOverDraft;
        }

        public int getAccNumTrans()
        {
            return accNumTrans;
        }


        public void setAccSort(string inAccSort)
        {
            accSort = inAccSort;
        }

        public void setAccNumber(string inAccNumber)
        {
            try
            {
                accNumber = Convert.ToInt64(inAccNumber);
            }
            catch (FormatException invalidInput)
            {
                System.Windows.Forms.MessageBox.Show("ERROR" + invalidInput.Message + "Please enter a valid number");
            }
        }

        public void setAccNick(string inAccNick)
        {
            accNick = inAccNick;
        }

        public void setAccDate(string inAccDate)
        {
            accDate = inAccDate;
        }

        public void setAccCurBal(string inAccCurBal)
        {
            try
            {
                accCurBal = Convert.ToDouble(inAccCurBal);
            }
            catch (FormatException invalidInput)
            {
                System.Windows.Forms.MessageBox.Show("ERROR" + invalidInput.Message + "Please enter a valid number");
            }
        }

        public void setAccOverDraft(string inAccOverDraft)
        {
            try
            {
                accOverDraft = Convert.ToDouble(inAccOverDraft);
            }
            catch (FormatException invalidInput)
            {
                System.Windows.Forms.MessageBox.Show("ERROR" + invalidInput.Message + "Please enter a valid number");
            }
        }

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

交易:

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

namespace e_SOFT_Banking
{
    class Transaction
    {
        private string date;
        private string type;
        private string description;
        private double amount;  //not required - defaults to null
        private double balAfter;


        public Transaction(string theDate, string theType, string theDescription,
                       string theAmount, string theBalAfter)
        {
            date = theDate;
            type = theType;
            description = theDescription;
            setAmount(theAmount);
            setBalAfter(theBalAfter);
        }

        public string getDate()
        {
            return date;
        }

        public string getType()
        {
            return type;
        }

        public string getDescription()
        {
            return description;
        }

        public double getAmount()
        {
            return amount;
        }

        public double getBalAfter()
        {
            return balAfter;
        }

        public void setDate(string inDate)
        {
            date = inDate;
        }

        public void setType(string inType)
        {
            type = inType;
        }

        public void setDescription(string inDescription)
        {
            description = inDescription;
        }

        public void setAmount(string inAmount)
        {
            try
            {
                amount = Convert.ToDouble(inAmount);
            }
            catch (FormatException invalidInput)
            {
                System.Windows.Forms.MessageBox.Show("ERROR" + invalidInput.Message + "Please enter a valid number");
            }
        }

        public void setBalAfter(string inBalAfter)
        {
            try
            {
                balAfter = Convert.ToDouble(inBalAfter);
            }
            catch (FormatException invalidInput)
            {
                System.Windows.Forms.MessageBox.Show("ERROR" + invalidInput.Message + "Please enter a valid number");
            }
        }
    }
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您的问题始于以下

string[] customerData = new string[numCustItems];
string[] accountData = new string[numAccItems];
string[] transactionData = new string[numTransItems];

使用此结构和调用

anyMoreData = getNextCustomer(readerIn, customerData, accountData, transactionData);

一个客户只能获得一个accountData和一个transactionData。

请重新设计您的代码,以便您的数据对象知道如何从数据流中读取它们。