Showing posts with label Program. Show all posts
Showing posts with label Program. Show all posts

Friday, October 16, 2015

Latest Banking Application (All except Persistence)

Account.java

import java.util.*;
import java.io.*;
public class Account implements Serializable{
private int accountId;
private String holderName;
private double balance;
private Date creationDate;
public static final String TRA_DEPOSIT = "deposit";
public static final String TRA_WITHDRAW = "withdraw";

ArrayList<Transaction> passbook = new ArrayList<Transaction>();

Account(String holderName, int accountId, double balance){
this.holderName = holderName;
this.accountId = accountId;
this.balance = balance;
this.creationDate = new Date();
}
Account(String holderName, int accountId){
this(holderName, accountId, 0);
}

public void setAccountId(int id){
this.accountId = id;
}
public int getAccountId(){
return this.accountId;
}
public void setName(String name){
this.holderName = name;
}
public String getName(){
return this.holderName;
}
public double getBalance(){
return this.balance;
}
public String toString(){
return "\nAccount No. :"+this.accountId+"\nHolder Name: "+this.holderName+"\nCurrent Balance: "+this.balance+"\n";
}
public boolean deposit(double amt){
if(amt<=0){
return false;
}
else{
this.balance += amt;
Account.Transaction t1 = this.new Transaction(amt, Account.TRA_DEPOSIT);
this.passbook.add(t1);
return true;
}
}
public boolean withdraw(double amt){
if(amt<=0){
return false;
}
else if(amt>balance){
return false;
}
else{
this.balance -= amt;
Account.Transaction t1 = this.new Transaction(amt, Account.TRA_WITHDRAW);
this.passbook.add(t1);
return true;
}
}

public class Transaction{
double amt;
String type;
Date transactionDate;

public Transaction(double amt,String type){
this.amt = amt;
this.type = type;
this.transactionDate = new Date();
}
public String toString(){
return "Amount: "+ ((this.type.equals(Account.TRA_DEPOSIT))?"+":"-") + this.amt+" \nTransaction Date: "+ transactionDate;
}

}

boolean printPassbook(){
System.out.println(this+"\n----------------------------------\n");
for(Transaction t : this.passbook){
System.out.println(t+"\n\n");
}
return true;
}
}



Bank.java

import java.util.*;
public class Bank{
String name;
int nextAccountId = 1001;
ArrayList<Account> accounts = new ArrayList<Account>();

Bank(String name){
this.name = name;
}

Account createAccount(String holderName, double balance){
Account a1 = new Account(holderName, nextAccountId++, balance);
this.accounts.add(a1);
return a1;
}

double getAccountBalance(int accountId){
Account a = findAccount(accountId);
if(a==null){
return -1;
}
else{
return a.getBalance();
}
}
boolean removeAccount(int accountId){
for(int i = 0; i < accounts.size(); i++){
if(accounts.get(i).getAccountId() == accountId){
accounts.remove(i);
return true;
}
}
return false;
}
boolean deposit(int accountId, double amt){
Account a = findAccount(accountId);
if(a==null){
return false;
}
else{
return a.deposit(amt);
}
}
boolean withdraw(int accountId, double amt){
Account a = findAccount(accountId);
if(a==null){
return false;
}
else{
return a.withdraw(amt);
}
}
Account findAccount(int accountId){
for(Account a: accounts){
if(a.getAccountId() == accountId){
return a;
}
}
return null;
}
boolean printPassbook(int accountId){
Account a = findAccount(accountId);
if(a==null){
return false;
}
return a.printPassbook();
}


}


TestBank.java

import java.util.*;
class TestBank{
static Bank b1;
static Scanner sc;
public static void main(String[] args){
if(!findAndLoadBank()){
createBank();
}
sc = new Scanner(System.in);
while(true){
int selection = showMenu();
if(selection==6){
System.out.println("Thank you !!");
saveBank();
break;
}
if(selection >0 && selection<6){
handleSelection(selection);
}
}
}
public static int showMenu(){
System.out.println("Select an option:\n-------------------------\n1. Create Accoun\n2. Deposit\n3. Withdraw\n4. Check Balance\n5. Print Passbook\n6. QUIT\n");
try{
int selection = Integer.parseInt(sc.nextLine());
return selection;
}catch(Exception e){
System.out.println("Invalid Selection\n");
return 0;
}
}
public static void handleSelection(int selection){
System.out.println("I am currently handling : "+ selection);
switch(selection){
case 1:

{
System.out.println("Enter your name: ");

String name = sc.nextLine();

System.out.println("Enter initial balance: ");
String balance = sc.nextLine();
try{
Account a = b1.createAccount(name, Double.parseDouble(balance));
System.out.println(a);
}catch(Exception e){
System.out.println("Invalid Data, Please try again !!");
}
}

break;
case 2:

{
System.out.println("Enter your account no: ");
String acno = sc.nextLine();
System.out.println("Enter deposit amount: ");
String amount = sc.nextLine();
try{
boolean confirm = b1.deposit(Integer.parseInt(acno), Double.parseDouble(amount));
System.out.println(confirm?"Deposit Successful":"Deposit Failed" );
}catch(Exception e){
System.out.println("Invalid Data, Please try again !!");
}

}
break;
case 3:

{
System.out.println("Enter your account no: ");
String acno = sc.nextLine();
System.out.println("Enter withdraw amount: ");
String amount = sc.nextLine();
try{
boolean confirm = b1.withdraw(Integer.parseInt(acno), Double.parseDouble(amount));
System.out.println(confirm?"Withdraw Successful":"Withdraw Failed" );
}catch(Exception e){
System.out.println("Invalid Data, Please try again !!");
}

}
break;
case 4:
{
System.out.println("Enter your account no: ");
String acno = sc.nextLine();
double balance = b1.getAccountBalance(Integer.parseInt(acno));
System.out.println("Balance is: "+ balance);
}
break;
case 5:
{
System.out.println("Enter your account no: ");
String acno = sc.nextLine();
b1.printPassbook(Integer.parseInt(acno));
}
break;
default:


}



}
public static void createBank(){
b1 = new Bank("SBI Alkapuri");
}

}

Using Generic Collections

import java.util.*;

class GenericPractice{
    public static void main(String args[]){
        ArrayList<String> al=new ArrayList<String>();
        al.add(12);
        al.add("world");
        String s=(String)al.get(0);
        System.out.println(s);
        
    }
}

Creating Generic Type (class)

import java.util.*;

class Message<T>{
    private T msg;
    public void setMsg(T t){
        msg=t;
    }
    public T getMsg(){
        return msg;
    }
    public static void main(String[] args){
        
        Message<Object> m1=new Message<String>();
        m1.setMsg("Hello");
        String s1=m1.getMsg();
        System.out.println(s1);
        
        Message<Integer> m2=new Message<Integer>();
        m2.setMsg(12);
        int i=m2.getMsg();
        System.out.println(i);
    }
}

Program to display all files in given folder with permissions and size

import java.io.*;

class FileUtility{
    public static void main(String[] args){
        File f1=new File(args[0]);
        if(!f1.exists()){
            System.out.println("File does not exists");
            return;
        }
        if(f1.isFile()){
            System.out.println("Folder required");
            return;
        }
        File[] arr=f1.listFiles();
        for(int i=0;i<arr.length;i++){
            System.out.print(arr[i].getName()+"\t");
            if(arr[i].canRead()){
                System.out.print("R");
            }           
            if(arr[i].canWrite()){
                System.out.print("W");
            }           
            if(arr[i].canExecute()){
                System.out.print("X");
            }           
            System.out.print("\t");
            System.out.print(arr[i].length()+" Bytes\t");
            if(arr[i].isFile()){
                System.out.print("File");
            }
            else{
                System.out.print("Folder");
            }
           
            System.out.println();
        }
    }
}

Program: Read from console and write everything to a file

import java.io.*;

class ConsoleRead{
    public static void main(String[] args){
        try{
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            FileWriter fw=new FileWriter("CreateFile.txt");
            String s1;
            while(!(s1=br.readLine()).equals("STOP")){
                fw.write(s1+"\r\n");
            
            }
            
            fw.close();
            br.close();
            
        }catch(IOException e){
            System.out.println("Exception occured");
        }
    }
}

Program: Write to file

import java.io.*;

class WriteToFile{
    public static void main(String[] args){
    
        try{
        
        File f1=new File("WriteToFile.txt");
        FileWriter fw=new FileWriter(f1);
        fw.write("Hello\r\nWorld");
        fw.close();
                
        }catch(Exception e){
            System.out.println("Some Error");
        }
    }
}

Program: Read From File and print contents of file

import java.io.*;
class ReadFromFile{
    public static void main(String[] args){
        try{
            FileReader fr=new FileReader("ReadFromFile.java");
            int i;
            while(true){
                i=fr.read();
                if(i!=-1){
                    System.out.print((char)i);
                }
                else{
                    break;
                }
            }
            fr.close();
        }catch(IOException e){
            System.out.println("Ecxeption occurred");
        }
    }
}

Program: Read from File using BufferedReader

import java.io.*;
class BuufferedRead{
    public static void main(String[] args){
        try{
            BufferedReader br=new BufferedReader(new FileReader("ReadFromFile.txt"));
            String line;
            while(true){
                line=br.readLine();
                if(line!=null){
                    System.out.println(line);
                }
                else{
                    break;
                }
            }
            br.close();
            
            
        }catch(IOException e){
            System.out.println("Ecxeption occurred");
        }
    }
}

Read from Console using BufferedReader (Similar to scanf function in C lang)

import java.io.*;

class Sample2{
    public static void main(String[] args){
        try{
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            
            System.out.println("Enter your name:");
            String name=br.readLine();
            
            System.out.println("Enter your age:");
            String age=br.readLine();
            
            System.out.println("---------------------");
            System.out.println("Your name:"+name);
            System.out.println("Your age:"+age);
            System.out.println("---------------------");
            
            br.close();
        }catch(IOException e){
            System.out.println("Error Occurred");
        }
    }
}

Monday, October 5, 2015

Banking Application Code

Account.java

import java.util.*;1
class Account{
private int accountId;
private String holderName;
private double balance;

ArrayList<Transaction> passbook = new ArrayList<Transaction>();

Account(String holderName, int accountId, double balance){
this.holderName = holderName;
this.accountId = accountId;
this.balance = balance;
}
Account(String holderName, int accountId){
this(holderName, accountId, 0);
}

public void setAccountId(int id){
this.accountId = id;
}
public int getAccountId(){
return this.accountId;
}
public void setName(String name){
this.holderName = name;
}
public String getName(){
return this.holderName;
}
public double getBalance(){
return this.balance;
}
public boolean deposit(double amt){
if(amt<=0){
return false;
}
else{
this.balance += amt;
return true;
}
}
public boolean withdraw(double amt){
if(amt<=0){
return false;
}
else if(amt>balance){
return false;
}
else{
this.balance -= amt;
return true;
}
}

class Transaction{
double amt;
String type;

}


}





Bank.java
import java.util.*;
class Bank{
String name;
int nextAccountId = 1001;
ArrayList<Account> accounts = new ArrayList<Account>();

Bank(String name){
this.name = name;
}

Account createAccount(String holderName, double balance){
Account a1 = new Account(holderName, nextAccountId++, balance);
this.accounts.add(a1);
return a1;
}

double getAccountBalance(int accountId){
Account a = findAccount(accountId);
if(a==null){
return -1;
}
else{
return a.getBalance();
}
}
boolean removeAccount(int accountId){
for(int i = 0; i < accounts.size(); i++){
if(accounts.get(i).getAccountId() == accountId){
accounts.remove(i);
return true;
}
}
return false;
}
boolean deposit(int accountId, double amt){
Account a = findAccount(accountId);
if(a==null){
return false;
}
else{
return a.deposit(amt);
}
}
boolean withdraw(int accountId, double amt){
Account a = findAccount(accountId);
if(a==null){
return false;
}
else{
return a.withdraw(amt);
}
}
Account findAccount(int accountId){
for(Account a: accounts){
if(a.getAccountId() == accountId){
return a;
}
}
return null;
}

}

Tuesday, August 18, 2015

Static and Dynamic Binding

class A{
int i= 10;
void m1(){
System.out.println("This is A");
}
}

class B extends A{
int i = 12;
void m1(){
System.out.println("This is B");
}
}

class Demo{
public static void main(String[] args){
A a1 = new B();
System.out.println(a1.i);
a1.m1();
}
}



Output:
------------------
10
This is B



Summary:
------------------
Variable binding is static in java
Method binding is dynamic in java

Fan class

class Fan{
public final static int SLOW = 11;
public final static int MEDIUM = 12;
public final static int FAST = 13;

int speed=Fan.SLOW;
boolean fOn = false;
int radius = 4;
String color = "blue";

Fan(){

}


Fan(int speed, boolean status, int radius, String color){
this.speed= speed;
this.fOn = status;
this.radius = radius;
this.color= color;
}


void display(){
if(this.fOn==true){
System.out.println("\n\nSpeed is "+this.speed);
System.out.println("Color is "+this.color);
System.out.println("Radius is "+this.radius);


}else{
System.out.println("\n\nColor is "+this.color);
System.out.println("Radius is "+this.radius);
System.out.println("Fan is Off");

}

}

}

class TestFan{
public static void main(String[] args){
Fan f1 = new Fan();
Fan f2 = new Fan(Fan.MEDIUM, true, 6, "Brown");


f1.display();
f2.display();


}
}

Vegetable Class

abstract class Vegetable{
String color;
}
class Potato extends Vegetable{
Potato(String color){
this.color = color;
}
/*public String toString(){
return "Name: Potato, Color:"+this.color;
}
*/

}
class Brinjal extends Vegetable{

}
class Tomato extends Vegetable{

}
class TestVegetable{
public static void main(String[] args){
Potato p1 = new Potato("Blue");
Potato p2 = new Potato("Red");

System.out.println(p1);
System.out.println(p2);


}
}


Wednesday, July 29, 2015

Java Code to Print age of user from B.Date

import java.util.*;
class Demo{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);       // to fetch user input
System.out.println("Enter B.date (dd-mm-yyyy):");
String bdate = sc.nextLine();
String year = bdate.substring(6); // to extract yyyy
int y = Integer.parseInt(year);  // convert into numberic value
int age = 2015 - y;                   // find age
System.out.println("Your age is "+ age+" hopefully");

// ans may not be correct as we need to take month into account as well.
}
}

Java Code to write method that returns both max and min number from an array of integers

class Demo{
static int[] getMaxMin(int[] arr){
int max = arr[0], min = arr[0];
for(int i = 0 ; i< arr.length; i++){
if(arr[i]>max){
max = arr[i];
}
if(arr[i]<min){
min = arr[i];
}
}
int[] ans = new int[2];
ans[0] = max;
ans[1] = min;
return ans;
}
public static void main(String[] args){
int[] arr = new int[]{1,2,3,6,4,3,9,7,0};
int[] ans  = Demo.getMaxMin(arr);
System.out.println("Max is: "+ ans[0]+" Min is: "+ ans[1]);
}

}