Загрузка данных


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class BankApp extends JFrame {

    public BankApp() {
        setTitle("Банк");
        setSize(400, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new GridLayout(3, 1));

        JButton accountsBtn = new JButton("Счета");
        JButton cardsBtn = new JButton("Карты");
        JButton creditsBtn = new JButton("Кредиты");

        add(accountsBtn);
        add(cardsBtn);
        add(creditsBtn);

        accountsBtn.addActionListener(e -> new CategoryWindow(
                "Счета",
                new String[]{
                        "Дебетовый счет - Удобный счет для покупок",
                        "Сберегательный счет - 5% годовых",
                        "Валютный счет - Хранение долларов и евро"
                }
        ));

        cardsBtn.addActionListener(e -> new CategoryWindow(
                "Карты",
                new String[]{
                        "Зарплатная карта - Бесплатное обслуживание",
                        "Виртуальная карта - Для онлайн покупок",
                        "Премиальная карта - Кэшбэк и бонусы"
                }
        ));

        creditsBtn.addActionListener(e -> new CategoryWindow(
                "Кредиты",
                new String[]{
                        "Ипотека - 12% на 20 лет",
                        "Автокредит - 10% на 5 лет",
                        "Потребительский кредит - 15% на 3 года"
                }
        ));

        setVisible(true);
    }

    public static void main(String[] args) {
        new BankApp();
    }
}

class CategoryWindow extends JFrame {

    public CategoryWindow(String title, String[] items) {
        setTitle(title);
        setSize(500, 400);
        setLayout(new GridLayout(items.length, 1));

        for (String item : items) {

            JPanel panel = new JPanel();
            panel.setLayout(new BorderLayout());

            JLabel label = new JLabel(item);

            JButton favBtn = new JButton("Избранное");

            favBtn.addActionListener(e -> saveFavorite(item));

            panel.add(label, BorderLayout.CENTER);
            panel.add(favBtn, BorderLayout.EAST);

            add(panel);
        }

        setVisible(true);
    }

    private void saveFavorite(String item) {
        try {

            File file = new File("favorites.txt");

            int count = 0;

            if (file.exists()) {
                BufferedReader reader = new BufferedReader(new FileReader(file));
                String line = reader.readLine();

                if (line != null) {
                    count = Integer.parseInt(line);
                }

                reader.close();
            }

            count++;

            BufferedWriter writer = new BufferedWriter(new FileWriter(file));
            writer.write(String.valueOf(count));
            writer.close();

            JOptionPane.showMessageDialog(this,
                    item + "\nДобавлено в избранное!\nВсего: " + count);

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this,
                    "Ошибка записи файла");
        }
    }
}