Esempio n. 1
0
 public void save() {
   for (Pair<String, Integer> pair : portfolio) {
     try {
       Connection con = (Connection) DatabaseConnection.getConnection();
       PreparedStatement ps;
       if (newlyAdded.contains(pair)) {
         ps =
             con.prepareStatement(
                 "INSERT INTO `maplestocks_data` (`cid`, `stockid`, `shares`) VALUES (?, ?, ?)");
         ps.setInt(1, cid);
         ps.setInt(2, MapleStocks.getInstance().idOf(pair.getLeft()));
         ps.setInt(3, pair.getRight());
         Output.print(ps.toString());
         ps.executeUpdate();
       } else {
         ps =
             con.prepareStatement(
                 "UPDATE maplestocks_data SET shares = ? WHERE cid = ? AND stockid = ?");
         ps.setInt(1, pair.getRight());
         ps.setInt(2, cid);
         ps.setInt(3, MapleStocks.getInstance().idOf(pair.getLeft()));
         Output.print(ps.toString());
         ps.executeUpdate();
       }
     } catch (SQLException e) {
       Output.print("Something went wrong while saving a MapleStockPortfolio.");
       GameLogger.print(GameLogger.EXCEPTION_CAUGHT, e);
     }
   }
 }
Esempio n. 2
0
 public boolean add(Pair<String, Integer> shares) {
   Output.print("add() called");
   if (!this.hasStock(MapleStocks.getInstance().getStock(shares.getLeft()))) {
     portfolio.add(shares);
     newlyAdded.add(shares);
     return true;
   }
   return false;
 }
Esempio n. 3
0
 public static MapleStockPortfolio load(int cid) {
   MapleStockPortfolio ret = new MapleStockPortfolio(cid);
   try {
     Connection con = (Connection) DatabaseConnection.getConnection();
     PreparedStatement ps =
         con.prepareStatement("SELECT * FROM `maplestocks_data` WHERE `cid` = ?");
     ps.setInt(1, cid);
     ResultSet rs = ps.executeQuery();
     while (rs.next()) {
       ret.add(
           new Pair<String, Integer>(
               MapleStocks.getInstance().tickerOf(rs.getInt("stockid")), rs.getInt("shares")));
     }
   } catch (SQLException e) {
     Output.print("Failed to load MapleStockPortfolio " + cid + ".");
     GameLogger.print(GameLogger.EXCEPTION_CAUGHT, e);
   }
   return ret;
 }