Files
Bankapp/src/main/java/me/zacharias/bank/app/TransferWindow.java
Zacharias 86b505b4e3 Setup system for handeling transactions between acounts.
Made so each Transaction handler in the Account class to return the transaction object.
Updated the Utils class to handle the transactions que reading and writing.
Added org.json to aid in trnsaction que handling.
made so accounts can update there balance and transactions from the transaction que.
Added a tranfer window to handle the transfer of money between acounts.
2025-04-23 13:36:49 +02:00

185 lines
6.1 KiB
Java

package me.zacharias.bank.app;
import me.zacharias.bank.Account;
import me.zacharias.bank.Utils;
import me.zacharias.bank.transaction.Transaction;
import me.zacharias.bank.transaction.TransactionType;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.*;
import java.util.UUID;
public class TransferWindow extends JPanel {
Main bankApplication;
Account account;
JLabel accountName;
JLabel balance;
JLabel AccountNumber;
JTextField amount;
JTextField description;
JTextField destination;
JLabel warning;
JButton transferButton;
float finalAmount;
UUID finalDestination;
String finalDescription;
JFrame frame;
Timer t = new Timer(100, e -> frame.repaint());
public TransferWindow(Main bankApplication, Account account) {
this.bankApplication = bankApplication;
this.account = account;
frame = new JFrame();
frame.setSize(400, 300);
frame.setLocationRelativeTo(bankApplication);
frame.setTitle("Transfer");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(null);
accountName = new JLabel(account.getName());
accountName.setBounds(10, 10, 300, 30);
balance = new JLabel(account.getBalance() + " kr");
balance.setBounds(10, 50, 300, 30);
AccountNumber = new JLabel(account.getId().toString());
AccountNumber.setBounds(10, 90, 300, 30);
destination = new JTextField();
destination.setBounds(10, 130, 300, 30);
destination.setText("Destination");
amount = new JTextField();
amount.setBounds(10, 170, 300, 30);
amount.addActionListener((a) -> {
if(!amount.getText().matches("[0-9]*(.[0-9]{1,2})?")) {
amount.setBorder(new LineBorder(Color.RED));
}
if(Float.parseFloat(amount.getText()) < 0 || Float.parseFloat(amount.getText()) > account.getBalance()) {
amount.setBorder(new LineBorder(Color.RED));
}
else {
amount.setBorder(new LineBorder(Color.BLACK));
}
});
amount.setBorder(new LineBorder(Color.BLACK));
amount.setText("Amount");
description = new JTextField();
description.setBounds(10, 200, 300, 30);
description.setText("Description");
warning = new JLabel("Warning: This will transfer the amount to the destination account. The amount will be deducted from the source account. If the destination account does not exist, we cannot guarantee that the amount will be returned.");
warning.setForeground(Color.RED);
warning.setBounds(10, 240, 300, 30);
transferButton = new JButton("Transfer");
transferButton.setBounds(10, 270, 300, 30);
transferButton.addActionListener(e -> {
if(amount.getText().matches("[0-9]*(.[0-9]{1,2})?") && Float.parseFloat(amount.getText()) > 0 && Float.parseFloat(amount.getText()) <= account.getBalance()) {
finalAmount = Float.parseFloat(amount.getText());
}
else
{
amount.setBorder(new LineBorder(Color.RED));
return;
}
if(destination.getText().matches("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}")) {
finalDestination = UUID.fromString(destination.getText());
}
else
{
destination.setBorder(new LineBorder(Color.RED));
return;
}
if(!description.getText().isEmpty()) {
finalDescription = description.getText();
}
else
{
description.setBorder(new LineBorder(Color.RED));
return;
}
float finalAmount = this.getAmount();
String finalDescription = this.getDescription();
UUID finalDestination = this.getDestination();
if(finalAmount > 0 && finalAmount <= account.getBalance() && finalDestination != null) {
Transaction transaction = account.WithdrawTransaction(finalAmount, finalDescription, finalDestination);
Transaction t2 = transaction.copy(TransactionType.DEPOSIT);
Utils.AddTrnsationQue(t2);
}
frame.dispose();
});
this.add(accountName);
this.add(balance);
this.add(AccountNumber);
this.add(amount);
this.add(description);
this.add(transferButton);
this.add(destination);
this.add(warning);
frame.add(this);
frame.setVisible(true);
t.start();
}
@Override
protected void paintComponent(Graphics g) {
accountName.setBounds(10, 10, accountName.getWidth(), accountName.getHeight());
balance.setBounds(10, 50, balance.getWidth(), balance.getHeight());
AccountNumber.setBounds(10, 90, AccountNumber.getWidth(), AccountNumber.getHeight());
destination.setBounds(10, 130, destination.getWidth(), destination.getHeight());
amount.setBounds(10, 170, amount.getWidth(), amount.getHeight());
description.setBounds(10, 200, description.getWidth(), description.getHeight());
warning.setBounds(10, 240, warning.getWidth(), warning.getHeight());
transferButton.setBounds(10, 270, transferButton.getWidth(), transferButton.getHeight());
super.paintComponents(g);
}
public void requestFocus() {
//this.requestFocus(FocusEvent.Cause.ACTIVATION);
}
public boolean isOpen() {
return frame.isVisible();
}
public String getDescription() {
return finalDescription;
}
public float getAmount() {
return finalAmount;
}
public UUID getDestination() {
return finalDestination;
}
public void makeVisible() {
frame.setVisible(true);
}
}