/* * Reads data lines from a text file, split at ',' into * an array line[]. Then sets them as * either a Customer or VIPCustomer object, based on * length of line array. Then adds them to * an array of Customer objects. Both types of objects can be held, * as VIPCustomer is a subclass of Customer. */ static void ReadingInputFile(Customer[] custData) { try { TextReader tr = new StreamReader("C:/BIT694_TM1_3431274/input_assignment_1.txt"); //reads data from file in location in StreamReaders signature. for (int i = 0; i < custData.Length; i++) //iterates custData length number of times. { String currentLine; //declares a String variable String[] line; //declares a string array int tranCount = 0; //declares and sets an int variable currentLine = tr.ReadLine(); //sets a String variable to a line of text from StreamReaders file. line = currentLine.Split(','); //Splits a string, and adds each variable as a separate array item. if (line.Length > 5) //fires if line array contains the sixth field, VIP. { line[5] = "VIP Customer"; //changes VIP to VIP Customer VIPCustomer t = new VIPCustomer(line[0], line[1], line[2], line[3], double.Parse(line[4]), line[5], tranCount); //Sets a new VIPCustomer object to values from line array, and variable tranCount. custData[i] = t; //Adds the VIPCustomer object to the custData array. } if (line.Length < 6) //fires if array does not contain the sixth field. { Customer s = new Customer(line[0], line[1], line[2], line[3], double.Parse(line[4]), tranCount); //sets a new customer object to values from line array, and variable tranCount. custData[i] = s;//Adds the Customer object to the custData array. } } tr.Close(); //closes the TextReader. } catch (FileNotFoundException exA) //StreamReader error { Console.WriteLine("" + exA); } catch (FormatException exB)//Parse error { Console.WriteLine(exB); } catch (ArgumentNullException exC)//ReadLine and StreamReader error { Console.WriteLine(exC); } catch (ArgumentOutOfRangeException exD)//ReadLine and StreamReader error { Console.WriteLine(exD); } catch (IOException exE)//ReadLine and StreamReader error { Console.WriteLine(exE); } }
/* * Helper method that handles class specific withdrawal * method. Charges no fee, and allows accounts to become * overdrawn. * Takes in and uses that values for the custData[i] * object that was used to call it. * Returns a Customer object to Customer Class's Withdraw() method. * Protected as it is an overridden method, that is only used within class * and subclass. * See virtual method in Customer for most comments and logic. */ protected override Customer WithdrawHelper() { bool successfull = false; VIPCustomer s = new VIPCustomer(this.firstName, this.lastName, this.dob, this.accNum, this.balance, this.custType, this.tranCount); try { Console.WriteLine("Withdrawing from the account of " + firstName + " " + lastName + ". Current balance is: " + balance.ToString("C")); Console.WriteLine("Enter amount to withdraw: "); double amount = double.Parse(Console.ReadLine()); if (amount < 0)//error handling for negative amounts. { Console.WriteLine("Please enter a non-negative amount. Press any key to continue"); Console.ReadKey(); s.WithdrawHelper(); } else//allows the program to continue with positive amounts. { double totalAmount = amount; double aBalance = balance - amount; int aTranCount = tranCount + 1; Console.WriteLine("Successfully withdrew " + amount.ToString("C") + ". Current balance: " + aBalance.ToString("C")); VIPCustomer t = new VIPCustomer(this.firstName, this.lastName, this.dob, this.accNum, aBalance, this.custType, aTranCount); s = t; successfull = true; if (successfull == false)//handles unexpected processing errors. { Console.WriteLine("Sorry, due to a bank error, the transaction did not work"); return(s); } } } catch (ArgumentOutOfRangeException)//ReadLine error { Console.WriteLine("Please enter a valid amount. Press any key to continue"); Console.ReadKey(); s.WithdrawHelper(); //Catching operator error, allows program recovery. } catch (FormatException) //Parse error { Console.WriteLine("Please enter a valid amount. Press any key to continue"); Console.ReadKey(); s.WithdrawHelper();//Catching operator error, allows program recovery. } return(s); }
/* * Helper method that handles class specific deposit * method. Charges no fees. * Takes in and uses that values for the custData[i] * object that was used to call it. * Returns a Customer object to Customer Class's Deposit() method. * Protected as it is an overridden method, that is only used within class * and subclass. * See virtual method in Customer for comments and logic. */ protected override Customer DepositHelper() { VIPCustomer s = new VIPCustomer(this.firstName, this.lastName, this.dob, this.accNum, this.balance, this.custType, this.tranCount); try { Console.WriteLine("Depositing into the account of " + firstName + " " + lastName + ". Current balance is: " + balance.ToString("C")); Console.WriteLine("Enter amount to deposit: "); double amount = double.Parse(Console.ReadLine()); if (amount < 0)//error handling, does not allow negative amounts to be deposited. { Console.WriteLine("Please enter a non negative amount. Press any key to continue"); Console.ReadKey(); s.DepositHelper(); } else//allows the program to continue if number is non-negative, and after amount has been corrected. { double aBalance = balance + amount; int aTranCount = tranCount + 1; Console.WriteLine("Success fully deposited " + amount.ToString("C") + ". Current balance: " + aBalance.ToString("C")); VIPCustomer t = new VIPCustomer(this.firstName, this.lastName, this.dob, this.accNum, aBalance, this.custType, aTranCount); s = t; } } catch (ArgumentOutOfRangeException)//ReadLine error { Console.WriteLine("Please enter a valid amount. Press any key to continue"); Console.ReadKey(); s.DepositHelper(); //Catching operator error, allows program recovery. } catch (FormatException) //Parse error { Console.WriteLine("Please enter a valid amount. Press any key to continue"); Console.ReadKey(); s.DepositHelper();//Catching operator error, allows program recovery. } return(s); }