public boolean sell(Stock stock, int amount) { Message m = new Message(player); if (stock.exists()) { // CHECK THE PLAYER HAS ENOUGH TO SELL if (this.stock.get(stock.getID()).amount - amount < 0) { m.errorMessage("Failed to sell! Check that you have that many!"); return false; } // OKAY THEY DO, LETS SELL EM this.stock.get(stock.getID()).amount -= amount; // OK NOW LETS UPADTE THE DATABASE MySQL mysql = new MySQL(); PreparedStatement stmt = mysql.prepareStatement("UPDATE players SET " + stock.getID() + " = ? WHERE name LIKE ?"); try { stmt.setInt(1, this.stock.get(stock.getID()).amount); stmt.setString(2, player.getName()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } mysql.execute(stmt); // UPDATE AMOUNT IF NOT INFINITE if (stock.getAmount() != -1) { stmt = mysql.prepareStatement("UPDATE stocks SET amount = amount + ? WHERE StockID LIKE ?"); try { stmt.setInt(1, amount); stmt.setString(2, stock.getID()); } catch (SQLException e) { e.printStackTrace(); return false; } mysql.execute(stmt); } mysql.close(); StockMarket.economy.depositPlayer(player.getName(), amount * stock.getPrice()); m.successMessage( "Successfully sold " + amount + " " + stock + " stocks for " + stock.getPrice() + " " + StockMarket.economy.currencyNamePlural() + " each."); return true; } else { m.errorMessage("Invalid stock ID"); return false; } }
public void listAll() { Message m = new Message(player); DecimalFormat newFormat = new DecimalFormat("#.##"); m.successMessage("List of stocks:"); for (PlayerStock ps : stock.values()) m.regularMessage( ps.stock.getID() + " - Current Amount: " + ps.stock.getAmount() + " - Price: " + newFormat.format(ps.stock.getPrice()) + " " + StockMarket.economy.currencyNamePlural()); }
public void listMine() { Message m = new Message(player); DecimalFormat newFormat = new DecimalFormat("#.##"); if (!hasStocks()) { m.errorMessage("You don't own any stocks. /sm help for help."); return; } m.successMessage("List of your stocks:"); for (PlayerStock ps : stock.values()) if (ps.amount > 0) m.regularMessage( ps.stock.getID() + " - Amount: " + ps.amount + " - Price: " + newFormat.format(ps.stock.getPrice()) + " " + StockMarket.economy.currencyNamePlural()); }
public boolean buy(Stock stock, int amount) { Message m = new Message(player); if (stock.exists()) { if ((stock.getAmount() >= amount || stock.getAmount() == -1)) { // CHECK THE PLAYER HAS ENOUGH MONEY TO BUY THIS MANY if (StockMarket.economy.getBalance(player.getName()) < amount * stock.getPrice()) { m.errorMessage("Not enough money!"); return false; } // CHECK THE PLAYER ISNT OVER HIS TOTAL LIMIT OF STOCKS if (numTotal() + amount > StockMarket.maxPerPlayer) { m.errorMessage("Buying that many would put you over the limit for total stocks!"); return false; } // CHECK THE PLAYER ISNT OVER HIS LIMIT FOR THIS STOCK if (numStock(stock) + amount > StockMarket.maxPerPlayerPerStock) { m.errorMessage("Buying that many would put you over the limit for that stock!"); return false; } // OKAY THEY DO, LETS BUY EM this.stock.get(stock.getID()).amount += amount; // OK NOW LETS UPADTE THE DATABASE MySQL mysql = new MySQL(); PreparedStatement stmt = mysql.prepareStatement( "UPDATE players SET " + stock.getID() + " = ? WHERE name LIKE ?"); try { stmt.setInt(1, this.stock.get(stock.getID()).amount); stmt.setString(2, player.getName()); } catch (SQLException e) { e.printStackTrace(); return false; } mysql.execute(stmt); // UPDATE AMOUNT IF NOT INFINITE if (stock.getAmount() != -1) { stmt = mysql.prepareStatement("UPDATE stocks SET amount = amount - ? WHERE StockID LIKE ?"); try { stmt.setInt(1, amount); stmt.setString(2, stock.getID()); } catch (SQLException e) { e.printStackTrace(); return false; } mysql.execute(stmt); } mysql.close(); StockMarket.economy.withdrawPlayer(player.getName(), amount * stock.getPrice()); m.successMessage( "Successfully purchased " + amount + " " + stock + " stocks for " + stock.getPrice() + " " + StockMarket.economy.currencyNamePlural() + " each."); return true; } else { m.errorMessage("There is not enough of that stock left to buy that many!"); return false; } } else { m.errorMessage("Invalid stock ID"); return false; } }