Guest User

Untitled

a guest
Jul 30th, 2016
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 65.04 KB | None | 0 0
  1.  
  2. /** Radoslaw Burkacki
  3. * 29/05/2016
  4. * Bankingapp class
  5. * This class is a program driver
  6. * All menus are here and all of the program functionality is here */
  7.  
  8. /** Importing libriaries*/
  9. import java.sql.*;
  10. import java.util.ArrayList;
  11. import java.util.InputMismatchException;
  12. import java.util.Random;
  13. import java.util.Scanner;
  14. /** Importing libriaries*/
  15.  
  16.  
  17. public class Bankingapp { /** Creating class called Bankingapp*/
  18.  
  19.  
  20. public static void main(String[] args) { /** Creating main class, when the program starts this method is loaded first!*/
  21. boolean isdbconnected=false; /** Creating new boolean variable*/
  22. isdbconnected = isDbConnected(); /**Setting isdbconnected to whatever will be returned from function isDbConnected*/
  23.  
  24. if(isdbconnected == false){ /**if statement if isdbconnected equals to false then...*/
  25. System.out.println("Cannot connect to database."); /**display message*/
  26. }else if(isdbconnected){ /**if isd connected is equal to true then run program...*/
  27.  
  28. Scanner sc = new Scanner(System.in); /**create new instance of scanner*/
  29. int menu=0; /** create variable menu as int*/
  30.  
  31. String dbpath ="jdbc:mysql://localhost:3306/gradedunit2?autoReconnect=true&useSSL=false"; /** Creating a string called dbpath and setting its value to my database path. I will use it later to connect to my database */
  32. String dbid = "root"; /** Creating a string called dbit setting its value to root. I will use this variable each time i connect to database*/
  33. String dbpassword = "1234"; /** Creating a string called dbpassword setting its value to 1234. I will use this variable each time i connect to database*/
  34.  
  35. do{ /** Starting loop*/
  36. System.out.println("___________________________________________________________________________________________________________"); /** Displaying message */
  37. System.out.println("1. Open New Account"); /** Displaying message */
  38. System.out.println("2. Login"); /** Displaying message */
  39. System.out.println("3. Exit"); /** Displaying message */
  40. System.out.println("Please enter 1-3"); /** Displaying message */
  41.  
  42. try { /** Starting try statement(erro handling for menu(missmatchexception) */
  43. menu = sc.nextInt(); /** saving input from user to menu */
  44.  
  45. } catch (InputMismatchException e) {/** Catchning missmatchexception occours when user enters string instead of numbers*/
  46. sc.nextLine(); /** clear buffer */
  47. }
  48.  
  49.  
  50. if(menu ==0 || menu >3) { /** if menu is equal to 0 or equal to more than 3 then ... */
  51. System.out.println("Please use numbers 1 to 3 to navigate throught menu"); /** Displaying message */
  52. } /** Close if statement */
  53.  
  54.  
  55. switch (menu){ /** starting switch statement(menu) */
  56.  
  57. case 1: /** Starting case one. If menu = 1 then...*/
  58.  
  59. String title = null, fname = null, lname = null, address = null, postcode = null, city = null; /** Create variables to store customer details*/
  60. sc.nextLine(); /** Clearing buffer*/
  61.  
  62.  
  63. System.out.println("Please enter title"); /** Displaying message */
  64. title = sc.nextLine(); /** Saving input from user to title */
  65. if(title.equals("")){ /** if title is empty then... */
  66. do{ /** Start loop */
  67. System.out.println("Please re-enter title"); /** Displaying message */
  68. title = sc.nextLine(); /** saving input from user to title */
  69. }while(title.equals("")); /** end loop when title is not empty
  70. p when title is not empty */
  71. } /** closing if statement */
  72. if(!Character.isUpperCase(title.charAt(0))){ /** if statement, if first letter of title is lower case then... */
  73. title = ToUpperCase(title); /** set title equal to(call function ToUpperCase and pass title into it) */
  74. } /** Close if */
  75.  
  76. System.out.println("Please enter first name"); /** Displaying message */
  77. fname = sc.nextLine(); /** Saving input from user to fname */
  78. if(fname.equals("")){ /** if fname is empty then... */
  79. do{ /** Start loop */
  80. System.out.println("Please re-enter first name"); /** Displaying message */
  81. fname = sc.nextLine(); /** set fname to whatever was enterd by user*/
  82. }while(fname.equals("")); /** end loop when fname is not equal to nothing*/
  83. }/**close if */
  84. if(!Character.isUpperCase(fname.charAt(0))){ /** if if index 0 of fname is equal to lower letter thenn.. */
  85. fname = ToUpperCase(fname); /** set fname equal to(call function ToUpperCase and pass fname into it) */
  86. } /**close if*/
  87.  
  88.  
  89.  
  90. System.out.println("Please enter last name"); /** Displaying message */
  91. lname = sc.nextLine(); /** set lname equal to input from user*/
  92. if(lname.equals("")){ /** start if statement if lname equals to nothing*/
  93. do{ /**start do while loop*/
  94. System.out.println("Please re-enter last name"); /** Displaying message */
  95. lname = sc.nextLine(); /** set lname equal to input from user*/
  96. }while(lname.equals("")); /** stop loop when lname is not equal to nothing*/
  97. } /**close if statement*/
  98. if(!Character.isUpperCase(lname.charAt(0))){ /** if index 0 of lname is lower case then...*/
  99. lname = ToUpperCase(lname); /** set lname equal to(call function ToUpperCase and pass lname into it) */
  100. }
  101.  
  102.  
  103.  
  104. System.out.println("Please enter address"); /** Displaying message */
  105. address = sc.nextLine(); /** set address equal to input from user */
  106. if(address.equals("")){ /** if address is empty */
  107. do{ /** start do while loop */
  108. System.out.println("Please re-enter address"); /** Displaying message */
  109. address = sc.nextLine(); /** set address equal to input from user */
  110. }while(address.equals("")); /** ending loop if address it not empty */
  111. }
  112.  
  113.  
  114. System.out.println("Please enter postcode"); /** Displaying message */
  115. postcode = sc.nextLine(); /** set postcode equal to input from user */
  116. if(postcode.equals("")){ /** if postcode is empty */
  117. do{ /** start do while loop */
  118. System.out.println("Please re-enter postcode"); /** Displaying message */
  119. postcode = sc.nextLine(); /** set postcode equal to input from user*/
  120. }while(postcode.equals("")); /** end while loop when postcode is not empty */
  121. }
  122.  
  123.  
  124. System.out.println("Please enter city"); /** Displaying message */
  125. city = sc.nextLine(); /** set city equal to input from user */
  126. if(city.equals("")){ /** if city is empty then... */
  127. do{ /** start do while loop */
  128. System.out.println("Please enter re-city"); /** Displaying message */
  129. city = sc.nextLine(); /** set city equal to input from user */
  130. }while(city.equals("")); /** end while loop when city is not empty */
  131. }
  132. if(!Character.isUpperCase(city.charAt(0))){ /** if index 0 of city is lower case */
  133. city = ToUpperCase(city); /** set city equal to(call function ToUpperCase and pass city into it) */
  134. }
  135.  
  136.  
  137. /** Setting first letter of title,fname,lname,address,postcode and city to capital letter */
  138.  
  139. Customer c = new Customer(title, fname , lname , city, postcode, address); /** Creating new object called c of customer class and passing parameters */
  140.  
  141.  
  142. int accountnumber=0,sortcode=0; String password=null,password2=null,accounttype = null; /** Creating new variables and setting values to null */
  143.  
  144.  
  145. /** Making sure that user has repeated has repeated his password */
  146. do{ /** Starting loop */
  147. System.out.println("Please enter your password"); /** Displaying message */
  148. password = sc.nextLine(); /** Reading input from user and saving it to variable password */
  149. System.out.println("Please reenter your password"); /** Displaying message */
  150. password2= sc.nextLine(); /** Reading input from user and saving it to variable password2 */
  151. if(!password.equals(password2)){ /** Creating if statement if password is not equal to password2 then ... */
  152. System.out.println("Passwords do not match"); /** Displaying message */
  153. }
  154. }while (!password.equals(password2)); /** closing loop, if password is the same as password2 then stop loop otherwise repeat loop */
  155. /** Making sure that user has repeated has repeated his password */
  156.  
  157.  
  158. /** Validating input from user user will be able to enter only "Standard" or "Premium" other wise error message will be displayed */
  159. do { /** Starting loop */
  160. System.out.println("Please select account type. Standard or Premium(£5)"); /** Displaying message */
  161. accounttype = sc.nextLine(); /** Getting input from user and saving it to accounttype variable */
  162.  
  163. if(!Character.isUpperCase(accounttype.charAt(0))){ /** if character at index 0 of variable accounttype is not uppercase then*/
  164. accounttype= ToUpperCase(accounttype);
  165. }/** close if statement */
  166.  
  167. if((!accounttype.equals("Standard")) & (!accounttype.equals("Premium"))){ /** Creating if statment, if input from user is not equal to "Standard" or "Premium" then ... */
  168. do{ /** Start new loop */
  169. System.out.println("Please select account type. Standard or Premium(£5)"); /** Display message */
  170. accounttype = sc.nextLine(); /** Getting input from user and saving it to accounttype variable */
  171.  
  172. if(!Character.isUpperCase(accounttype.charAt(0))){ /** if character at index 0 of variable accounttype is not uppercase then*/
  173. accounttype = ToUpperCase(accounttype);
  174. }/** close if statement */
  175.  
  176. }while((!accounttype.equals("Standard")) & (!accounttype.equals("Premium"))); /** closing loop, if accounttype is not equal to "Standard" or "Premium" then go back to loop other wise exit loop */
  177. } /** closing if statement */
  178. } while ((accounttype.equals("Standard")) & (accounttype.equals("Premium"))); /** closing loop, if acconttype equals to "Standard" or "Premium" then quit loop other wise loop again */
  179. /** Validating input from user user will be able to enter only "Standard" or "Premium" other wise error message will be displayed */
  180.  
  181.  
  182. Random r = new Random(); /** Creating new Random instance called r, will use that to generate random numbers */
  183.  
  184. boolean repeat = true;
  185.  
  186. do{ /** Start loop(need to make sure that unique account number is generated)*/
  187.  
  188. accountnumber = 10000000 + r.nextInt(90000000); /** Generating random number (xxxxxxxx) which will be customers account number */
  189. sortcode = 100000 + r.nextInt(900000); /** Generating random number (xxxxxx) which will be customers sortcode */
  190.  
  191. String sqlcommand8 = "SELECT AccountNumber FROM gradedunit2.account where AccountNumber = "+accountnumber; /** SQL command*/
  192. ArrayList <String> accnoexist = new ArrayList<String>(readdatabase(dbpath,dbid,dbpassword,sqlcommand8)); /** Creating array list called accnoexistt, calling a function readdatabase wich will return a arraylist with result of a query , passing variables (dbpath,dbid,dbpassword,sqlcommand8, anything that is returned by function readdatabase is saved into dbresult arraylist*/
  193.  
  194. if(accnoexist.get(0).equals("NotFound")){ /** If accnoexists at index 0 equals to notnull then ... */
  195. repeat = false; /**set repeat to false */
  196. }
  197. }while(repeat); /** when repeat is set to false then stop looping */
  198.  
  199. Account a = null; /** Create new account named a */
  200. if (accounttype.equals("Standard")){ /** if accounttype is equal to standard then... */
  201. a = new Standard(password, accountnumber, sortcode, accounttype); /** set a as Standard account and pass variables into it */
  202. }
  203.  
  204. else if(accounttype.equals("Premium")){ /** if accounttype is equal to premium */
  205. a = new Premium(password, accountnumber, sortcode, accounttype); /** set a as Premium account and pass variables into it */
  206. }
  207.  
  208. System.out.println("You opened your account. Your account number is: "+a.getAccountNumber()+" Your sort code is: " +a.getSortCode()); /** Displays message which customers account number and sort code */
  209.  
  210.  
  211.  
  212. String sqlcommandd = "SELECT COUNT(*) FROM customer"; /** Create new string and set its value to a sql command*/
  213.  
  214. ArrayList <String> noofrows1 = new ArrayList<String>(readdatabase(dbpath,dbid,dbpassword,sqlcommandd));
  215. /** Creating array list, calling a function readdatabase wich will return a arraylist with result of a query
  216. * passing variables (dbpath,dbid,dbpassword,sqlcommandd), anything that is returned by function readdatabase is saved into arraylisy*/
  217.  
  218.  
  219. int noofrows = Integer.parseInt(noofrows1.get(0)); /** create new int and set its value to noofrows1 at index 0 (convert it to string)*/
  220. noofrows ++; /** Increase noofrows by 1*/
  221.  
  222. /** adding new customer to account in database */
  223.  
  224. Connection conn1 = null; /** Create new connection*/
  225. Statement stmt1 = null; /** Create new statement*/
  226.  
  227. try /** Start new try block*/
  228. {
  229. conn1 = DriverManager.getConnection(dbpath,dbid,dbpassword); /** let connection connect to database and pass parameters*/
  230. stmt1 = conn1.createStatement();
  231.  
  232. stmt1.execute("INSERT INTO Customer (Title,FirstName,LastName,Address,PostCode,City)"+ "VALUES ('"+c.getTitle()+"','"+c.getFName()+"','"+c.getLName()+"','"+c.getAddress()+"','"+c.getPostCode()+"','"+c.getCity()+"')");
  233. /** executeing sql command*/
  234. stmt1.execute("INSERT INTO Account (CustomerID,Password,AccountNumber,SortCode,Balance,AccountType) "+ "VALUES ('"+noofrows+"','"+a.getPassword()+"','"+a.getAccountNumber()+"','"+a.getSortCode()+"','"+a.getBalance()+"','"+a.getAccountType()+"')");
  235. /** executeing sql command*/
  236. } catch (Exception d) { /** Catching exception*/
  237. d.printStackTrace(); /** printing error message*/
  238. }finally {
  239. try { /**Starting try block*/
  240. stmt1.close(); /** close statement*/
  241. conn1.close(); /** close connection*/
  242. } catch (Exception e){}
  243. }
  244.  
  245.  
  246. break; /** end of case 1*/
  247.  
  248. case 2: /**if user enterd 2 then...*/
  249.  
  250. String accnoinput=null; /**Create new string*/
  251. String passwordinput=null; /**Create new String*/
  252.  
  253. String dbaccounttype=null, dbsortcode=null, dbcustomerid = null, dbpass = null, dbaccno = null, dbtitle=null, dbfname=null,dblname=null; double dbbalance=0.00; /**Create variables*/
  254.  
  255.  
  256. sc.nextLine(); /**clearing buffer*/
  257.  
  258. System.out.println("Please enter your account number"); /**Displaying message*/
  259. accnoinput = sc.nextLine(); /**Saving input from user to accnoinput*/
  260.  
  261. System.out.println("Please enter your password"); /**Displaying message*/
  262. passwordinput = sc.nextLine(); /**Saving input from user to passwordinput*/
  263.  
  264. ////////////////////////////////////// Reaeding from database (CustomerID, AccountNumber, Password)
  265. Connection conn2 = null; /**create new connection */
  266. Statement stmt2 = null; /**create new statement */
  267. ResultSet rows2 = null; /**create new result set */
  268.  
  269. try { /** start try block */
  270.  
  271. Class.forName("com.mysql.jdbc.Driver");
  272. conn2 = DriverManager.getConnection(dbpath,dbid,dbpassword); /**connect to database and pass parameters into it @param dbpath, @param dbid, @param dbpassword */
  273. stmt2 = conn2.createStatement();
  274. rows2 = stmt2.executeQuery("Select account.AccountType, account.SortCode, account.CustomerID, account.AccountNumber, account.Password, account.Balance, customer.Title, customer.FirstName, customer.LastName FROM customer, account WHERE account.AccountNumber = "+accnoinput+" and customer.CustomerID = (select account.CustomerID where account.AccountNumber);");
  275. /** run sql command */
  276.  
  277. while(rows2.next()){ /** if rows2(return from sql statement) is NOT empty then loop*/
  278. dbaccounttype = rows2.getString("AccountType"); /**set dbaccounttype equal to whatever is inside in AccountType */
  279. dbsortcode = rows2.getString("SortCode"); /** set dbsortcode equal to whatever is inside SortCode column */
  280. dbcustomerid = rows2.getString("account.CustomerID"); /**set dbcustomerid equal to whatever is inside CustomerID column */
  281. dbaccno = rows2.getString("AccountNumber"); /**set dbaccno equal to whatever is inside AccountNumber column */
  282. dbpass = rows2.getString("Password"); /**set dbpass equal to whatever is inside Password column */
  283. dbbalance = rows2.getDouble("Balance"); /**set dbbalance equal to whatever is inside Balance column */
  284. dbtitle = rows2.getString("Title"); /**set dbtitle equal to whatever is inside Title column */
  285. dbfname = rows2.getString("FirstName"); /**set dbfname equal to whatever is inside FirstName column */
  286. dblname = rows2.getString("LastName"); /**set dblname equal to whatever is inside LastName column */
  287. }
  288. }catch (SQLException ex) { /** Catching sqlexception error*/
  289. System.out.println("SQLException: " + ex.getMessage());
  290. System.out.println("VendorError: " + ex.getErrorCode());
  291. } catch (ClassNotFoundException e) { /**Catching errors*/
  292. e.printStackTrace();
  293. } finally {
  294. try { /** starting try block*/
  295. rows2.close(); /**closing resultset*/
  296. stmt2.close(); /**closing statement*/
  297. conn2.close(); /**closing connection*/
  298. } catch (Exception e){} /**Catching errors*/
  299. }
  300. try{ /**Starting try block*/
  301.  
  302. if (dbaccno.equals(accnoinput)){ /** Checking if account number was found in database */
  303. if(dbpass.equals(passwordinput)){ /** checking if password matches */
  304. if(dbaccounttype.equals("Standard") || dbaccounttype.equals("Premium")){ /** if accounttype is equal to standard or premium then run menu for users */
  305.  
  306.  
  307. do{ /**Starting do while loop*/
  308. System.out.println("___________________________________________________________________________________________________________");
  309. System.out.println("Welcome " + dbtitle + " " + dbfname + " " + dblname + " | Your account number : " + dbaccno ); /**Display message*/
  310. System.out.println("1. Transfer funds"); /**Display message*/
  311. System.out.println("2. Check balance"); /**Display message*/
  312. System.out.println("3. View Statement"); /**Display message*/
  313. System.out.println("4. Manage loan"); /**Display message*/
  314. System.out.println("5. Upgrade account"); /**Display message*/
  315. System.out.println("6. Logout");/**Display message*/
  316.  
  317.  
  318. System.out.println("Please enter 1-6"); /**Display message*/
  319. menu = 0; /**Setting menu equal to 0*/
  320.  
  321. try { /**Starting try block*/
  322. menu = sc.nextInt(); /** menu equals to input from user*/
  323. } catch (InputMismatchException e) { /**Catching inputmissmatch exception*/
  324. sc.nextLine(); /**Clearing buffer*/
  325. }
  326.  
  327. if(menu ==0 || menu >6) { /**if menu is equal to 0 or greater than 6 then....*/
  328. System.out.println("Please use numbers 1 to 6 to navigate throught menu"); /**display message*/
  329. }
  330.  
  331.  
  332. switch(menu){ /**starting switch statement*/
  333.  
  334. case 1: /**if menu is equal to 1 then....*/
  335. String raccno= null, rsortcode=null, rtitle=null, rfname=null, rlname=null; double amount; /**create variables*/
  336. sc.nextLine(); /**clear buffer*/
  337. System.out.println("Please enter receivers account number"); /**display message*/
  338. raccno = sc.nextLine(); /**set value of raccno equal to input from user*/
  339.  
  340.  
  341. if(raccno.equals(dbaccno)) /** If customer accno is equal to own acc no and sortcode is equal to own sort code then*/
  342. {
  343. System.out.println("You cant send cash to yourself");/**displaying message*/
  344. }
  345. else if(!raccno.equals(dbaccno)){ /**if raccno is different than customers(logged in) then...*/
  346.  
  347. System.out.println("Please enter receivers sortcode"); /** display message*/
  348. rsortcode = sc.nextLine(); /**save input from user*/
  349.  
  350. System.out.println("Please enter amount you want to send"); /**display message*/
  351. amount = sc.nextDouble(); /**save input from user*/
  352.  
  353. if(amount > dbbalance) { /** if customer wants to send more cash than he have then...*/
  354. System.out.println("You want to send more cash than you have."); /**display message*/
  355. }
  356. else if(dbbalance <= 0){ /**checking uf customer has enough cash*/
  357. System.out.println("Your dont have enought cash."); /**display message*/
  358. }
  359. else if(amount <= dbbalance){ /** if customer has enough cash then get receiver details from database*/
  360. String s = "Select account.SortCode, account.Balance, customer.Title, customer.FirstName, customer.LastName FROM customer, account WHERE account.AccountNumber =" + raccno + " and account.SortCode = "+ rsortcode +" and customer.CustomerID = (select account.CustomerID where account.AccountNumber)";
  361. /**create new string called s and set its value to sqlcommand*/
  362.  
  363.  
  364. ArrayList <String> dbresult = new ArrayList<String>(readdatabase(dbpath,dbid,dbpassword,s));
  365. /** Creating array list called dbresult, calling a function readdatabase wich will return a arraylist with result of a query
  366. * , passing variables (dbpath,dbid,dbpassword,s(sql command), anything that is returned by function readdatabase is saved into dbresult arraylist*/
  367.  
  368. if(dbresult.get(0).equals("NotFound")){ /** if function readdatabase returns arraylist with index0 = NotFound then error*/
  369. System.out.println("Receiver doesnt exist. Make sure u enterd right acc no and sort code"); /** Displays message*/
  370. }
  371. else if(!dbresult.get(0).equals("NotFound")){ /** if function readdatabase returns arraylist with index0 = anything else than NotFound then account found*/
  372. System.out.printf("Transfer was succesful! £" + amount + " was removed from your account."); /**display message*/
  373. System.out.print("\n");
  374.  
  375.  
  376. /** make a note in statment table about transfer*/
  377.  
  378. double receiverbalance = Double.parseDouble(dbresult.get(1)); /**convert from string to double and set value of receiverbalance equal to it*/
  379.  
  380. double receiverbalancetotal = receiverbalance + amount; /** amount after transfer was added*/
  381.  
  382.  
  383. double senderbalancetotal = dbbalance - amount; /** senderbalancetotal = balance of sender with deductions*/
  384.  
  385. String sendingcashto = "Sent cash to : " +raccno + " " + dbresult.get(0); /**setting value of sendigcashto*/
  386. String receivingcashfrom = "Received from: " + dbaccno + " " +dbsortcode; /**setting value of receivingcashfrom*/
  387. Connection conn3 = null; /**create new conncetion*/
  388. Statement stmt3 = null; /**create new syatement*/
  389.  
  390. try /**statring try block*/
  391. {
  392. conn3 = DriverManager.getConnection(dbpath,dbid,dbpassword); /**connect to database and pass parameters, @param dbpath @param dbid @param dbpassword*/
  393. stmt3 = conn3.createStatement();
  394.  
  395. stmt3.execute("INSERT INTO statement (AccountID, Details,CashIn,CashOut,Balance) VALUES ((SELECT AccountID FROM account WHERE AccountNumber = "+ dbaccno +"),'"+ sendingcashto +"',0.00, "+amount+", "+senderbalancetotal+");"); /**sql statement*/
  396. stmt3.execute("INSERT INTO statement (AccountID, Details,CashIn,CashOut,Balance) VALUES ((SELECT AccountID FROM account WHERE AccountNumber = "+ raccno +"),'"+ receivingcashfrom +"',"+amount+",0.00, "+receiverbalancetotal+");"); /**sql statement*/
  397.  
  398. } catch (Exception d) { /**catching errors*/
  399. d.printStackTrace();
  400. }finally {
  401. try { /**starting try block*/
  402. stmt3.close(); /**closing statement*/
  403. conn3.close(); /**closing connection*/
  404. } catch (Exception e){} /**catching errors*/
  405. }
  406.  
  407. /** update balance after cash was moved*/
  408. double cal1 = Double.parseDouble(dbresult.get(1)); /**create new variable, convert string to double(dbresult at index 1) and set it equal to new variable*/
  409.  
  410. cal1 = cal1 + amount; /**do calulation add amount to balance*/
  411. dbbalance = dbbalance - amount; /**do calulation substract amount from balance*/
  412.  
  413. String sqlcommand3 = "UPDATE account SET Balance ='" + receiverbalancetotal + "' WHERE AccountNumber = " +raccno; /**sql command*/
  414. String sqlcommand4 = "UPDATE account SET Balance ='" + senderbalancetotal + "' WHERE AccountNumber = " +dbaccno; /**sql command*/
  415.  
  416. updatedatabase(dbpath,dbid,dbpassword,sqlcommand3); /**call function and pass parameters into it @param dbpath , @param dbit , @param dbpassword , @param sqlcommand3*/
  417. updatedatabase(dbpath,dbid,dbpassword,sqlcommand4); /**call function and pass parameters into it @param dbpath , @param dbit , @param dbpassword , @param sqlcommand4*/
  418.  
  419. dbresult.clear(); /**clear array*/
  420. }
  421. }
  422.  
  423. }
  424. break;
  425. case 2: /** Display balance */
  426. System.out.printf("Your balance is: £%.2f", dbbalance ); /**display message*/
  427. System.out.printf("\n");
  428. break;
  429. case 3: /** Display statemenet */
  430. Connection conn3 = null; /**creating new connection*/
  431. Statement stmt3 = null; /**creating new statement*/
  432. ResultSet rows3 = null; /**creating new result set*/
  433.  
  434. try {
  435. Class.forName("com.mysql.jdbc.Driver");
  436. conn3 = DriverManager.getConnection(dbpath,dbid,dbpassword); /**connect to database*/
  437. stmt3 = conn3.createStatement();
  438. String sqlcommand = "Select statement.Date, statement.Details, statement.CashIn,statement.CashOut, statement.Balance FROM account , statement WHERE statement.AccountID = (Select account.AccountID WHERE account.AccountNumber ="+ dbaccno + ")"; /**create sql command*/
  439. rows3 = stmt3.executeQuery(sqlcommand); /**send sql command to database*/
  440. System.out.println("___________________________________________________________________________________________________________"); /**display message*/
  441. System.out.println(" Date | Details | CashIn| CashOut| Balance"); /**display message*/
  442. while(rows3.next()){ /** start while loop*/
  443. String date, cashin, cashout, balance; /**call variables*/
  444.  
  445. System.out.println("___________________________________________________________________________________________________________"); /**display message*/
  446.  
  447. date = rows3.getString("Date"); /**set date equal to whatever is inside Date column*/
  448. date = date.substring(0, 19); /**date will only have 19 characters */
  449. System.out.print(date); /**display message*/
  450. System.out.print(" | ");/**display message*/
  451. System.out.print(rows3.getString("Details"));
  452. System.out.print(" | ");/**display message*/
  453. cashin = rows3.getString("CashIn"); /**set cashin equal to whatever is inside CashIn column*/
  454. cashin =SetFormat(cashin); /**call function SetFormat and pass cahin into it and set cahin equal to whatever is returned by this function*/
  455. System.out.print(cashin);/**display message*/
  456. System.out.print("| ");/**display message*/
  457. cashout = rows3.getString("CashOut"); /**set cashout equal to whatever is inside CashOut column*/
  458. cashout = SetFormat(cashout);/**call function SetFormat and pass cashout into it and set cashout equal to whatever is returned by this function*/
  459. System.out.print(cashout); /**display message*/
  460. System.out.print("| ");/**display message*/
  461. balance = rows3.getString("Balance");/**set balance equal to whatever is inside Balance column*/
  462. balance = SetFormat(balance); /**call function SetFormat and pass balance into it and set balance equal to whatever is returned by this function*/
  463. System.out.print(balance);/**display message*/
  464. System.out.print(" \n");
  465. }
  466. }
  467.  
  468. catch (SQLException ex) { /**catch errors*/
  469. System.out.println("SQLException: " + ex.getMessage()); /**display message*/
  470. System.out.println("VendorError: " + ex.getErrorCode());/**display message*/
  471. }
  472. catch (ClassNotFoundException e) { /**catch errors*/
  473. e.printStackTrace();
  474. }
  475.  
  476. break;
  477.  
  478.  
  479. case 4: /** Apply for a loan */
  480.  
  481. String sqlcommand = "Select AccountID, LoanAmount,Status from loan where AccountID =" + dbcustomerid; /**create new string variable*/
  482. ArrayList <String> dbresult = new ArrayList<String>(readdatabase(dbpath,dbid,dbpassword,sqlcommand));
  483. /** Creating array list called dbresult, calling a function readdatabase wich will return a arraylist with result of a query
  484. * , passing variables (dbpath,dbid,dbpassword,sqlcommand) anything that is returned by function readdatabase is saved into dbresult arraylist*/
  485.  
  486.  
  487.  
  488. if(dbresult.get(0).equals("NotFound")){ /** if user has no loan then allow him to apply */
  489.  
  490.  
  491. sc.nextLine(); /**clear buffer*/
  492. System.out.println("How much you want to loan?");/**display message*/
  493.  
  494. double loanamount = sc.nextDouble(); /**get input from user*/
  495.  
  496. Connection conn4 = null; /**create new connection*/
  497. Statement stmt4 = null; /**create new statement*/
  498.  
  499. try { /**start try block*/
  500.  
  501. conn4 = DriverManager.getConnection(dbpath,dbid,dbpassword); /**connect to database and pass parameters*/
  502. stmt4 = conn4.createStatement();
  503. stmt4.execute("INSERT INTO Loan (AccountID,LoanAmount,Status) VALUES ("+dbcustomerid+",'"+loanamount+"','Pending')"); /**run command*/
  504. System.out.println("You have applied for a loan"); /**display message*/
  505. } catch (Exception d) {/**catch errors*/
  506. d.printStackTrace();
  507. }finally {
  508. try {
  509. stmt4.close(); /**close statement*/
  510. conn4.close(); /**close connection*/
  511. } catch (Exception e){} /**catch errors*/
  512. }
  513. }
  514.  
  515. else if ((dbresult.get(0).equals(dbcustomerid)) & (dbresult.get(2).equals("Pending"))){ /** if user has already applied and state equals to pending */
  516. System.out.println("You already applied for loan, current status:"+ dbresult.get(2));/**display message*/
  517. }
  518.  
  519. else if(dbresult.get(2).equals("Accepted")){ /** if loan was accepted then...*/
  520. System.out.println("Your loan was accepted £" + dbresult.get(1)+"."); /** Display message */
  521. System.out.println("Would you like to payoff loan now? Yes/No"); /** Display message */
  522.  
  523. sc.nextLine(); /**clear buffer*/
  524.  
  525. String pay=null; /**create new string*/
  526. pay = sc.nextLine(); /**set pay eqal to input*/
  527.  
  528.  
  529. if(!Character.isUpperCase(pay.charAt(0))){ /**if pay index 0 is equal to lower letter then...*/
  530. pay = ToUpperCase(pay); /**set pay to upper case*/
  531. }
  532.  
  533. if(pay.equals("Yes")){ /**if pay is eqal to yes then...*/
  534. System.out.println("How much you want to pay?");/**display message*/
  535. double amount1;
  536.  
  537. amount1 = sc.nextDouble();
  538. double loan = Double.parseDouble(dbresult.get(1));
  539. if(amount1 > loan){
  540. System.out.println("You cant pay more than you borrowed!"); /**display message*/
  541. }
  542. else if(amount1 < loan){
  543.  
  544. loan = loan - amount1;
  545. dbbalance = dbbalance - amount1;
  546.  
  547.  
  548. String sqlcommand8= "UPDATE loan SET LoanAmount ="+loan+" where AccountID ="+dbcustomerid; /**sql command*/
  549. updatedatabase(dbpath,dbid,dbpassword,sqlcommand8); /**calling method updatedatabase and passing parameters into it*/
  550.  
  551. String sqlcommand1 = "UPDATE account SET Balance ='" + dbbalance + "' where account.AccountNumber = "+dbaccno+" and account.sortcode ="+dbsortcode;/**sql command*/
  552. updatedatabase(dbpath,dbid,dbpassword,sqlcommand1);/**calling method updatedatabase and passing parameters into it*/
  553.  
  554. // need to add note in statement
  555.  
  556.  
  557. /** Insert row into statement*/
  558. Connection conn11 = null; /**creating new connection*/
  559. Statement stmt11 = null;
  560.  
  561. try/**start try block*/
  562. {
  563. conn11 = DriverManager.getConnection(dbpath,dbid,dbpassword);
  564. stmt11 = conn11.createStatement();
  565.  
  566. stmt11.execute("INSERT INTO statement (AccountID, Details,CashIn,CashOut,Balance) VALUES ("+dbcustomerid+", ' Repay a loan ',0.00,"+amount1+","+dbbalance+");");
  567.  
  568. } catch (Exception d) {/**catch errors*/
  569. d.printStackTrace();
  570. }finally {
  571. try {
  572. stmt11.close();
  573. conn11.close();
  574. } catch (Exception e){} /**catch errors*/
  575. }
  576. /** Insert row into statement*/
  577.  
  578. }
  579. else if(amount1 <= loan){ /**if */
  580.  
  581.  
  582. /** Insert row into statement*/
  583. Connection conn11 = null;
  584. Statement stmt11 = null;
  585.  
  586. try/**start try block*/
  587. {
  588. conn11 = DriverManager.getConnection(dbpath,dbid,dbpassword);
  589. stmt11 = conn11.createStatement();
  590.  
  591. stmt11.execute("delete from loan where AccountID = "+dbcustomerid+";");
  592.  
  593. } catch (Exception d) {/**catch errors*/
  594. d.printStackTrace();
  595. }finally {
  596. try { /**start try block*/
  597. stmt11.close();
  598. conn11.close();
  599. } catch (Exception e){}/**catch errors*/
  600. }
  601. /** Insert row into statement*/
  602.  
  603.  
  604. dbbalance = dbbalance - amount1;
  605.  
  606.  
  607. String sqlcommand1 = "UPDATE account SET Balance = "+dbbalance+" where account.AccountNumber = "+dbaccno+" and account.sortcode ="+dbsortcode;
  608. updatedatabase(dbpath,dbid,dbpassword,sqlcommand1);// updating balance
  609.  
  610.  
  611. Connection conn12 = null;
  612. Statement stmt12 = null;
  613.  
  614. try/**start try block*/
  615. {
  616. conn12 = DriverManager.getConnection(dbpath,dbid,dbpassword);
  617. stmt12 = conn12.createStatement();
  618.  
  619. stmt12.execute("INSERT INTO statement (AccountID, Details,CashIn,CashOut,Balance) VALUES ("+dbcustomerid+", ' Repay a loan ',0.00,"+amount1+","+dbbalance+")");
  620.  
  621. } catch (Exception d) {/**catch errors*/
  622. d.printStackTrace();
  623. }finally {
  624. try { /**start try block*/
  625. stmt12.close();
  626. conn12.close();
  627. } catch (Exception e){}/**catch errors*/
  628. }
  629. /** Insert row into statement*/
  630.  
  631.  
  632. }
  633. else if(amount1 > dbbalance){
  634. System.out.println("You dont have enought cash");/**display message*/
  635. }
  636.  
  637. }
  638. }
  639. else if(dbresult.get(2).equals("Declined")){
  640. System.out.println("Your loan was Declined");/**display message*/
  641. }
  642.  
  643.  
  644.  
  645. break;
  646.  
  647. case 5: /** Upgrade to premium */
  648. sc.nextLine();
  649. if(dbaccounttype.equals("Premium")){
  650. System.out.println("You are already premium!");/**display message*/
  651. }
  652. else if(dbaccounttype.equals("Standard")){
  653. System.out.println("Cost of premium is £5.00. Would you like to upgrade? Yes/No");/**display message*/
  654. String upgrade=null;
  655. upgrade = sc.nextLine();
  656.  
  657.  
  658. if(!Character.isUpperCase(upgrade.charAt(0))){
  659. upgrade = ToUpperCase(upgrade);
  660. }
  661.  
  662.  
  663. if(upgrade.equals("Yes")){ /**if upgrade equals to yes then....*/
  664.  
  665. dbbalance = dbbalance - 5.00; /**do calculation*/
  666. String sqlcommand4 = "UPDATE account SET Balance ='" + dbbalance + "' where account.AccountNumber = "+dbaccno+" and account.sortcode ="+dbsortcode;
  667. String sqlcommand5 = "UPDATE account SET AccountType = 'Premium' where account.AccountNumber = "+dbaccno+" and account.sortcode ="+dbsortcode;
  668.  
  669. updatedatabase(dbpath,dbid,dbpassword,sqlcommand4);
  670. updatedatabase(dbpath,dbid,dbpassword,sqlcommand5);
  671.  
  672. System.out.println("£5.00 was removed from your account. Now you are premium member!");/**display message*/
  673. dbaccounttype = "Premium";
  674.  
  675.  
  676. Connection conn12 = null;
  677. Statement stmt12 = null;
  678.  
  679. try/**start try block*/
  680. {
  681. conn12 = DriverManager.getConnection(dbpath,dbid,dbpassword);
  682. stmt12 = conn12.createStatement();
  683.  
  684. stmt12.execute("INSERT INTO statement (AccountID, Details,CashIn,CashOut,Balance) VALUES ("+dbcustomerid+", ' Upgrade Account ',0.00,5.00,"+dbbalance+")");
  685.  
  686. } catch (Exception d) {/**catch errors*/
  687. d.printStackTrace();
  688. }finally {
  689. try { /**start try block*/
  690. stmt12.close();
  691. conn12.close();
  692. } catch (Exception e){} /**catch errors*/
  693. }
  694. /** Insert row into statement*/
  695.  
  696.  
  697.  
  698. }
  699. }
  700.  
  701. break;
  702.  
  703. }
  704. }while(menu != 6); /**quit loop when menu is eqal to 6*/
  705. }
  706.  
  707. else if(dbaccounttype.equals("Staff")){ /** if accounttype is equal to Staff then run menu for staff */
  708.  
  709. do{
  710. System.out.println("___________________________________________________________________________________________________________"); /**display message*/
  711. System.out.println("1. Add cash to user accounts(Deposit)"); /**display message*/
  712. System.out.println("2. Remove cash from user accounts(Withdraw)"); /**display message*/
  713. System.out.println("3. Accept or decline loan"); /**display message*/
  714. System.out.println("4. Logout"); /**display message*/
  715. System.out.println("Please enter 1-4");/**display message*/
  716. menu = 0;
  717.  
  718.  
  719. try { /**start try block*/
  720. menu = sc.nextInt();
  721.  
  722.  
  723. } catch (InputMismatchException e) {
  724. sc.nextLine();
  725. }
  726.  
  727.  
  728. if(menu ==0 || menu >4) { /** if menu is equal to 0 or equal to more than 3 then ... */
  729. System.out.println("Please use numbers 1 to 3 to navigate throught menu"); /** Displaying message */
  730. } /** Close if statement */
  731.  
  732.  
  733. switch(menu){
  734.  
  735. case 1: /** Adding cash to user account(Deposit) */
  736. sc.nextLine();
  737. String accnoo=null,sortcodee=null;
  738. System.out.println("Please enter account number");/**display message*/
  739. accnoo = sc.nextLine(); /**save input from user*/
  740. System.out.println("Please enter sort code");/**display message*/
  741. sortcodee = sc.nextLine();/**save input from user*/
  742.  
  743. String s = "Select account.Balance FROM account where account.AccountNumber = "+accnoo+" and account.sortcode ="+sortcodee;
  744.  
  745. ArrayList <String> dbresult = new ArrayList<String>(readdatabase(dbpath,dbid,dbpassword,s));
  746. /** Creating array list called dbresult, calling a function readdatabase wich will return a arraylist with result of a query
  747. * , passing variables (dbpath,dbid,dbpassword,s(sql command), anything that is returned by function readdatabase is saved into dbresult arraylist*/
  748.  
  749. if(!dbresult.get(0).equals("NotFound")){
  750. System.out.println("That customer does have £" + dbresult.get(0));/**display message*/
  751. System.out.println("How much you want to add?");/**display message*/
  752. double amount = sc.nextDouble();/**save input from user*/
  753.  
  754. double bal = Double.parseDouble(dbresult.get(0));
  755. bal = bal + amount;
  756. String sqlcommand2 = "UPDATE account SET Balance ='" + bal + "' where account.AccountNumber = "+accnoo+" and account.sortcode ="+sortcodee;
  757. updatedatabase(dbpath,dbid,dbpassword,sqlcommand2);
  758. System.out.println("You added £" + amount + " Successfully");/**display message*/
  759.  
  760. /** Insert row into statement*/
  761. Connection conn33 = null;
  762. Statement stmt33 = null;
  763.  
  764. try/**start try block*/
  765. {
  766. conn33 = DriverManager.getConnection(dbpath,dbid,dbpassword);
  767. stmt33 = conn33.createStatement();
  768.  
  769. stmt33.execute("INSERT INTO statement (AccountID, Details,CashIn,CashOut,Balance) VALUES ((Select AccountID from Account where AccountNumber = "+accnoo+"), ' Cash Deposited ',"+amount+",0.00,"+bal+")");
  770.  
  771. } catch (Exception d) {/**catch errors*/
  772. d.printStackTrace();
  773. }finally {
  774. try { /**start try block*/
  775. stmt33.close();
  776. conn33.close();
  777. } catch (Exception e){} /**catch errors*/
  778. }
  779. /** Insert row into statement*/
  780.  
  781. }
  782.  
  783. else
  784. {
  785. System.out.println("User cannot be found");/**display message*/
  786. }
  787.  
  788. break;
  789. case 2:/** Removing cash to user account(Withdraw) */
  790.  
  791. sc.nextLine();
  792. String accnoo1=null,sortcodee1=null;
  793. System.out.println("Please enter account number");/**display message*/
  794. accnoo1 = sc.nextLine();/**save input from user*/
  795. System.out.println("Please enter sort code");/**display message*/
  796. sortcodee1 = sc.nextLine();/**save input from user*/
  797.  
  798. String sqlcommand = "Select account.Balance FROM account where account.AccountNumber = "+accnoo1+" and account.sortcode ="+sortcodee1;
  799.  
  800. ArrayList <String> dbresult1 = new ArrayList<String>(readdatabase(dbpath,dbid,dbpassword,sqlcommand));
  801. /** Creating array list called dbresult, calling a function readdatabase wich will return a arraylist with result of a query
  802. * , passing variables (dbpath,dbid,dbpassword,s(sql command), anything that is returned by function readdatabase is saved into dbresult arraylist*/
  803.  
  804. if(!dbresult1.get(0).equals("NotFound")){
  805.  
  806. System.out.println("That customer does have £" + dbresult1.get(0));/**display message*/
  807. System.out.println("How much you want to remove?");/**display message*/
  808. double amount1 = sc.nextDouble();/**save input from user*/
  809.  
  810.  
  811.  
  812. double bal1 = Double.parseDouble(dbresult1.get(0));
  813. if(amount1 <= bal1){
  814.  
  815.  
  816. bal1 = bal1 - amount1;
  817.  
  818. String sqlcommand1 = "UPDATE account SET Balance ='" + bal1 + "' where account.AccountNumber = "+accnoo1+" and account.sortcode ="+sortcodee1;
  819. updatedatabase(dbpath,dbid,dbpassword,sqlcommand1);
  820. System.out.println("You removed £" + amount1 + " Successfully");/**display message*/
  821.  
  822.  
  823. /** Insert row into statement*/
  824. Connection conn22 = null;
  825. Statement stmt22 = null;
  826.  
  827. try/**start try block*/
  828. {
  829. conn22 = DriverManager.getConnection(dbpath,dbid,dbpassword);
  830. stmt22 = conn22.createStatement();
  831.  
  832. stmt22.execute("INSERT INTO statement (AccountID, Details,CashIn,CashOut,Balance) VALUES ((Select AccountID from Account where AccountNumber = "+accnoo1+"), ' Cash Withdrawn ',"+amount1+",0.00,"+bal1+")");
  833.  
  834. } catch (Exception d) {/**catch errors*/
  835. d.printStackTrace();
  836. }finally {
  837. try { /**start try block*/
  838. stmt22.close();
  839. conn22.close();
  840. } catch (Exception e){} /**catch errors*/
  841. }
  842. /** Insert row into statement*/
  843. }
  844. else{
  845. System.out.println("Cannot withdraw more then user has on account");/**display message*/
  846. }
  847. }else
  848. {
  849. System.out.println("User cannot be found");/**display message*/
  850. }
  851.  
  852. break;
  853. case 3:
  854.  
  855. /** Getting all the loans that are pending */
  856. String sqlcommand6 = "SELECT AccountID, LoanAmount, Status from loan where Status = 'Pending'";
  857. ArrayList <String> dbresult2 = new ArrayList<String>(readdatabase(dbpath,dbid,dbpassword,sqlcommand6));
  858.  
  859.  
  860. /** if no loan found then*/
  861. if(dbresult2.get(0).equals("NotFound")){
  862. System.out.println("No pedning loans");/**display message*/
  863. }
  864.  
  865.  
  866. else{
  867.  
  868. System.out.println("Id, amount, status");
  869. for(int i =0; i < dbresult2.size()-1; i++){
  870. System.out.print(dbresult2.get(i) + " ");
  871. if(dbresult2.get(i).equals("Pending")){
  872. System.out.println();/**display message*/
  873. }
  874.  
  875. }
  876. System.out.println("Please make sure that you pick a valid id");/**display message*/
  877. int select=0;
  878. boolean isint=false;
  879.  
  880. do{
  881. System.out.println("Select record");/**display message*/
  882.  
  883.  
  884. try { /**start try block*/
  885. select = sc.nextInt();
  886. isint = false;
  887.  
  888. } catch (InputMismatchException e) {
  889. sc.nextLine();
  890. isint=true;
  891. }
  892. }while(isint);
  893.  
  894.  
  895.  
  896. System.out.println("Accept or decline?"); /**display message*/
  897.  
  898. sc.nextLine();
  899. String accdec = sc.nextLine();/**save input from user*/
  900. if(!Character.isUpperCase(accdec.charAt(0))){ //
  901. accdec = ToUpperCase(accdec);
  902. }
  903.  
  904. if(accdec.equals("Accept")){// when accepted then
  905.  
  906. /** 1 update loan table
  907. * 2 update balance
  908. * 3 insert new row into statemenet
  909. * */
  910.  
  911.  
  912. /** Update loan table*/
  913. String sqlcommand7 = "UPDATE loan SET Status = 'Accepted' where AccountID =" + select;
  914. updatedatabase(dbpath,dbid,dbpassword,sqlcommand7);
  915. /** Update loan table*/
  916.  
  917. /** Update balance table*/
  918. String sqlcommand9 = "UPDATE account SET Balance = Balance+ (Select LoanAmount from loan where AccountID = "+select+") WHERE AccountID = "+select; /***/
  919. updatedatabase(dbpath,dbid,dbpassword,sqlcommand9);
  920. /** Update balance table*/
  921.  
  922.  
  923. /** Insert row into statement*/
  924. Connection conn3 = null;
  925. Statement stmt3 = null;
  926.  
  927. try/**start try block*/
  928. {
  929. conn3 = DriverManager.getConnection(dbpath,dbid,dbpassword);
  930. stmt3 = conn3.createStatement();
  931.  
  932. stmt3.execute("INSERT INTO statement (AccountID, Details,CashIn,CashOut,Balance) VALUES ("+select+", ' Loan Accepted ',(Select LoanAmount from loan where AccountID = "+select+"),0.00,(Select Balance from Account where AccountID = "+select+"))");
  933.  
  934. } catch (Exception d) {/**catch errors*/
  935. d.printStackTrace();
  936. }finally {
  937. try { /**start try block*/
  938. stmt3.close();
  939. conn3.close();
  940. } catch (Exception e){} /**catch errors*/
  941. }
  942. /** Insert row into statement*/
  943. System.out.println("Loan Accepted!");
  944. }
  945. else if(accdec.equals("Declined")){ /** If accdec equals to Declined then...*/
  946.  
  947. String sqlcommand7 = "UPDATE loan SET Status = 'Declined' where AccountID =" + select; /** set sqlcommand7 equal to ... */
  948. updatedatabase(dbpath,dbid,dbpassword,sqlcommand7); /** Call a updatedatabase function and pass parameters*/
  949. }
  950.  
  951. }
  952.  
  953. break;
  954. case 4:
  955. System.out.println("Logout");/**display message*/
  956. break;
  957. }
  958.  
  959. }while(menu != 4);
  960. }
  961.  
  962.  
  963. }
  964.  
  965. else{
  966.  
  967. System.out.println("Wrong password"); /**display message*/
  968. }
  969. }
  970. }catch(NullPointerException e){
  971. System.out.println("Account doexnt exist");/**display message*/
  972. }
  973. // enter login details check database new menu depends on user type (admin/standard)
  974. break;
  975.  
  976. case 3:
  977. System.out.println("Closing Application");/**display message*/
  978. break;
  979. }
  980. }while(menu != 3);
  981.  
  982. }
  983.  
  984. }
  985.  
  986. public static boolean isDbConnected() {
  987. String dbpath ="jdbc:mysql://localhost:3306/gradedunit2?autoReconnect=true&useSSL=false"; /** Creating a string called dbpath and setting its value to my database path. I will use it later to connect to my database */
  988. String dbid = "root"; /** Creating a string called dbit setting its value to root. I will use this variable each time i connect to database*/
  989. String dbpassword = "1234"; /** Creating a string called dbpassword setting its value to 1234. I will use this variable each time i connect to database*/
  990.  
  991. final String CHECK_SQL_QUERY = "SELECT 1";
  992. boolean isConnected = false;
  993. try {/**start try block*/
  994. Connection db = null;
  995. db = DriverManager.getConnection(dbpath,dbid,dbpassword);
  996. final PreparedStatement statement = db.prepareStatement(CHECK_SQL_QUERY);
  997. isConnected = true;
  998. } catch (SQLException | NullPointerException e) {/**catch errors*/
  999. // handle SQL error here!
  1000. }
  1001. return isConnected;
  1002. }
  1003.  
  1004.  
  1005. public static String SetFormat(String s){
  1006. /**im using this function to properly display statement*/
  1007.  
  1008. int length = s.length( );
  1009. while(length != 15){
  1010. s = " "+s;
  1011.  
  1012. length = s.length();
  1013. }
  1014. return s;
  1015. /**@return s*/
  1016. }
  1017.  
  1018. public static String ToUpperCase(String s){
  1019. /**it takes the string and sets the first letter of string to capital letter*/
  1020.  
  1021.  
  1022. String ss = s; /** Create new variable called s which is equal to title*/
  1023. ss = ss.substring(0,1).toUpperCase() + ss.substring(1).toLowerCase();/** setting first letter of s (index 0) to upper case and rest of them to lower*/
  1024. s = ss; /** set s equal to ss */
  1025.  
  1026. return s; /** @return s*/
  1027.  
  1028. }
  1029.  
  1030. public static void updatedatabase(String dbpath, String dbid,String dbpassword,String sqlcommand){
  1031. /**im using this function multiple times to update database*/
  1032. Connection conn5 = null;
  1033. Statement stmt5 = null;
  1034. try /**start try block*/
  1035. {
  1036. conn5 = DriverManager.getConnection(dbpath,dbid,dbpassword);
  1037. stmt5 = conn5.createStatement();
  1038. stmt5.execute(sqlcommand);
  1039. } catch (Exception d) {/**catch errors*/
  1040. d.printStackTrace();
  1041. }finally {
  1042. try { /**start try block*/
  1043. stmt5.close();
  1044. conn5.close();
  1045. } catch (Exception e){} /**catch errors*/
  1046. }
  1047.  
  1048. }
  1049.  
  1050. public static ArrayList readdatabase(String dbpath, String dbid,String dbpassword,String sqlcommand){
  1051. /** Function that will return a arraylist with results from query if query returns null then at the first index there will be a text "NotFound"
  1052. */
  1053. Connection conn = null;
  1054. Statement stmt = null;
  1055. ResultSet res = null;
  1056.  
  1057. ArrayList <String> dbresult = new ArrayList<String>();
  1058. ArrayList <String> colname = new ArrayList<String>();
  1059.  
  1060. try {/**start try block*/
  1061.  
  1062. Class.forName("com.mysql.jdbc.Driver");
  1063. conn = DriverManager.getConnection(dbpath,dbid,dbpassword);
  1064. stmt = conn.createStatement();
  1065. res = stmt.executeQuery(sqlcommand);
  1066.  
  1067. ResultSetMetaData rsmd = res.getMetaData();
  1068. int noofcolumns = rsmd.getColumnCount();
  1069.  
  1070.  
  1071. for(int i = 1 ; i < noofcolumns+1; i++){ // loop getting column names and saving them to arraylist(unknown number or columns)
  1072. String s = rsmd.getColumnName(i);
  1073. colname.add(s);
  1074. }
  1075.  
  1076. while(res.next()){/** if rows2(return from sql statement) is NOT empty then loop */
  1077. for(int i = 0 ; i < noofcolumns; i++){ // loop saving results to arraylist
  1078. dbresult.add(res.getString(colname.get(i)));
  1079. }
  1080. // dbresult.add(Integer.toString(noofcolumns));
  1081. }
  1082. if(!res.next()){
  1083. dbresult.add("NotFound");
  1084. }
  1085.  
  1086.  
  1087.  
  1088. }catch (SQLException ex) {
  1089. System.out.println("SQLException: " + ex.getMessage());
  1090. System.out.println("VendorError: " + ex.getErrorCode());
  1091. } catch (ClassNotFoundException e) { /**catch errors*/
  1092. e.printStackTrace();
  1093. } finally {
  1094. try { /**start try block*/
  1095. res.close();
  1096. stmt.close();
  1097. conn.close();
  1098. } catch (Exception e){}/**catch errors*/
  1099. }
  1100.  
  1101.  
  1102. return dbresult;
  1103. /**@return dbresult*/
  1104. }
  1105.  
  1106. // later : make sure that all reads from database are made throught above class with returns result set!
  1107.  
  1108. }
  1109.  
  1110.  
  1111.  
  1112. ////////////////////////////////////////////////////////////////////////////////////
  1113. ////////////////////////////////////////////////////////////////////////////////////
  1114. ////////////////////////////////////////////////////////////////////////////////////
  1115. ////////////////////////////////////////////////////////////////////////////////////
  1116.  
  1117.  
  1118.  
  1119.  
  1120. /** Radoslaw Burkacki
  1121. * 29/05/2016
  1122. * Account class
  1123. * This class is a blue print for Account objects */
  1124.  
  1125.  
  1126.  
  1127. public class Account {
  1128.  
  1129. private String Password; /** Creating new variable of String type called Password */
  1130. private int AccountNumber; /** Creating new variable of int type called AccountNumber */
  1131. private int SortCode; /** Creating new variable of int type called SortCode */
  1132. private double Balance; /** Creating new variable of double type called Balance */
  1133. private String AccountType; /** Creating new variable of String type called AccountType */
  1134.  
  1135. /** All of above variables are private due to encapsulation which I am using */
  1136.  
  1137.  
  1138. public Account(String password, int accountnumber, int sortcode, String accounttype) {
  1139. /** Constructor that is expecting variables to be passed when object created
  1140. * @param password
  1141. * @param accountnumber
  1142. * @param sortcode
  1143. * @param accounttype
  1144. * */
  1145.  
  1146. this.Password = password; /**set variable password equal to password(was passed when called object) */
  1147. this.AccountNumber = accountnumber; /**set variable AccountNumber equal to accountnumber(was passed when called object) */
  1148. this.SortCode = sortcode; /**set variable SortCode equal to sortcode(was passed when called object) */
  1149. this.Balance = 0; /**set variable Balance equal 0(it will be overwritten by premium or standard)*/
  1150. this.AccountType = accounttype; /**set variable AccountType equal to accounttype(was passed when called object) */
  1151. }
  1152.  
  1153.  
  1154.  
  1155.  
  1156. /**
  1157. * Setters
  1158. *
  1159. * @param password
  1160. */
  1161. public void setPassword(String password){
  1162. Password = password;
  1163. }
  1164.  
  1165.  
  1166. /**
  1167. * @param accountnumber
  1168. */
  1169. public void setAccountNumber(int accountnumber){
  1170. AccountNumber = accountnumber;
  1171. }
  1172.  
  1173.  
  1174. /**
  1175. * @param sortcode
  1176. */
  1177. public void setSortCode(int sortcode){
  1178. SortCode = sortcode;
  1179. }
  1180.  
  1181.  
  1182. /**
  1183. * @param balance
  1184. */
  1185. public void setBalance(double balance){
  1186. Balance = balance;
  1187. }
  1188.  
  1189.  
  1190. /**
  1191. * @param accounttype
  1192. */
  1193. public void setAccountType(String accounttype){
  1194. AccountType = accounttype;
  1195. }
  1196.  
  1197.  
  1198.  
  1199.  
  1200. /**
  1201. * Getters
  1202. *
  1203. * @return Password
  1204. */
  1205. String getPassword(){
  1206. return Password;
  1207. }
  1208.  
  1209. /**@return AccountNumber */
  1210. int getAccountNumber(){
  1211. return AccountNumber;
  1212. }
  1213.  
  1214.  
  1215. /**@return SortCode */
  1216. int getSortCode(){
  1217. return SortCode;
  1218. }
  1219.  
  1220. /**@return Balance */
  1221. double getBalance(){
  1222. return Balance;
  1223. }
  1224.  
  1225. /**@return AccountType */
  1226. String getAccountType(){
  1227. return AccountType;
  1228. }
  1229. }
  1230.  
  1231. ////////////////////////////////////////////////////////////////////////////////////
  1232. ////////////////////////////////////////////////////////////////////////////////////
  1233. ////////////////////////////////////////////////////////////////////////////////////
  1234. ////////////////////////////////////////////////////////////////////////////////////
  1235.  
  1236. /** Radoslaw Burkacki
  1237. * 29/05/2016
  1238. * Account class
  1239. * This class is a blue print for Customers objects */
  1240.  
  1241.  
  1242. public class Customer {
  1243.  
  1244. private String Title; /** Creating new variable of String type called Title */
  1245. private String FName; /** Creating new variable of String type called FName */
  1246. private String LName; /** Creating new variable of String type called LName */
  1247. private String City; /** Creating new variable of String type called City */
  1248. private String PostCode; /** Creating new variable of String type called PostCode */
  1249. private String Address; /** Creating new variable of String type called Address */
  1250. /** All of above variables are private due to encapsulation which I am using */
  1251.  
  1252.  
  1253.  
  1254. public Customer(String title, String fname, String lname, String city, String postcode, String address) { /** Constructor that is expecting variables to be passed when object created
  1255. *@param title
  1256. *@param fname
  1257. *@param lname
  1258. *@param city
  1259. *@param postcode
  1260. *@param address
  1261. */
  1262.  
  1263. this.Title = title; /**set variable Title equal to title(was passed when called object) */
  1264. this.FName = fname; /**set variable FName equal to fname(was passed when called object) */
  1265. this.LName = lname; /**set variable LName equal to lname(was passed when called object) */
  1266. this.City = city; /**set variable City equal to city(was passed when called object) */
  1267. this.PostCode = postcode; /**set variable PostCode equal to postcode(was passed when called object) */
  1268. this.Address = address; /**set variable Address equal to address(was passed when called object) */
  1269. }
  1270.  
  1271.  
  1272.  
  1273. /**
  1274. * Setters
  1275. * @param title String that was passed when object was created(title)
  1276. */
  1277. public void setTitle(String title){
  1278. Title = title;
  1279. }
  1280.  
  1281.  
  1282. /**
  1283. * @param fname String that was passed when object was created(first name)
  1284. */
  1285. public void setFName(String fname){
  1286. FName = fname;
  1287. }
  1288.  
  1289.  
  1290. /**
  1291. * @param lname String that was passed when object was created(last name)
  1292. */
  1293. public void setLName(String lname){
  1294. LName = lname;
  1295. }
  1296.  
  1297.  
  1298. /**
  1299. * @param city String that was passed when object was created(city)
  1300. */
  1301. public void setCity(String city){
  1302. City = city;
  1303. }
  1304.  
  1305.  
  1306. /**
  1307. * @param postcode String that was passed when object was created(postcode)
  1308. */
  1309. public void setPostCode(String postcode){
  1310. PostCode = postcode;
  1311. }
  1312.  
  1313. /**
  1314. * @param address String that was passed when object was created(address)
  1315. */
  1316. public void setAddress(String address){
  1317. Address = address;
  1318. }
  1319.  
  1320.  
  1321. /**
  1322. * Getters
  1323. * @return Title
  1324. */
  1325. public String getTitle(){
  1326. return Title;
  1327. }
  1328.  
  1329.  
  1330. /**
  1331. * @return FName
  1332. */
  1333. public String getFName(){
  1334. return FName;
  1335. }
  1336.  
  1337.  
  1338. /**
  1339. * @return LName
  1340. */
  1341. public String getLName(){
  1342. return LName;
  1343. }
  1344.  
  1345.  
  1346. /**
  1347. * @return City
  1348. */
  1349. public String getCity(){
  1350. return City;
  1351. }
  1352.  
  1353.  
  1354. /**
  1355. * @return PostCode
  1356. */
  1357. public String getPostCode(){
  1358. return PostCode;
  1359. }
  1360.  
  1361.  
  1362. /**
  1363. * @return Address
  1364. */
  1365. public String getAddress(){
  1366. return Address;
  1367. }
  1368.  
  1369. }
  1370.  
  1371.  
  1372. ////////////////////////////////////////////////////////////////////////////////////
  1373. ////////////////////////////////////////////////////////////////////////////////////
  1374. ////////////////////////////////////////////////////////////////////////////////////
  1375. ////////////////////////////////////////////////////////////////////////////////////
  1376.  
  1377. /** Radoslaw Burkacki
  1378. * 29/05/2016
  1379. * Sub class of Account
  1380. * This class is used to set balance to -5.00 */
  1381.  
  1382. public class Premium extends Account {
  1383.  
  1384. public Premium(String password, int accountnumber, int sortcode, String accounttype) {
  1385. super(password, accountnumber, sortcode, accounttype);
  1386. super.setBalance(-5.00);
  1387. }
  1388.  
  1389.  
  1390.  
  1391.  
  1392.  
  1393.  
  1394. }
  1395.  
  1396.  
  1397. ////////////////////////////////////////////////////////////////////////////////////
  1398. ////////////////////////////////////////////////////////////////////////////////////
  1399. ////////////////////////////////////////////////////////////////////////////////////
  1400. ////////////////////////////////////////////////////////////////////////////////////
  1401.  
  1402. /** Radoslaw Burkacki
  1403. * 29/05/2016
  1404. * Sub class of Account
  1405. * This class is used to set balance to 0.00 */
  1406.  
  1407. public class Standard extends Account{
  1408.  
  1409. public Standard(String password, int accountnumber, int sortcode, String accounttype) {
  1410. super(password, accountnumber, sortcode, accounttype);
  1411. super.setBalance(0.00);
  1412. }
  1413.  
  1414. }
Add Comment
Please, Sign In to add comment