public static int RebuildBankDataFromTextFiles( ) //************************************************************************************************************************************************ { // iterate thru reading bankaccount objects from disk and add them to our LinkedList and /BankArray int count = 0; string dir = BankAccount.ReadBankFilePath( ); dir += "Textfiles\\"; string[] files = Directory.GetFiles(dir); // clear the lists- JIC DataArray.ArrayClearBank( ); // Clear ( ); BankAccount.BankAccountsLinkedList.Clear( ); foreach (var fi in files) { bool result = fi.Contains("BankObject"); if (!result) { continue; } else { string input = File.ReadAllText(fi); char[] ch = { ',' }; string[] items = input.Split(ch); BankAccount B = new BankAccount( ); B.BankAccountNumber = Convert.ToInt32(items[0]); B.CustAccountNumber = Convert.ToInt32(items[1]); B.AccountType = Convert.ToInt16(items[2]); B.Balance = Convert.ToDecimal(items[3]); B.DateOpened = Convert.ToDateTime(items[4]); B.DateClosed = Convert.ToDateTime(items[5]); B.Balance = Convert.ToDecimal(items[6]); B.Status = Convert.ToInt16(items[7]); //Write it back as OBj and TXT - yes even though we onlt just read it in. SerializeData.WriteBankAccountToDiskAndText(B, B.FullFileName); // add each one to our new List so we cna use the Enumeration later try { BankAccount.BankAccountsLinkedList.AddLast(B); DataArray.ArrayAddBank(B); if (BankAccount.BankDict != null) { if (!BankAccount.BankDict.ContainsKey(B.BankAccountNumber)) { BankAccount.BankDict.Add(B.BankAccountNumber, B); } } } catch { new Exception(" Failed to update LinkeList or Bank array in RebuildBankDataFromTextFiles at line 311"); } count++; B.Dispose( ); } } // This saves the bank LinkedList to both an object file and a Text file Lists.SaveAllBankAccountListData( ); BankListChangedEvent?.Invoke(null, "ALLDATA REBUILT FROM TEXTFILES"); return(count); }
//******************************************************************************************// //************************************************************************************************************************************************ public static int RebuildBankLinkedListFromObjects() //************************************************************************************************************************************************ { // iterate thru reading bankaccount objects from disk and add them to our linkedList int count = 0; string dir = ReadBankFilePath(); string[] files = Directory.GetFiles(dir, "*.bnk"); // clear the lists- JIC DataArray.ArrayClearBank(); // Clear ( ); foreach (var fi in files) { bool result = fi.Contains("BankObject"); if (!result) { continue; } { BankAccount B = SerializeData.ReadBankAccountFromDisk(fi); //BankAccount B = directories . GetBankObjectFromPath ( fi ); // add each one to our new List so we cna use the Enumeration later if (B != null) { try { count++; BankAccountsLinkedList.AddLast(B); DataArray.ArrayAddBank(B); if (BankAccount.BankDict != null) { if (!BankAccount.BankDict.ContainsKey(B.BankAccountNumber)) { BankAccount.BankDict.Add(B.BankAccountNumber, B); } } } catch { } new Exception(" Failed ot update LinkeList of sortedlist in RebuildCustLinkedList atliner 366"); } B.Dispose(); } } // This saves the bank LinkedList to both an object file and a Text file Lists.SaveAllBankAccountListData(); BankListChangedEvent?.Invoke(null, "ALLDATA REBUILT FROM OBJECTS"); return(count); }
public static int RebuildBankListFromText( ) { int count = 0; string fi = BankAccount.ReadBankFilePath( ) + "Textfiles\\BankAccountListData.txt"; if (File.Exists(fi)) { string input = File.ReadAllText(fi); // you gotta delete them first, else it appends the data constantly char[] ch1 = { '\r' }; char[] ch2 = { ',' }; string[] record = input.Split(ch1); for (int i = 0; i < record.Length; i++) { string str = record[i]; string[] Item2 = str.Split(ch2); if (record.Length < 6) { break; } for (int x = 0; x < record.Length; x++) { // get a sring from string[] array Item1[] BankAccount B = new BankAccount( ); if (B != null) { B.AccountType = Convert.ToInt16(Item2[x]); B.CustAccountNumber = Convert.ToInt32(Item2[x + 1]); B.Balance = Convert.ToInt32(Item2[x + 2]); B.DateClosed = Convert.ToDateTime(Item2[x + 3]); B.InterestRate = Convert.ToDecimal(Item2[x + 5]); B.Status = Convert.ToInt16(Item2[x + 6]); B.Status = Convert.ToInt16(Item2[x + 6]); BankAccount.BankAccountsLinkedList.AddLast(B); // Add this one to our linked list of customers. count++; break; } } // save linked list to disk in text format Lists.SaveAllBankAccountListData( ); } } return(count); }
//******************************************************************************************// // called from Deposit and withdrawl only so far to update BankACcount LinkedList // This automatically updates the current Bank account Balance, so beware of doing it again elsewhere public static bool UpdateBankLinkedList(Int32 Bankno, decimal deposit) { bool result = false; foreach (var B in BankAccount.BankAccountsLinkedList) { if (B.BankAccountNumber == Bankno) { // got it BankAccount.BankAccountsLinkedList.Remove(B); B.Balance += deposit; BankAccount.BankAccountsLinkedList.AddLast(B); // This saves the bank LinkedList to both an object file and a Text file Lists.SaveAllBankAccountListData(); BankListChangedEvent?.Invoke(B, "DEPOSIT"); result = true; break; } } return(result); }
//************************************************************************************************************************************************ public static void ReWriteBankLinkedList( ) //************************************************************************************************************************************************ { // iterate thru bankaccount objects and add threm to our linkedList string dir = BankAccount.ReadBankFilePath( ); string[] files = System.IO.Directory.GetFiles(dir, "*.bnk"); // iterate thru all the files on disk, startinmg at 1 // Iterate trhu them and handle as required int count = 0; foreach (var fi in files) { bool result = fi.Contains("BankObject"); if (!result) { continue; } { using BankAccount B = (BankAccount)SerializeData.ReadBankAccountFromDisk(fi); // As we have red it in, we need to delete it so we do not // duplicate it when saving all files at end of a user sessoin. if (B != null) { BankAccount.BankAccountsLinkedList.AddLast(B); // Add this one to our linked list of customers. BankTransaction newbankaccount = new BankTransaction(B.DateOpened, B.AccountType, B.CustAccountNumber, B.BankAccountNumber, B.Balance, "Customer account updated successfuly", B.Status); BankTransaction.allBankTransactions.AddLast(newbankaccount); } count++; } } // save linked list to disk in text format Lists.SaveAllBankAccountListData( ); }
//**************************************************************************************************************************** // this handles making a deposit call. creating the unique a/c #... // and creates a transaction record, and adds it to the linkedlist //**********************************************************************************************************// public static BankAccount CreateNewBankAccount(BankAccount bank, string CustNo, Int16 accounttype, decimal amount, decimal interest, string reason = "") //**********************************************************************************************************// { // create unique account number and set it in the account. bank.BankAccountNumber = GetBankAccountNumberSeed();// this post increments it's value internally bank.CustAccountNumber = Convert.ToInt32(CustNo); bank.Balance = amount; bank.AccountType = accounttype; bank.DateOpened = DateTime.Today.Date; bank.InterestRate = interest; // //This call increments the file numbering seed data bank.FileName = "BankObject" + bank.BankAccountNumber + ".bnk"; // add full filename to the object bank.FullFileName = BankAccount.ReadBankFilePath() + bank.FileName; //This call increments the total banks seed data // Even though we do NOT need its content here. IncrementTotalBanks(); // Ensure we save the file to disk for the Bank object itself SerializeData.WriteBankAccountToDiskAndText(bank, bank.FullFileName); BankTransaction newbankaccount = new BankTransaction(); newbankaccount.TransDate = DateTime.Now; newbankaccount.AccountType = bank.AccountType; newbankaccount.CustAccountNumber = bank.CustAccountNumber; newbankaccount.BankAccountNumber = bank.BankAccountNumber; newbankaccount.Transamount = (decimal)bank.Balance; if (reason == "") { newbankaccount.Notes = "Opening Balance"; } else { if (reason.Contains("Secondary Bank account for Customer")) { reason += bank.BankAccountNumber.ToString(); } newbankaccount.Notes = reason; } newbankaccount.Status = bank.Status; // Add a transaction record BankTransaction.allBankTransactions.AddLast(newbankaccount); BankAccountsLinkedList.AddLast(bank); DataArray.ArrayAddBank(bank); // Update our new Dictionary system if (BankAccount.BankDict != null) { if (!BankAccount.BankDict.ContainsKey(bank.BankAccountNumber)) { BankAccount.BankDict.Add(bank.BankAccountNumber, bank); } } // This saves the bank LinkedList to both an object file and a Text file Lists.SaveAllBankAccountListData(); //Add data ot our TWO hash tables // First the Customer hash tables try { if (CustomerBalanceHashTable.FindHashCustBalEntry(bank.CustAccountNumber.ToString())) { CustomerBalanceHashTable.DeleteHashCustBalEntry(bank.CustAccountNumber.ToString()); } CustomerBalanceHashTable.AddHashCustBalEntry(bank.CustAccountNumber.ToString(), bank.Balance); if (CustomerFileHashTable.CustFileNoHashTable.ContainsKey(bank.CustAccountNumber)) { CustomerFileHashTable.CustFileNoHashTable.Remove(bank.CustAccountNumber.ToString()); } CustomerFileHashTable.CustFileNoHashTable.Add(bank.CustAccountNumber.ToString(), bank.FullFileName.ToString()); } catch { } BankArrayChangedEvent?.Invoke(bank, "NEW BANKACCOUNT"); return(bank); }
//====================================================================== private void rewrite_Click(object sender, EventArgs e) //====================================================================== { string output = ""; if (notes.Text == "" || AccountNumber.Text == "" || AccountBalance.Text == "" || OpenDate.Text == "" || Interest.Text == "" || custno.Text == "") { MessageBox.Show("One or more data items are Empty - All fields must be completed before saving it ..." + "\nRecommeded action is to use the DropDown list to reselect a valid Bank Account", "Data Validation ERROR"); info.Text = "One or more data items are Empty - All fields must be completed before saving it ..."; return; } string stat; if (status.Text == "Active") { stat = "1"; } else { stat = "0"; } int type = AccountType.SelectedIndex + 1; // format is "Bank A/c # + "," + Customer A/c # + "," + A/c Type + "," + Balance + "," + Date Opened (short) + "," + Date Closed (short) + "," + Interest + "," + Status (0/1)+ "\r\n" output = AccountNumber.Text + "," + custno.Text + "," + type.ToString() + "," + AccountBalance.Text + ","; output += Convert.ToDateTime(OpenDate.Text).ToShortDateString() + "," + Convert.ToDateTime("01/01/0001").ToShortDateString() + "," + Interest.Text; output += "," + stat + "\r\n"; string path = BankAccount.ReadBankFilePath(); path += "Textfiles\\BankObject" + AccountNumber.Text + ".txt"; if (File.Exists(path)) { File.Delete(path); // you gotta delete them first, else it appends the data constantly } File.AppendAllText(path, output); // update the bank object path = BankAccount.ReadBankFilePath(); path += "Bankobject" + AccountNumber.Text + ".bnk"; // read the bank a/c in fresh from disk so it is clean BankAccount Bank = SerializeData.ReadBankAccountFromDisk(path); if (Bank == null) { MessageBox.Show("Bank Account Object file cannot be loaded (or saved)!", "Bank Object Error"); return; } Int32 bankno = Bank.BankAccountNumber; // update the Bank object Bank.AccountType = Convert.ToInt16(AccountType.SelectedIndex); Bank.AccountType++;// cos th reindex starts at ZERO !! Bank.Balance = Convert.ToDecimal(AccountBalance.Text); Bank.InterestRate = Convert.ToDecimal(Interest.Text); string banknostring = Bank.BankAccountNumber.ToString(); //Update the version in Bank Array int index = DataArray.ArrayFindBank(Bank); DataArray.BankNo[index] = Bank; // Update the version in our Bank LinkedList foreach (var B in BankAccount.BankAccountsLinkedList) { if (B.BankAccountNumber == bankno) { // got it, rpelace with our new one BankAccount.BankAccountsLinkedList.Remove(B); BankAccount.BankAccountsLinkedList.AddLast(Bank); break; } } // This saves the bank LinkedList to both an object file and a Text file Lists.SaveAllBankAccountListData(); // write theBank object to disk SerializeData.WriteBankAccountToDiskAndText(Bank, path); // Add a bank Transaction BankTransaction newbankaccount = new BankTransaction(Bank.DateOpened, Bank.AccountType, Bank.CustAccountNumber, Bank.BankAccountNumber, Bank.Balance, "Bank details edited from Textfile input", Bank.Status); BankTransaction.allBankTransactions.AddLast(newbankaccount); // that's it ***ALL***all Bank Data structures have been updated info.Text = "Bank Account data has been fully updated throughout System"; }
public static int RebuildBankListsFromText( ) { int count = 0; string fi = BankAccount.ReadBankFilePath( ) + "Textfiles\\BankAccountListData.txt"; if (File.Exists(fi)) { string input = File.ReadAllText(fi); // you gotta delete them first, else it appends the data constantly char[] ch1 = { '\r' }; char[] ch2 = { ',' }; // char [ ] ch3 = { '~' }; string[] record = input.Split(ch1); string[] BAccts; DataArray.ArrayClearBank( ); BankAccount.BankAccountsLinkedList.Clear( ); // record now has an array of strings that were split on \t // this means we should have at least one, possibly multiple B.Accs in the last element // iterate through and split these out of record into string[] Item2 for (int i = 0; i < record.Length; i++) { string str = record[i]; //This should give us x strings,, the first one can be ignored //, the rest will be bank account numbers cos the format is "xxxxxx,xxxx,yyyy,~4455554544~65645 // and we are after the data immediately past the first ~ string[] Item2 = str.Split(ch2); // check. The plit always gives us ~ONE element, even though it is empty if (Item2.Length == 1 && Item2[0].Length == 0) { break; // Were all done here } BAccts = Item2[7].Split('~'); // go get em (one or more B Accts in a BAccts[] string[] if (BAccts.Length > 1) { // got one or more - do something with them ? } //Baacts MAY contain a leading EMPTY string, so beware // by here , we have an array of strings of Bank accounts owned by this customer. ? for (int x = 0; x < record.Length; x++) { // get a string from string[] array Item1[] BankAccount B = new BankAccount( ); if (B != null) { B.AccountType = Convert.ToInt16(Item2[x]); B.CustAccountNumber = Convert.ToInt32(Item2[x + 1]); B.BankAccountNumber = Convert.ToInt32(Item2[x + 2]); B.Balance = Convert.ToDecimal(Item2[x + 3]); B.DateClosed = Convert.ToDateTime(Item2[x + 4]); B.InterestRate = Convert.ToDecimal(Item2[x + 6]); // We dont seem to save this ????? B.Status = 1; BankAccount.BankAccountsLinkedList.AddLast(B); // Add this one to our linked list of customers. DataArray.ArrayAddBank(B); if (BankAccount.BankDict != null) { if (!BankAccount.BankDict.ContainsKey(B.BankAccountNumber)) { BankAccount.BankDict.Add(B.BankAccountNumber, B); } } SerializeData.WriteBankAccountToDiskAndText(B, fi); count++; break; } } // save linked list to disk in text format Lists.SaveAllBankAccountListData( ); } } return(count); }