Write a program called BankAccountJava This program contains

Write a program called BankAccount.Java. This program contains the following methods: 1) Database Methods a. initializeDB : Makes a connection to the database and returns connection object b. findAccountType (int accountNumber): i. Create a select query to find account type based on account number ii. Returns account type c. selectBalance (int accountNumber, char accountType): i. Using accountNumber and accountType, create select query for finding account balance. ii. Return account balance d. updateBalance (int accountNumber, char accountType, double amount): i. Using accountNumber, accountType, and amount update the account balance 2) Non-DB Methods a. findBalance(int accountNumber) i. Call findAccountType to get the account type 1. If account doesn’t exist, display message ii. Call selectBalance to get the balance b. withdraw (int accountNumber, double amount): i. If the amount being withdrawn is greater than current balance, display message “Amount exceeds balance” ii. If the amount being withdrawn is less than current balance, reduces balance by the amount. c. deposit (int accountNumber, double amount): i. Update balance with the amount being deposited 3) Use try…catch blocks 4) Use Prepared Statements Functionality: Enter Account Number: Enter your choice: (1) Find Balance (2) Withdraw Amount (3) Deposit Amount Finding Balance: 1) Use initializeDB method to make database connection that returns connection object 2) Call findBalance a. Get account type i. Call findAccountType method to find the account type based on the account number entered ii. If account number doesn’t exist display appropriate message b. Get Account balance i. Call selectBalance method to find the balance 3) Display account details using JOptionPane

Solution

import java.sql.*;
public class BankAccount{


//Database methods
private Connection connection;
private url = \"jdbc:odbc:bankaccount\";
private username = \"anonymous\";
private password = \"admin\";
private char accType;
private int accountNumber;
private int accountType;
private double balance;
public Connection initializeDB( )
{
try{
Class.forName(\"sun.jdbc.odbc.JdbcOdbcDriver\");
Connection connection1 = DriverManager.getConnection(url, username, password);
}
catch(ClassNotFoundException cnfex){
System.err.println(\"Failed to load JDBC/ODBC driver.\");
cnfex.printStackTrace();
System.exit(1);
}
catch(SQLException sqlex){
System.err.println(\"Unable to connect to database\");
sqlex.printStackTrace();
}
return connection1;
}
public char findAccountType(int accountNumber)
{
try{
String query = \"SELECT accountType FROM Account WHERE accountNumber = \'\" + accountNumber + \"\'\";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);
char acType = resultSet.getString(\"accountType\");
return acType;
}
catch(SQLException sqlex){
sqlex.printStackTrace();
}
}
public selectBalance(int accountNumber, char accountType)
{
try{
String query = \"SELECT balance FROM Account WHERE (accountNumber = \'\" + accountNumber + \"\') AND (accountType =\'\"+
accountType + \"\')\";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);
double acType = resultSet.getString(\"balance\");
return acType;
}
catch(SQLException sqlex){
sqlex.printStackTrace();
}
}

public void updateBalance(int accountNumber, char accountType, double amount)
{
try{
String query = \"UPDATE Account SET \" + \"accountNumber=\'\" + accountNumber + \"\', accountType =\'\"
+ accountType +\"\', balance =\'\" + amount + \"\'\";
Statement statement = connection.createStatement();
int result = statement.executeUpdate(query);
if(result == 1){
JOptionPane.showMessageDialog(null, \"Amount updated successfully for the given account\");
}
else{
JOptionPane.showMessageDialog(null, \"Amount update failed!\");
}
}
catch(SQLException sqlex){
sqlex.printStackTrace();
}
}

//Non-db methods
//Assume details in arrays

public double findBalance(int accountNumber)
{
  
if(accountNumber != this.accountNumber){
JOptionPane.showMessageDialog(\"The account with the given account number is not available\");
return null;
}
else{
return balance;
}
}

public boolean withDraw(int accountNumber, double amount)
{
  
if(accountNumber != this.accountNumber){
JOptionPane.showMessageDialog(\"The account with the given account number is not available. Amount cannot be withdrawn\");
return false;
}   
else{
if(balance < amount){
JOptionPane.showMessageDialog(\"Amount Exceeds balance. \"+
\"Hence withdrawal cannot be done.\");
return false;
}
else{
JOptionPane.showMessageDialog(\"Successfully withdrawn the given amount. Remaining balance is \"+
balance - amount);
balance -= amount;
}
}
}

public boolean deposit(int accountNumber, double amount)
{

if(accountNumber != this.accountNumber){
JOptionPane.showMessageDialog(\"The account with the given account number is not available. Amount cannot be deposited\");
return false;
}
else{
balance += amount;
}
}

public void accountMenu()
{
while(1){
try{
String firstOption =
JOptionPane.showInputDialog(\"Enter your choice using the integer - 1. Find balance, 2. Withdraw amount, 3. Deposit Amount\" );
int option = Integer.parseString(firstOption);
String secondOption = JOptionPane.showInputDialog(\"Enter your account number\");
int accountNo = Integer.parseString(secondOption);

initializeDB();
char acType = findAccountType(accountNo);
double balanceAmount = selectBalance(accountNo, acType);
if(option == 1){
JOptionPane(\"Your balance is \" + balanceAmount);
}
if(option == 2){
String option2 = JOptionPane.showInputDialog(\"Enter the amount to be withdrawn: \");
double withdrawAmount = Double.parseDouble(option2);
withDraw(accountNo, withdrawAmount);
}
if(option == 3){
String option3 = JOptionPane.showInputDialog(\"Enter the amount to be deposited: \");
double depositAmount = Double.parseDouble(option3);
deposit(accountNo, depositAmount);
}
else{
JOptionPane.ShowMessageDialog(\"Your option is wrong. Cannot be processed.\");
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
public static void main(String []args){
BankAccount bankAc = new BankAccount();
bankAc.accountMenu();
}
}

Write a program called BankAccount.Java. This program contains the following methods: 1) Database Methods a. initializeDB : Makes a connection to the database a
Write a program called BankAccount.Java. This program contains the following methods: 1) Database Methods a. initializeDB : Makes a connection to the database a
Write a program called BankAccount.Java. This program contains the following methods: 1) Database Methods a. initializeDB : Makes a connection to the database a
Write a program called BankAccount.Java. This program contains the following methods: 1) Database Methods a. initializeDB : Makes a connection to the database a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site