package me.teridax.jcash.gui.account; import me.teridax.jcash.banking.*; import me.teridax.jcash.decode.StringUtils; import javax.swing.*; import java.awt.*; public class AccountView extends JPanel { private JTextField iban; private JTextField name; private JTextField address; private JTextField bankName; private JTextField blz; private JTextField type; private JTextField typeSpecialProperty; private JTextField balance; private JLabel typeSpecialLabel; private JComboBox accountSelection; private JPanel content; private JButton logout; private JButton transfer; private JButton deposit; private JButton takeoff; public AccountView() { createComponents(); createLayout(); setBorder(BorderFactory.createEmptyBorder(8,8,8,8)); } public void setProfile(Profile profile) { var bank = profile.getBank(); var account = profile.getPrimaryAccount(); var owner = profile.getOwner(); this.blz.setText(bank.getBlz()); this.bankName.setText(bank.getName()); this.iban.setText(String.valueOf(account.getIban())); this.name.setText(owner.getName() + " " + owner.getFamilyName()); this.address.setText(owner.getStreet() + " " + owner.getCity()); this.balance.setText(StringUtils.LOCAL_NUMBER_FORMAT.format(account.getBalance()) + " €"); this.type.setText(account.getClass().getSimpleName()); if (account instanceof Girokonto) { this.typeSpecialLabel.setText("Overdraft"); this.typeSpecialProperty.setText( StringUtils.LOCAL_NUMBER_FORMAT.format(((Girokonto) account).getOverdraft()) + " €"); } else if (account instanceof SavingsAccount) { this.typeSpecialLabel.setText("Interest rate"); this.typeSpecialProperty.setText( ((SavingsAccount) account).getInterest() + " %" ); } this.accountSelection.removeAllItems(); for (var otherAccount : profile.getAccounts()) { this.accountSelection.addItem(otherAccount.getDescription()); } this.accountSelection.setSelectedItem(account.getDescription()); } private void createLayout() { setLayout(new BorderLayout(16, 16)); add(new JScrollPane(content), BorderLayout.CENTER); content.setLayout(new GridBagLayout()); var constraints = new GridBagConstraints(); constraints.gridwidth = 4; constraints.insets = new Insets(12,12,12,12); var accountSelectionPanel = new JPanel(new BorderLayout(12, 0)); accountSelectionPanel.add(iban, BorderLayout.CENTER); accountSelectionPanel.add(accountSelection, BorderLayout.EAST); addInputRow(constraints, content, accountSelectionPanel, 1, new JLabel("IBAN", SwingConstants.RIGHT)); addInputRow(constraints, content, name, 2, new JLabel("Name/Family-name", SwingConstants.RIGHT)); addInputRow(constraints, content, address, 3, new JLabel("Address", SwingConstants.RIGHT)); addInputRow(constraints, content, bankName, 4, new JLabel("Bank", SwingConstants.RIGHT)); addInputRow(constraints, content, blz, 5, new JLabel("BLZ", SwingConstants.RIGHT)); addInputRow(constraints, content, type, 6, new JLabel("Account", SwingConstants.RIGHT)); addInputRow(constraints, content, typeSpecialProperty, 7, typeSpecialLabel); addInputRow(constraints, content, balance, 8, new JLabel("Balance", SwingConstants.RIGHT)); var buttonPanel = Box.createHorizontalBox(); buttonPanel.add(Box.createHorizontalStrut(4)); buttonPanel.add(logout); buttonPanel.add(Box.createHorizontalStrut(4)); buttonPanel.add(Box.createGlue()); buttonPanel.add(transfer); buttonPanel.add(Box.createHorizontalStrut(4)); buttonPanel.add(deposit); buttonPanel.add(Box.createHorizontalStrut(4)); buttonPanel.add(takeoff); add(buttonPanel, BorderLayout.SOUTH); } private void createComponents() { this.blz = new JTextField(); this.iban = new JTextField(); this.address = new JTextField(); this.bankName = new JTextField(); this.name = new JTextField(); this.type = new JTextField(); this.balance = new JTextField(); this.typeSpecialProperty = new JTextField(); this.blz.setEditable(false); this.iban.setEditable(false); this.address.setEditable(false); this.bankName.setEditable(false); this.name.setEditable(false); this.type.setEditable(false); this.balance.setEditable(false); this.typeSpecialProperty.setEditable(false); this.typeSpecialLabel = new JLabel("", SwingConstants.RIGHT); this.accountSelection = new JComboBox<>(); this.content = new JPanel(); this.logout = new JButton("Logout"); this.transfer = new JButton("Transfer"); this.deposit = new JButton("Deposit"); this.takeoff = new JButton("Takeoff"); } private void addInputRow(GridBagConstraints constraints, JComponent target, JComponent comp, int row, JLabel label) { constraints.gridwidth = 1; constraints.gridx = 1; constraints.gridy = row; constraints.weightx = 0; constraints.fill = GridBagConstraints.HORIZONTAL; target.add(label, constraints); constraints.gridx = 2; constraints.gridy = row; constraints.weightx = 1; constraints.fill = GridBagConstraints.HORIZONTAL; target.add(comp, constraints); } public JComboBox getAccountSelection() { return accountSelection; } public JButton getLogout() { return logout; } public JButton getTransfer() { return transfer; } public JButton getDeposit() { return deposit; } public JButton getTakeoff() { return takeoff; } }