The Programming Project: inner class
Showing posts with label inner class. Show all posts
Showing posts with label inner class. Show all posts

Tuesday, September 10, 2013

Nested Class Demonstration

This program will generate a Bank A/C class for some customers with provision for Locker and FixedDeposit classes, both of which will be inner-class of Bank class. One deducts a fixed locker fee from the outer class balance variable and the other one adds interest to the balance variable, calculated on the amount to be fixed.



import java.util.*;
public class BankAccount
    {
    public static void main(String[] args)
        {
        String choice="";
        Bank[] customer = new Bank[10];
        for(int i=0;i<10;i++)
            customer[i]=new Bank();
            int k=0;
            boolean flag=false;
            Scanner in = new Scanner(System.in);
        do
            {
            customer[k].setData();
            for(int i=0;i<k;i++)
                  if(customer[k].getAccountNumber()==customer[i].getAccountNumber())
                    flag=true;
            if(flag==true)
                {
                System.out.println("Account Number already exisits:");
                flag=false;
                continue;       
                }
            k++;   
            System.out.println(" Want to add another record yes/no:");
            choice = in.next();
            }while("YES".equalsIgnoreCase(choice));
            flag=false;
            int i;
            System.out.println(" Do you want to get locker facility  yes/no:");
            choice = in.next();
                if("YES".equalsIgnoreCase(choice))
                    {
                    System.out.println(" Enter the A/C number:");
                    int ac = in.nextInt();
                    for(i=0;i<k;i++)
                         if(ac==customer[i].getAccountNumber())
                         {
                         flag=true;
                         break;
                         }
                    if(flag)
                        customer[i].displayLocker();
                    else
                        System.out.println(" Invalid A/C Number:");
                    }
            flag=false;
            System.out.println(" Do you want to fix an amount yes/no:");
            choice = in.next();
                if("YES".equalsIgnoreCase(choice))
                    {
                    System.out.println(" Enter the A/C number:");
                    int ac = in.nextInt();
                    for(i=0;i<k;i++)
                         if(ac==customer[i].getAccountNumber())
                         {
                         flag=true;
                         break;
                         }
                    if(flag)
                        {
                        System.out.println(" Enter the amount you want to deposite:");
                        double amt = in.nextDouble();
                        customer[i].displayFixedDeposit(amt);
                        }
                    else
                        System.out.println(" Invalid A/C Number:");
                    }
        }
    }
class Bank
    {
    Bank()
        {
        accountNumber=0;
        balance=0.0;
        name="";
        }
    public void setData()
        {
        Scanner in = new Scanner(System.in);
        System.out.println(" Enter the name:");
        name=in.nextLine();
        System.out.println(" Enter the A/C number:");
        accountNumber=in.nextInt();
        System.out.println(" Enter the balance:");
        balance=in.nextDouble();
        }
    public void displayFixedDeposit(double amt)
        {
        FixedDeposit fd = new FixedDeposit();
        fd.addInterest(amt);
        fd.afterAdd();
        }
    public void displayLocker()
        {
        Locker loc = new Locker();
        loc.deduct();
        loc.afterDeduct();
        }
    public int getAccountNumber()
        {
        return accountNumber;
        }
    public void getData()
        {
        System.out.println("A/C number:"+accountNumber+" Name:"+name+" Balance:"+balance);
        }
    class Locker{
            void deduct()
                {   
                balance -= lockerFee;
                }
            void afterDeduct()
                {
                System.out.println(" After Deduction:");
                getData();            
                }
            final double lockerFee = 30.0;
                }
    class FixedDeposit{
              void addInterest(double amt)
                  {
                  balance += (8.25*amt)/100;
                  }
              void afterAdd()
                  {
                  System.out.println(" After adding Interest:");
                  getData();
                  }
              final double interestRate = 8.25;
              }
    private int accountNumber;
    private double balance;
    private String name;
    }