1import java.io.*;
2import java.util.ArrayList;
3import java.util.List;
4
5abstract class Account implements Serializable {
6 @Serial
7 private static final long serialVersionUID = 101L;
8
9 protected String accountNumber;
10 protected String holderName;
11 protected double balance;
12
13 public Account(String accNum, String name, double bal) {
14 this.accountNumber = accNum;
15 this.holderName = name;
16 this.balance = bal;
17 }
18
19 public void deposit(double amount) {
20 if (amount > 0) balance += amount;
21 }
22
23 public abstract boolean withdraw(double amount);
24
25 public void printSummary() {
26 System.out.printf("Acc: %s | Holder: %-15s | Balance: ₹%,.2f%n", accountNumber, holderName, balance);
27 }
28}
29
30class SavingsAccount extends Account {
31 public SavingsAccount(String accNum, String name, double bal) {
32 super(accNum, name, bal);
33 }
34
35 @Override
36 public boolean withdraw(double amount) {
37 if (amount > 0 && (balance - amount) >= 1000.0) {
38 balance -= amount;
39 return true;
40 }
41 return false;
42 }
43}
44
45public class BankSystem {
46 public static void main(String[] args) throws Exception {
47 String dbFile = "bank_data.ser";
48 List<Account> accounts = new ArrayList<>();
49
50 accounts.add(new SavingsAccount("SB-101", "Aarav Sharma", 15000.0));
51 accounts.add(new SavingsAccount("SB-102", "Priya Verma", 35000.0));
52
53 // Serialize
54 try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(dbFile))) {
55 oos.writeObject(accounts);
56 System.out.println("Saved " + accounts.size() + " accounts to disk.");
57 }
58
59 // Deserialize
60 try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(dbFile))) {
61 @SuppressWarnings("unchecked")
62 List<Account> loaded = (List<Account>) ois.readObject();
63 System.out.println("\n--- Loaded Accounts from Disk ---");
64 loaded.forEach(Account::printSummary);
65 }
66 }
67}