Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.65 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package java_assignment_10;
  6.  
  7. import java.sql.*;
  8.  
  9. /**
  10. *
  11. * @author Emergency Loaner 1
  12. */
  13. public class atmDB {
  14.  
  15. Connection con = null;
  16.  
  17. public atmDB() {
  18.  
  19. try {
  20. String path = "jdbc:mysql://localhost/atmdatabase";
  21. String user = "root";
  22. String pass = "";
  23. con = DriverManager.getConnection(path, user, pass);
  24. } catch (SQLException e) {
  25. System.out.println(e);
  26. }
  27. }
  28.  
  29. public boolean createAccount(Account a) {
  30.  
  31. try {
  32. Statement stmt = con.createStatement();
  33. String query = "INSERT INTO account VALUES("
  34. + a.getAccountNumber() + ", "
  35. + a.getBalance() + ", "
  36. + a.getPin() + ", "
  37. + a.getAccountType() + ", "
  38. + a.getCustomer().getCustomerID() + ")";
  39. int rs = stmt.executeUpdate(query);
  40.  
  41. if (rs > 0) {
  42. return true;
  43. } else {
  44. return false;
  45. }
  46.  
  47. } catch (Exception e) {
  48. System.out.println(e);
  49. return false;
  50. }
  51. }
  52.  
  53. public boolean checkUnique(long i) {
  54. try {
  55. Statement stmt = con.createStatement();
  56. ResultSet rs = stmt.executeQuery("SELECT accountNumber from account where accountNumber = " + i);
  57. if (rs.next()) {
  58. return false;
  59. } else {
  60. return true;
  61. }
  62. } catch (Exception e) {
  63. return false;
  64. }
  65.  
  66. }
  67.  
  68. public int getNewCID() {
  69. int max = 0;
  70. try {
  71. Statement stmt = con.createStatement();
  72. ResultSet rs = stmt.executeQuery("SELECT MAX(customerID) from customer");
  73. while (rs.next()) {
  74. max = rs.getInt(1);
  75. }
  76. } catch (Exception e) {
  77. System.out.println(e);
  78. }
  79. return max;
  80. }
  81.  
  82. public boolean createCustomer(Customer c) {
  83. String firstName = c.getFirstName();
  84. String lastName = c.getLastName();
  85. String email = c.getEmail();
  86. int phone = c.getPhone();
  87. int mobilePhone = c.getMobilePhone();
  88. String addressLine1 = c.getAddressLine1();
  89. String addressLine2 = c.getAddressLine2();
  90. String city = c.getCity();
  91. String state = c.getState();
  92. String postalCod = c.getPostalCod();
  93. int dateOfBirth = c.getDateOfBirth();
  94. int monthOfBirth = c.getMonthOfBirth();
  95. int yearOfBirth = c.getYearOfBirth();
  96.  
  97. try {
  98. Statement stmt = con.createStatement();
  99. String query = "INSERT into customer (firstName, lastName, email, fixedPhone, mobilePhone, addressLine1, addressLine2, city, state, postalCode, dateOfBirth, monthOfBirth, yearOfBirth) values ('" + firstName + "','"
  100. + lastName + "','"
  101. + email + "','"
  102. + phone + "','"
  103. + mobilePhone + "','"
  104. + addressLine1 + "','"
  105. + addressLine2 + "','"
  106. + city + "','"
  107. + state + "','"
  108. + postalCod + "','"
  109. + dateOfBirth + "','"
  110. + monthOfBirth + "','"
  111. + yearOfBirth + "')";
  112. System.out.println(query);
  113. int i = stmt.executeUpdate(query);
  114. if (i > 0) {
  115. return true;
  116. } else {
  117. return false;
  118.  
  119.  
  120. }
  121. } catch (Exception e) {
  122. System.out.println(e);
  123. return false;
  124. }
  125. }
  126.  
  127. public boolean checkAccount(long aN, int aP) {
  128. try {
  129. Statement stmt = con.createStatement();
  130. String query = "SELECT accountNumber, pin FROM account WHERE accountNumber = " + aN + " AND pin = " + aP;
  131. ResultSet rs = stmt.executeQuery(query);
  132. if (rs.next()) {
  133. return true;
  134. } else {
  135. return false;
  136. }
  137. } catch (Exception e) {
  138. return false;
  139. }
  140. }
  141.  
  142. public boolean testAccount(long aN) {
  143. try {
  144. Statement stmt = con.createStatement();
  145. String query = "SELECT accountNumber, pin FROM account WHERE accountNumber = " + aN;
  146. ResultSet rs = stmt.executeQuery(query);
  147. if (rs.next()) {
  148. return true;
  149. } else {
  150. return false;
  151. }
  152. } catch (Exception e) {
  153. return false;
  154. }
  155. }
  156.  
  157. public double getBalance(long aN) {
  158. try {
  159. Statement stmt = con.createStatement();
  160. String query = "SELECT balance FROM account WHERE accountNumber = " + aN;
  161. ResultSet rs = stmt.executeQuery(query);
  162. if (rs.next()) {
  163. return rs.getDouble(1);
  164.  
  165. } else {
  166.  
  167. return 0;
  168. }
  169. } catch (Exception e) {
  170. System.out.println(e);
  171. return 0;
  172. }
  173. }
  174.  
  175. public void withdraw(long aN, double a) {
  176. try {
  177. Statement stmt = con.createStatement();
  178. String query = "SELECT balance FROM account WHERE accountNumber = " + aN;
  179. ResultSet rs = stmt.executeQuery(query);
  180. if (rs.next()) {
  181. double balance = rs.getDouble(1);
  182. if (balance > a) {
  183. double newBalance = balance - a;
  184. query = "UPDATE account set balance = " + newBalance + " where accountNumber = " + aN;
  185. int res = stmt.executeUpdate(query);
  186. }
  187. } else {
  188. }
  189. } catch (Exception e) {
  190. }
  191. }
  192.  
  193. public void deposit(long aN, double a) {
  194. try {
  195. Statement stmt = con.createStatement();
  196. String query = "SELECT balance FROM account WHERE accountNumber = " + aN;
  197. ResultSet rs = stmt.executeQuery(query);
  198. if (rs.next()) {
  199. double balance = rs.getDouble(1);
  200. double newBalance = balance + a;
  201. query = "UPDATE account set balance = " + newBalance + " where accountNumber = " + aN;
  202. int res = stmt.executeUpdate(query);
  203. } else {
  204. }
  205. } catch (Exception e) {
  206. }
  207. }
  208.  
  209. public boolean transfer(long fAN, long tAN, double a) {
  210. try {
  211. if (this.testAccount(tAN)) {
  212. double tBal = this.getBalance(tAN);
  213. double fBal = this.getBalance(fAN);
  214. double newTBal = tBal + a;
  215. double newFBal = fBal - a;
  216. System.out.println(newFBal);
  217.  
  218. if (newFBal > 0) {
  219. Statement stmt = con.createStatement();
  220. String query = "UPDATE account set balance = " + newTBal + " where accountNumber = " + tAN;
  221. stmt.executeUpdate(query);
  222. query = "UPDATE account set balance = " + newFBal + " where accountNumber = " + fAN;
  223. int rs = stmt.executeUpdate(query);
  224. if (rs > 0) {
  225. return true;
  226. }
  227.  
  228. } else {
  229. return false;
  230. }
  231. } else {
  232. return false;
  233.  
  234. }
  235. } catch (Exception e) {
  236. return false;
  237. }
  238. return false;
  239. }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement