Repository navigation
Expand file tree
/
Copy pathFootballManagement.java
More file actions
197 lines (167 loc) · 6.59 KB
/
Copy pathFootballManagement.java
File metadata and controls
197 lines (167 loc) · 6.59 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FootballManagement extends JFrame implements ActionListener {
JButton addBtn, deleteBtn, editBtn, searchBtn, exitBtn;
JTextField idField, nameField, searchField;
JLabel idLabel, nameLabel, searchLabel, titleLabel;
JScrollPane scrollPane;
JPanel panel;
private Player[] players = new Player[100];
private int count = 0; // current number of players
public FootballManagement() {
super("Football Management System");
setSize(600, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);
getContentPane().setBackground(new Color(25, 25, 50));
// Title
titleLabel = new JLabel("Football Management", SwingConstants.CENTER);
titleLabel.setBounds(150, 10, 300, 30);
titleLabel.setFont(new Font("Arial", Font.BOLD, 24));
add(titleLabel);
titleLabel.setForeground(Color.WHITE);
// ID Label and Field
idLabel = new JLabel("Player ID:");
idLabel.setBounds(50, 60, 100, 25);
add(idLabel);
idLabel.setForeground(Color.WHITE);
idField = new JTextField();
idField.setBounds(150, 60, 150, 25);
add(idField);
// Name Label and Field
nameLabel = new JLabel("Player Name:");
nameLabel.setBounds(50, 100, 100, 25);
add(nameLabel);
nameLabel.setForeground(Color.WHITE);
nameField = new JTextField();
nameField.setBounds(150, 100, 150, 25);
add(nameField);
// Search Label and Field
searchLabel = new JLabel("Search by ID:");
searchLabel.setBounds(50, 140, 100, 25);
add(searchLabel);
searchLabel.setForeground(Color.WHITE);
searchField = new JTextField();
searchField.setBounds(150, 140, 150, 25);
add(searchField);
// Buttons
addBtn = new JButton("Add");
addBtn.setBounds(350, 60, 100, 30);
addBtn.setBackground(new Color(46, 204, 113));
addBtn.setForeground(Color.WHITE);
addBtn.addActionListener(this);
add(addBtn);
deleteBtn = new JButton("Delete");
deleteBtn.setBounds(350, 100, 100, 30);
deleteBtn.setBackground(Color.RED);
deleteBtn.setForeground(Color.WHITE);
deleteBtn.addActionListener(this);
add(deleteBtn);
editBtn = new JButton("Edit");
editBtn.setBounds(350, 140, 100, 30);
editBtn.setBackground(new Color(52,152,219));
editBtn.setForeground(Color.WHITE);
editBtn.addActionListener(this);
add(editBtn);
searchBtn = new JButton("Search");
searchBtn.setBounds(350, 180, 100, 30);
searchBtn.setBackground(new Color(155,89,182));
searchBtn.setForeground(Color.WHITE);
searchBtn.addActionListener(this);
add(searchBtn);
exitBtn = new JButton("Exit");
exitBtn.setBounds(350, 220, 100, 30);
exitBtn.setBackground(Color.BLACK);
exitBtn.setForeground(Color.WHITE);
exitBtn.addActionListener(this);
add(exitBtn);
setVisible(true);
}
// ActionListener method
public void actionPerformed(ActionEvent e) {
String id = idField.getText().trim();
String name = nameField.getText().trim();
String searchId = searchField.getText().trim();
if (e.getSource() == addBtn) {
if (id.isEmpty() || name.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter both Player ID and Name.");
return;
}
if (findPlayerById(id) != -1) {
JOptionPane.showMessageDialog(this, "Player ID already exists.");
return;
}
if (count >= players.length) {
JOptionPane.showMessageDialog(this, "Player list is full.");
return;
}
players[count++] = new Player(id, name);
JOptionPane.showMessageDialog(this, "Player added successfully.");
clearFields();
} else if (e.getSource() == deleteBtn) {
if (searchId.isEmpty()) {
JOptionPane.showMessageDialog(this, "Enter Player ID to delete.");
return;
}
int index = findPlayerById(searchId);
if (index == -1) {
JOptionPane.showMessageDialog(this, "Player not found.");
return;
}
// Shift players to delete
for (int i = index; i < count - 1; i++) {
players[i] = players[i + 1];
}
players[--count] = null;
JOptionPane.showMessageDialog(this, "Player deleted successfully.");
clearFields();
} else if (e.getSource() == editBtn) {
if (id.isEmpty() || name.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter both Player ID and Name to edit.");
return;
}
int index = findPlayerById(id);
if (index == -1) {
JOptionPane.showMessageDialog(this, "Player not found.");
return;
}
players[index].setName(name);
JOptionPane.showMessageDialog(this, "Player updated successfully.");
clearFields();
} else if (e.getSource() == searchBtn) {
if (searchId.isEmpty()) {
JOptionPane.showMessageDialog(this, "Enter Player ID to search.");
return;
}
int index = findPlayerById(searchId);
if (index == -1) {
JOptionPane.showMessageDialog(this, "Player not found.");
clearFields();
return;
}
idField.setText(players[index].getId());
nameField.setText(players[index].getName());
} else if (e.getSource() == exitBtn) {
System.exit(0);
}
}
// Helper to find player index by ID
private int findPlayerById(String id) {
for (int i = 0; i < count; i++) {
if (players[i].getId().equals(id)) {
return i;
}
}
return -1;
}
// Clear all text fields
private void clearFields() {
idField.setText("");
nameField.setText("");
searchField.setText("");
}
public static void main(String[] args) {
new FootballManagement();
}
}