Advertisement
Guest User

Untitled

a guest
Nov 9th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.70 KB | None | 0 0
  1. import java.io.FileInputStream;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.Statement;
  6. import java.util.Properties;
  7.  
  8. /**
  9. * Manage connection to database and perform SQL statements.
  10. */
  11. public class BankingSystem {
  12. // Connection properties
  13. private static String driver;
  14. private static String url;
  15. private static String username;
  16. private static String password;
  17.  
  18. // JDBC Objects
  19. private static Connection con;
  20. private static Statement stmt;
  21. private static ResultSet rs;
  22.  
  23. // other global variables
  24. private static String activeAcc = "A";
  25. private static String inactiveAcc = "I";
  26.  
  27. /**
  28. * Initialize database connection given properties file.
  29. * @param filename name of properties file
  30. */
  31. public static void init(String filename) {
  32. try {
  33. Properties props = new Properties(); // Create a new Properties object
  34. FileInputStream input = new FileInputStream(filename); // Create a new FileInputStream object using our filename parameter
  35. props.load(input); // Load the file contents into the Properties object
  36. driver = props.getProperty("jdbc.driver"); // Load the driver
  37. url = props.getProperty("jdbc.url"); // Load the url
  38. username = props.getProperty("jdbc.username"); // Load the username
  39. password = props.getProperty("jdbc.password"); // Load the password
  40. } catch (Exception e) {
  41. e.printStackTrace();
  42. }
  43. }
  44.  
  45. /**
  46. * Test database connection.
  47. */
  48. public static void testConnection() {
  49. System.out.println(":: TEST - CONNECTING TO DATABASE");
  50. try {
  51. Class.forName(driver);
  52. con = DriverManager.getConnection(url, username, password);
  53. con.close();
  54. System.out.println(":: TEST - SUCCESSFULLY CONNECTED TO DATABASE");
  55. } catch (Exception e) {
  56. System.out.println(":: TEST - FAILED CONNECTED TO DATABASE");
  57. e.printStackTrace();
  58. }
  59. }
  60.  
  61. /**
  62. * Create a new customer.
  63. * @param name customer name
  64. * @param gender customer gender
  65. * @param age customer age
  66. * @param pin customer pin
  67. * Prints out message that customer has been added and also prints out the customer's id.
  68. */
  69. public static void newCustomer(String name, String gender, String age, String pin)
  70. {
  71. Statement stmt = con.createStatement();
  72. String query = "SELECT ID FROM P1.Customer (INSERT INTO P1.EMPLOYEE VALUES (DEFAULT,'" + name + "','" + gender + "','" + age + "','" + pin + "'))";
  73. ResultSet rs = stmt.executeQuery(query);
  74.  
  75. while(rs.next()){
  76. int id = rs.getInt(1);
  77. System.out.println(":: CUSTOMER ID: " + id + ".");
  78. }
  79.  
  80. rs.close();
  81. stmt.close();
  82.  
  83. System.out.println(":: CREATE NEW CUSTOMER - SUCCESS");
  84.  
  85. //ids++;
  86.  
  87. }
  88.  
  89. /**
  90. * Open a new account.
  91. * @param id customer id
  92. * @param type type of account
  93. * @param amount initial deposit amount
  94. */
  95. public static void openAccount(String id, String type, String amount)
  96. {
  97.  
  98. Statement stmt = con.createStatement();
  99. String query = "SELECT Number FROM P1.Account (INSERT INTO P1.ACCOUNT VALUES (DEFAULT,'" + id + "','" + amount + "','" + type + "', DEFAULT))";
  100. ResultSet rs = stmt.executeQuery(query);
  101.  
  102. while(rs.next()){
  103. int accNum = rs.getInt(1);
  104. System.out.println(":: ACCOUNT NUMBER: " + accNum + ".");
  105. }
  106.  
  107. rs.close();
  108. stmt.close();
  109.  
  110. System.out.println(":: OPEN ACCOUNT - SUCCESS");
  111.  
  112. //numbers++;
  113. }
  114.  
  115. /**
  116. * Close an account.
  117. * @param accNum account number
  118. */
  119. public static void closeAccount(String accNum)
  120. {
  121. Statement stmt = con.createStatement();
  122. String query = "UPDATE P1.ACCOUNT SET BALANCE = 0, STATUS = '" + inactiveAcc + "' WHERE NUMBER = '" + accNum + "'";
  123. ResultSet rs = stmt.executeQuery(query);
  124.  
  125. rs.close();
  126. stmt.close();
  127.  
  128. System.out.println(":: CLOSE ACCOUNT - SUCCESS");
  129. }
  130.  
  131. /**
  132. * Deposit into an account.
  133. * @param accNum account number
  134. * @param amount deposit amount
  135. */
  136. public static void deposit(String accNum, String amount)
  137. {
  138. Statement stmt = con.createStatement();
  139. String query = "UPDATE P1.ACCOUNT SET BALANCE = BALANCE + " + amount + " WHERE NUMBER = '" + accNum + "'";
  140. ResultSet rs = stmt.executeQuery(query);
  141.  
  142. rs.close();
  143. stmt.close();
  144.  
  145. System.out.println(":: DEPOSIT - SUCCESS");
  146. }
  147.  
  148. /**
  149. * Withdraw from an account.
  150. * @param accNum account number
  151. * @param amount withdraw amount
  152. */
  153. public static void withdraw(String accNum, String amount)
  154. {
  155. Statement stmt = con.createStatement();
  156. String query = "UPDATE P1.ACCOUNT SET BALANCE = BALANCE - " + amount + " WHERE NUMBER = '" + accNum + "'";
  157. ResultSet rs = stmt.executeQuery(query);
  158.  
  159. rs.close();
  160. stmt.close();
  161.  
  162. System.out.println(":: WITHDRAW - SUCCESS");
  163. }
  164.  
  165. /**
  166. * Transfer amount from source account to destination account.
  167. * @param srcAccNum source account number
  168. * @param destAccNum destination account number
  169. * @param amount transfer amount
  170. */
  171. public static void transfer(String srcAccNum, String destAccNum, String amount)
  172. {
  173. Statement stmt = con.createStatement();
  174. String query = "UPDATE P1.ACCOUNT SET BALANCE = BALANCE - " + amount + " WHERE NUMBER = '" + srcAccNum + "'";
  175. String query2 = "UPDATE P1.ACCOUNT SET BALANCE = BALANCE + " + amount + " WHERE NUMBER = '" + destAccNum + "'";
  176. ResultSet rs = stmt.executeQuery(query);
  177. ResultSet rs2 = stmt.executeQuery(query2);
  178.  
  179. rs.close();
  180. rs2.close();
  181. stmt.close();
  182.  
  183. System.out.println(":: TRANSFER - SUCCESS");
  184. }
  185.  
  186. /**
  187. * Display account summary.
  188. * @param accNum account number
  189. * Displays account number, balance, and total balance for all accounts under the same customer id.
  190. */
  191. public static void accountSummary(String accNum)
  192. {
  193. Statement stmt = con.createStatement();
  194. String query = "SELECT Number, Balance, COUNT(Balance) AS Total Balance FROM P1.Account WHERE ID = (SELECT ID FROM P1.Account WHERE Number = " + accNum + ") AND Type = '" + activeAcc "'";
  195. ResultSet rs = stmt.executeQuery(query);
  196.  
  197. rs.close();
  198. stmt.close();
  199.  
  200. System.out.println(":: ACCOUNT SUMMARY - SUCCESS");
  201. }
  202.  
  203. /**
  204. * Display account summary for admins.
  205. * @param custID customer id
  206. * Displays account number, balance, and total balance for all accounts under the same customer id.
  207. */
  208. public static void adminAccountSummary(String custID)
  209. {
  210. Statement stmt = con.createStatement();
  211. String query = "SELECT Number, Balance, COUNT(Balance) AS Total FROM P1.Account WHERE ID = " + custID + ") AND Type = '" + activeAcc "'";
  212. ResultSet rs = stmt.executeQuery(query);
  213.  
  214. rs.close();
  215. stmt.close();
  216.  
  217. System.out.println(":: ACCOUNT SUMMARY - SUCCESS");
  218. }
  219.  
  220.  
  221. /**
  222. * Display Report A - Customer Information with Total Balance in Decreasing Order.
  223. */
  224. public static void reportA()
  225. {
  226. Statement stmt = con.createStatement();
  227. String query = "SELECT Customer.ID, Customer.Name, Customer.Age, Customer.Gender, COUNT(Account.Balance) AS Total FROM P1.Customer INNER JOIN P1.Account ON Balance.ID = Customer.ID ORDER BY Total DESC";
  228. ResultSet rs = stmt.executeQuery(query);
  229.  
  230. rs.close();
  231. stmt.close();
  232.  
  233. System.out.println(":: REPORT A - SUCCESS");
  234. }
  235.  
  236. /**
  237. * Display Report B - Customer Information with Total Balance in Decreasing Order.
  238. * @param min minimum age
  239. * @param max maximum age
  240. */
  241. public static void reportB(String min, String max)
  242. {
  243. Statement stmt = con.createStatement();
  244. String query = "SELECT Customer.Age, COUNT(Account.Balance) AS Total FROM P1.Account INNER JOIN P1.Customer ON Balance.ID = Customer.ID WHERE Customer.Age >= '" + min + "' AND Customer.Age <= '" + max + "' GROUP BY Customer.Age ORDER BY Total DESC";
  245. ResultSet rs = stmt.executeQuery(query);
  246.  
  247. rs.close();
  248. stmt.close();
  249.  
  250. System.out.println(":: REPORT B - SUCCESS");
  251. }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement