我将如何阅读此内容?

时间:2011-03-20 12:09:26

标签: c# file text

我有这个包含客户,帐户和交易信息的文本文件。每个项目都按照刚刚提到的顺序显示。

Dr
James
M
Magee
12/05/1978
12
Dakedon Avenue
Mutley
Plymouth
Devon
PL87TS
babiesrus123
2
54-25-36
2455871265
jennies uni account !!!
02/12/1999
-25.36
2000
8
02/02/2010
OTR
Scottish Highlands Vacations Inc.
-650
-675.36
02/02/2010
C/R
Mobiles Inc  - month 4  cash back 
20
-655.36
05/02/2010
OTR
C.Oldgam Travel Insurance service
-44.55
-699.91
08/02/2010
POS
Wrapping Up.Net
-800
-1499.91
12/02/2010
OTR
Carphone Warehome
-145
-1644.91
17/02/2010
D/D
Leather World - loan repayment
-75
-1719.91
22/02/2010
D/D
park deans - loan repayment
-88.56
-1808.47
02/03/2010
ATM
Stirling University
-120
-1928.47
58-69-71
12455871265
johns uni account !!!
02/12/1999
-25.36
1500
6
02/02/2010
D/D
SUBar Monthly Bill- Plymouth
-259.99
-285.35
02/02/2010
C/R
Monthly allowance
450
164.65
02/02/2010
D/D
SUBar Monthly Bill- Plymouth
-325.36
-160.71
02/02/2010
C/R
Monthly allowance
450
289.29
02/02/2010
D/D
SUBar Monthly Bill- Plymouth
-78.36
210.93
02/02/2010
C/R
Monthly allowance
450
660.93

在上面的文件片段中是: 1客户 2帐户 每个帐户都有多个交易。

我如何将这些信息读入单身? ArrayList数据结构,同时在类(Customer,Account,Transaction)中使用get和set方法。令我困惑的是我如何读取这些数据并跟踪客户有多少账户以及账户有多少交易。

我认为需要做的是一个大的嵌套循环来读取文本文件中的信息并跟踪每个项目有多少信息。

Loop through Customer 1
    Loop through Customer Account 1
       Loop through Customer Account 1 Transactions
    Loop through Customer Account 2
       Loop through Customer Account 2 Transactions
Loop through Customer 2
...

关于文本文件:

•   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
•   ...
•   ...
•   ...

任何帮助都非常令人满意!

感谢。

4 个答案:

答案 0 :(得分:2)

  1. 定义客户,帐户和交易的类
  2. 使用System.IO.File.OpenText()获取TextReader并使用reader.ReadLine()获取您的行。
  3. 使用ArrayList,它已经过时了。使用List<Customer>List<Account>
  4. 在查看上一个问题之后编辑:

    您需要采取的主要步骤是将Acoounts集合添加到Customer类,将Transactions集合添加到Account类。

    只是草图:

    public class Customer
    {
        public string CustomerNumber { get; set; }
        // other properties
    
        public List<Account> Accounts { get; private set; }  // collection property
    
        public Customer()  // default constructor
        {
             Accounts = new List<Account>();
    
        }
    
    }
    

    现在,当您创建帐户时,可以使用currentCustomer.Accounts.Add(currentAccount);

答案 1 :(得分:1)

那么也许这会有所帮助:

使用通用帐户列表属性更新Customer类。

 private int numberAccounts;

        public List<Account> Accounts {
            get;
            protected set;
        }

        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);
            Accounts = new List<Account>();
        }

使用通用交易清单属性更新帐户

public List<Transaction> Transactions {
    get;
    protected set;
}

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);
    Transactions = new List<Transaction>();
}

注释掉你的“readData()”方法并替换为:

 private void readData() {
            ///read all the data into memory (if you can)
            string[] _data = File.ReadAllLines(inputDataFile);
            Queue<String> _lines = new Queue<string>();
            foreach (string _line in _data) {
                _lines.Enqueue(_line.Trim()); //put it into a queue for convience
            }

            ///iterate through the data and extract the details based on 
            ///known record delimiters.
            while (_lines.Count > 0) {
                Customer _customer = new Customer(
                    _lines.Dequeue(),
                    _lines.Dequeue(),
                    _lines.Dequeue(),
                    _lines.Dequeue(),
                    _lines.Dequeue(),
                    _lines.Dequeue(),
                    _lines.Dequeue(),
                    _lines.Dequeue(),
                    _lines.Dequeue(),
                    _lines.Dequeue(),
                    _lines.Dequeue(),
                    _lines.Dequeue(),
                    _lines.Dequeue(),
                    _lines.Dequeue());
                int _numberOfAccounts = _customer.getNumberAccounts();
                for (int i = 1; i <= _numberOfAccounts; i++) {
                    Account _account = new Account(
                        _lines.Dequeue(),
                        _lines.Dequeue(),
                        _lines.Dequeue(),
                        _lines.Dequeue(),
                        _lines.Dequeue(),
                        _lines.Dequeue(),
                        _lines.Dequeue());
                    int _numberOfTransactions = _account.getAccNumTrans();
                    for (int j = 1; j <= _numberOfTransactions; j++) {
                        Transaction _transaction = new Transaction(
                            _lines.Dequeue(),
                            _lines.Dequeue(),
                            _lines.Dequeue(),
                            _lines.Dequeue(),
                            _lines.Dequeue());
                        _account.Transactions.Add(_transaction);
                    }
                    _customer.Accounts.Add(_account);
                }

                ///update the legacy part of the system.
                bankDetails.Add(_customer);
                foreach (Account _account in _customer.Accounts) {
                    accDetails.Add(_account);
                    foreach (Transaction _transaction in _account.Transactions) {
                        tranDetails.Add(_transaction);
                    }
                }
            }
        }

编辑:注释掉你的“showListsOfCust”方法并把它放在它的位置

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 c.Accounts) {
                    listBox1.Items.Add(a.getAccSort() + " " + a.getAccNumber() + " " + a.getAccNick() + " " + a.getAccDate()
                                       + " " + a.getAccCurBal() + " " + a.getAccOverDraft() + " " + a.getAccNumTrans());

                    foreach (Transaction t in a.Transactions) {
                        listBox1.Items.Add(t.getDate() + " " + t.getType() + " " + t.getDescription() + " " + t.getAmount()
                                           + " " + t.getBalAfter());
                    }
                }
            }
        }

答案 2 :(得分:0)

这样的东西?

While more customers
  Read Customer Information
  Read Number of Accounts
    Loop for each Account
       Read Account information
       Read transaction count
       Loop for each transaction
         Read transaction information
       End loop
    End loop
End loop

答案 3 :(得分:0)

可以使用嵌套循环按照您的建议读取它。这取决于它需要的可扩展性,如果您确定数据的格式永远不会改变,那么您不需要制作灵活的系统。

为了存储数据,我的印象是你想要一个数据库,但假设你想将它存储在内存中的对象中,这是你可以做到的一种方式。

您可以为客户,帐户和交易提供所有ID号,并将其存储在词典中。然后,您可以向客户帐户提供其帐户的ID号列表,同样为该帐户提供其交易列表。这样,帐户和交易数据本身就不会嵌套在客户中。还有其他方法,这是一种方式。

例如:

// Not global variables, you'd have to put them in a class somewhere
Dictionary<int,Customer> Customers;
Dictionary<int,Account> Accounts;

class Customer
{
    List<int> MyAccounts;
}

要访问帐户,如果您拥有该ID,则可以使用Accounts [id]; 例如:

foreach(int id in MyAccounts)
{
    var account = Accounts[id];
    // Do whatever with the account here
}

希望这是有帮助的,如果我在某种程度上忽略了这一点,请告诉我。由于您引用了ArrayLists和getter / setter,我假设您具有Java背景。这里的List就像一个ArrayList。 Getters / setter在C#中以“Properties”的形式完成。字典就像Java中的地图。