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: iban uzunlugunun kontrol edilmesi gerekli 70 // todo: kullanici kendi ibanina transfer para 71 double depositSender, depositReceiver; 72 String temp = SqlQuery.StringGetSQL("SELECT deposit FROM clients WHERE id=" + id, "deposit"); 73 depositSender = stringToDouble(temp); 74 depositSender = depositSender - amount; 75 if (amount < 1) { 76 System.out.println("Gondereceginiz tutar 0'dan buyuk olmalıdır. "); 77 return false; 78 } 79 if (depositSender < amount) { 80 System.out.println("Bakiye yetersiz. "); 81 return false; 82 } 83 temp = 84 SqlQuery.StringGetSQL( 85 "SELECT deposit FROM clients WHERE IBAN LIKE '%" + IBAN + "'", "deposit"); 86 depositReceiver = stringToDouble(temp); 87 88 depositReceiver = depositReceiver + amount; 89 90 temp = String.valueOf(depositReceiver); 91 SqlQuery.UpdateData("UPDATE clients SET deposit=" + temp + " WHERE IBAN LIKE '%" + IBAN + "'"); 92 93 temp = String.valueOf(depositSender); 94 SqlQuery.UpdateData("UPDATE clients SET deposit=" + temp + "WHERE id=" + id); 95 return true; 96 } 97 98 // todo: borç öde 99 public static boolean payOffDebt(String id, double amount) {100 double deposit, debt;101 String temp = SqlQuery.StringGetSQL("SELECT debt FROM clients WHERE id=" + id, "debt");102 debt = stringToDouble(temp);103 temp = SqlQuery.StringGetSQL("SELECT deposit FROM clients WHERE id=" + id, "deposit");104 deposit = stringToDouble(temp);105 if (amount > deposit) {106 System.out.println("Odemek istediginiz tutar bakiyenizden fazla olamaz. ");107 return false;108 } else if (amount > debt) {109110 deposit = deposit - debt;111 debt = 0;112 temp = String.valueOf(deposit);113 SqlQuery.UpdateData("UPDATE clients SET deposit=" + temp + "WHERE id=" + id);114 SqlQuery.UpdateData("UPDATE clients SET debt=0 WHERE id=" + id);115 System.out.println(116 "girdiginiz tutar borcunuzdan fazladir, borcunuz: " + debt + " TL ödenmistir. ");117 return true;118119 } else {120 deposit = deposit - amount;121 temp = String.valueOf(deposit);122 SqlQuery.UpdateData("UPDATE clients SET deposit=" + temp + "WHERE id=" + id);123 debt = debt - amount;124 temp = String.valueOf(debt);125 SqlQuery.UpdateData("UPDATE clients SET debt=" + temp + "WHERE id=" + id);126 return true;127 }128 }129130 // todo: pin değiştir131 // todo: ...132}
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 String CardNum; 8 do { 9 System.out.println("Lutfen Kart Numaranizi giriniz... ");10 CardNum = sc.next();11 Card card = new Card(CardNum);1213 } while (!Login.isCardValid());14 int attemps = -1;15 do {1617 System.out.println("Hosgeldiniz! lutfen bir pin saglayin");18 if (attemps > 3) {19 SqlQuery.UpdateData("UPDATE clients SET PIN='BLOCKED' WHERE CardNum=" + CardNum);20 System.out.println("Sifreniz bloke edilmistir lutfen musteri hizmetleri ile gorusun.. ");21 System.exit(2);22 } else {2324 System.out.println("Lutfen pininizi giriniz... ");25 String Pin = sc.next();26 Card.setPin(Pin);27 attemps++;28 }2930 } while (!Login.isPinTrue());3132 String currentID = Card.returnID();33 int checker = -1;3435 do {36 // terminal her seferinde temizlenmeli37 System.out.println(38 "Kullanibilir bakiyeniz: "39 + SqlQuery.StringGetSQL(40 "SELECT deposit FROM clients WHERE id=" + currentID, "deposit"));41 System.out.println("Lutfen yapmak istediginiz islemi seciniz. ");42 System.out.println("1: Para Cek 2: Para Yatir");43 System.out.println("3: Para Gonder 4: Borc Ode");44 System.out.println(" 0:Cikis Yap ");45 checker = sc.nextInt();46 Scanner scd = new Scanner(System.in);47 double amount;48 switch (checker) {49 case 0:50 System.out.println("Yine Bekleriz");51 break;52 case 1:53 System.out.println(54 "lutfen cekmek istediginiz tutari giriniz, bu minimum 10 en fazla 1000'dir");55 amount = scd.nextDouble();56 if (Transactions.withdraw(currentID, amount)) {57 System.out.println("para cekme basarili");58 } else {59 System.out.println("para cekme islemi basarisiz");60 }61 break;62 case 2:63 System.out.println("Lutfen yatirmak istediginiz tutari giriniz. ");64 amount = scd.nextDouble();65 if (Transactions.deposit(currentID, amount)) {66 System.out.println("para yatirma islemi basarili.");67 } else {68 System.out.println("para yatirma islemi basarisiz.");69 }70 break;71 case 3:72 System.out.println("Para gondermek istediginiz IBANI giriniz.. ");73 System.out.printf("TR ");74 String IBAN = sc.next();75 System.out.println("Gondermek istediginiz miktari giriniz.. ");76 amount = scd.nextDouble();77 if (Transactions.transfer(currentID, IBAN, amount)) {78 System.out.println("para gonderildi");79 } else System.out.println("para gonderilemedi.");80 break;81 case 4:82 System.out.printf("Borcunuz: ");83 System.out.printf(84 SqlQuery.StringGetSQL("SELECT debt FROM clients WHERE id=" + currentID, "debt"));85 System.out.printf(" lutfen odemek istediginiz tutari giriniz.. ");86 amount = scd.nextDouble();87 if (Transactions.payOffDebt(currentID, amount))88 System.out.println("borcunuz basariyla odendi");89 else System.out.println("borc odenemedi");90 break;91 default:92 System.out.println("Eksik ya da hatali bir tuslama yaptiniz lutfen tekrar deneyiniz..");93 }9495 } while (checker != 0);96 }97}
1import java.sql.ResultSet;
2import java.sql.SQLException;
3
4public class Login extends Card { 5 6 public Login(String cardNum) { 7 super(cardNum); 8 } 910 public static boolean isCardValid() throws SQLException {11 ResultSet checkRs = SqlQuery.getResult(("SELECT id FROM clients WHERE CardNum=" + cardNum));1213 while (checkRs.next()) {14 return true;15 }16 System.out.println("hatali bir kart numarasi girdiniz lutfen tekrar deneyiniz.. ");17 return false;18 }1920 public static boolean isPinTrue() {2122 String cardsPin =23 SqlQuery.StringGetSQL("SELECT PIN FROM clients WHERE CardNum=" + cardNum, "PIN");2425 if (cardsPin.equals("BLOCKED")) {26 System.out.println("Pininiz bloke");27 System.exit(2);28 }2930 if (!(pin.length() == 4)) {31 System.out.println("Pininiz Dort Haneden Olusmalidir");32 return false;33 }34 try {35 int x = Integer.parseInt(pin);36 } catch (Exception e) {3738 System.out.println("Pin rakamlardan olusmalidir. ");39 return false;40 }41 if (!(pin.equals(cardsPin))) {42 System.out.println("sifreniz hatalidir. ");43 return false;44 }45 return true;46 }47}
1import java.sql.ResultSet;
2import java.sql.SQLException;
3
4public class Card { 5 6 protected static String cardNum; 7 8 protected static String pin; 910 public Card(String cardNum) {11 this.cardNum = cardNum;12 this.pin = "0000";13 }1415 public static void setPin(String pin) {16 Card.pin = pin;17 }1819 public static String getCardNum() {20 return cardNum;21 }2223 public static String getPin() {24 return pin;25 }2627 public static String returnID() throws SQLException {28 // todo:sqlden id çekilecek ve ana programa pushlanacak29 ResultSet rs = SqlQuery.getResult(("SELECT id FROM clients WHERE CardNum=" + cardNum));30 while (rs.next()) {31 return rs.getString("id");32 }33 return null;34 }35}
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.