Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a sample database to solve this questions,take screenshots that shows ow

ID: 3886859 • Letter: C

Question

Create a sample database to solve this questions,take screenshots that shows ow you connected to the internet and the output result.

Add 2 methods:

1.) deposit(amt);   // adds the amt to the balance in the object and

                             // also updates the balance in the DB

2.) withdraw(amt); //same as deposit above, except subtract

Account a1 = new Account();

//so to find an account in the database, call selectDB().

a1.selectDB(3001);

// so now to make a deposit we simply

a1.deposit(300.00);

// this method will display the new account balance

a1.display();

Part II – Now build an AccountList Business Class. This class will have a list of Account Objects. You can use an Array or an ArrayList to store this list. I would also have a count property to keep track of how many Accounts are in the list. Methods should include a display() method, and an add(Account a1) method.

Part III – Now add a new Property to the Customer class.

          AccountList aList;

Make it so that when an User calls the selectDB(3001) method(and passes a custid), the Customer class will go look in the database for all accounts that belong to this customer and put them in the use the aList object. So the Customer class will contain a list of Accounts that the customer owns.

Explanation / Answer

package com.demo;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class Account {

private int accountId;

private double balance;

public int getAccoutnId() {

return accountId;

}

public void setAccoutnId(int accountId) {

this.accountId = accountId;

}

public double getBalance() {

return balance;

}

public void setBalance(double balance) {

this.balance = balance;

}

public Account display() {

Connection connection = getConnection();

Statement statement = null;

Account account = null;

try {

statement = connection.createStatement();

ResultSet resultSet = statement

.executeQuery("SELECT * FROM Account");

while (resultSet.next()) {

account = new Account();

account.setAccoutnId(resultSet.getInt(1));

account.setBalance(resultSet.getDouble(2));

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

try {

statement.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

return account;

}

public Account selectDB(int accountId) {

Connection connection = getConnection();

Statement statement = null;

Account account = null;

String sql = "SELECT * FROM Account WHERE accountId=" + accountId;

try {

statement = connection.createStatement();

ResultSet resultSet = statement.executeQuery(sql);

while (resultSet.next()) {

account = new Account();

account.setAccoutnId(resultSet.getInt(1));

account.setBalance(resultSet.getDouble(2));

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

try {

statement.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

return account;

}

public void deposit(double amount) {

double balance = 0.0;

String select = "SELECT balance FROM Account";

String update = "UPDATE Account SET balance =" + balance + amount;

Connection connection = getConnection();

try {

Statement statement = connection.createStatement();

ResultSet seletResultSet = statement.executeQuery(select);

while (seletResultSet.next()) {

balance = seletResultSet.getDouble(2);

}

statement.executeUpdate(update);

} catch (SQLException e) {

e.printStackTrace();

} finally {

try {

connection.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

public void withdraw(double amount) {

double balance = 0.0;

String select = "SELECT balance FROM Account";

String update = "UPDATE Account SET balance =" + (balance - amount);

Connection connection = getConnection();

try {

Statement statement = connection.createStatement();

ResultSet seletResultSet = statement.executeQuery(select);

while (seletResultSet.next()) {

balance = seletResultSet.getDouble(2);

}

statement.executeUpdate(update);

} catch (SQLException e) {

e.printStackTrace();

} finally {

try {

connection.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

public static Connection getConnection() {

Connection connection = null;

try {

Class.forName("com.mysql.jdbc.Driver");

connection = DriverManager.getConnection(

"jdbc:mysql://localhost:3306/sonoo", "root", "root");

} catch (Exception exception) {

exception.printStackTrace();

}

return connection;

}

@Override

public String toString() {

return "Account [balance=" + balance + "]";

}

}

================================================================================

package com.demo;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

public class AccountList {

List<Account> accounts = new ArrayList<Account>();

public void addAcount(Account account) {

accounts.add(account);

}

public Account display(int index) {

return accounts.get(index);

}

public int count() {

int count = 0;

Iterator<Account> iterator = accounts.iterator();

while (iterator.hasNext()) {

Account account = (Account) iterator.next();

count++;

}

return count;

}

}

==============================================================================

package com.demo;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

public class Customer {

AccountList aList;

public Account selectDB(int accountId) {

Connection connection = getConnection();

Statement statement = null;

Account account = null;

String sql = "SELECT * FROM Account WHERE accountId=" + accountId;

try {

statement = connection.createStatement();

ResultSet resultSet = statement.executeQuery(sql);

while (resultSet.next()) {

account = new Account();

account.setAccoutnId(resultSet.getInt(1));

account.setBalance(resultSet.getDouble(2));

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

try {

statement.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

return account;

}

public static Connection getConnection() {

Connection connection = null;

try {

Class.forName("com.mysql.jdbc.Driver");

connection = DriverManager.getConnection(

"jdbc:mysql://localhost:3306/sonoo", "root", "root");

} catch (Exception exception) {

exception.printStackTrace();

}

return connection;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote