Now let’s create a Java class to represent those individual bank accounts. Instances of this class are created when people actually log in online. Let’s name the class Bank Account . This class has a method called “deposit” that’s used to deposit funds into the bank account. This class also has another method called “transfer” to transfer funds from the bank to another account. 

Here best way to really understand threading and the need for synchronization is a one of the example program.Here we will present an example of an online banking system to really help see the potential problems with multi - threading, and their solutions through the use of a thread synchronization constructor.

 import java.util.Scanner;
public class BankAccount {

    int accountNumber=0002;
  
    double accountBalance=500;


    // transfer funds from one account to another
    public boolean transfer (double amount)
    {
        double newAccountBalance=0;

        if( amount > accountBalance)
    {
System.out.println("You don't have sufficient balance for Transfer:"+newAccountBalance);
             return false;
    }
      
    else
    {
            newAccountBalance = accountBalance - amount;
            accountBalance = newAccountBalance;
System.out.println("You have sufficient balance for Transfer:"+accountBalance);
            return true;
        }

    }

    public boolean deposit(double amount)
    {
    double newAccountBalance;

    if( amount < 0.0)
    {
System.out.println("You are deposited -Ve vlaue:"+amount);
          return false;
    }
      
    else
    {
          newAccountBalance = accountBalance + amount;
          accountBalance = newAccountBalance;
System.out.println("Your "+amount+"is deposited  successfully  Toatl amount is:"+accountBalance);
      return true;
         }


     }

public static void main(String df[]){
BankAccount  ba=new BankAccount();
Scanner s=new Scanner(System.in);
System.out.println("Enter a Amount");
double amt=s.nextDouble();
ba.transfer(amt);
ba.deposit(amt);
}
}












D:\per>java BankAccount
Enter a Amount
300
You have sufficient balance for Transfer:200.0
Your 300.0is deposited  successfully  Toatl amount is:500.0

D:\per>java BankAccount
Enter a Amount
600
You don't have sufficient balance for Transfer:0.0
Your 600.0is deposited  successfully  Toatl amount is:1100.0

D:\per>java BankAccount
Enter a Amount
-1
You have sufficient balance for Transfer:501.0
You are deposited -Ve vlaue:-1.0

D:\per>




import java.util.Scanner;
public class BankAccount {

    int accountNumber=0002;
   
    double accountBalance=500;


    // transfer funds from one account to another
    public synchronized boolean transfer (double amount) 
    {
        double newAccountBalance=0;

        if( amount > accountBalance)
    {
System.out.println("You don't have sufficient balance for Transfer:"+newAccountBalance);
             return false;
    }
       
    else
    {
            newAccountBalance = accountBalance - amount;
            accountBalance = newAccountBalance;
System.out.println("You have sufficient balance for Transfer:"+accountBalance);
            return true;
        }

    }

    public boolean deposit(double amount)
    {
    double newAccountBalance;

    if( amount < 0.0)
    {
System.out.println("You are deposited -Ve vlaue:"+amount);
          return false;
    }
       
    else
    {
          newAccountBalance = accountBalance + amount;
          accountBalance = newAccountBalance;
System.out.println("Your "+amount+"is deposited  successfully  Toatl amount is:"+accountBalance);
      return true;
         }


     }

public static void main(String df[]){
BankAccount  ba=new BankAccount();
Scanner s=new Scanner(System.in);
System.out.println("Enter a Amount");
double amt=s.nextDouble();
ba.transfer(amt);
ba.deposit(amt);
}
}







0 comments:

Post a Comment