C# 我怎样才能开始读这本书呢?

C# 我怎样才能开始读这本书呢?,c#,file,text,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. -6

我有一个包含客户、账户和交易信息的文本文件。每个项目都按刚才提到的顺序逐行显示

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数据结构,同时在类(客户、帐户、事务)中使用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
•   ...
•   ...
•   ...
非常感谢您的帮助

谢谢。

像这样的东西

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
  • 为客户、帐户和交易定义类
  • 使用
    System.IO.File.OpenText()
    获取文本阅读器,使用
    reader.ReadLine()
    获取行
  • 不要使用ArrayList,它已经过时了。使用
    列表
    列表
  • 查看上一个问题后,编辑:

    似乎您需要采取的主要步骤是向Customer类添加acounts集合,并向Account类添加Transactions集合

    只是一个草图:

    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>();
    
        }
    
    }
    
    公共类客户
    {
    公共字符串CustomerNumber{get;set;}
    //其他属性
    公共列表帐户{get;private set;}//集合属性
    public Customer()//默认构造函数
    {
    账户=新列表();
    }
    }
    

    现在,当您创建了一个帐户后,您可以使用
    currentCustomer.Accounts.Add(currentcount)

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

    对于存储数据,我的印象是您需要一个数据库,但假设您希望将其存储在内存中的对象中,这里有一种方法

    您可以为客户、账户和交易提供一个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;
    }
    
    foreach(int id in MyAccounts)
    {
        var account = Accounts[id];
        // Do whatever with the account here
    }
    

    希望这是有帮助的,如果我在某些方面错过了要点,一定要让我知道。由于您提到了ArrayList和getter/setter,我假设您有Java背景。这里的列表类似于ArrayList。getter/setter在C#中以“Properties”的形式完成。字典就像Java中的地图。

    好吧,也许这会有帮助:

    使用通用帐户列表属性更新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>();
            }
    

    你已经有了解决方案的想法,为什么不着手解决呢?这是一种预先确定数字(账户、交易)的格式,更清晰@亨克,这和他问的另一个问题有关。似乎他的读取方法没有按预期的方式工作。@user667430所以客户后面跟着一个或多个相关账户,账户后面跟着一个或多个相关交易?@user667430这个程序是从Java移植来的吗?@Kurtnelle下面的答案确实工作正常,读取了所有数据。在没有嵌套循环的情况下,数据会像客户、账户和交易一样打印到列表框中;不按每个客户拥有的内容分组。我尝试过嵌套循环,但当我将数据内容输出到屏幕上时,这似乎不起作用,因为foreach循环访问每个ArrayList会打印出客户和帐户,但似乎会在文本文件中为每个帐户提供每笔交易。是的,这起作用了,谢谢。但是当我在foreach语句中打印时,我会打印每个客户下的每种类型的数据。所以:客户1,然后是文件中的每一笔交易。客户2,然后是文件中的每笔交易。那么您想先打印客户,然后打印他的帐户,然后打印该帐户的交易,然后打印到下一个客户?
    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>();
    }
    
     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);
                        }
                    }
                }
            }
    
    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());
                        }
                    }
                }
            }