You are on page 1of 6

using using using using using using

System; System.Collections.Generic; System.Linq; System.Text; System.IO; System.Globalization;

namespace _2B { class Program { static List<Account> accounts = Account.GetAccounts(); static List<Transaction> transactions = Transaction.GetTransactions(); static void Main(string[] args) { Query3(); Query4(); Query5(); Query6(); Query7(); Query8(); Query9(); } /// <summary> /// Uses a foreach loop to display all the accounts /// </summary> static void Query1() { Console.WriteLine("All the accounts"); foreach (Account a in accounts) Console.WriteLine("{0} {1} {2:c2}", a.AccountNumber, a.Name, a.B alance); Console.Write("Press a key to continue ..."); Console.ReadKey(); } /// <summary> /// Uses a foreach loop to display all the transations /// </summary> static void Query2() { Console.WriteLine("All the transactions"); foreach (Transaction t in transactions) Console.WriteLine("{0} {1:c2} {2} {3}", t.AccountNumber, t.Amoun t, t.Time.ToShortDateString(), t.Time.ToShortTimeString()); Console.Write("Press a key to continue ..."); Console.ReadKey(); } /// <summary> /// Uses a linq query to get an ordered list of names of all the account , then /// using a foreach loop diplays the names /// /// 3 + 2 Marks /// </summary> static void Query3() { // static List<Account> accounts = Account.GetAccounts();

Console.WriteLine("Using linq to get an ordered list of names of all the account holders"); var enumerable = from a in accounts orderby a.Name select new { Name = a.Name }; foreach (var a in enumerable) { Console.WriteLine("{0}", a.Name); } Console.Write("Press a key to continue ..."); Console.ReadKey(); } /// <summary> /// Uses a linq query to get all transaction over $300.00, then /// using a foreach loop diplays the the account number, the amount, and the date and time /// /// 3 + 2 Marks /// </summary> static void Query4() { Console.WriteLine("Using linq to get all transaction over $300.00"); IEnumerable<Transaction> enumerable = from t in transactions where t.Amount > 300.0 select t; foreach (Transaction transaction in enumerable) { Console.WriteLine("{0} {1:c2} {2} {3}", new object[] { transacti on.AccountNumber, transaction.Amount, transaction.Time.ToShortDateString(), tran saction.Time.ToShortTimeString() }); } Console.Write("Press a key to continue ..."); Console.ReadKey(); } /// <summary> /// Uses a linq query to join the account and the transation list, then /// using a foreach loop diplays the account name, the transaction amoun t and time /// /// 4 + 2 Marks /// </summary> static void Query5() { Console.WriteLine("Using linq to get all name of all the accounts") ; var enumerable = from a in accounts from t in transactions where a.AccountNumber == t.AccountNumber select new { Name = a.Name, Amount = t.Amount, Time = t.Time }; foreach (var type in enumerable) { Console.WriteLine("{0} {1:c2} {2} {3}", new object[] { type.Name , type.Amount, type.Time.ToShortDateString(), type.Time.ToShortTimeString() }); } Console.Write("Press a key to continue ...");

Console.ReadKey(); } /// <summary> /// Uses a linq query to group the account based on the first four lette rs of the account number, then /// using nested foreach loops diplays the account number, name and bala nce /// /// 3 + 4 Marks /// </summary> static void Query6() { Console.WriteLine("Grouping the accounts by type"); var enumerable = from a in accounts group a by a.AccountNumber.Substring(0, 4) into bin s select new { Key = bins.Key, bins = bins }; foreach (var type in enumerable) { Console.WriteLine("{0}", (type.Key == "SAV-") ? "Saving Accounts " : "Checking Accounts"); foreach (Account account in type.bins) { Console.WriteLine("{0} {1} {2:c2}", account.AccountNumber, a ccount.Name, account.Balance); } } Console.Write("Press a key to continue ..."); Console.ReadKey(); } /// <summary> /// Writes the current time to the fiel and then saves all the transacti on information to a text file /// /// 4 Marks /// </summary> static void Query7() { string path = "transaction.sav.txt"; Console.WriteLine("Saving the transaction to the file " + path); using (StreamWriter writer = new StreamWriter(path)) { writer.WriteLine("File created on: " + DateTime.Now.ToLongTimeSt ring()); foreach (Transaction transaction in transactions) { writer.WriteLine("{0}{3}{1:c2}{3}{2}", new object[] { transa ction.AccountNumber, transaction.Amount, transaction.Time, Transaction.DELIMITER [0] }); } } Console.Write("Press a key to continue ..."); Console.ReadKey(); }

/// <summary> /// Loads all the transaction information from a text file, then /// using a foreach loop displays all the transactions /// /// 4 + 2 Marks /// </summary> static void Query8() { string path = "transaction.good.txt"; Console.WriteLine("Loading the transaction from the file " + path); List<Transaction> list = new List<Transaction>(); using (StreamReader reader = new StreamReader(path)) { while (!reader.EndOfStream) { list.Add(Transaction.Parse(reader.ReadLine())); } } Console.WriteLine("Displaying the loaded transactions"); foreach (Transaction transaction in list) { Console.WriteLine("{0} {1:c2} {2} {3}", new object[] { transacti on.AccountNumber, transaction.Amount, transaction.Time.ToShortDateString(), tran saction.Time.ToShortTimeString() }); } Console.Write("Press a key to continue ..."); Console.ReadKey(); } /// /// /// /// /// the /// public static Transaction Parse(string line) method of the Transacti on class /// i.e. throw an exception from that method. /// /// You will also need to necessary support to handle the exception. /// You do not need to change the color of the screen /// /// 5 Marks /// </summary> static void Query9() { string path = "transaction.bad.txt"; Console.WriteLine("Loading the transaction from the file " + path); List<Transaction> list = new List<Transaction>(); using (StreamReader reader = new StreamReader(path)) { while (!reader.EndOfStream) { try { list.Add(Transaction.Parse(reader.ReadLine())); } catch (Exception exception) <summary> Loads all the transaction information from a text file, then using a foreach loop displays all the transactions The text file has a problem, you will need to detect the problem in

{ ConsoleColor backgroundColor = Console.BackgroundColor; Console.BackgroundColor = ConsoleColor.Yellow; Console.WriteLine("\n" + exception.Message + "\n"); Console.BackgroundColor = backgroundColor; } } } } } //////////////////////////////////////////////////////////////////////////// ////////// ////////////////////////////////// Question 2 Account Class /////////////// ////////// public abstract class Account { public static long LastNumber { get; set; } public string AccountNumber { get; set; } public string Name { get; set; } public double Balance { get; set; } static Account() { LastNumber = 100000; } public static List<Account> GetAccounts() { List<Account> results = new List<Account>(); //results.Add(new CheckingAccount("Narendra")); //results.Add(new SavingAccount("Bhim")); //results.Add(new CheckingAccount("Ilia", 1000)); //results.Add(new CheckingAccount("Sadat", 1000)); return results; } } public class CheckingAccount : Account { public CheckingAccount(string name, double balance = 0.0) { long lastNumber = Account.LastNumber; Account.LastNumber = lastNumber + 1L; base.AccountNumber = "CHK-" + lastNumber; base.Name = name; base.Balance = balance; } } public class SavingAccount : Account { public SavingAccount(string name, double balance = 500.0) { long lastNumber = Account.LastNumber; Account.LastNumber = lastNumber + 1L; base.AccountNumber = "SAV-" + lastNumber; base.Name = name; base.Balance = balance; }

//////////////////////////////////////////////////////////////////////////// ////////// ///////////////////////////// Question 2 Transaction Class /////////////// ////////// public class Transaction { public string AccountNumber { get; set; } public double Amount { get; set; } public DateTime Time { get; set; } public static char[] DELIMITER = { '\t' }; public static List<Transaction> GetTransactions() { List<Transaction> transactions = new List<Transaction>(); transactions.Add(new Transaction() { AccountNumber = "CHK-100000", 111, Time = new DateTime(2012, 8, 9, 10, 11, 0) }); transactions.Add(new Transaction() { AccountNumber = "SAV-100001", 222, Time = new DateTime(2012, 8, 9, 10, 22, 0) }); transactions.Add(new Transaction() { AccountNumber = "CHK-100002", 333, Time = new DateTime(2012, 8, 9, 10, 33, 0) }); transactions.Add(new Transaction() { AccountNumber = "CHK-100000", 444, Time = new DateTime(2012, 8, 9, 10, 44, 0) }); transactions.Add(new Transaction() { AccountNumber = "SAV-100001", -555, Time = new DateTime(2012, 8, 9, 10, 55, 0) }); transactions.Add(new Transaction() { AccountNumber = "CHK-100000", 666, Time = new DateTime(2012, 8, 9, 10, 06, 6) }); transactions.Add(new Transaction() { AccountNumber = "CHK-100002", 777, Time = new DateTime(2012, 8, 9, 10, 07, 7) }); return transactions; }

A A A A A A A

mount = mount = mount = mount = mount = mount = mount =

public static Transaction Parse(string line) { string[] strArray = line.Split(DELIMITER); if (strArray.Length != 3) { throw new Exception("Insufficient arguments to create transactio n object"); } return new Transaction { AccountNumber = strArray[0], Amount = doubl e.Parse(strArray[1], NumberStyles.Any), Time = DateTime.Parse(strArray[2]) }; } }

You might also like