-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankAccount.java
More file actions
99 lines (84 loc) · 3.28 KB
/
BankAccount.java
File metadata and controls
99 lines (84 loc) · 3.28 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
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 🏦 Banka Hesap Yönetimi
* Kavramlar: OOP, Encapsulation, ArrayList, Exception Handling
*/
// İşlem kaydı
class Transaction {
private String type;
private double amount;
private double balance;
private Date date;
public Transaction(String type, double amount, double balance) {
this.type = type;
this.amount = amount;
this.balance = balance;
this.date = new Date();
}
@Override
public String toString() {
return String.format("[%s] %s: %.2f TL | Bakiye: %.2f TL", date, type, amount, balance);
}
}
// Banka hesabı
class BankAccount {
private String owner;
private String accountNumber;
private double balance;
private List<Transaction> transactions;
public BankAccount(String owner, String accountNumber, double initialBalance) {
this.owner = owner;
this.accountNumber = accountNumber;
this.balance = initialBalance;
this.transactions = new ArrayList<>();
transactions.add(new Transaction("AÇILIŞ", initialBalance, balance));
}
public void deposit(double amount) {
if (amount <= 0) throw new IllegalArgumentException("Yatırma miktarı 0'dan büyük olmalıdır!");
balance += amount;
transactions.add(new Transaction("YATIRIM", amount, balance));
System.out.printf("✅ %.2f TL yatırıldı. Yeni bakiye: %.2f TL%n", amount, balance);
}
public void withdraw(double amount) {
if (amount <= 0) throw new IllegalArgumentException("Çekme miktarı 0'dan büyük olmalıdır!");
if (amount > balance) throw new IllegalStateException("Yetersiz bakiye!");
balance -= amount;
transactions.add(new Transaction("ÇEKİM", amount, balance));
System.out.printf("✅ %.2f TL çekildi. Yeni bakiye: %.2f TL%n", amount, balance);
}
public void transfer(BankAccount target, double amount) {
this.withdraw(amount);
target.deposit(amount);
System.out.printf("✅ %s → %s transferi tamamlandı.%n", this.owner, target.owner);
}
public void printStatement() {
System.out.println("\n========== HESAP EKSTRESİ ==========");
System.out.printf("Hesap Sahibi : %s%n", owner);
System.out.printf("Hesap No : %s%n", accountNumber);
System.out.printf("Güncel Bakiye: %.2f TL%n", balance);
System.out.println("--- İşlem Geçmişi ---");
transactions.forEach(System.out::println);
System.out.println("=====================================\n");
}
public double getBalance() { return balance; }
public String getOwner() { return owner; }
}
public class BankAccount {
public static void main(String[] args) {
BankAccount bengu = new BankAccount("Bengü Gedik", "TR001", 1000.0);
BankAccount ahmet = new BankAccount("Ahmet Yılmaz", "TR002", 500.0);
bengu.deposit(2000);
bengu.withdraw(500);
bengu.transfer(ahmet, 300);
bengu.printStatement();
ahmet.printStatement();
// Exception handling testi
try {
bengu.withdraw(99999);
} catch (IllegalStateException e) {
System.out.println("❌ Hata: " + e.getMessage());
}
}
}