Complete a Java program named BankTeller that maintains bank accounts in a datab
ID: 3674114 • Letter: C
Question
Complete a Java program named BankTeller that maintains bank accounts in a database.
The code to initialize the BankAccounts database table and add accounts is provided.
Finish the code needed to deposit(double amount), withdraw(double amount), and getBalance() methods in BankAccountDB.java that update and query the database.
Download the derby.jar file and save it where Eclipse can use it, such as on your Desktop.
Download the Homework4Files.zip file containing these files
https://www.dropbox.com/sh/f3gsfjnwchksnri/AACQ9N2XUxPovCqHyHrOqOPTa?dl=0
1.) database.properties
-Properties file for the database connection
2.) BankAccountDB.java
-Class to update and query bank account rows
3.) BankDB.java
-Class to initialize the BankAccounts table and operate on all bank accounts
4.) BankTeller.java
-Main method with user interface
SimpleDataSource.java
SimpleDataSource class to simplify database connections
To run the BankTeller Java program in Eclipse, create a new project, copy all of the Java files into the src folder of your project, and copy the database.properties file into the project’s folder. Open the Project’s Properties dialog and select Java Build Paths, then click the Libraries tab. Click the Add JARs button, use the Open dialog to find the file derby.jar, and click the Open button. Back in the Project’s Properties dialog, select Run/Debug Settings, then click the Arguments tab and enter “database.properties” in the Arguments field. Click OK to save these changes to the Project’s properties.
Here is a sample run of the program – the user’s input is in green:
I) Initialize database A)dd Account D)eposit W)ithraw B)alance Inquiry Q)uit
I
Enter 'YES' if you wish to reinitialize the bank:
YES
Notice: Exception during DROP TABLE BankAccounts: 'DROP TABLE' cannot be performed on 'BANKACCOUNTS' because it does not exist.
Notice: inserted bank account 1001 balance 100.00
Notice: inserted bank account 1002 balance 200.00
Notice: inserted bank account 1003 balance 300.00
Notice: inserted bank account 1004 balance 400.00
Notice: inserted bank account 1005 balance 500.00
Notice: inserted bank account 1006 balance 600.00
I) Initialize database A)dd Account D)eposit W)ithraw B)alance Inquiry Q)uit
A
Enter new account number: 1001
Error: account 1001 already exists.
I) Initialize database A)dd Account D)eposit W)ithraw B)alance Inquiry Q)uit
D
Enter account number for deposit: 1001
Enter amount to deposit: 100
Account 1001 now has balance 200.00.
I) Initialize database A)dd Account D)eposit W)ithraw B)alance Inquiry Q)uit
B
Enter account number for inquiry: 1001
Account 1001 has balance 200.00.
I) Initialize database A)dd Account D)eposit W)ithraw B)alance Inquiry Q)uit
B
Enter account number for inquiry: 2000
Error: account 2000 does not exist.
I) Initialize database A)dd Account D)eposit W)ithraw B)alance Inquiry Q)uit
W
Enter account number for withdrawl: 1001
Enter amount to wthdraw: 100
Account 1001 now has balance 100.00.
I) Initialize database A)dd Account D)eposit W)ithraw B)alance Inquiry Q)uit
D
Enter account number for deposit:
Error: '' was not recognized as an integer. Please try again.
Enter account number for deposit: 1001
Enter amount to deposit: 0
Account 1001 now has balance 100.00.
I) Initialize database A)dd Account D)eposit W)ithraw B)alance Inquiry Q)uit
W
Enter account number for withdrawl: 1001
Enter amount to wthdraw: 0
Account 1001 now has balance 100.00.
I) Initialize database A)dd Account D)eposit W)ithraw B)alance Inquiry Q)uit
q
When you run the program, if you get the error:
Exception in thread "main" java.io.FileNotFoundException: database.properties
Then make sure you have the database.properties file copied into your project folder, and make sure you have the database.properties filename specified correctly in the Run/Debug Settings.
If you receive the error:
Exception in thread "main" java.sql.SQLException: No suitable driver
Then try changing the last line in database.properties by removing the ‘#’ in front of the “jdbc.driver=org.apache.derby.jdbc.EmbeddedDriver” line. Also check to make sure that derby.jar has been added to the Libraries on the Project Properties’ Java Build Paths page.
Explanation / Answer
Below is the updated code for deposit(), withdraw() and getBalance(). Please do rate the answer if it helped. Thank you very much.
BankAccountDB.java
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
/**
A bank account has a balance that can be changed by
deposits and withdrawals. This implementation uses
a database table to contain its data.
*/
public class BankAccountDB
{
private int accountNumber;
/**
Constructs a bank account for operations on the BankAccounts table.
@param anAccountNumber the account number
*/
public BankAccountDB(int anAccountNumber)
{
accountNumber = anAccountNumber;
}
/**
* Add the data for a bank account to the database.
* @param balance - initial balance for the account
* @throws SQLException - on any database error
*/
public void addAccount(double balance) throws SQLException
{
Connection conn = SimpleDataSource.getConnection();
try
{
PreparedStatement stat = conn.prepareStatement("INSERT INTO BankAccounts (AccountNumber, Balance) VALUES (?, ?)");
try
{
stat.setInt(1, accountNumber);
stat.setDouble(2, balance);
stat.execute();
}
finally
{
stat.close();
}
}
finally
{
conn.close();
}
}
/**
* Deposits money into a bank account.
* @param amount the amount to deposit
* @throws SQLException - on any database error
*/
public void deposit(double amount)
throws SQLException
{
// Update the BankAccounts table for this
// object's account number
Connection conn = SimpleDataSource.getConnection();
try
{
PreparedStatement stat = conn.prepareStatement("UPDATE BankAccounts SET Balance = Balance + ? WHERE AccountNumber = ?");
try
{
stat.setDouble(1, amount);
stat.setInt(2, accountNumber);
stat.execute();
}
finally
{
stat.close();
}
}
finally
{
conn.close();
}
}
/**
* Withdraws money from a bank account.
* @param amount the amount to withdraw
* @throws SQLException - on any database error
*/
public void withdraw(double amount)
throws SQLException
{
// Update the BankAccounts table for this
// object's account number
Connection conn = SimpleDataSource.getConnection();
try
{
PreparedStatement stat = conn.prepareStatement("UPDATE BankAccounts SET Balance = Balance - ? WHERE AccountNumber = ?");
try
{
stat.setDouble(1, amount);
stat.setInt(2, accountNumber);
stat.execute();
}
finally
{
stat.close();
}
}
finally
{
conn.close();
}
}
/**
* Gets the balance of a bank account.
* @return the account balance
* @throws SQLException - on any database error
*/
public double getBalance()
throws SQLException
{
// Query the BankAccounts table for the balance
// of this object's account number
Connection conn = SimpleDataSource.getConnection();
try
{
PreparedStatement stat = conn.prepareStatement("Select Balance from BankAccounts where AccountNumber = ?");
try
{
stat.setInt(1, accountNumber);
ResultSet rs =stat.executeQuery();
//there has to be 1 row
rs.next();
return rs.getDouble(1);
}
finally
{
stat.close();
}
}
finally
{
conn.close();
}
}
/**
* Return a string describing this bank account.
*/
public String toString()
{
String result;
try
{
result = "Account: " + accountNumber + " Balance: " + String.format("%.2f", getBalance());
}
catch (SQLException e)
{
result = "SQLException while getting account balance: " + e.getMessage();
}
return result;
}
}
output
I) Initialize database A)dd Account D)eposit W)ithraw B)alance Inquiry Q)uit
d
Enter account number for deposit: 111
Error: account 111 does not exist.
I) Initialize database A)dd Account D)eposit W)ithraw B)alance Inquiry Q)uit
109
I) Initialize database A)dd Account D)eposit W)ithraw B)alance Inquiry Q)uit
d
Enter account number for deposit: 1009
Enter amount to deposit: 111
Account 1009 now has balance 5111.00.
I) Initialize database A)dd Account D)eposit W)ithraw B)alance Inquiry Q)uit
w
Enter account number for withdrawl: 1009
Enter amount to wthdraw: 55
Account 1009 now has balance 5056.00.
I) Initialize database A)dd Account D)eposit W)ithraw B)alance Inquiry Q)uit
b
Enter account number for inquiry: 1006
Account 1006 has balance 600.00.
I) Initialize database A)dd Account D)eposit W)ithraw B)alance Inquiry Q)uit
q
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.