2016-02-25 23 views
-3

我正在爲讀取文本文件的ATM項目編寫代碼。該文本文件是這樣寫的格式:如何比較文本文件中的數組中的字符串

帳號名*端子*查詢餘額*節省平衡

所以,我通過它變成把他們分爲數組的方法。

public void linesplitter (string line) 
    { 

    string[] split = line.Split('*'); 
     account = int.Parse(info [0]) ; 
     name = info [1] ; 
     pin = int.Parse(info [2]) ; 
     check = info [3] ; 
     saving = info [4] ; 
} 

因此,當用戶輸入帳號時,它會進入登錄屏幕,在那裏他們將輸入與該帳號關聯的名稱和PIN。

我的問題是如何將名稱和PIN與帳號進行比較?

+0

你是什麼意思「比較」? –

+0

如果名稱和密碼已關聯,或者是使用帳號 – user3558676

+0

註冊的名稱,則必須將該信息存儲在數據庫中,並使用查詢確定名稱和密碼是否與帳戶號碼相關聯。你也可以使用XML文件。 –

回答

1

適合你的樣品。

List<AccountDetails> accountDetails = new List<AccountDetails>(); 

     //Declare class to store your info 
     public class AccountDetails 
     { 
      public String account { get; set; } 
      public String name { get; set; } 
      public String pin { get; set; } 
      public String check { get; set; } 
      public String saving { get; set; } 
     } 

     //This function is a just an example, you need to expand it to fit your solution 
     public void linesplitter(String line) 
     { 
      //You can place your text file reader code here. 
      //And call this function from Page_Load. 
      AccountDetails dtl = new AccountDetails(); 
      string[] split = line.Split('*'); 
      dtl.account = int.Parse(info[0]); 
      dtl.name = info[1]; 
      dtl.pin = int.Parse(info[2]); 
      dtl.check = info[3]; 
      dtl.saving = info[4]; 

      //Every line from your text file will store to the Global List as declare in 1st line. 
      accountDetails.Add(dtl); 
     } 


     //this is the function that trigger by your Button Event. 
     //Pass in parameters to this function to get account details. 
     //So the output for this function, something like 
     //if(result == null) 
     // login fail. etc 
     public AccountDetails GetAccountDetails(String AccountNumber, String Name, String Pin) 
     { 
      var result = (from a in accountDetails 
          where a.account == AccountNumber 
          && a.name == Name 
          && a.pin == Pin 
          select a).FirstOrDefault();            

      return result; 
     } 
+1

我相信他是一個新的c-sharp,所以linq to object語句會給他帶來一些麻煩。你介意簡化它,讓每個人都瞭解代碼嗎?這個問題尤其適用於新人。 – Jannik

+0

通常我不直接爲新人提供答案,希望他們能讀入和寫出自己。順便說一句,我只是簡單地鍵入一些代碼作爲他的參考:) –

0

我更喜歡你創建一個類帳戶類似如下:

public class Account 
    { 
     public int account { get; set; } 
     public string name { get; set; } 
     public int pin { get; set; } 
     public string check { get; set; } 
     public string saving { get; set; } 
    } 

進而形成accountObjects的列表,其中包含從文件中的所有賬戶的細節,其中賬戶信息存儲潔具。迭代列表以檢查是否有任何匹配的憑證庫可用。這個完整場景的代碼將是這樣的:

 List<Account> ATMAccountList = new List<Account>(); 
     foreach (var line in File.ReadAllLines("your Path Here")) 
     { 
      ATMAccountList.Add(linesplitter(line)); 
     } 
     int accountNumberInput = 123, pinInput = 6565; 
     // This will be the input values from the user 
     bool matchFound = false; 
     foreach (var accountItem in ATMAccountList) 
     { 
      if (accountItem.account == accountNumberInput && accountItem.pin == pinInput) 
      { 
       matchFound = true; 
       break; 
      } 
     } 
     if (matchFound) 
     { 
      //Proceed with account 
     } 
     else 
     { 
      // display error ! in valid credential 
     }   

凡方法linesplitter將被定義爲像這樣:

public Account linesplitter(string line) 
    { 
     Account AccountObject = new Account(); 
     string[] info = line.Split('*'); 
     AccountObject.account = int.Parse(info[0]); 
     AccountObject.name = info[1]; 
     AccountObject.pin = int.Parse(info[2]); 
     AccountObject.check = info[3]; 
     AccountObject.saving = info[4]; 
     return AccountObject; 
    } 

注:

你可以這樣做通過使用LINQ

 if (ATMAccountList.Where(x => x.account == accountNumberInput && x.pin == pinInput).Count() == 1) 
     { 
      //Proceed with account 
     } 
     else 
     { 
      // display error ! in valid credentials 
     } 
+0

我相信他是一個新的c-sharp,所以linq to object語句會給他一些麻煩。你介意簡化它,讓每個人都瞭解代碼嗎?這個問題尤其適用於新人。 – Jannik

+0

@Jannik:我已經用循環內的條件檢查更新了答案 –

0

創建一個類來存儲所有帳戶信息。在閱讀該行時,創建該類的實例並填寫信息並存儲在列表中。現在比較名稱和別針,你可以使用Linq查詢。

Class AccountInfo 
    { 
    public int AccountNumber {get;set;} 
    public string Name {get;set;} 
    public int AtmPin {get;set;} 
    public string Check {get;set;} 
    public string Saving {get;set;} 
    } 

    // Now in your Main program, use that class like this 
    List<AccountInfo> accountInfoList= new List<AccountInfo>(); 
    foreach(string line in File.ReadAllLines("filename")) 
    { 
     AccountInfo ai =new AccountInfo(); 
     string[] split = line.Split('*'); 
      ai.AccountNumber = int.Parse(info [0]) ; 
      ai.Name = info [1] ; 
      ai.AtmPin = int.Parse(info [2]) ; 
      ai.Check = info [3] ; 
      ai.Saving = info [4] ; 
      accountInfoList.Add(ai); 
    } 

    // Some code to accept name and pin from user 

    // Now compare name and pine like this 

    var validationSuccessful = accountInfoList.Any(x=>x.AccountNumber==userEnteredAccountNumber && 
         x.Name==userEnteredName && 
         x.AtmPin==userEnteredPin); 

    if(validationSuccessful) 
    // Display next screen 
    else 
    // Display message log in failure 

請記住這是最簡單的方法。我假設你正在做一些實踐項目..真正的ATM項目將有很多的安全性,並會使用鹽和散列機制做Pin的比較。它肯定會有一些數據庫或服務。

由於Jannik建議,這是你怎麼能這樣做比較沒有Linq查詢

bool validationSuccessful=false; 

foreach(AccountInfo accountInfo in AccountInfoList) 
{ 
    if(accountInfo.AccountNumber==userEnteredAccountNumber && 
          accountInfo.Name==userEnteredName && 
          accountInfo.AtmPin==userEnteredPin) 
     { 
     validationSuccessful = true; 
     break; 
     } 
} 
+0

我相信他是一個新的c-sharp,所以linq to object語句會給他帶來一些麻煩。你介意簡化它,讓每個人都瞭解代碼嗎?這個問題將特別針對更新的人 – Jannik

+0

@Jannik更新的代碼以簡單的方式進行比較,但您應該始終選擇最佳的代碼...所以我還保留Linq查詢代碼以及 – Viru

+0

@ un-lucky感謝您告訴我 – Viru

0

作爲一個選項,你必須收集從文本文件中的所有信息爲包裝類實例的集合,用戶enteres帳戶,然後後號碼,您可以通過號碼在您的收藏中找到一件物品,如果此類物品存在,您希望獲得所需的別針和名稱,進行驗證檢查等等。

例如創建類:

class AccountInfo 
{ 
    public long AccountNumber { get;set; } 
    public int Pin { get;set; } 
    public string Name { get;set; } 
    public string Check { get;set; } 
    public string Saving { get;set; } 
} 

,並轉換YOUT文本數據轉換成項目的集合:

var textLines = sourceTextData.Split('\r\n').ToList(); 
var accountsCollection = textLines.Select(i => 
new AccountInfo 
{ 
    AccountNumber = Convert.ToDouble(i.Split('*')[0]), 
    Pin = Int32.Parse(i.Split('*')[1]), 
    Name = i.Split('*')[2], 
    Check = i.Split('*')[3], 
    Saving = i.Split('*')[4] 
}).ToList(); 

或代替一個列表,你可以準備一本字典,在字典鍵會帳號。

0

下面是可能給你一些想法基本的控制檯應用程序....

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.IO; 
using System.Linq; 
using System.Xml.Linq; 

namespace ConsoleApplication1 
{ 
class Program 
{ 
    //private static XElement AccountData = new XElement("Root"); 
    private static DataSet dataSet = new DataSet(); 
    static void Main(string[] args) 
    { 
     DataSet dataSet = new DataSet(); 
     string[] readText = File.ReadAllLines(@"<Path to your account data file>\AccountData.txt"); 
     string strAccountNumber = string.Empty; 
     string strPin = string.Empty; 
     string strName = string.Empty; 

     dataSet.ReadXml(new StringReader(new XElement("Root", 
          from str in readText 
          let fields = str.Split('*') 
          select new XElement("AccountData", 
           new XAttribute("AccountNo", fields[0]), 
           new XAttribute("Name", fields[1]), 
           new XAttribute("Pin", fields[2]), 
           new XAttribute("ChkBalance", fields[3]), 
           new XAttribute("SavBalance", fields[4]) 
          ) 
         ).ToString())); 

     do 
     { 
      Console.WriteLine("Please enter your Account Number (press 'Q' to exit)"); 
      strAccountNumber = Console.ReadLine().ToLower(); 

      if (dataSet.Tables[0].Select(string.Format("AccountNo = '{0}'", strAccountNumber)).Count() == 1) 
      { 
       Console.WriteLine("Please enter your Pin"); 
       strPin = Console.ReadLine().ToLower(); 
       Console.WriteLine("Please enter your Name"); 
       strName = Console.ReadLine(); 

       if (dataSet.Tables[0].Select(string.Format("Pin = '{0}' AND Name = '{1}'", strPin, strName)).Count() == 1) 
       { 
        DataRow[] result = dataSet.Tables[0].Select(string.Format("Pin = '{0}' AND Name = '{1}'", strPin, strName)); 
        foreach (DataRow row in result) 
        { 
         Console.WriteLine(string.Format("Account Info for :: {0}", strName)); 
         Console.WriteLine("{0}, {1}, {2}, {3}, {4}", row[0], row[1], row[2], row[3], row[4]); 
        } 
       } 
       else 
       { 
        Console.WriteLine("Incorrect Details"); 
       } 
      } 
     } while (strAccountNumber != "q"); 
    } 
} 
} 

不要忘了替換路徑.....

string[] readText = File.ReadAllLines .... 

與路徑到您的數據.....

相關問題