Sunday, February 8, 2015

Extension Methods and Lambda Expressions


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



namespace ExtensionMethodsAndLambdas_Lab
{
    public class Account
    {
        private string _accountNumber;

        public string AccountNumber
        { get { return _accountNumber; } }

        private decimal _balance;

        public decimal Balance
        { get { return _balance; } }

        public Account(string acntNumber, decimal balance)
        {
            _accountNumber = acntNumber;
            _balance = balance;
        }

        public override string ToString()
        {
            return String.Format("Account Num: {0}".PadRight(40) + "Current Balance: {1:C}", _accountNumber, _balance);
        }
    }
}
//===================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ExtensionMethodsAndLambdas_Lab
{
    public partial class Form1 : Form
    {
        List<Account> acntList = new List<Account>(20);
        List<Account> currentList;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Account a = new Account("98044", 5000m);
            acntList.Add(a);
            a = new Account("98043", 10000m);
            acntList.Add(a);
            a = new Account("98042", 20000m);
            acntList.Add(a);
            a = new Account("98041", 25000m);
            acntList.Add(a);
            a = new Account("98040", 30000m);
            acntList.Add(a);
            a = new Account("98039", 35000m);
            acntList.Add(a);
            a = new Account("98038", 40000m);
            acntList.Add(a);
            a = new Account("98037", 45000m);
            acntList.Add(a);
            a = new Account("98036", 50000m);
            acntList.Add(a);
            a = new Account("98035", 55000m);
            acntList.Add(a);
            a = new Account("98034", 60000m);
            acntList.Add(a);
            a = new Account("98033", 65000m);
            acntList.Add(a);
            a = new Account("98032", 75000m);
            acntList.Add(a);
            a = new Account("98031", 85000m);
            acntList.Add(a);
            a = new Account("98030", 95000m);
            acntList.Add(a);
            a = new Account("98029", 105000m);
            acntList.Add(a);
            a = new Account("98028", 110000m);
            acntList.Add(a);
            a = new Account("98027", 115000m);
            acntList.Add(a);
            a = new Account("98026", 120000m);
            acntList.Add(a);
            a = new Account("98025", 125000m);
            acntList.Add(a);
            a = new Account("98024", 230000m);
            acntList.Add(a);
            a = new Account("98023", 1300m);
            acntList.Add(a);
            a = new Account("98022", 255000m);
            acntList.Add(a);
            a = new Account("98021", 25300m);
            acntList.Add(a);
            a = new Account("98020", 23000m);
            acntList.Add(a);
            a = new Account("98019", 2110000m);
            acntList.Add(a);
            a = new Account("98018", 8500m);
            acntList.Add(a);
            a = new Account("98017", 80401m);
            acntList.Add(a);
            a = new Account("98016", 584899m);
            acntList.Add(a);
            a = new Account("98015", 68485m);
            acntList.Add(a);
            a = new Account("98014", 15478m);
            acntList.Add(a);
            a = new Account("98013", 15059m);
            acntList.Add(a);
            a = new Account("98012", 98000m);
            acntList.Add(a);

            Display(acntList);
            currentList = acntList;
        }

        private void btn_AvgAccountBalance_Click(object sender, EventArgs e)
        {
            decimal averageBalance = acntList.Average(acnt => acnt.Balance);
            MessageBox.Show("Average Account Balance: " + averageBalance.ToString("C"));
        }

        private void btn_GetAccountsWithBalanceBelow_Click(object sender, EventArgs e)
        {
            decimal userValue;
            if (decimal.TryParse(txtAcntBalance.Text, out userValue))
            {
                List<Account> AccountsBelowValue = new List<Account>();
                AccountsBelowValue = acntList.FindAll(acnt => acnt.Balance < userValue);
                Display(AccountsBelowValue);
                currentList = AccountsBelowValue;
            }
        }

        private void Display(List<Account> list)
        {
            listBox1.Items.Clear();
            foreach (Account act in list)
            {
                listBox1.Items.Add(act.ToString());
            }
        }

        private void btn_SortByBalance_Click(object sender, EventArgs e)
        {
            currentList.Sort((a1, a2) => a1.Balance.CompareTo(a2.Balance));
            Display(currentList);
        }

        private void btn_SortByAccount_Click(object sender, EventArgs e)
        {
            currentList.Sort((a1, a2) => a1.AccountNumber.CompareTo(a2.AccountNumber));
            Display(currentList);
        }

        private void btnHighLow_Click(object sender, EventArgs e)
        {
            //currentList.Sort((a1, a2) => a1.Balance.CompareTo(a2.Balance));
            //Account lowBalance = currentList[0];
            //Account highBalance = currentList[currentList.Count - 1];

            //decimal lowBalance = currentList.Min(account => account.Balance);
            //decimal highBalance = currentList.Max(account => account.Balance);

            Account lowA = acntList.Find(acc => acc.Balance == acntList.Min(account => account.Balance));
            Account highA = acntList.Find(acc => acc.Balance == acntList.Max(account => account.Balance));

            listBox1.Items.Clear();
            listBox1.Items.Add(lowA.ToString());
            listBox1.Items.Add(highA.ToString());
        }
    }
}
//===================================================
//Advanced c#
//Lab Assignment. Due Wednesday 1 / 21/ 15
//Create New Project, Add a class Account with accountNumber and balance
//In Form1 Create a List of Accounts
//In Form Load, Initialize the list with 20 different accounts
//Use the List Methods, defined in the List<T> class, as well as its extension methods, 
//defined in the Enumerator class. 
//(you should be able to see all the metods and extension methods in the intellisence)
//For the questions below use .Net methods and lambda expressions
//Add a button to determine and display the average balance of all the balances. 
//Add a button to get all the accounts whose balance is below a certain value (provided by user). Display
//Add a button to Sort the accounts by balances and display
//Add button Sort the accounts by accountNumbers. Display
//Add button to Get and Display the account with the smallest and Highest balances



No comments:

Post a Comment