public IAccount AddAccount(AccountSelect pSelectAdd, string pName, string pAddress, decimal pBalance, decimal pOverdraft) { ///Look through the array of references looking for ///an appropriate place to store a reference to the new account for (uint i = 0; i < m_Accounts.Length; ++i) { if (m_Accounts[i] == null) //if there is an empty space to store an Account reference { ///this switch statement works on the enum type that was passed into this method ///it finds out what type of account you wish to create and makes an instance of it ///which can be pointed to/accessed by the IAccount array l_index, m_Accounts[i] switch (pSelectAdd) { ///in the case the variable pSelectAdd ///holds the enum type AccountSelect.Account case AccountSelect.Account: ///make a new Account class instance by calling its constructor ///with the bracketed parameters and reference to it ///from the IAccount array l_index m_Accounts[i] m_Accounts[i] = new Account(dt.Year.ToString() + "A" + sm_NextAccountNumber.ToString("X"), pName, pAddress, pBalance, pOverdraft); break; case AccountSelect.BabyAccount: ///ditto but making a new BabyAccount class instance ///the constructor has no overdraft! m_Accounts[i] = new BabyAccount(dt.Year.ToString() + "A" + sm_NextAccountNumber.ToString("X"), pName, pAddress, pBalance); break; case AccountSelect.StudentAccount: ///currentyear, "A", HexAccountNo, name, address, balance, overdraft m_Accounts[i] = new StudentAccount(dt.Year.ToString() + "A" + sm_NextAccountNumber.ToString("X"), pName, pAddress, pBalance, pOverdraft); break; default: continue; } // increment the next account number sm_NextAccountNumber++; // and return the reference to the new account return(m_Accounts[i]); } } ///if you can't find a place to store the new account ///(i.e. the bank is full) then return null. return(null); }
public Bank(string pFileName) { ///Reference l_reader can point to instances of TextReader ///we point it to null because in the finally block we wish ///to close a Stream its pointing to only if its pointing to anything StreamReader l_reader = null; try { ///point l_reader reference to new StreamReader instance given the parameter pFileName ///supplied by the constructor call, as the filename to look for l_reader = new StreamReader(pFileName); ///ask l_reader reference to find .ReadLine() method in its instance StreamReader ///which reads the first line of the txt file and assign its string value to m_Name m_Name = l_reader.ReadLine(); ///create an int to hold number of accounts ///its value will be the value of int.Parse(second line of the txt file) int l_numberOfAccounts = int.Parse(l_reader.ReadLine()); //create array of IAccount interfaces the size of the pNumberOfAccounts parameter that was passed into this constructor m_Accounts = new IAccount[l_numberOfAccounts]; ///reads the next line of the txt file and this value ///is the next account number to be assigned sm_NextAccountNumber = uint.Parse(l_reader.ReadLine()); ///counter increments when StreamReader.ReadLine() == to the const ACCOUNT_KEY ///used to move to next element of m_Accounts[l_index] array ///when an account is about to be stored in array int l_index = 0; ///while the StreamReader.ReadLine() value is == to ACCOUNT_KEY const ///ie. the line read from the StreamReader is equal to ACCOUNT_KEY while (l_reader.ReadLine() == ACCOUNT_KEY) { ///because of the way we had to override the AbstractAccount ///saving method the abstract account saves the balance & overdraft ///immediatly after ACCOUNT_KEY which is why i have to use these temp variables decimal l_tempBalance = decimal.Parse(l_reader.ReadLine()); decimal l_tempOverdraft = decimal.Parse(l_reader.ReadLine()); //this is the class name of the type of Account string l_tempTypeofAccount = l_reader.ReadLine(); switch (l_tempTypeofAccount) { ///in the case when the string (l_tempTypeofAccount) contains "Account" case "Account": m_Accounts[l_index] = new Account(l_reader.ReadLine(), l_reader.ReadLine(), l_reader.ReadLine(), l_tempBalance, l_tempOverdraft); break; ///The reference to the array of IAccounts is pointed to the subscript of the int value contained in [l_index] ///create new StudentAccount instance calling its constructor & Assign the constructed object to the array case "StudentAccount": m_Accounts[l_index] = new StudentAccount(l_reader.ReadLine(), l_reader.ReadLine(), l_reader.ReadLine(), l_tempBalance, l_tempOverdraft); break; case "BabyAccount": m_Accounts[l_index] = new BabyAccount(l_reader.ReadLine(), l_reader.ReadLine(), l_reader.ReadLine(), l_tempBalance); //no overdraft for BabyAccount (but we read the default value of zero before) break; default: throw new Exception(); } l_index++;//increment l_index because we found a line with value == ACCOUNT_KEY } } catch (Exception e) { throw e; } finally { if (l_reader != null) //if l_reader reference is pointing anywhere { l_reader.Close(); //close the stream its pointed to } } }