Class Api

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible
Direct Known Subclasses:
CreateJar, Deposit, DepositJar, Login, Main, Send, SignUp, WithdrawJar

public class Api extends JPanel
API class for the Banking Application. This class handles the connection to the database and provides methods for user authentication, sign up, deposit, withdraw, and other operations. It uses JDBC to interact with a PostgreSQL database. It also provides methods to create and manage jars, transactions, and deposit requests.
See Also:
  • Field Details

    • DATABASE_URL

      private final String DATABASE_URL
      The URL of the database.
    • dbpassword

      private final String dbpassword
      The password for the database connection.
    • connection

      private Connection connection
      The Connection object that represents the connection to the database.
    • pstat

      private PreparedStatement pstat
      The PreparedStatement object used to execute SQL queries.
  • Constructor Details

    • Api

      public Api()
  • Method Details

    • authenticate

      void authenticate(String login, char[] password) throws SQLException
      Checks the provided card number and password against the database. If the credentials are valid, it stores the user information in the UserSession.
      Parameters:
      login - the card number of the user
      password - the password of the user
      Throws:
      SQLException - if there is an error during the database operation
    • signUp

      void signUp(String name, char[] password) throws SQLException
      Signs up a new user by creating a new entry in the database. Generates a unique card number and stores the user information.
      Parameters:
      name - the name of the user
      password - the password of the user
      Throws:
      SQLException - if there is an error during the database operation
    • deposit

      void deposit(BigDecimal payInfo, Double amount) throws SQLException
      Creates a deposit request for the user with the payInfo card number and saves it in the database.
      Parameters:
      payInfo - the card number of the user who receives the deposit request
      amount - the amount of money to be deposited
      Throws:
      SQLException - if there is an error during the database operation
    • deposit

      void deposit(Double amount) throws SQLException
      Deposits money into the user's account using the Free Money feature. This method adds the specified amount to the user's balance and creates a transaction for the operation.
      Parameters:
      amount - the amount of money to be deposited
      Throws:
      SQLException - if there is an error during the database operation
    • getTransactionData

      Transaction getTransactionData(int id) throws SQLException
      Retrieves the transaction data for a given transaction ID. This method establishes a connection to the database, executes a query to get the transaction details, and creates a Transaction object with the retrieved data.
      Parameters:
      id - the ID of the transaction to be retrieved
      Returns:
      the Transaction object with the specified ID
      Throws:
      SQLException - if there is an error during the database operation
    • getDepositRequestData

      DepositRequest getDepositRequestData(int id) throws SQLException
      Retrieves the deposit request data for a given request ID. This method establishes a connection to the database, executes a query to get the deposit request details, and creates a DepositRequest object with the retrieved data.
      Parameters:
      id - the ID of the deposit request to be retrieved
      Returns:
      the DepositRequest object with the specified ID
      Throws:
      SQLException - if there is an error during the database operation
    • getJarData

      Jar getJarData(int id) throws SQLException
      Retrieves the jar data for a given jar ID. This method establishes a connection to the database, executes a query to get the jar details, and creates a Jar object with the retrieved data.
      Parameters:
      id - the ID of the jar to be retrieved
      Returns:
      the Jar object with the specified ID
      Throws:
      SQLException - if there is an error during the database operation
    • createJar

      void createJar(String title, Double goal) throws SQLException
      Creates a new jar for the user and saves it in the database. This method establishes a connection to the database, executes an insert query to create the jar, and updates the user's account by adding the new jar ID.
      Parameters:
      title - the title of the jar
      goal - the goal of the jar, null if there is no goal
      Throws:
      SQLException - if there is an error during the database operation
    • depositToJar

      void depositToJar(int id, double amount) throws SQLException
      Deposits money into a jar. This method checks if the user has enough money to deposit. If they do, it subtracts the amount from the user's account, adds the amount to the jar, creates a transaction for the operation, and saves the transaction in the user's account.
      Parameters:
      id - the ID of the jar to be deposited into
      amount - the amount of money to be deposited
      Throws:
      SQLException - if there is an error during the database operation
    • withdrawFromJar

      void withdrawFromJar(int id, double amount, boolean isBroken) throws SQLException
      Withdraws money from a jar. This method checks if the user has enough money in the jar to withdraw. If they do, it subtracts the amount from the jar, adds the amount to the user's account, creates a transaction for the operation, and saves the transaction in the user's account. If the user breaks the jar, it is deleted from the database.
      Parameters:
      id - the ID of the jar to withdraw from
      amount - the amount of money to withdraw
      isBroken - true if the jar is being broken, false otherwise
      Throws:
      SQLException - if there is an error during the database operation
    • fulfillDepositRequest

      void fulfillDepositRequest(int id) throws SQLException
      Fulfills a deposit request by transferring money from the user's account to the target's account. This method checks if the user has enough money to fulfill the request. If they do, it subtracts the amount from the user's account, adds the amount to the target's account, creates transactions for both the user and the target, saves the transactions in their respective accounts, and deletes the request from the database.
      Parameters:
      id - the ID of the deposit request to be fulfilled
      Throws:
      SQLException - if there is an error during the database operation
    • deleteDepositRequest

      void deleteDepositRequest(int id) throws SQLException
      Deletes a deposit request from the database. This method removes the request from the database and updates the user's account by removing the request ID.
      Parameters:
      id - the ID of the deposit request to be deleted
      Throws:
      SQLException - if there is an error during the database operation
    • send

      void send(double amount, BigDecimal cardNumber, String name) throws SQLException
      Sends money from the user's account to another user's account. This method checks if the user has enough money to send. If they do, it subtracts the amount from the user's account, adds the amount to the target's account, creates transactions for both the user and the target, saves the transactions in their respective accounts, and updates the user data.
      Parameters:
      amount - the amount of money to be sent
      cardNumber - the card number of the target user
      name - the name of the target user
      Throws:
      SQLException - if there is an error during the database operation
    • update

      void update() throws SQLException
      Updates the user data in the UserSession. This method retrieves the data of the current user from the database and stores it in the UserSession.
      Throws:
      SQLException - if there is an error during the database operation
    • storeUser

      private void storeUser(ResultSet resSet) throws SQLException
      Stores the user data in the UserSession. Gets the transaction IDs, deposit request IDs, and jar IDs from the ResultSet, and uses them to get the data needed to create the corresponding objects. It then stores a new User object in the UserSession.
      Parameters:
      resSet - the ResultSet containing the user data
      Throws:
      SQLException - if there is an error during the database operation