Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.ekranos.misc.economy;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class StandardDeposit implements Deposit {
- private int id;
- private String name;
- public StandardDeposit(int id) {
- this.id = id;
- }
- public StandardDeposit(String name) {
- this.name = name;
- }
- @Override
- public String getName() {
- if (name != null) return name;
- try (Connection connection = StandardEconomyService.getConnection()) {
- PreparedStatement statement = connection.prepareStatement("SELECT name FROM deposits WHERE id = ?");
- statement.setInt(1, id);
- ResultSet result = statement.executeQuery();
- if (result.next()) {
- name = result.getString("name");
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return name;
- }
- @Override
- public double getAmount() {
- if (getName().equals("server")) return Double.MAX_VALUE;
- double amount = Double.MIN_VALUE;
- try (Connection connection = StandardEconomyService.getConnection()) {
- PreparedStatement statement = connection.prepareStatement("SELECT amount FROM deposits WHERE id = ?");
- statement.setInt(1, id);
- ResultSet result = statement.executeQuery();
- if (result.next()) {
- amount = result.getDouble("amount");
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return amount;
- }
- public void setAmount(double amount) {
- if (getName().equals("server")) return;
- try (Connection connection = StandardEconomyService.getConnection()) {
- PreparedStatement statement = connection.prepareStatement("UPDATE deposits SET amount = ? WHERE id = ?");
- statement.setDouble(1, amount);
- statement.setInt(2, id);
- statement.execute();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public int getId() {
- return id;
- }
- public void recalculate() {
- try (Connection connection = StandardEconomyService.getConnection()) {
- PreparedStatement statement = connection.prepareStatement("SELECT SUM(amount) AS amount FROM transactions WHERE source = ?");
- statement.setInt(1, id);
- ResultSet result = statement.executeQuery();
- double amount = 0d;
- if (result.next()) {
- amount -= result.getDouble("amount");
- }
- statement = connection.prepareStatement("SELECT SUM(amount) AS amount FROM transactions WHERE target = ?");
- statement.setInt(1, id);
- result = statement.executeQuery();
- if (result.next()) {
- amount += result.getDouble("amount");
- }
- setAmount(amount);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public StandardDeposit save() {
- try (Connection connection = StandardEconomyService.getConnection()) {
- PreparedStatement statement = connection.prepareStatement("INSERT INTO deposits (name, amount) VALUES (?, ?)");
- statement.setString(1, name);
- statement.setDouble(2, 0);
- statement.execute();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- id = new StandardDeposit(name).getId();
- return this;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement