Advertisement
Guest User

Untitled

a guest
Jun 8th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.59 KB | None | 0 0
  1. package dbms;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.text.SimpleDateFormat;
  9. import java.util.ArrayList;
  10. import java.util.Date;
  11.  
  12. import utiles.User;
  13.  
  14. public class DBMS implements IDBMS {
  15.  
  16. private Connection con;
  17. private ArrayList<String> shoppingCart;
  18. private Statement stmt;
  19. private String user;
  20.  
  21. @Override
  22. public void addAuthor(String author) throws SQLException {
  23. stmt.executeUpdate("insert into author values (" + addQuotes(author) + ");");
  24. }
  25.  
  26. @Override
  27. public void addBook(String bookID, String title, String publisher, String publicationDate, String price,
  28. String category, String threshold, String quantity, ArrayList<String> book_authors) throws SQLException {
  29.  
  30. stmt.executeUpdate("insert into book values (" + bookID + ", \"" + title + "\", \"" + publisher + "\", \""
  31. + publicationDate + "\", " + price + ", \"" + category + "\", " + threshold + ");");
  32.  
  33. stmt.executeUpdate("insert into stock values (" + bookID + ", " + quantity + ");");
  34.  
  35. for (String authorName : book_authors) {
  36. if (!isAuthorExists(authorName))
  37. addAuthor(authorName);
  38. stmt.executeUpdate("insert into book_authors values (" + bookID + ", " + addQuotes(authorName) + ");");
  39. }
  40. }
  41.  
  42. @Override
  43. public void addBookToCart(String bookID) {
  44. shoppingCart.add(bookID);
  45. }
  46.  
  47. @Override
  48. public void addPublisher(String publisherName, String address, String telephone) throws SQLException {
  49. stmt.executeUpdate("insert into publisher values (" + addQuotes(publisherName) + "," + addQuotes(address) + ","
  50. + addQuotes(telephone) + ");");
  51. }
  52.  
  53. private String addQuotes(String str) {
  54. return "\"" + str + "\"";
  55. }
  56.  
  57. @Override
  58. public void checkoutCart(String creditCardNum, String expirtDate) throws Exception {
  59. SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
  60. java.util.Date date = sdf1.parse(expirtDate);
  61.  
  62. if (date.getTime() < new Date().getTime())
  63. throw new RuntimeException("Expired credircard.");
  64.  
  65. for (String bookID : shoppingCart) {
  66. stmt.executeUpdate("udpate stock set quantity = quantity - 1 where isbn = " + bookID);
  67. stmt.executeUpdate("insert into sales values (" + bookID + " , " + user + " , " + "now()" + ")");
  68. }
  69. shoppingCart.clear();
  70. }
  71.  
  72. @Override
  73. public void confirmOrder(String id) throws SQLException {
  74. stmt.executeUpdate("delete from orders where id = " + id);
  75. }
  76.  
  77. @Override
  78. public void connect(String url, String user, String password) throws Exception {
  79. // TimeZone timeZone = TimeZone.getTimeZone("Europe/Rome");
  80. // TimeZone.setDefault(timeZone);
  81.  
  82. Class.forName("com.mysql.cj.jdbc.Driver");
  83. con = DriverManager.getConnection(url, user, password);
  84.  
  85. // ("jdbc:mysql://localhost:3306/Library?serverTimezone=UTC","SAMPLE","admin");
  86.  
  87. stmt = con.createStatement();
  88. shoppingCart = new ArrayList<String>();
  89. }
  90.  
  91. @Override
  92. public void deleteBook(String bookID) throws SQLException {
  93. stmt.executeUpdate("delete from book where isbn = " + bookID + ";");
  94. }
  95.  
  96. // todo creditcard check
  97.  
  98. @Override
  99. public void disconnect() throws SQLException {
  100. con.close();
  101. shoppingCart = null;
  102. user = null;
  103. }
  104.  
  105. @Override
  106. public void editUserInfo(String password, String lastName, String firstName, String email, String phone,
  107. String shippingAddress) throws SQLException {
  108. stmt.executeUpdate("update user " + " set password = " + addQuotes(password) + " , fistName = "
  109. + addQuotes(firstName) + " , lastName = " + addQuotes(lastName) + " , email = " + addQuotes(email)
  110. + " , phone = " + addQuotes(phone) + " , shippingAddress = " + addQuotes(shippingAddress)
  111. + " where username = " + "\"" + addQuotes(user) + "\"");
  112. }
  113.  
  114. @Override
  115. public ResultSet getAllBooks() throws SQLException {
  116. return stmt.executeQuery("select * from book;");
  117. }
  118.  
  119. @Override
  120. public ResultSet getOrders() throws SQLException {
  121. ResultSet r = stmt.executeQuery("Select * from orders");
  122. return r;
  123. }
  124.  
  125. private String getSearchSpaceFromShoppingCart() {
  126. StringBuilder ret = new StringBuilder("( ");
  127. for (int i = 0; i < shoppingCart.size(); ++i) {
  128. ret.append(shoppingCart.get(i));
  129. if (i + 1 < shoppingCart.size())
  130. ret.append(", ");
  131. }
  132.  
  133. ret.append(")");
  134. return ret.toString();
  135. }
  136.  
  137. @Override
  138. public ResultSet getShoppingCartContent() throws SQLException {
  139. return stmt.executeQuery("select * from book where isbn in" + getSearchSpaceFromShoppingCart());
  140. }
  141.  
  142. @Override
  143. public int getTotalPriceOfCart() throws SQLException {
  144. int ret = 0;
  145. for (String bookID : shoppingCart) {
  146. ResultSet r = stmt.executeQuery("select price from book where isbn =" + bookID);
  147. ret += r.getInt("price");
  148. }
  149. return ret;
  150. }
  151.  
  152. @Override
  153. public ResultSet getUserInfoWithMail(String mail) throws SQLException {
  154. return stmt.executeQuery("select * from user where username = " + addQuotes(mail));
  155. }
  156.  
  157. @Override
  158. public ResultSet getUserInfoWithName(String username) throws SQLException {
  159. return stmt.executeQuery("select * from user where username = " + addQuotes(username));
  160. }
  161.  
  162. @Override
  163. public ResultSet getUsers() throws SQLException {
  164. ResultSet r = stmt.executeQuery("select * from user where usertype <> \"manager\"");
  165. return r;
  166. }
  167.  
  168. @Override
  169. public String getUserType() throws SQLException {
  170. return stmt.executeQuery("select usertype from user where username = " + addQuotes(user)).getString("usertype");
  171. }
  172.  
  173. private boolean isAuthorExists(String author) throws SQLException {
  174. ResultSet r = stmt.executeQuery("select * from author where author_name = " + addQuotes(author));
  175. return r.next();
  176. }
  177.  
  178. @Override
  179. public void loginWithMail(String mail, String password) throws Exception {
  180. ResultSet rs = stmt.executeQuery(
  181. "select * from user where email = " + addQuotes(mail) + " and password = " + addQuotes(password) + ";");
  182. if (rs.getFetchSize() != 1)
  183. throw new RuntimeException("Failed to login.");
  184.  
  185. user = rs.getString("username");
  186. }
  187.  
  188. @Override
  189. public void loginWithUsername(String username, String password) throws Exception {
  190. ResultSet rs = stmt.executeQuery("select * from user where username = " + addQuotes(username)
  191. + " and password = " + addQuotes(password) + ";");
  192. if (rs.getFetchSize() != 1)
  193. throw new RuntimeException("Failed to login.");
  194. user = username;
  195. }
  196.  
  197. @Override
  198. public void placeOrder(String bookID, String quantity) throws SQLException {
  199. stmt.executeUpdate("insert into orders (publisher, isbn, quantity, checkout) " + "values(" + bookID + ","
  200. + quantity + ", CURDATE());");
  201. }
  202.  
  203. @Override
  204. public void promoteUser(String username) throws SQLException {
  205. stmt.executeUpdate(
  206. "update user " + "set usertype = " + " \"manager\" " + " where username = " + "\"" + username + "\"");
  207. }
  208.  
  209. @Override
  210. public void removeBookFromCart(String bookID) {
  211. shoppingCart.remove(bookID);
  212. }
  213.  
  214. @Override
  215. public ResultSet searchBookByIndex(String index, String val) throws SQLException {
  216. if (index.equalsIgnoreCase("author"))
  217. return stmt
  218. .executeQuery("select * from book natural join book_authors where author_name = " + addQuotes(val));
  219.  
  220. if (index.equalsIgnoreCase("isbn") || index.equalsIgnoreCase("price") || index.equalsIgnoreCase("threshold"))
  221. return stmt.executeQuery("select * from book where " + index + " = " + val);
  222.  
  223. return stmt.executeQuery("select * from book where " + index + " = " + addQuotes(val));
  224. }
  225.  
  226. @Override
  227. public void signup(User user) throws SQLException {
  228.  
  229. stmt.executeUpdate("insert into user values (" + addQuotes(user.getUsername()) + ","
  230. + addQuotes(user.getPassward()) + "," + addQuotes(user.getLname()) + "," + addQuotes(user.getFname())
  231. + "," + addQuotes(user.getMail()) + "," + addQuotes(user.getPhone()) + ","
  232. + addQuotes(user.getAddress()) + "," + addQuotes(user.getType()) + ");");
  233. }
  234.  
  235. @Override
  236. public ResultSet topFiveCustomers() throws SQLException {
  237. // TODO Auto-generated method stub
  238. ResultSet r = stmt.executeQuery("select *, sum(price) purchase" + "from user u natural join sales s "
  239. + "where month(s.checkout)+3 >= month(now()) " + "group by u.username " + "ordered by purchase DESC "
  240. + "LIMIT 5;");
  241. return r;
  242. }
  243.  
  244. @Override
  245. public ResultSet topTenSellingBooks() throws SQLException {
  246. // TODO Auto-generated method stub
  247. ResultSet r = stmt.executeQuery(
  248. "select *, count(*) selling " + "from book b, sales s " + "where month(s.checkout)+3 >= month(now()) "
  249. + "group by b.isbn " + "ordered by selling DESC " + "LIMIT 10;");
  250. return r;
  251. }
  252.  
  253. @Override
  254. public ResultSet totalSalesForPrevoisMonth() throws SQLException {
  255. ResultSet r = stmt.executeQuery("select *, count(*) " + "from book b natural join sales s"
  256. + "where month(s.checkout)+1 >= month(now())" + "group by b.isbn;");
  257. return r;
  258. }
  259.  
  260. @Override
  261. public void updateBook(String bookID, String title, String publisher, String publicationDate, String price,
  262. String category, String threshold, ArrayList<String> authors) throws SQLException {
  263. stmt.executeUpdate("update book " + "set title = " + addQuotes(title) + " , publisher = " + addQuotes(publisher)
  264. + " , publication_date = " + addQuotes(publicationDate) + " , price = " + price + " , category = "
  265. + addQuotes(category) + " , threshold = " + threshold + " where isbn = " + bookID + ";");
  266.  
  267. stmt.executeUpdate("delete from book_authors where isbn = " + bookID);
  268. for (String author : authors) {
  269. if (!isAuthorExists(author))
  270. addAuthor(author);
  271. stmt.executeUpdate("insert into book_authors values (" + bookID + " , " + addQuotes(author) + " );");
  272. }
  273.  
  274. }
  275.  
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement