Sunday, 25 October 2015

Object oriented programming using JAVA

Use of inheritance and polymorphism

package com.iss.club;

public class ClubApplication {
public static void main(String[] args){
// Person p1=new Person("Hemakumar","Samyuktha");
// Person p2=new Person("Hemakumar", "Adithya");
// Person p3=new Person("Tan", "Ah","Beng");

// //Polymorphism and inheritance
Member p1=new Member("Hemakumar", "Adithya",1323245);
Member p2=new Member("Hemakumar", "Samyuktha",1234567);
Member p3=new Member("Tan", "Ah","Beng",123467);

Person[] persons=new Person[]{p1,p2,p3};
for(Person p: persons){
p.show();
}

Facility basketBall=new Facility("Basket Ball","Play basket Ball");
Facility footBall=new Facility("Foot Ball");
Facility[] facilities=new Facility[]{basketBall,footBall};
System.out.println("\n The Facilities are ");
for(Facility f:facilities){
f.show();
}

System.out.println("\n Below is use of Static");

Club c=new Club();
Member m1=c.addMembers("Hemakumar", "Adithya", "Adi");
Member m2=c.addMembers("Hemakumar", "Samyuktha", "Sam");
Member m3=c.addMembers("Tan", "Ah","Beng");
c.showMembers();

c.removeMembers(3);
System.out.println("\n After removing a member");
c.showMembers();




}
}

package com.iss.club;

public class Person {
private String surname;
private String firstName;
private String secondName;
public Person(String surname_Val,String firstName_Val,String secondName_Val){
this.surname=surname_Val;
this.firstName=firstName_Val;
this.secondName=secondName_Val;
}

public Person(String surname_Val,String firstName_Val){
this(surname_Val,firstName_Val,null);
}

public String getSurname() {
return surname;
}

public String getFirstName() {
return firstName;
}

public String getSecondName() {
return secondName;
}

public void showOld(){
String fullName="";
if(secondName!=null){
fullName+=" "+getSecondName();
//the above is equal to
//fullname=fullname+" "+getSecondName();
}
fullName+=" "+getSurname();
System.out.println(fullName);
}

public void show(){
System.out.println(this);
}


public String toString(){
String s;
if(secondName!=null){
   s="Person ["+getFirstName()+" "+getSurname()+" "+getSecondName()+"]";
}else{
s="Person ["+getFirstName()+" "+getSurname()+"]";
}
return s;
}
}


package com.iss.club;

public class Facility {
String facilityName;
String shortDescription;
public Facility(String facName,String shortDesc){
this.facilityName=facName;
this.shortDescription=shortDesc;
}
public Facility(String facName){
this(facName,null);
}
public String getFacilityName() {
return facilityName;
}
public String getShortDescription() {
return shortDescription;
}

public void show(){
System.out.println(this);
}

public String toString(){
String facilityName="";
if(shortDescription!=null){
facilityName=getFacilityName()+" ("+getShortDescription()+")";
}else{
facilityName=getFacilityName();
}
return facilityName;
}
}

package com.iss.club;

public class Member extends Person{
int memberNumber;


public Member(String firstName,String surname,int memberNumber_Val){
super(firstName,surname);
this.memberNumber=memberNumber_Val;
}

public Member(String firstName,String surname,String secondNameVal, int memberNumber_Val){
super(firstName,surname,secondNameVal);
this.memberNumber=memberNumber_Val;
}

public int getMemberNumber(){
return memberNumber;
}

public String toString(){
return super.toString()+" "+getMemberNumber();
}



}


package com.iss.club;



public class Club {
private  int currentNumber=0;
private Member[] members=new Member[0];
private static int index;
private static int ARRAY_SIZE_INCREMENT=1;



public Member addMembers(String surname,String firstName,String secondName){
ensureArraySize();
Member m=new Member(surname,firstName,secondName,++currentNumber);
members[currentNumber-1]=m;
return m;
}

public void ensureArraySize(){
if (currentNumber >= members.length) {
Member[] newMembers;
int newSize=currentNumber+ARRAY_SIZE_INCREMENT;
newMembers=new Member[newSize];
for(int i=0;i<currentNumber;i++){
newMembers[i]=members[i];
}
members=newMembers;
}

}

public void showMembers(){
for(int i=0;i<members.length;i++){
if(members[i]!=null){
System.out.println(members[i]);
}
}
}

public void removeMembers(int memNum){
members[--memNum]=null;
}

}

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);
            }
        }
    }
}