Started implementing the Account listing of a User

This commit is contained in:
2025-03-24 09:34:14 +01:00
parent d8e0c12b9f
commit 545606deb0
4 changed files with 87 additions and 22 deletions

View File

@@ -13,15 +13,16 @@ public class Main {
User user; User user;
public static void main(String[] args) { public static void main(String[] args) {
String user = SHA256("user"); String username = "user";
String user = SHA256(username);
File userFile = new File("./users/" + user + ".json"); File userFile = new File("./users/" + user + ".json");
if(!userFile.exists()) { if(!userFile.exists()) {
User u = new User("user"); User u = new User(username);
u.createAccount("Konto"); u.createAccount("Konto");
Account a = u.getAccount("Konto"); Account a = u.getAccount("Konto");
a.DepositTransaction(104, "Deposit", UUID.randomUUID()); a.DepositTransaction(100, "Deposit", UUID.randomUUID());
String json = gson.toJson(u); String json = gson.toJson(u);
try { try {

View File

@@ -2,6 +2,8 @@ package me.zacharias.bank.app;
import me.zacharias.bank.Account; import me.zacharias.bank.Account;
import me.zacharias.bank.User; import me.zacharias.bank.User;
import me.zacharias.bank.app.component.DrawManager;
import me.zacharias.bank.app.component.InteractiveList;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@@ -14,7 +16,7 @@ public class MainMenu extends JPanel {
JLabel welcome; JLabel welcome;
JButton logout; JButton logout;
JList<Account> accountList; InteractiveList<Account> accountList;
public MainMenu(User user, Main bankApplication) { public MainMenu(User user, Main bankApplication) {
this.user = user; this.user = user;
@@ -31,23 +33,20 @@ public class MainMenu extends JPanel {
bankApplication.logout(); bankApplication.logout();
}); });
accountList = new JList<>(); accountList = new InteractiveList<>();
accountList.setBounds(getWidth()/2-100, 80, 200, 30); accountList.setBounds(0, 100, getWidth(), getHeight()-200);
accountList.setModel(new DefaultListModel<>()); accountList.setDrawManager((DrawManager<Account>) (interactiveList, item, index, isHovered, isSelected) -> {
accountList.setBackground(Color.GRAY); JLabel label = new JLabel(item.getName());
accountList.setCellRenderer(new ListCellRenderer<Account>() { label.setOpaque(true);
@Override label.setBackground(isSelected ? Color.GREEN : Color.WHITE);
public Component getListCellRendererComponent(JList<? extends Account> list, Account value, int index, boolean isSelected, boolean cellHasFocus) { label.setForeground(isHovered ? Color.WHITE : Color.BLACK);
JButton label = new JButton(); return label;
label.setText(value.getName() + " " + value.getBalance() + " kr");
label.setBackground(Color.GRAY);
label.addActionListener(e -> {
bankApplication.showAccount(value);
});
return label;
}
}); });
for (Account account : user.getAccounts()) {
accountList.add(account);
}
this.add(welcome); this.add(welcome);
this.add(logout); this.add(logout);
this.add(accountList); this.add(accountList);
@@ -57,10 +56,9 @@ public class MainMenu extends JPanel {
protected void paintComponent(Graphics g) { protected void paintComponent(Graphics g) {
welcome.setBounds(getWidth()/2-100, 10, 200, 30); welcome.setBounds(getWidth()/2-100, 10, 200, 30);
logout.setBounds(getWidth()/2-100, getHeight()-50, 200, 30); logout.setBounds(getWidth()/2-100, getHeight()-50, 200, 30);
accountList.setBounds(getWidth()/2-100, 80, 200, 30); accountList.setBounds(getWidth()/2-100, getHeight()-80, 200, 300);
accountList.setList(user.getAccounts());
accountList.setListData(user.getAccounts().toArray(new Account[0]));
super.paintComponent(g); super.paintComponent(g);
} }
} }

View File

@@ -0,0 +1,7 @@
package me.zacharias.bank.app.component;
import java.awt.*;
public interface DrawManager<T> {
Component draw(InteractiveList<T> interactiveList, T item, int index, boolean isHovered, boolean isSelected);
}

View File

@@ -0,0 +1,59 @@
package me.zacharias.bank.app.component;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
public class InteractiveList<T> extends JPanel {
ArrayList<T> list;
DrawManager<T> drawManager;
public InteractiveList() {
list = new ArrayList<>();
drawManager = new DrawManager<T>() {
@Override
public Component draw(InteractiveList<T> interactiveList, T item, int index, boolean isHovered, boolean isSelected) {
JLabel label = new JLabel(item.toString());
label.setOpaque(true);
label.setBackground(isSelected ? Color.GREEN : Color.WHITE);
label.setForeground(isHovered ? Color.WHITE : Color.BLACK);
return label;
}
};
}
public void add(T item) {
list.add(item);
}
public void remove(T item) {
list.remove(item);
}
public void clear() {
list.clear();
}
public void setList(ArrayList<T> list) {
this.list = list;
}
public ArrayList<T> getList() {
return list;
}
public void setDrawManager(DrawManager drawManager) {
this.drawManager = drawManager;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//this.removeAll();
for (int i = 0; i < list.size(); i++) {
Component component = drawManager.draw(this, list.get(i), i, false, false);
component.setBounds(0, i * component.getHeight(), getWidth(), component.getHeight());
//add(component);
component.paint(g);
}
}
}