-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathATMSystem.java
More file actions
206 lines (177 loc) · 7.8 KB
/
ATMSystem.java
File metadata and controls
206 lines (177 loc) · 7.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
/**
* 🏧 ATM Sistemi
* Kavramlar: OOP, File I/O, HashMap, Exception Handling, Enum
*/
enum TransactionType {
DEPOSIT, WITHDRAW, TRANSFER, BALANCE_CHECK
}
class ATMTransaction {
private TransactionType type;
private double amount;
private double balanceAfter;
private LocalDateTime dateTime;
public ATMTransaction(TransactionType type, double amount, double balanceAfter) {
this.type = type;
this.amount = amount;
this.balanceAfter = balanceAfter;
this.dateTime = LocalDateTime.now();
}
@Override
public String toString() {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
return String.format("%s | %-15s | Miktar: %8.2f TL | Bakiye: %8.2f TL",
dateTime.format(fmt), type, amount, balanceAfter);
}
}
class ATMAccount {
private String cardNumber;
private String pin;
private String ownerName;
private double balance;
private boolean isLocked;
private int failedAttempts;
private List<ATMTransaction> transactions;
private static final int MAX_ATTEMPTS = 3;
public ATMAccount(String cardNumber, String pin, String ownerName, double balance) {
this.cardNumber = cardNumber;
this.pin = pin;
this.ownerName = ownerName;
this.balance = balance;
this.isLocked = false;
this.failedAttempts = 0;
this.transactions = new ArrayList<>();
}
public boolean validatePin(String enteredPin) {
if (isLocked) throw new IllegalStateException("❌ Kart bloke edilmiştir! Bankanızla iletişime geçin.");
if (this.pin.equals(enteredPin)) {
failedAttempts = 0;
return true;
} else {
failedAttempts++;
if (failedAttempts >= MAX_ATTEMPTS) {
isLocked = true;
throw new IllegalStateException("❌ 3 hatalı giriş! Kartınız bloke edildi.");
}
throw new IllegalArgumentException("❌ Hatalı PIN! " + (MAX_ATTEMPTS - failedAttempts) + " hakkınız kaldı.");
}
}
public void deposit(double amount) {
if (amount <= 0) throw new IllegalArgumentException("❌ Geçersiz miktar!");
if (amount > 10000) throw new IllegalArgumentException("❌ Tek seferde max 10.000 TL yatırabilirsiniz!");
balance += amount;
transactions.add(new ATMTransaction(TransactionType.DEPOSIT, amount, balance));
System.out.printf("✅ %.2f TL yatırıldı. Güncel bakiye: %.2f TL%n", amount, balance);
}
public void withdraw(double amount) {
if (amount <= 0) throw new IllegalArgumentException("❌ Geçersiz miktar!");
if (amount > 5000) throw new IllegalArgumentException("❌ Günlük çekim limiti 5.000 TL!");
if (amount > balance) throw new IllegalStateException("❌ Yetersiz bakiye!");
balance -= amount;
transactions.add(new ATMTransaction(TransactionType.WITHDRAW, amount, balance));
System.out.printf("✅ %.2f TL çekildi. Güncel bakiye: %.2f TL%n", amount, balance);
}
public void transfer(ATMAccount target, double amount) {
this.withdraw(amount);
target.balance += amount;
target.transactions.add(new ATMTransaction(TransactionType.DEPOSIT, amount, target.balance));
System.out.printf("✅ %s hesabına %.2f TL transfer edildi.%n", target.ownerName, amount);
}
public void printMiniStatement() {
System.out.println("\n======= MİNİ EKSTRE =======");
System.out.println("Kart: **** **** **** " + cardNumber.substring(12));
System.out.println("Sahip: " + ownerName);
System.out.printf("Bakiye: %.2f TL%n", balance);
System.out.println("--- Son 5 İşlem ---");
int start = Math.max(0, transactions.size() - 5);
transactions.subList(start, transactions.size()).forEach(System.out::println);
System.out.println("===========================\n");
}
public void saveTransactionsToFile() throws IOException {
String filename = cardNumber.substring(12) + "_transactions.txt";
try (BufferedWriter w = new BufferedWriter(new FileWriter(filename, true))) {
for (ATMTransaction t : transactions) {
w.write(t.toString());
w.newLine();
}
}
System.out.println("✅ İşlemler " + filename + " dosyasına kaydedildi.");
}
public String getCardNumber() { return cardNumber; }
public String getOwnerName() { return ownerName; }
public double getBalance() { return balance; }
public boolean isLocked() { return isLocked; }
}
class ATM {
private Map<String, ATMAccount> accounts = new HashMap<>();
private ATMAccount currentSession = null;
public void addAccount(ATMAccount account) {
accounts.put(account.getCardNumber(), account);
}
public boolean insertCard(String cardNumber) {
ATMAccount account = accounts.get(cardNumber);
if (account == null) { System.out.println("❌ Kart bulunamadı!"); return false; }
if (account.isLocked()) { System.out.println("❌ Bu kart bloke edilmiştir!"); return false; }
currentSession = account;
System.out.println("✅ Kart takıldı. Hoş geldiniz, " + account.getOwnerName() + "!");
return true;
}
public boolean enterPin(String pin) {
if (currentSession == null) { System.out.println("❌ Önce kart takın!"); return false; }
try {
return currentSession.validatePin(pin);
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
public void performDeposit(double amount) {
try { currentSession.deposit(amount); }
catch (Exception e) { System.out.println(e.getMessage()); }
}
public void performWithdraw(double amount) {
try { currentSession.withdraw(amount); }
catch (Exception e) { System.out.println(e.getMessage()); }
}
public void performTransfer(String targetCard, double amount) {
ATMAccount target = accounts.get(targetCard);
if (target == null) { System.out.println("❌ Hedef hesap bulunamadı!"); return; }
try { currentSession.transfer(target, amount); }
catch (Exception e) { System.out.println(e.getMessage()); }
}
public void ejectCard() {
if (currentSession != null) {
try { currentSession.saveTransactionsToFile(); }
catch (IOException e) { System.out.println("Log hatası: " + e.getMessage()); }
System.out.println("👋 Kartınızı alınız. Güle güle, " + currentSession.getOwnerName() + "!");
currentSession = null;
}
}
public ATMAccount getCurrentSession() { return currentSession; }
}
public class ATMSystem {
public static void main(String[] args) {
ATM atm = new ATM();
atm.addAccount(new ATMAccount("1234567890123456", "1234", "Bengü Gedik", 5000.0));
atm.addAccount(new ATMAccount("9876543210987654", "5678", "Ahmet Yılmaz", 2000.0));
System.out.println("=== ATM SİMÜLASYONU ===\n");
// Senaryo 1: Normal işlem
atm.insertCard("1234567890123456");
atm.enterPin("0000"); // hatalı
atm.enterPin("1234"); // doğru
atm.performDeposit(1000);
atm.performWithdraw(500);
atm.performTransfer("9876543210987654", 200);
atm.getCurrentSession().printMiniStatement();
atm.ejectCard();
System.out.println("\n--- Senaryo 2: Kart Bloke ---");
atm.insertCard("9876543210987654");
atm.enterPin("0000");
atm.enterPin("1111");
atm.enterPin("2222"); // 3. hatalı → bloke
atm.insertCard("9876543210987654"); // bloke kontrolü
}
}