Advertisement
Guest User

DatabaseSelectHelper Comments

a guest
Jul 10th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.88 KB | None | 0 0
  1. package com.bank.databasehelper;
  2.  
  3. import com.bank.accounts.Account;
  4. import com.bank.accounts.ChequingAccount;
  5. import com.bank.accounts.SavingsAccount;
  6. import com.bank.accounts.TaxFreeSavingsAccount;
  7. import com.bank.database.DatabaseSelector;
  8. import com.bank.roles.Admin;
  9. import com.bank.roles.Customer;
  10. import com.bank.roles.Teller;
  11. import com.bank.roles.User;
  12.  
  13. import java.math.BigDecimal;
  14. import java.sql.Connection;
  15. import java.sql.ResultSet;
  16. import java.sql.SQLException;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19.  
  20.  
  21. /**
  22. * The Class DatabaseSelectHelper.
  23. */
  24. public class DatabaseSelectHelper extends DatabaseSelector {
  25.  
  26. /**
  27. * Gets the name of the role for a particular roleID.
  28. *
  29. * @param id the role id
  30. * @return the name of the role
  31. */
  32. public static String getRole(int id) {
  33. String role;
  34. Connection connection = DatabaseDriverHelper.connectOrCreateDataBase();
  35. try {
  36. // Attempt to get role from database
  37. role = DatabaseSelector.getRole(id, connection);
  38. } catch (SQLException e) {
  39. // If attempt to get role fails, role will be returned as null
  40. role = null;
  41. }
  42. try {
  43. connection.close();
  44. } catch (SQLException e) {
  45. System.err.println("Failed to close connection to database");
  46. }
  47. return role;
  48. }
  49.  
  50. /**
  51. * Gets the hashed password of a user in the database.
  52. *
  53. * @param userId the user id
  54. * @return the hashed password for the user
  55. */
  56. public static String getPassword(int userId) {
  57. Connection connection = DatabaseDriverHelper.connectOrCreateDataBase();
  58. String hashPassword;
  59. try {
  60. // Attempt to get password from database
  61. hashPassword = DatabaseSelector.getPassword(userId, connection);
  62. } catch (SQLException e) {
  63. // If attempt to get password fails, password will be returned as null
  64. hashPassword = null;
  65. }
  66. try {
  67. connection.close();
  68. } catch (SQLException e) {
  69. System.err.println("Failed to close connection to database");
  70. }
  71. return hashPassword;
  72.  
  73. }
  74.  
  75. /**
  76. * Gets the user details (returned as a user object).
  77. *
  78. * @param userId the user id
  79. * @return a user object holding user information
  80. */
  81. public static User getUserDetails(int userId) {
  82. User user = null;
  83. ResultSet results;
  84. Connection connection = DatabaseDriverHelper.connectOrCreateDataBase();
  85. try {
  86. // Attempt to get user details from the database
  87. results = DatabaseSelector.getUserDetails(userId, connection);
  88. } catch (SQLException e) {
  89. // If attempt to get user details fails, it will be returned as null
  90. results = null;
  91. }
  92. // Initialize variables to store user details
  93. String name = "";
  94. int age = -1;
  95. String address = "";
  96. int roleid = -1;
  97.  
  98. // Parse the resultSet for the user details
  99. try {
  100. if (!results.isClosed()) {
  101. while (results.next()) {
  102. name = results.getString("NAME");
  103. age = results.getInt("AGE");
  104. address = results.getString("ADDRESS");
  105. roleid = results.getInt("ROLEID");
  106. }
  107. // Determine which role type the user is
  108. String role = getRole(roleid);
  109. // Call a helper that creates a user object according to the user details
  110. user = createUserOfRole(role, userId, name, age, address);
  111. }
  112. } catch (SQLException e) {
  113. System.err.println("Error while parsing data from database.");
  114. }
  115. try {
  116. connection.close();
  117. } catch (SQLException e) {
  118. System.err.println("Failed to close connection to database");
  119. }
  120. return user;
  121. }
  122.  
  123. /**
  124. * Gets the account ids belonging to a particular user.
  125. *
  126. * @param userId the user id
  127. * @return the account ids (owned by the user)
  128. */
  129. public static List<Integer> getAccountIds(int userId) {
  130. // Create an arrayList to store all account ids
  131. List<Integer> accountIds = new ArrayList<Integer>();
  132. Connection connection = DatabaseDriverHelper.connectOrCreateDataBase();
  133. try {
  134. // Attempt to get account ids from the database
  135. ResultSet results = DatabaseSelector.getAccountIds(userId, connection);
  136. // Parse through table of account ids and store them in the list
  137. while (results.next()) {
  138. accountIds.add(results.getInt("ACCOUNTID"));
  139. }
  140. } catch (SQLException e) {
  141. // If attempt to get account ids fails, the list will be returned empty
  142. e.printStackTrace();
  143. }
  144.  
  145. try {
  146. connection.close();
  147. } catch (SQLException e) {
  148. System.err.println("Failed to close connection to database");
  149. }
  150. return accountIds;
  151. }
  152.  
  153. /**
  154. * Gets the account details for a specifics account.
  155. *
  156. * @param accountId the account id
  157. * @return the account details
  158. * @throws SQLException the SQL exception
  159. */
  160. public static Account getAccountDetails(int accountId) {
  161. Account account = null;
  162. ResultSet results;
  163. Connection connection = DatabaseDriverHelper.connectOrCreateDataBase();
  164. try {
  165. // Attempt to get account details from the database
  166. results = DatabaseSelector.getAccountDetails(accountId, connection);
  167. } catch (SQLException e) {
  168. System.err.println("Failed to get details for the given userId.");
  169. results = null;
  170. }
  171. String name = "";
  172. String balance = "";
  173. int type = -1;
  174. try {
  175. // try to get the name, type and balance of the account
  176. if (!results.isClosed()) {
  177. while (results.next()) {
  178. name = results.getString("NAME");
  179. balance = results.getString("BALANCE");
  180. type = results.getInt("TYPE");
  181. }
  182. String typeName = getAccountTypeName(type);
  183. // create account using the retrieved attributes
  184. account = createAccountOfType(typeName, accountId, name, new BigDecimal(balance));
  185. }
  186. } catch (SQLException e) {
  187. System.err.println("Error while parsing data from database.");
  188. }
  189. try {
  190. connection.close();
  191. } catch (SQLException e) {
  192. System.err.println("Failed to close connection to database");
  193. }
  194. return account;
  195. }
  196.  
  197.  
  198. /**
  199. * Gets the balance.
  200. *
  201. * @param accountId the account id
  202. * @return the balance of the account
  203. * @throws SQLException the SQL exception
  204. */
  205. public static BigDecimal getBalance(int accountId) {
  206. Connection connection = DatabaseDriverHelper.connectOrCreateDataBase();
  207. BigDecimal balance;
  208. try {
  209. // attempt to get balance
  210. balance = DatabaseSelector.getBalance(accountId, connection);
  211. } catch (SQLException e) {
  212. balance = null;
  213. }
  214. try {
  215. connection.close();
  216. } catch (SQLException e) {
  217. System.err.println("Failed to close connection to database");
  218. }
  219. return balance;
  220. }
  221.  
  222. /**
  223. * Gets the interest rate.
  224. *
  225. * @param accountType the account type
  226. * @return the interest rate of the account
  227. * @throws SQLException the SQL exception
  228. */
  229. public static BigDecimal getInterestRate(int accountType) {
  230. BigDecimal interestRate;
  231. Connection connection = DatabaseDriverHelper.connectOrCreateDataBase();
  232. try {
  233. // attempt to get the interest rate
  234. interestRate = DatabaseSelector.getInterestRate(accountType, connection);
  235. } catch (SQLException e) {
  236. interestRate = null;
  237. }
  238. try {
  239. connection.close();
  240. } catch (SQLException e) {
  241. System.err.println("Failed to close connection to database");
  242. }
  243. return interestRate;
  244. }
  245.  
  246. /**
  247. * Gets the account types ids.
  248. *
  249. * @return the account types ids
  250. * @throws SQLException the SQL exception
  251. */
  252. public static List<Integer> getAccountTypesIds() {
  253. List<Integer> ids = new ArrayList<>();
  254. ResultSet results;
  255. Connection connection = DatabaseDriverHelper.connectOrCreateDataBase();
  256. try {
  257. // attempt to get list of account type id's
  258. results = DatabaseSelector.getAccountTypesId(connection);
  259. while (results.next()) {
  260. ids.add(results.getInt("ID"));
  261. }
  262. } catch (SQLException e) {
  263. System.err.println("Failed to fetch account type data.");
  264. }
  265. try {
  266. connection.close();
  267. } catch (SQLException e) {
  268. System.err.println("Failed to close connection to database");
  269. }
  270. return ids;
  271. }
  272.  
  273. /**
  274. * Gets the account type name.
  275. *
  276. * @param accountTypeId the account type id
  277. * @return the account type name
  278. * @throws SQLException the SQL exception
  279. */
  280. public static String getAccountTypeName(int accountTypeId) {
  281. String typeName;
  282. Connection connection = DatabaseDriverHelper.connectOrCreateDataBase();
  283. try {
  284. // attempt to get account type name
  285. typeName = DatabaseSelector.getAccountTypeName(accountTypeId, connection);
  286. } catch (SQLException e) {
  287. typeName = null;
  288. }
  289. try {
  290. connection.close();
  291. } catch (SQLException e) {
  292. System.err.println("Failed to close connection to database");
  293. }
  294. return typeName;
  295. }
  296.  
  297. /**
  298. * Gets the roles.
  299. *
  300. * @return the roles
  301. * @throws SQLException the SQL exception
  302. */
  303. public static List<Integer> getRoles() {
  304. List<Integer> ids = new ArrayList<>();
  305. Connection connection = DatabaseDriverHelper.connectOrCreateDataBase();
  306. try {
  307. // attempt to get list of roles
  308. ResultSet results = DatabaseSelector.getRoles(connection);
  309. while (results.next()) {
  310. ids.add(results.getInt("ID"));
  311. }
  312. } catch (SQLException e) {
  313. System.err.println("Failed to fetch role data.");
  314. }
  315. try {
  316. connection.close();
  317. } catch (SQLException e) {
  318. System.err.println("Failed to close connection to database");
  319. }
  320. return ids;
  321.  
  322. }
  323.  
  324. /**
  325. * Gets the account type.
  326. *
  327. * @param accountId the account type id
  328. * @return the account type
  329. * @throws SQLException the SQL exception
  330. */
  331. public static int getAccountType(int accountId) {
  332. int accountType;
  333. Connection connection = DatabaseDriverHelper.connectOrCreateDataBase();
  334. try {
  335. // attempt to get account type
  336. accountType = DatabaseSelector.getAccountType(accountId, connection);
  337. } catch (SQLException e) {
  338. accountType = -1;
  339. }
  340. try {
  341. connection.close();
  342. } catch (SQLException e) {
  343. System.err.println("Failed to close connection to database");
  344. }
  345. return accountType;
  346.  
  347. }
  348.  
  349. /**
  350. * Gets the user role.
  351. *
  352. * @param userId the user id
  353. * @return the user role
  354. * @throws SQLException the SQL exception
  355. */
  356. public static int getUserRole(int userId) {
  357. int userRole;
  358. Connection connection = DatabaseDriverHelper.connectOrCreateDataBase();
  359. try {
  360. // attempt to get user role
  361. userRole = DatabaseSelector.getUserRole(userId, connection);
  362. } catch (SQLException e) {
  363. userRole = -1;
  364. }
  365. try {
  366. connection.close();
  367. } catch (SQLException e) {
  368. System.err.println("Failed to close connection to database");
  369. }
  370. return userRole;
  371. }
  372.  
  373. /**
  374. * Creates the user of role.
  375. *
  376. * @param role the user role
  377. * @param userId the user id
  378. * @param name the user name
  379. * @param age the user age
  380. * @param address the user address
  381. * @return the user
  382. */
  383. private static User createUserOfRole(String role, int userId, String name, int age,
  384. String address) {
  385. User newUser = null;
  386. // check if role is Admin
  387. if (role.equalsIgnoreCase("Admin")) {
  388. newUser = new Admin(userId, name, age, address);
  389. // check if role is Teller
  390. } else if (role.equalsIgnoreCase("Teller")) {
  391. newUser = new Teller(userId, name, age, address);
  392. // check if role is Customer
  393. } else if (role.equalsIgnoreCase("Customer")) {
  394. Customer newCustomer = new Customer(userId, name, age, address);
  395. List<Integer> accountIds = getAccountIds(userId);
  396. for (int accountId : accountIds) {
  397. newCustomer.addAccount(getAccountDetails(accountId));
  398. }
  399. newUser = newCustomer;
  400. }
  401. return newUser;
  402. }
  403.  
  404. /**
  405. * Creates the account of type.
  406. *
  407. * @param type the account type
  408. * @param id the account id
  409. * @param name the account name
  410. * @param balance the account balance
  411. * @return the account
  412. */
  413. private static Account createAccountOfType(String type, int id, String name, BigDecimal balance) {
  414. Account newAccount = null;
  415. // check if account is chequing
  416. if (type.equalsIgnoreCase("Chequing")) {
  417. newAccount = new ChequingAccount(id, name, balance);
  418. // check if account is saving
  419. } else if (type.equalsIgnoreCase("Saving")) {
  420. newAccount = new SavingsAccount(id, name, balance);
  421. // check if account is TFSA
  422. } else if (type.equalsIgnoreCase("TFSA")) {
  423. newAccount = new TaxFreeSavingsAccount(id, name, balance);
  424. }
  425. return newAccount;
  426. }
  427.  
  428. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement