Advertisement
Guest User

Untitled

a guest
Nov 16th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.23 KB | None | 0 0
  1. import java.io.Console;
  2. import java.sql.*;
  3.  
  4. /**
  5. *
  6. * @author Payton M. Dunning &
  7. * @author Evan J. Overweg
  8. *
  9. * Java program that interacts with a series of mySQL tables that resemble a very simple banking system.
  10. * This program allows the user to either transfer money between a saving and checking account, or view all account under a specific customer.
  11. */
  12. public class bank {
  13.  
  14. Connection conn = null;
  15. Statement sttmnt = null;
  16. ResultSet rSet = null;
  17.  
  18. /** Connects the user to the MTU database pmdunnin in order to use the simple banking system.
  19. * @return - Returns a 1 on success. Otherwise, returns a 0 if the connection fails.
  20. */
  21. public int connectDB() {
  22. String username = "";
  23. String password = "";
  24.  
  25. // Gets the user's username and password for use in connecting to the database.
  26. try {
  27. Console console = System.console();
  28.  
  29. if(console == null) {
  30. System.out.println("There was an issue with the console. Please run the rerun the program in terminal");
  31. System.out.println("For example: java -cp '.:SQL JAR FILE PATH HERE' bank FUNCTION_NAME ARGUMENTS");
  32. return 0;
  33. }
  34.  
  35. // Gets the user's username and password.
  36. username = console.readLine("Please enter in your username:");
  37. password = String.valueOf(console.readPassword("Please enter in your password:"));
  38.  
  39. } catch (Exception exc1) {
  40. exc1.printStackTrace();
  41. }
  42.  
  43. // Uses the given username and password to connect to the bank system's database.
  44. try {
  45. conn = DriverManager.getConnection("jdbc:mysql://classdb.it.mtu.edu/pmdunnin", username, password);
  46.  
  47. } catch (SQLException exc2) {
  48. System.out.println(exc2.getMessage());
  49. exc2.printStackTrace();
  50. return 1;
  51. }
  52.  
  53. return 1;
  54. }
  55.  
  56. /**
  57. * Disconnects the user from the database server.
  58. */
  59. public void disconnect() {
  60.  
  61. try {
  62. conn.close();
  63.  
  64. } catch (SQLException exc) {
  65. System.out.println("SQLException: " + exc.getMessage());
  66. System.out.println("SQLState: " + exc.getSQLState());
  67. System.out.println("VendorError: " + exc.getErrorCode());
  68. }
  69. }
  70.  
  71. /**
  72. * Transfers the given amount of money, balanceAmount, from a given saving account into a given saving account.
  73. * @param savingAccountNum - The account number for the saving account that will have money transfered into it.
  74. * @param checkingAccountNum - The account number for the checking account that will have money transfered from it.
  75. * @param balanceAmount - The amount of money to be transfered.
  76. * @return - Returns a 1 on successful transfer. Returns 0 if the transfer fails at any point.
  77. */
  78. public int transfer(String savingAccountNum, String checkingAccountNum, double balanceAmount) {
  79.  
  80. PreparedStatement statement = null;
  81. ResultSet rSet = null;
  82. int rowCount;
  83. //Console transferConsole = System.console();
  84.  
  85. double oldSavingBalance = 0;
  86. double oldCheckingBalance = 0;
  87. double newSavingBalance = 0;
  88. double newCheckingBalance = 0;
  89.  
  90. //System.out.println("Transfer Test 1");
  91. // Start Transaction:
  92. try {
  93. conn.setAutoCommit(false);
  94. conn.setTransactionIsolation(conn.TRANSACTION_SERIALIZABLE);
  95. } catch (SQLException exc3) {
  96. exc3.printStackTrace();
  97. return 0;
  98. }
  99.  
  100. //System.out.println("Transfer Test 2");
  101. // Checks if savingAccountNum is valid. If it is, see if it has enough money in it to execute the transfer.
  102. try {
  103. statement = conn.prepareStatement("SELECT * FROM saving WHERE account_number = ?");
  104. statement.setString(1, savingAccountNum);
  105. rSet = statement.executeQuery();
  106. // .next returns FALSE if at the last. In this case, it means that the account doesn't exist.
  107. if( rSet.next() ) {
  108. oldSavingBalance = rSet.getDouble(3);
  109. System.out.println("\nSaving Account Valid, Current Balance: " + oldSavingBalance);
  110.  
  111. if ( oldSavingBalance < balanceAmount) {
  112. System.out.println("This savings account does not have enough money in it to complete the transfer.");
  113. conn.rollback();
  114. return 0;
  115. }
  116.  
  117. } else {
  118. System.out.println("The given savings account does not exist. Please use a valid account number.");
  119. conn.rollback();
  120. return 0;
  121. }
  122.  
  123. } catch (SQLException exc) {
  124. // Handle any errors
  125. System.out.println("SQLException: " + exc.getMessage());
  126. System.out.println("SQLState: " + exc.getSQLState());
  127. System.out.println("VendorError: " + exc.getErrorCode());
  128. }
  129.  
  130. // Reset statement and rSet just to be safe;
  131. statement = null;
  132. rSet = null;
  133.  
  134. // Checks if checkingAccountNum is valid.
  135. try {
  136. statement = conn.prepareStatement("SELECT * FROM checking WHERE account_number = ?");
  137. statement.setString(1, checkingAccountNum);
  138. rSet = statement.executeQuery();
  139.  
  140. if (rSet.next() ) {
  141. oldCheckingBalance = rSet.getDouble(3);
  142. System.out.println("\nChecking Account Valid, Current Balance: " + oldCheckingBalance);
  143. } else {
  144. System.out.println("The given checkings account does not exist. Please use a valid account number.");
  145. conn.rollback();
  146. return 0;
  147. }
  148.  
  149. } catch (SQLException exc) {
  150. // Handle any errors
  151. System.out.println("SQLException: " + exc.getMessage());
  152. System.out.println("SQLState: " + exc.getSQLState());
  153. System.out.println("VendorError: " + exc.getErrorCode());
  154. }
  155.  
  156. // Reset statement and rSet just to be safe;
  157. statement = null;
  158. rSet = null;
  159.  
  160. // Once input has been checked, begin performing the actual transfer.
  161. try {
  162. statement = conn.prepareStatement("UPDATE checking SET balance = balance + ? WHERE account_number = ?");
  163. statement.setDouble(1, balanceAmount);
  164. statement.setString(2, checkingAccountNum);
  165. int rowsUpdated = statement.executeUpdate();
  166.  
  167. if (rowsUpdated != 1) {
  168. System.out.println("Something went wrong with transfering money to the checking account. Please try again later.");
  169. conn.rollback();
  170. return 0;
  171. }
  172.  
  173. } catch (SQLException exc) {
  174. // Handle any errors
  175. System.out.println("SQLException: " + exc.getMessage());
  176. System.out.println("SQLState: " + exc.getSQLState());
  177. System.out.println("VendorError: " + exc.getErrorCode());
  178. }
  179.  
  180. // Reset statement and rSet just to be safe;
  181. statement = null;
  182. rSet = null;
  183.  
  184. try {
  185. statement = conn.prepareStatement("UPDATE saving SET balance = balance - ? WHERE account_number = ?");
  186. statement.setDouble(1, balanceAmount);
  187. statement.setString(2, savingAccountNum);
  188. int rowsUpdated = statement.executeUpdate();
  189.  
  190. if (rowsUpdated != 1) {
  191. System.out.println("Something went wrong with transfering money from the saving account. Please try again later.");
  192. conn.rollback();
  193. return 0;
  194. }
  195.  
  196. } catch (SQLException exc) {
  197. // Handle any errors
  198. System.out.println("SQLException: " + exc.getMessage());
  199. System.out.println("SQLState: " + exc.getSQLState());
  200. System.out.println("VendorError: " + exc.getErrorCode());
  201. }
  202.  
  203.  
  204. try {
  205. conn.commit();
  206. } catch (SQLException exc) {
  207. // Handle any errors
  208. System.out.println("SQLException: " + exc.getMessage());
  209. System.out.println("SQLState: " + exc.getSQLState());
  210. System.out.println("VendorError: " + exc.getErrorCode());
  211. }
  212.  
  213. System.out.println("\nTransfer Completed!\n");
  214.  
  215. newCheckingBalance = oldCheckingBalance + balanceAmount;
  216. newSavingBalance = oldSavingBalance - balanceAmount;
  217. // Displays the balances of the saving and checking accounts before and after the transfer.
  218. System.out.println("| Pre-Transfer Savings Account Balance = " + oldSavingBalance);
  219. System.out.println("| Pre-Transfer Checkings Account Balance = " + oldCheckingBalance);
  220. System.out.println("| Post-Transfer Savings Account Balance = " + newSavingBalance);
  221. System.out.println("| Post-Transfer Checkings Account Balance = " + newCheckingBalance + "\n");
  222.  
  223. return 1;
  224. }
  225.  
  226. public int display(int customerID) {
  227. try {
  228. conn.setAutoCommit(false);
  229. conn.setTransactionIsolation(conn.TRANSACTION_SERIALIZABLE);
  230. } catch (SQLException exc3) {
  231. exc3.printStackTrace();
  232. return 0;
  233. }
  234.  
  235. //
  236. try {
  237. //first confirm that customer_id exists
  238.  
  239. PreparedStatement statement = conn.prepareStatement("SELECT * FROM customer WHERE id = 13");
  240. // statement.setString(1, Integer.toString(customerID));
  241. //PreparedStatement statement = conn.prepareStatement("SELECT * FROM customer");
  242. ResultSet rSet = statement.executeQuery();
  243. System.out.println("Fetch Size = " + rSet.getFetchSize());
  244. /*if(rSet.getFetchSize() != 1)
  245. {
  246.  
  247. //customer doesn't exist
  248. System.out.println("Customer ID "+ customerID +" not found");
  249. conn.rollback();
  250. }
  251. else
  252. {*/
  253. if(rSet.next()){
  254. System.out.println("Customer Name: " + rSet.getString(1));
  255. }
  256. //checking account statement
  257. statement = conn.prepareStatement("SELECT balance FROM checking WHERE customer_id = ?");
  258. statement.setString(1, Integer.toString(customerID));
  259. rSet = statement.executeQuery();
  260. if(rSet.getFetchSize() < 1) {
  261. System.out.printf("No checking accounts found for customer %d", customerID);
  262. conn.rollback();
  263. }
  264. else {
  265. while(rSet.next())
  266. {
  267. System.out.println(rSet.getString("balance"));
  268. }
  269. }
  270.  
  271. //savings account statement
  272. statement = conn.prepareStatement("SELECT balance FROM saving WHERE customer_id = ?");
  273. statement.setString(1, Integer.toString(customerID));
  274. rSet = statement.executeQuery();
  275. if(rSet.getFetchSize() < 1) {
  276. System.out.printf("No saving accounts found for customer %d", customerID);
  277. conn.rollback();
  278. }
  279. else {
  280. //print saving account balances here
  281. while(rSet.next())
  282. {
  283. System.out.println(rSet.getString("balance"));
  284. }
  285. }
  286. conn.commit();
  287. //}
  288.  
  289. //System.out.println("Test");
  290. } catch (SQLException exc4) {
  291. // Handle any errors
  292. System.out.println("SQLException: " + exc4.getMessage());
  293. System.out.println("SQLState: " + exc4.getSQLState());
  294. System.out.println("VendorError: " + exc4.getErrorCode());
  295. }
  296.  
  297. return 1;
  298.  
  299.  
  300. }
  301.  
  302. public static void main(String args[]) {
  303. bank mtuBank = new bank();
  304. //Console mainConsole = System.console();
  305. int customerID;
  306. String savingAccount = "";
  307. String checkingAccount = "";
  308. String functionSelect = "";
  309. double balanceAmount;
  310.  
  311. if (args.length < 2) {
  312. System.out.println("Incomplete program arguments. Please run program again.");
  313.  
  314. } else {
  315.  
  316. functionSelect = args[0];
  317. mtuBank.connectDB();
  318.  
  319. //mainConsole.printf("\n Function Check:\n" + functionSelect + "\n\n");
  320.  
  321. // Runs the display function based on command line input.
  322. if ( functionSelect.equals("display")) {
  323. System.out.println("\nDisplay Selected");
  324. //mainConsole.printf("\n Display Check \n");
  325. //mainConsole.flush();
  326. customerID = Integer.parseInt(args[1]);
  327. mtuBank.display(customerID);
  328.  
  329. // Runs the transfer function based on command line input.
  330. } else if (functionSelect.equals("transfer") && args.length >= 4) {
  331. System.out.println("\nTransfer Selected");
  332. //mainConsole.printf("\n Transfer Check \n");
  333. //mainConsole.flush();
  334. savingAccount = args[1];
  335. checkingAccount = args[2];
  336. balanceAmount = Double.parseDouble(args[3]);
  337. mtuBank.transfer(savingAccount, checkingAccount, balanceAmount);
  338.  
  339. }
  340. //mainConsole.printf("\n\n" + "args length:" + args.length + "\n\n");
  341.  
  342. //mainConsole.printf("\nPrint Check\n\n");
  343. //mainConsole.flush();
  344.  
  345. mtuBank.disconnect();
  346.  
  347. }
  348. }
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement