Advertisement
Guest User

StandardDeposit.java

a guest
May 7th, 2015
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.69 KB | None | 0 0
  1. package me.ekranos.misc.economy;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8. public class StandardDeposit implements Deposit {
  9.  
  10.     private int id;
  11.     private String name;
  12.  
  13.     public StandardDeposit(int id) {
  14.         this.id = id;
  15.     }
  16.  
  17.     public StandardDeposit(String name) {
  18.         this.name = name;
  19.     }
  20.  
  21.     @Override
  22.     public String getName() {
  23.         if (name != null) return name;
  24.         try (Connection connection = StandardEconomyService.getConnection()) {
  25.             PreparedStatement statement = connection.prepareStatement("SELECT name FROM deposits WHERE id = ?");
  26.             statement.setInt(1, id);
  27.             ResultSet result = statement.executeQuery();
  28.  
  29.             if (result.next()) {
  30.                 name = result.getString("name");
  31.             }
  32.         } catch (SQLException e) {
  33.             e.printStackTrace();
  34.         }
  35.  
  36.         return name;
  37.     }
  38.  
  39.     @Override
  40.     public double getAmount() {
  41.         if (getName().equals("server")) return Double.MAX_VALUE;
  42.         double amount = Double.MIN_VALUE;
  43.         try (Connection connection = StandardEconomyService.getConnection()) {
  44.             PreparedStatement statement = connection.prepareStatement("SELECT amount FROM deposits WHERE id = ?");
  45.             statement.setInt(1, id);
  46.             ResultSet result = statement.executeQuery();
  47.  
  48.             if (result.next()) {
  49.                 amount = result.getDouble("amount");
  50.             }
  51.         } catch (SQLException e) {
  52.             e.printStackTrace();
  53.         }
  54.  
  55.         return amount;
  56.     }
  57.  
  58.     public void setAmount(double amount) {
  59.         if (getName().equals("server")) return;
  60.         try (Connection connection = StandardEconomyService.getConnection()) {
  61.             PreparedStatement statement = connection.prepareStatement("UPDATE deposits SET amount = ? WHERE id = ?");
  62.             statement.setDouble(1, amount);
  63.             statement.setInt(2, id);
  64.             statement.execute();
  65.         } catch (SQLException e) {
  66.             e.printStackTrace();
  67.         }
  68.     }
  69.  
  70.     public void setName(String name) {
  71.         this.name = name;
  72.     }
  73.  
  74.     @Override
  75.     public int getId() {
  76.         return id;
  77.     }
  78.  
  79.     public void recalculate() {
  80.         try (Connection connection = StandardEconomyService.getConnection()) {
  81.             PreparedStatement statement = connection.prepareStatement("SELECT SUM(amount) AS amount FROM transactions WHERE source = ?");
  82.             statement.setInt(1, id);
  83.  
  84.             ResultSet result = statement.executeQuery();
  85.  
  86.             double amount = 0d;
  87.  
  88.             if (result.next()) {
  89.                 amount -= result.getDouble("amount");
  90.             }
  91.  
  92.             statement = connection.prepareStatement("SELECT SUM(amount) AS amount FROM transactions WHERE target = ?");
  93.             statement.setInt(1, id);
  94.  
  95.             result = statement.executeQuery();
  96.  
  97.             if (result.next()) {
  98.                 amount += result.getDouble("amount");
  99.             }
  100.  
  101.             setAmount(amount);
  102.         } catch (SQLException e) {
  103.             e.printStackTrace();
  104.         }
  105.     }
  106.  
  107.     public StandardDeposit save() {
  108.         try (Connection connection = StandardEconomyService.getConnection()) {
  109.             PreparedStatement statement = connection.prepareStatement("INSERT INTO deposits (name, amount) VALUES (?, ?)");
  110.             statement.setString(1, name);
  111.             statement.setDouble(2, 0);
  112.             statement.execute();
  113.  
  114.         } catch (SQLException e) {
  115.             e.printStackTrace();
  116.         }
  117.  
  118.         id = new StandardDeposit(name).getId();
  119.  
  120.         return this;
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement