Advertisement
Guest User

Untitled

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