-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainScreen.java
More file actions
166 lines (136 loc) · 5.86 KB
/
MainScreen.java
File metadata and controls
166 lines (136 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* MainScreen class - Main ATM interface after login
*/
public class MainScreen extends JFrame {
private BankAccount currentAccount;
private BasicATM atm;
private JLabel balanceLabel;
private JLabel accountTypeLabel;
private JLabel accountNumberLabel;
public MainScreen(BankAccount account) {
this.currentAccount = account;
this.atm = BasicATM.getInstance();
setTitle("ATM Simulation - Main Menu");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 400);
setLocationRelativeTo(null);
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
// Header section
JPanel headerPanel = new JPanel(new GridLayout(3, 1));
accountTypeLabel = new JLabel("Account Type: " + account.getAccountType());
accountTypeLabel.setFont(new Font("Arial", Font.BOLD, 16));
String maskedNumber = "xxxx-xxxx-xxxx-" + account.getAccountNumber().substring(12);
accountNumberLabel = new JLabel("Account: " + maskedNumber);
balanceLabel = new JLabel("Balance: $" + String.format("%.2f", account.getBalance()));
balanceLabel.setFont(new Font("Arial", Font.BOLD, 16));
headerPanel.add(accountTypeLabel);
headerPanel.add(accountNumberLabel);
headerPanel.add(balanceLabel);
// Button section
JPanel buttonPanel = new JPanel(new GridLayout(2, 2, 10, 10));
JButton checkBalanceButton = createMenuButton("Check Balance");
JButton withdrawButton = createMenuButton("Withdraw Cash");
JButton depositButton = createMenuButton("Deposit Funds");
JButton historyButton = createMenuButton("Transaction History");
buttonPanel.add(checkBalanceButton);
buttonPanel.add(withdrawButton);
buttonPanel.add(depositButton);
buttonPanel.add(historyButton);
// Footer section
JPanel footerPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
JButton logoutButton = new JButton("Logout");
footerPanel.add(logoutButton);
// Action listeners
checkBalanceButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showBalanceDetails();
}
});
withdrawButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showWithdrawScreen();
}
});
depositButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showDepositScreen();
}
});
historyButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showTransactionHistory();
}
});
logoutButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
logout();
}
});
// Layout structure
mainPanel.add(headerPanel, BorderLayout.NORTH);
mainPanel.add(buttonPanel, BorderLayout.CENTER);
mainPanel.add(footerPanel, BorderLayout.SOUTH);
add(mainPanel);
}
private JButton createMenuButton(String text) {
JButton button = new JButton(text);
button.setFont(new Font("Arial", Font.PLAIN, 14));
return button;
}
private void showBalanceDetails() {
JDialog dialog = new JDialog(this, "Account Balance", true);
dialog.setSize(400, 300);
dialog.setLocationRelativeTo(this);
JPanel panel = new JPanel(new BorderLayout(10, 10));
panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
JPanel infoPanel = new JPanel(new GridLayout(0, 1, 5, 10));
infoPanel.add(new JLabel("Account Type: " + currentAccount.getAccountType()));
infoPanel.add(new JLabel("Current Balance: $" + String.format("%.2f", currentAccount.getBalance())));
if (currentAccount instanceof SavingsAccount) {
SavingsAccount sa = (SavingsAccount) currentAccount;
infoPanel.add(new JLabel("Interest Rate: " + sa.getInterestRate() + "%"));
infoPanel.add(new JLabel("Estimated Monthly Interest: $" + String.format("%.2f", sa.calculateInterest())));
} else if (currentAccount instanceof CheckingAccount) {
CheckingAccount ca = (CheckingAccount) currentAccount;
infoPanel.add(new JLabel("Overdraft Limit: $" + String.format("%.2f", ca.getOverdraftLimit())));
}
JButton closeButton = new JButton("Close");
closeButton.addActionListener(e -> dialog.dispose());
panel.add(infoPanel, BorderLayout.CENTER);
panel.add(closeButton, BorderLayout.SOUTH);
dialog.add(panel);
dialog.setVisible(true);
}
private void showWithdrawScreen() {
WithdrawScreen ws = new WithdrawScreen(this, currentAccount, atm);
ws.setVisible(true);
updateBalanceLabel();
}
private void showDepositScreen() {
DepositScreen ds = new DepositScreen(this, currentAccount, atm);
ds.setVisible(true);
updateBalanceLabel();
}
private void showTransactionHistory() {
TransactionHistoryScreen ts = new TransactionHistoryScreen(this, currentAccount);
ts.setVisible(true);
}
private void logout() {
LoginScreen login = new LoginScreen();
login.setVisible(true);
dispose();
}
public void updateBalanceLabel() {
balanceLabel.setText("Balance: $" + String.format("%.2f", currentAccount.getBalance()));
}
}