1public class Transactions { 2 3 // hata: eger fonksiyon exception verirse sorun olabilir 4 private static double stringToDouble(String text) { 5 // handle in 5 usages that if catch statement works, return an escape integer 6 double val = 0; 7 try { 8 9 val = Double.parseDouble(text); 10 11 } catch (Exception e) { 12 e.printStackTrace(); 13 } 14 return val; 15 } 16 17 public static boolean withdraw(String id, double amount) { 18 double deposit; 19 if (amount < 10 || amount > 1000) { 20 System.out.println("cekilecek tutar 10'dan kucuk olamaz"); 21 return false; 22 } 23 24 String temp = SqlQuery.StringGetSQL("SELECT deposit FROM clients WHERE id=" + id, "deposit"); 25 26 try { 27 deposit = Double.parseDouble(temp); 28 System.out.println(deposit); 29 } catch (Exception e) { 30 e.printStackTrace(); 31 return false; 32 } 33 34 if (deposit < amount) { 35 System.out.println("cekilmek istenen tutar bakiyeden fazla.. "); 36 return false; 37 } else if (deposit > amount) { 38 deposit = deposit - amount; 39 temp = String.valueOf(deposit); 40 41 SqlQuery.UpdateData("UPDATE clients SET deposit=" + temp + "WHERE id=" + id); 42 43 return true; 44 } 45 return false; 46 } 47 48 public static boolean deposit(String id, double amount) { 49 double deposit; 50 51 String temp = SqlQuery.StringGetSQL("SELECT deposit FROM clients WHERE id=" + id, "deposit"); 52 if (amount < 1) { 53 System.out.println("yatiralacak tutar sifirdan kucuk olamaz"); 54 return false; 55 } 56 57 deposit = stringToDouble(temp); 58 59 deposit = deposit + amount; 60 temp = String.valueOf(deposit); 61 SqlQuery.UpdateData("UPDATE clients SET deposit=" + temp + "WHERE id=" + id); 62 63 return true; 64 } 65 66 public static boolean transfer(String id, String IBAN, double amount) { 67 // todo: iban uzunlugunun kontrol edilmesi gerekli 68 if (IBAN.length() != 24) return false; 69 70 double depositSender, depositReceiver; 71 String temp = SqlQuery.StringGetSQL("SELECT deposit FROM clients WHERE id=" + id, "deposit"); 72 depositSender = stringToDouble(temp); 73 depositSender = depositSender - amount; 74 if (amount < 1) { 75 System.out.println("Gondereceginiz tutar 0'dan buyuk olmalıdır. "); 76 return false; 77 } 78 if (depositSender < amount) { 79 System.out.println("Bakiye yetersiz. "); 80 return false; 81 } 82 temp = 83 SqlQuery.StringGetSQL( 84 "SELECT deposit FROM clients WHERE IBAN LIKE '%" + IBAN + "'", "deposit"); 85 depositReceiver = stringToDouble(temp); 86 87 depositReceiver = depositReceiver + amount; 88 89 temp = String.valueOf(depositReceiver); 90 SqlQuery.UpdateData("UPDATE clients SET deposit=" + temp + " WHERE IBAN LIKE '%" + IBAN + "'"); 91 92 temp = String.valueOf(depositSender); 93 SqlQuery.UpdateData("UPDATE clients SET deposit=" + temp + "WHERE id=" + id); 94 return true; 95 } 96 97 public static boolean payOffDebt(String id, double amount) { 98 double deposit, debt; 99 String temp = SqlQuery.StringGetSQL("SELECT debt FROM clients WHERE id=" + id, "debt");100 debt = stringToDouble(temp);101 temp = SqlQuery.StringGetSQL("SELECT deposit FROM clients WHERE id=" + id, "deposit");102 deposit = stringToDouble(temp);103 if (amount > deposit) {104 System.out.println("Odemek istediginiz tutar bakiyenizden fazla olamaz. ");105 return false;106 } else if (amount > debt) {107108 deposit = deposit - debt;109 debt = 0;110 temp = String.valueOf(deposit);111 SqlQuery.UpdateData("UPDATE clients SET deposit=" + temp + "WHERE id=" + id);112 SqlQuery.UpdateData("UPDATE clients SET debt=0 WHERE id=" + id);113 System.out.println(114 "girdiginiz tutar borcunuzdan fazladir, borcunuz: " + debt + " TL ödenmistir. ");115 return true;116117 } else {118 deposit = deposit - amount;119 // todo: handle if temp is not a string120 temp = String.valueOf(deposit);121 SqlQuery.UpdateData("UPDATE clients SET deposit=" + temp + "WHERE id=" + id);122 debt = debt - amount;123 temp = String.valueOf(debt);124 SqlQuery.UpdateData("UPDATE clients SET debt=" + temp + "WHERE id=" + id);125 return true;126 }127 }128}
3import java.sql.ResultSet;
4import java.sql.Statement;
5
6public class SqlQuery { 7 public static ResultSet getResult(String query) { 8 9 try {10 Class.forName("com.mysql.cj.jdbc.Driver");11 Connection con =12 DriverManager.getConnection("jdbc:mysql://localhost:3306/atm", "root", "root");13 Statement stmt = con.createStatement();14 return stmt.executeQuery(query);1516 } catch (Exception e) {17 return null;18 }19 }2021 // todo: fonksiyon bool veri döndürmeli22 public static void UpdateData(String query) {2324 try {25 Class.forName("com.mysql.cj.jdbc.Driver");26 Connection con =27 DriverManager.getConnection("jdbc:mysql://localhost:3306/atm", "root", "root");28 Statement stmt = con.createStatement();29 stmt.executeUpdate(query);3031 } catch (Exception e) {32 e.printStackTrace();33 }34 }3536 public static String StringGetSQL(String query, String label) {37 String temp = null;38 try {39 Class.forName("com.mysql.cj.jdbc.Driver");40 Connection con =41 DriverManager.getConnection("jdbc:mysql://localhost:3306/atm", "root", "root");42 Statement stmt = con.createStatement();43 ResultSet rs = stmt.executeQuery(query);44 while (rs.next()) {45 temp = rs.getString(label);46 }4748 } catch (Exception e) {49 e.printStackTrace();50 }51 return temp;52 }53}
This class does not have any documentation.
Consider adding a documentation comment to explain its use.
While it may seem like the functionality of a class is perfectly obvious, any consumers of your API may not be able to pick up on certain details.
Bad Practice
Consider a case where the class given below can be instantiated and provides certain functionalities within each instance in a thread-safe manner, perhaps it is a rest API client.
If there is no documentation comment on the class, it is not immediately obvious that the class is thread safe. Thus, multiple instances of the class may be created to perform operations concurrently, using up both memory as well as OS resources like sockets. If it were known from the beginning that the class were thread safe, the user would not need to create unnecessary extra instances of SomeClass
.
class SomeClass {
// ...
}
Recommended
Make sure to add useful information regarding the usage or implementation of a particular declaration, so that anything about it which can't be understood from the name or some other cue is correctly conveyed.
/**
* Instances of this class are used to perform xyz action.
*
* This class is thread safe and the same instance can be used over multiple threads.
*/
class SomeClass {
// ...
}
Exceptions
This issue will not be reported for model entity classes. If there is any non-obvious behavior associated with a particular class however, do consider documenting it.