package bank; import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.Properties; import java.util.StringTokenizer ; public class DBUtils { private static File bankFl = null ; private static File tempFl = null ; public static void init() { try { bankFl = new File("./bankaccounts") ; if(! bankFl.exists()) { bankFl.createNewFile() ; } tempFl = new File("./temp") ; if(!tempFl.exists()) { tempFl.createNewFile() ; } }catch(Exception e) { e.printStackTrace() ; } } synchronized public static void cleanUp() { } synchronized public static List findAccounts(AccountInformation accInfo) { System.out.println("findAccounts") ; try { String nextAccountStr = null ; ArrayList accountNumbers = new ArrayList(); BufferedReader br = new BufferedReader(new FileReader(bankFl)) ; while( (nextAccountStr = br.readLine()) != null) { StringTokenizer st = new StringTokenizer(nextAccountStr,",") ; String accNumTok = st.nextToken() ; String accTypeTok = st.nextToken() ; String balanceTok = st.nextToken() ; if(accInfo.accType.value() != AccountType._NIL) { if(accInfo.accType.value() == AccountType._Checking) { if(accTypeTok.equals("C")) { accountNumbers.add(accNumTok) ; } } else if(accInfo.accType.value() == AccountType._Saving) { if(accTypeTok.equals("S")) { accountNumbers.add(accNumTok) ; } } } else { accountNumbers.add(accNumTok) ; } } br.close() ; return accountNumbers ; }catch(Exception e) {e.printStackTrace() ;} return null ; } synchronized public static float getBalance(String accNum) { float bal = 0.0f; BufferedReader br = null ; try { String nextAccountStr = null ; br = new BufferedReader(new FileReader(bankFl)) ; while( (nextAccountStr = br.readLine()) != null) { StringTokenizer st = new StringTokenizer(nextAccountStr,",") ; String accNumTok = st.nextToken() ; String accTypeTok = st.nextToken() ; String balanceTok = st.nextToken() ; if(accNumTok.equals(accNum)) { bal = Float.parseFloat(balanceTok) ; } } br.close() ; }catch(Exception e) { e.printStackTrace() ; return -1.0f;} return bal; } synchronized public static void setBalance(String accNum, float balance) { try { BufferedWriter bw = new BufferedWriter(new FileWriter(tempFl)) ; BufferedReader br = new BufferedReader(new FileReader(bankFl)) ; String nextAccountStr = null ; while( (nextAccountStr = br.readLine()) != null) { StringTokenizer st = new StringTokenizer(nextAccountStr,",") ; String accNumTok = st.nextToken() ; String accTypeTok = st.nextToken() ; String balanceTok = st.nextToken() ; if(accNumTok.equals(accNum)) { balanceTok = Float.toString(balance) ; bw.write(accNumTok + "," + accTypeTok + "," + balanceTok); bw.newLine() ; bw.flush() ; } else { bw.write(nextAccountStr) ; bw.newLine() ; bw.flush() ; } } br.close() ; bw.close() ; BufferedWriter bw2 = new BufferedWriter(new FileWriter(bankFl)) ; BufferedReader br2 = new BufferedReader(new FileReader(tempFl)) ; nextAccountStr = null ; while( (nextAccountStr = br2.readLine()) != null) { bw2.write(nextAccountStr) ; bw2.newLine() ; bw2.flush() ; } br2.close() ; bw2.close() ; }catch(Exception e) {e.printStackTrace() ;} } synchronized public static AccountType getAccountType(String accNum) { AccountType accType = new AccountType(AccountType._NIL) ; try { String nextAccountStr = null ; BufferedReader br = new BufferedReader(new FileReader(bankFl)) ; while( (nextAccountStr = br.readLine()) != null) { StringTokenizer st = new StringTokenizer(nextAccountStr,",") ; String accNumTok = st.nextToken() ; String accTypeTok = st.nextToken() ; String balanceTok = st.nextToken() ; if(accNumTok.equals(accNum)) { if(accTypeTok.equals("C")) { accType = new AccountType(AccountType._Checking); } else if(accTypeTok.equals("S")) { accType = new AccountType(AccountType._Saving); } } br.close() ; } }catch(Exception e) {e.printStackTrace() ;} return accType; } synchronized public static void removeAccount(String accNum) { try { BufferedWriter bw = new BufferedWriter(new FileWriter(tempFl)) ; BufferedReader br = new BufferedReader(new FileReader(bankFl)) ; String nextAccountStr = null ; while( (nextAccountStr = br.readLine()) != null) { StringTokenizer st = new StringTokenizer(nextAccountStr,",") ; String accNumTok = st.nextToken() ; String accTypeTok = st.nextToken() ; String balanceTok = st.nextToken() ; if(accNumTok.equals(accNum)) { } else { bw.write(nextAccountStr) ; bw.newLine() ; bw.flush() ; } } br.close() ; bw.close() ; BufferedWriter bw2 = new BufferedWriter(new FileWriter(bankFl)) ; BufferedReader br2 = new BufferedReader(new FileReader(tempFl)) ; nextAccountStr = null ; while( (nextAccountStr = br2.readLine()) != null) { bw2.write(nextAccountStr) ; bw2.newLine() ; bw2.flush() ; } br2.close() ; bw2.close() ; }catch(Exception e) {e.printStackTrace() ;} //removeAccountStmt.setString(1,accNum); //removeAccountStmt.execute(); } synchronized public static void createAccount(AccountInformation accInfo) { try { BufferedWriter bw = new BufferedWriter(new FileWriter(bankFl, true)) ; accInfo.accNum = createAccountNumber() ; String accNumTok = accInfo.accNum ; String accTypeTok = null ; String accBalanceTok = "0.0" ; if(accInfo.accType.value() == AccountType._Checking) { accTypeTok = "C" ; } else if(accInfo.accType.value() == AccountType._Saving) { accTypeTok = "S" ; } else if(accInfo.accType.value() == AccountType._NIL) { accTypeTok = "NIL" ; } String accountStr = accNumTok + "," + accTypeTok + "," + accBalanceTok ; bw.write(accountStr) ; bw.newLine() ; bw.flush() ; bw.close() ; }catch(Exception e) {e.printStackTrace() ;} } private static String createAccountNumber() { double n = Math.random(); String s = new Double(n).toString(); return s.substring(2,17); } }