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

}

No comments:

Post a Comment