Simple Bank Application in Java console

Image result for image of java
Before you start to work, you should know about the classes and object. Here, we are going to make a simple Bank Application in java console. Here we can find two objects i.e. bankAccount and customer. Bank Account has account number, customer , balance etc and Customer has name, address, contact etc.  Now, Here we made BankAccount class which have attributes like account number, customer, balance. Here, customer is an object because customer has also name, address, contact etc. Another class named Customer and attributes are name and address. Again another class we made BankApplication in which we have implemented main method.  The code of Bank Account has shown below:-

public class BankAccount {
//attributes
//the attributes must be in private because in a bank there are many customers;
private int acc_no;
private Customer customer;
private float balance;
BankAccount(int custAcc_no, Customer cust, float deposit){
this.acc_no=custAcc_no;
this.customer=cust;
this.balance=deposit;
}
//methods
public int getAccountNo(){
return acc_no;
}
public Customer getCustomer(){
return customer;
}
public float getBalance(){
return balance;
}
public void deposit(float value){
balance=balance+value;
}
public void withdraw(float value){
if(balance<value){
System.out.println("Insufficient amount");
}
else{
balance=balance-value;
}
}
public void transfer(BankAccount accTo, float value){
if(balance<value){
System.out.println("Insufficient amount");
}
else{
accTo.deposit(value);
this.withdraw(value);
}
}
}

Above we have implemented methods. Methods means the task which the object can perform. And bank application should perform the following tasks like getBalance, deposit, transfer, withdraw, and many more. 

The code of Customer account


public class Customer {
//attributes
//the attributes must be in private because in a bank there are many customers;
private String name;
private String address;
//new make a constructor;
Customer(String custName,String custAddress){
this.name=custName;
this.address=custAddress;
}
//methods
public String getName(){
return name;
}
public String getAddress(){
return address;
}
}

In above also we have implemented methods

And in BankApplication class we have implemented main method.


public class BankApplication {
public static void main(String args[]){
Customer ram, sita;
ram=new Customer("Ram","Nepal");
sita=new Customer("Sita","Nepal");
BankAccount acc1,acc2;
acc1=new BankAccount(111,ram,4000);
acc2=new BankAccount(555,sita,5000);
System.out.println("Ram's account number:\t"+acc1.getAccountNo());
System.out.println("Ram's account name:\t"+acc1.getCustomer().getName());
System.out.println("Ram's account deposit:\t"+acc1.getBalance());
System.out.println("\n");
System.out.println("Ram withdrawn 2500");
acc1.withdraw(2500);
System.out.println("Ram's new balance is: \t"+acc1.getBalance());
System.out.println("\n");
System.out.println("Sita's account number:\t"+acc2.getAccountNo());
System.out.println("Sita's account name:\t"+acc2.getCustomer().getName());
System.out.println("Sita's account deposit:\t"+acc2.getBalance());
System.out.println("\n");
System.out.println("sita transfered 1000 to ram's account");
acc2.transfer(acc1, 1000);
System.out.println("Ram's new balance is: \t"+acc1.getBalance());
System.out.println("Sita's new balance is: \t"+acc2.getBalance());
}
}

After combining all classes  we get the following result

Ram's account number: 111
Ram's account name: Ram
Ram's account deposit: 4000.0


Ram withdrawn 2500
Ram's new balance is: 1500.0


Sita's account number: 555
Sita's account name: Sita
Sita's account deposit: 5000.0


sita transfered 1000 to ram's account
Ram's new balance is: 2500.0
Sita's new balance is: 4000.0

Above example show how object oriented java is. 

Comments

Popular posts from this blog

Logical operator in JAVA

Class and Object in Java

Loop Structure