Saturday, 6 September 2014

Exception Handling

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

namespace Triangle
{
    class InvalidTriangleException:ApplicationException
    {
        public InvalidTriangleException()
        {
        }
        public InvalidTriangleException(string ex)
            : base(ex)
        {

        }
    }
    class Triangle
    {
        double side1;
        double side2;
        double side3;

        public Triangle(double side1, double side2, double side3)
        {
            this.side1 = side1;
            this.side2 = side2;
            this.side3 = side3;
            double p=(side1+side2+side3)/2;
            double heron=p*(p-side1)*(p-side2)*(p-side3);
            if ((side1 <= 0 || side2 <= 0 || side3 <= 0))
            {
                throw new InvalidTriangleException("Sides of the triangle should not be negative");
            }
            else if (heron<0)
            {
                throw new InvalidTriangleException("Bad triangle dimesions");
            }
        }
        public double findArea()
        {
                double p = findPerimeter() / 2;
                System.Console.WriteLine("Perimeter of the triangle={0}", p);
                double area = Math.Sqrt(p * (p - side1) * (p - side2) * (p - side3));
                return area;
        }

        public double findPerimeter()
        {
            double perimeter = side1 + side2 + side3;
            return perimeter;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Triangle triangle1 = new Triangle(27.3, 11.9, 24);
                Triangle triangle2 = new Triangle(-27.3, -11.9, -24);
                Triangle triangle3 = new Triangle(1, 1, 7);
                System.Console.WriteLine("The area of the triangle1=" + triangle1.findArea());
                System.Console.WriteLine("The area of the triangle2=" + triangle2.findArea());
                System.Console.WriteLine("The area of the triangle3=" + triangle3.findArea());
            }
            catch (InvalidTriangleException ite)
            {
                System.Console.WriteLine(ite.Message);
            }
        }
    }
}

Monday, 1 September 2014

Bank example for inheritance and Polymorphism

using System;
using System.Collections;
using System.Linq;
using System.Text;

namespace Triangle
{
    class OutOfBalanceException : ApplicationException
    {
        public OutOfBalanceException(string ex):base(ex)
        {
        }
    }
    public class BankAccount4
    {
        string accountNumber = "";
        Customer accountHolder;
        protected double balance = 0.0;

        public BankAccount4()
            : this("", new Customer(), 100.0)
        {
        }

        public BankAccount4(string accountNumber, Customer customerName, double amount)
        {
            this.accountNumber = accountNumber;
            this.accountHolder = customerName;
            balance = amount;
        }

        public override string ToString()
        {
            return String.Format("BankAccount[{0},{1},{2}]", accountNumber, accountHolder.getCustomerName(), balance);
        }

        //public bool WithDraw(double amount)
        //{
        //    if (amount <= balance)
        //    {
        //        balance = balance - amount;
        //        return true;
        //    }
        //    else
        //    {
        //        Console.WriteLine("balance=" + this.balance + " account name=" + this.accountHolder.getCustomerName());
        //        Console.WriteLine("The amount to be withdrawn is greater than balance.Enter amount again");
        //        return false;
        //    }
        //}


        public void WithDraw(double amount)
        {
            if (amount <= balance)
            {
                balance = balance - amount;

            }
            else
            {
                throw new OutOfBalanceException("Insufficient balance");

            }
        }

        public virtual double CalculateInterest()
        {
            Console.WriteLine("The  balance from calculate interest=" + getBalance());
            return (1 * getBalance()) / 100;
        }

        public void CreditInterest(double balance_Amount)
        {
            Console.WriteLine("The interest =" + balance_Amount);
            Deposit(balance_Amount);
        }

        public double getBalance()
        {
            return balance;
        }

        //public void SetAccountNumber(string accountNumber)
        //{
        //    this.accountNumber = accountNumber;
        //}

        //public string getAccountNumber()
        //{
        //    return accountNumber;
        //}

        public Customer AccountHolder
        {
            set
            {
                accountHolder = value;
            }
            get
            {
                return accountHolder;
            }

        }

        public string AccountNumber
        {
            set
            {
                accountNumber = value;
            }
            get
            {
                return accountNumber;
            }
        }

        public double Balance
        {
            get
            {
                return balance;
            }

        }

        void Deposit(double amount)
        {
            balance = balance + amount;
        }

        void TransferTo(BankAccount4 bankAccountNumber, double amount)
        {
            //if (WithDraw(amount))
            //{
            //    bankAccountNumber.Deposit(amount);
            //}

            WithDraw(amount);        
            bankAccountNumber.Deposit(amount);
           
        }

        public string Show()
        {
            string m = String.Format
                         ("[Account:accountNumber={0},accountHolder={1},balance={2}]",
                          AccountNumber, AccountHolder, Balance);
            return (m);
        }

        static void Main(string[] args)
        {
            Customer customer1 = new Customer("samyu", "addr1", "1234566");
            BankAccount4 account1 = new BankAccount4("100001", customer1, 2000);

            account1.Deposit(2000);

            account1.WithDraw(2000);

            System.Console.WriteLine("The account1 customer is " + account1.Show());
            Customer customer2 = new Customer("adi", "addr2", "1234566");
            BankAccount4 account2 = new BankAccount4("100002", customer2, 3000);
            account2.TransferTo(account1, 3000);
            System.Console.WriteLine("The account2 customer is " + account2.Show());

            account1.CreditInterest(account1.CalculateInterest());
            System.Console.WriteLine("The account1 customer in savings accountis " + account1.Show());

            CurrentAccounts currentAccount = new CurrentAccounts("100001", customer1, 2000);
            currentAccount.CreditInterest(currentAccount.CalculateInterest());
            System.Console.WriteLine("The account1 customer is " + currentAccount.Show());

            OverDrafts overDraft = new OverDrafts("100001", customer1, 2000);
            overDraft.WithDraw(2000);
            overDraft.WithDraw(2000);
            overDraft.WithDraw(2000);
            overDraft.WithDraw(2000);
            overDraft.CreditInterest(overDraft.CalculateInterest());
            System.Console.WriteLine("The account1 customer is " + overDraft.Show());
        }
    }



    class CurrentAccounts : BankAccount4
    {
        public CurrentAccounts(string accountNumber, Customer customerName, double amount)
            : base(accountNumber, customerName, amount)
        {

        }
        public override double CalculateInterest()
        {
            Console.WriteLine("Inside credit interest " + getBalance());
            return (0.25 * getBalance()) / 100;
        }
    }

    class OverDrafts : BankAccount4
    {
        static double interest_rate = 0.25;
        static int overDraft_interest = 6;

        public OverDrafts(string accountNumber, Customer customerName, double amount)
            : base(accountNumber, customerName, amount)
        {

        }

        public override double CalculateInterest()
        {
            if (balance > 0)
            {
                return (getBalance() * interest_rate) / 100;
            }
            else
            {
                Console.WriteLine("Inside overdraft interest " + getBalance());
                return (overDraft_interest * getBalance()) / 100;
            }
        }

        public new void WithDraw(double amount)
        {
            Console.WriteLine("Inside overdraft");
            balance = balance - amount;
        }
    }

    class BankBranch
    {
        string BranchName;
        string BranchManager;
        ArrayList BankAccounts;

        public BankBranch(string BranchName, string BranchManager)
        {
            this.BranchName = BranchName;
            this.BranchManager = BranchManager;
            BankAccounts = new ArrayList();
        }


        public void AddAccount(BankAccount4 bankAccount)
        {
            BankAccounts.Add(bankAccount);
        }

        public void PrintCustomers()
        {
            ArrayList customers = new ArrayList();
            System.Console.WriteLine("Inside bank branch " + BankAccounts.Count);
            for (int i = 0; i < BankAccounts.Count; i++)
            {
                BankAccount4 accounts = (BankAccount4)BankAccounts[i];
                Customer cust = accounts.AccountHolder;
                int customerIndex = customers.IndexOf(cust);
                if (customerIndex < 0)
                {
                    customers.Add(cust);
                }
            }
            for (int j = 0; j < customers.Count; j++)
            {
                Customer cus = (Customer)customers[j];
                System.Console.WriteLine("Print customers " + cus);
            }
        }

        public double TotalDeposits()
        {
            double Bankbalance = 0;
            for (int i = 0; i < BankAccounts.Count; i++)
            {
                BankAccount4 bankAccount = (BankAccount4)BankAccounts[i];
                double bal = bankAccount.Balance;
                if (bal > 0)
                {
                    Bankbalance = Bankbalance + bal;
                }
            }
            return Bankbalance;
        }

        public double TotalInterestPaid()
        {
            double total_interest = 0;
            for (int i = 0; i < BankAccounts.Count; i++)
            {
                BankAccount4 bankAccount = (BankAccount4)BankAccounts[i];
                double intr = bankAccount.CalculateInterest();
                if (intr > 0)
                {
                    total_interest += intr;
                }
            }
            return total_interest;
        }

        public double TotalInterestEarn()
        {
            double totalInterestEarned = 0;
            for (int i = 0; i < BankAccounts.Count; i++)
            {
                BankAccount4 bankAccount = (BankAccount4)BankAccounts[i];
                Console.WriteLine("TotalInterestEarn {0}", bankAccount);
                double intrEarned = bankAccount.CalculateInterest();
                if (intrEarned < 0)
                {
                    totalInterestEarned = totalInterestEarned + (-intrEarned);
                }
            }
            return totalInterestEarned;
        }

        static void Main(string[] args)
        {
            try
            {
                Customer[] list = new Customer[3];

                Customer customer1 = new Customer("adi", "addr2", "1234566");
                Customer customer2 = new Customer("samyu", "addr1", "1234566");
                Customer customer3 = new Customer("xx", "add3", "12332");
                CurrentAccounts account4 = new CurrentAccounts("124578", customer3, 1000);
                list[0] = customer1;
                list[1] = customer2;
                list[2] = customer3;

                BankBranch bankBranch = new BankBranch("K.K. Nagar Branch", "Tim lee");
                bankBranch.AddAccount(new CurrentAccounts("123456", customer1, 200.02));
                bankBranch.AddAccount(new CurrentAccounts("456789", customer2, 400.02));
                bankBranch.AddAccount(new OverDrafts("1234586", customer3, -5000));
                bankBranch.AddAccount(account4);
                account4.WithDraw(2000);
                bankBranch.PrintCustomers();
                System.Console.WriteLine("Deposits={0}", bankBranch.TotalDeposits());
                System.Console.WriteLine("Total interest paid={0}", bankBranch.TotalInterestPaid());
                System.Console.WriteLine("Total interest earned={0}", bankBranch.TotalInterestEarn());

            }
            catch (OutOfBalanceException obe)
            {
                System.Console.WriteLine(obe);
            }
        }
    }
}