1public class Transactions { 2 3 // hata: eger fonksiyon exception verirse sorun olabilir 4 private static double stringToDouble(String text) { 5 double val = 0; 6 try { 7 8 val = Double.parseDouble(text); 9 10 } catch (Exception e) { 11 e.printStackTrace(); 12 } 13 return val; 14 } 15 16 // todo: para çek 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 // todo: güncelleme işleminin doğru olup olmadığı kontrol edilebilri 43 return true; 44 } 45 return false; 46 } 47 48 // todo: para yatır 49 public static boolean deposit(String id, double amount) { 50 double deposit; 51 52 String temp = SqlQuery.StringGetSQL("SELECT deposit FROM clients WHERE id=" + id, "deposit"); 53 if (amount < 1) { 54 System.out.println("yatiralacak tutar sifirdan kucuk olamaz"); 55 return false; 56 } 57 58 deposit = stringToDouble(temp); 59 60 deposit = deposit + amount; 61 temp = String.valueOf(deposit); 62 SqlQuery.UpdateData("UPDATE clients SET deposit=" + temp + "WHERE id=" + id); 63 // todo: güncelleme işleminin doğru olup olmadığı kontrol edilebilri 64 return true; 65 } 66 67 // todo: para gönder 68 public static boolean transfer(String id, String IBAN, double amount) { 69 // todo: kullanici kendi ibanina transfer para 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 // todo: borç öde 98 public static boolean payOffDebt(String id, double amount) { 99 double deposit, debt;100 String temp = SqlQuery.StringGetSQL("SELECT debt FROM clients WHERE id=" + id, "debt");101 debt = stringToDouble(temp);102 temp = SqlQuery.StringGetSQL("SELECT deposit FROM clients WHERE id=" + id, "deposit");103 deposit = stringToDouble(temp);104 if (amount > deposit) {105 System.out.println("Odemek istediginiz tutar bakiyenizden fazla olamaz. ");106 return false;107 } else if (amount > debt) {108109 deposit = deposit - debt;110 debt = 0;111 temp = String.valueOf(deposit);112 SqlQuery.UpdateData("UPDATE clients SET deposit=" + temp + "WHERE id=" + id);113 SqlQuery.UpdateData("UPDATE clients SET debt=0 WHERE id=" + id);114 System.out.println(115 "girdiginiz tutar borcunuzdan fazladir, borcunuz: " + debt + " TL ödenmistir. ");116 return true;117118 } else {119 deposit = deposit - amount;120 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 }128129 // todo: pin değiştir130 // todo: ...131}
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 public static void UpdateData(String query) {2223 try {24 Class.forName("com.mysql.cj.jdbc.Driver");25 Connection con =26 DriverManager.getConnection("jdbc:mysql://localhost:3306/atm", "root", "root");27 Statement stmt = con.createStatement();28 stmt.executeUpdate(query);2930 } catch (Exception e) {31 e.printStackTrace();32 }33 }3435 public static String StringGetSQL(String query, String label) {36 String temp = null;37 try {38 Class.forName("com.mysql.cj.jdbc.Driver");39 Connection con =40 DriverManager.getConnection("jdbc:mysql://localhost:3306/atm", "root", "root");41 Statement stmt = con.createStatement();42 ResultSet rs = stmt.executeQuery(query);43 while (rs.next()) {44 temp = rs.getString(label);45 }4647 } catch (Exception e) {48 e.printStackTrace();49 }50 return temp;51 }52}
1import java.sql.*;
2import java.util.Scanner;
3
4public class Main { 5 public static void main(String[] args) throws Exception { 6 Scanner sc = new Scanner(System.in); 7 do { 8 System.out.println("Lutfen Kart Numaranizi giriniz... "); 9 String CardNum = sc.next();10 System.out.println("Lutfen pininizi giriniz... ");11 String Pin = sc.next();1213 Card card = new Login(CardNum, Pin);14 } while (!Login.login());1516 String currentID = Card.returnID();17 int checker = -1;1819 do {20 // terminal her seferinde temizlenmeli21 System.out.println(22 "Kullanibilir bakiyeniz: "23 + SqlQuery.StringGetSQL(24 "SELECT deposit FROM clients WHERE id=" + currentID, "deposit"));25 System.out.println("Lutfen yapmak istediginiz islemi seciniz. ");26 System.out.println("1: Para Cek 2: Para Yatir");27 System.out.println("3: Para Gonder 4: Borc Ode");28 System.out.println(" 0:Cikis Yap ");29 checker = sc.nextInt();30 Scanner scd = new Scanner(System.in);31 double amount;32 switch (checker) {33 case 0:34 System.out.println("Yine Bekleriz");35 break;36 case 1:37 System.out.println(38 "lutfen cekmek istediginiz tutari giriniz, bu minimum 10 en fazla 1000'dir");39 amount = scd.nextDouble();40 if (Transactions.withdraw(currentID, amount)) {41 System.out.println("para cekme basarili");42 } else {43 System.out.println("para cekme islemi basarisiz");44 }45 break;46 case 2:47 System.out.println("Lutfen yatirmak istediginiz tutari giriniz. ");48 amount = scd.nextDouble();49 if (Transactions.deposit(currentID, amount)) {50 System.out.println("para yatirma islemi basarili.");51 } else {52 System.out.println("para yatirma islemi basarisiz.");53 }54 break;55 case 3:56 System.out.println("Para gondermek istediginiz IBANI giriniz.. ");57 System.out.printf("TR ");58 String IBAN = sc.next();59 System.out.println("Gondermek istediginiz miktari giriniz.. ");60 amount = scd.nextDouble();61 if (Transactions.transfer(currentID, IBAN, amount)) {62 System.out.println("para gonderildi");63 } else System.out.println("para gonderilemedi.");64 break;65 case 4:66 System.out.printf("Borcunuz: ");67 System.out.printf(68 SqlQuery.StringGetSQL("SELECT debt FROM clients WHERE id=" + currentID, "debt"));69 System.out.printf(" lutfen odemek istediginiz tutari giriniz.. ");70 amount = scd.nextDouble();71 if (Transactions.payOffDebt(currentID, amount))72 System.out.println("borcunuz basariyla odendi");73 else System.out.println("borc odenemedi");74 break;75 default:76 System.out.println("Eksik ya da hatali bir tuslama yaptiniz lutfen tekrar deneyiniz..");77 }7879 } while (checker != 0);80 }81}
1import java.sql.ResultSet;
2import java.sql.SQLException;
3
4public class Login extends Card { 5 6 public Login(String cardNum, String pin) { 7 super(cardNum, pin); 8 } 910 private static String cardsPin;11 private static String cardsNum;12 private static int attempts = 0;1314 private static boolean isCardValid(String cardNum) throws SQLException {15 ResultSet checkRs = SqlQuery.getResult(("SELECT Name FROM clients WHERE CardNum=" + cardNum));1617 if (checkRs == null) {18 System.out.println("hatali bir kart numarasi girdiniz lutfen tekrar deneyiniz.. ");19 return false;20 }2122 return true;23 }2425 private static boolean isPinTrue(String cardsPin, String cardPin) {2627 if (attempts > 3) {28 // todo: her seferinde yeni obje olusturuldugundan bunun yapılması o kadar da kolay degil29 // attemps degişkeni daima sıfır oluyor30 SqlQuery.UpdateData("UPDATE clients SET PIN='BLOCKED' WHERE CardNum=" + cardNum);3132 return false;3334 } else if (cardPin.length() == 4) {35 try {36 int x = Integer.parseInt(cardPin);37 ResultSet rs = SqlQuery.getResult(("SELECT PIN FROM clients WHERE CardNum=" + cardNum));3839 // burada varolan uyarı anlamsız, not null veri döndürüyor40 while (rs.next()) cardsPin = rs.getString("PIN");4142 if (pin.equals(cardsPin)) {43 System.out.println("Sifre Dogru");44 return true;45 } else {46 System.out.println("sifreniz hatali, lutfen tekrar deneyiniz..");47 }4849 } catch (Exception e) {50 attempts++;51 System.out.println("Pin rakamlardan olusmalidir. ");52 return false;53 }54 } else {55 attempts++;56 System.out.println("Lutfen Dort Haneli Pininizi Giriniz: ");57 return false;58 }59 return false;60 }6162 public static boolean login() throws SQLException {63 if (isCardValid(cardNum) && isPinTrue(cardNum, pin)) {64 System.out.println("Hosgeldiniz. Basariyla giris yaptiniz..");65 // todo: sql tabanindan kullanicinin adi cekilip ekrana basilmali66 return true;67 }68 return false;69 }70}
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.