1
2
3public class Transactions { 4 5 //hata: eger fonksiyon exception verirse sorun olabilir 6 private static double stringToDouble(String text) { 7 //handle in 5 usages that if catch statement works, return an escape integer 8 double val = 0; 9 try { 10 11 val = Double.parseDouble(text); 12 13 } catch (Exception e) { 14 e.printStackTrace(); 15 16 } 17 return val; 18 } 19 20 21 22 public static boolean withdraw(String id, double amount) { 23 double deposit; 24 if (amount < 10 || amount > 1000) { 25 System.out.println("cekilecek tutar 10'dan kucuk olamaz"); 26 return false; 27 } 28 29 String temp = SqlQuery.StringGetSQL("SELECT deposit FROM clients WHERE id=" + id, "deposit"); 30 31 32 try { 33 deposit = Double.parseDouble(temp); 34 System.out.println(deposit); 35 } catch (Exception e) { 36 e.printStackTrace(); 37 return false; 38 } 39 40 if (deposit < amount) { 41 System.out.println("cekilmek istenen tutar bakiyeden fazla.. "); 42 return false; 43 } else if (deposit > amount) { 44 deposit = deposit - amount; 45 temp = String.valueOf(deposit); 46 47 SqlQuery.UpdateData("UPDATE clients SET deposit=" + temp + "WHERE id=" + id); 48 49 return true; 50 } 51 return false; 52 } 53 54 55 public static boolean deposit(String id, double amount) { 56 double deposit; 57 58 String temp = SqlQuery.StringGetSQL("SELECT deposit FROM clients WHERE id=" + id, "deposit"); 59 if (amount < 1) { 60 System.out.println("yatiralacak tutar sifirdan kucuk olamaz"); 61 return false; 62 } 63 64 deposit = stringToDouble(temp); 65 66 deposit = deposit + amount; 67 temp = String.valueOf(deposit); 68 SqlQuery.UpdateData("UPDATE clients SET deposit=" + temp + "WHERE id=" + id); 69 70 return true; 71 72 } 73 74 75 76 public static boolean transfer(String id, String IBAN, double amount) { 77 //todo: iban uzunlugunun kontrol edilmesi gerekli 78 if(IBAN.length()!=24) return false; 79 80 double depositSender, depositReceiver; 81 String temp = SqlQuery.StringGetSQL("SELECT deposit FROM clients WHERE id=" + id, "deposit"); 82 depositSender = stringToDouble(temp); 83 depositSender = depositSender - amount; 84 if (amount < 1) { 85 System.out.println("Gondereceginiz tutar 0'dan buyuk olmalıdır. "); 86 return false; 87 } 88 if (depositSender < amount) { 89 System.out.println("Bakiye yetersiz. "); 90 return false; 91 } 92 temp = SqlQuery.StringGetSQL("SELECT deposit FROM clients WHERE IBAN LIKE '%" + IBAN + "'", "deposit"); 93 depositReceiver = stringToDouble(temp); 94 95 depositReceiver = depositReceiver + amount; 96 97 temp = String.valueOf(depositReceiver); 98 SqlQuery.UpdateData("UPDATE clients SET deposit=" + temp + " WHERE IBAN LIKE '%" + IBAN + "'"); 99100 temp = String.valueOf(depositSender);101 SqlQuery.UpdateData("UPDATE clients SET deposit=" + temp + "WHERE id=" + id);102 return true;103104 }105106107 public static boolean payOffDebt(String id, double amount) {108 double deposit, debt;109 String temp = SqlQuery.StringGetSQL("SELECT debt FROM clients WHERE id=" + id, "debt");110 debt = stringToDouble(temp);111 temp = SqlQuery.StringGetSQL("SELECT deposit FROM clients WHERE id=" + id, "deposit");112 deposit = stringToDouble(temp);113 if (amount > deposit) {114 System.out.println("Odemek istediginiz tutar bakiyenizden fazla olamaz. ");115 return false;116 } else if (amount > debt) {117118 deposit = deposit - debt;119 debt = 0;120 temp = String.valueOf(deposit);121 SqlQuery.UpdateData("UPDATE clients SET deposit=" + temp + "WHERE id=" + id);122 SqlQuery.UpdateData("UPDATE clients SET debt=0 WHERE id=" + id);123 System.out.println("girdiginiz tutar borcunuzdan fazladir, borcunuz: " + debt + " TL ödenmistir. ");124 return true;125126 } else {127 deposit = deposit - amount;128 //todo: handle if temp is not a string129 temp = String.valueOf(deposit);130 SqlQuery.UpdateData("UPDATE clients SET deposit=" + temp + "WHERE id=" + id);131 debt = debt - amount;132 temp = String.valueOf(debt);133 SqlQuery.UpdateData("UPDATE clients SET debt=" + temp + "WHERE id=" + id);134 return true;135 }136 }137138139}
5import java.sql.ResultSet;
6import java.sql.Statement;
7
8public class SqlQuery { 9 public static ResultSet getResult(String query) {1011 try {12 Class.forName("com.mysql.cj.jdbc.Driver");13 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/atm", "root", "root");14 Statement stmt = con.createStatement();15 return stmt.executeQuery(query);1617 } catch (Exception e) {18 return null;19 }2021 }2223 //todo: fonksiyon bool veri döndürmeli24 public static void UpdateData(String query) {2526 try {27 Class.forName("com.mysql.cj.jdbc.Driver");28 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/atm", "root", "root");29 Statement stmt = con.createStatement();30 stmt.executeUpdate(query);313233 } catch (Exception e) {34 e.printStackTrace();35 }3637 }3839 public static String StringGetSQL(String query, String label) {40 String temp = null;41 try {42 Class.forName("com.mysql.cj.jdbc.Driver");43 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/atm", "root", "root");44 Statement stmt = con.createStatement();45 ResultSet rs = stmt.executeQuery(query);46 while (rs.next()) {47 temp = rs.getString(label);48 }4950 } catch (Exception e) {51 e.printStackTrace();52 }53 return temp;54 }5556}
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.