-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFootballSystemGUI.java
More file actions
209 lines (176 loc) · 7.84 KB
/
FootballSystemGUI.java
File metadata and controls
209 lines (176 loc) · 7.84 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
198
199
200
201
202
203
204
205
206
207
208
209
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.mycompany.project2;
/**
*
* @author User
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class FootballSystemGUI {
private ArrayList<PlayersInfo> players;
private Team team;
private dates prac;
private dates gameday;
private LineUpGenerator gen;
private JTextArea rosterTextArea;
private static FootballSystemGUI instance;
public FootballSystemGUI() {
players = new ArrayList<PlayersInfo>();
players.add(new PlayersInfo("Ali", "Striker", "injured"));
players.add(new PlayersInfo("John", "Midfielder", "Recovering.."));
players.add(new PlayersInfo("Farred", "Defender", "Ready"));
team = new Team("team1", players);
prac = new practiceday("08/11/24");
gameday = new gameday("06/12/24");
gen = new LineUpGenerator();
JFrame frame = new JFrame("Centralized System");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 300);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
rosterTextArea = new JTextArea();
rosterTextArea.setEditable(false);
mainPanel.add(new JScrollPane(rosterTextArea), BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
JButton roasterButton = new JButton("Team Roaster");
roasterButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
displayRoster();
}
});
buttonPanel.add(roasterButton);
JButton practiceButton = new JButton("Team Practice Day");
practiceButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, prac.toString(), "Practice Day", JOptionPane.INFORMATION_MESSAGE);
}
});
buttonPanel.add(practiceButton);
JButton gameButton = new JButton("Team Game Day");
gameButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, gameday.toString(), "Game Day", JOptionPane.INFORMATION_MESSAGE);
}
});
buttonPanel.add(gameButton);
JButton removeButton = new JButton("Remove Player");
removeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
removePlayer();
}
});
buttonPanel.add(removeButton);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
frame.getContentPane().add(mainPanel);
frame.setVisible(true);
}
private void removePlayer() {
String[] playerNames = team.getPlayerNames();
String playerName = (String) JOptionPane.showInputDialog(null, "Select a player to remove:", "Remove Player",
JOptionPane.QUESTION_MESSAGE, null, playerNames, playerNames[0]);
if (playerName != null) {
team.removePlayer(playerName);
JOptionPane.showMessageDialog(null, "Player removed successfully!", "Player Removed", JOptionPane.INFORMATION_MESSAGE);
displayRoster();
}
}
private void displayRoster() {
StringBuilder roster = new StringBuilder("Current lineup:\n");
roster.append(team.getRoster());
rosterTextArea.setText(roster.toString());
String[] options = {"Choose Player", "Register Player"};
int choice = JOptionPane.showOptionDialog(null, "Do you want to choose a player from the available players or register a new player?",
"Choose an option", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if (choice == 0) {
String[] positions = {"Defender", "Midfielder", "Striker", "GoalKeeper"};
String position = (String) JOptionPane.showInputDialog(null, "Select the player position:", "Choose Position",
JOptionPane.QUESTION_MESSAGE, null, positions, positions[0]);
PlayersInfo newPlayer = null;
if ("Defender".equalsIgnoreCase(position)) {
newPlayer = new PlayersInfo("Yamin", "Defender", "Recovering(will be back in 2 months)");
} else if ("Striker".equalsIgnoreCase(position)) {
newPlayer = new PlayersInfo("Brian", "Striker", "Ready");
} else if ("GoalKeeper".equalsIgnoreCase(position)) {
newPlayer = new PlayersInfo("Jay", "GoalKeeper", "Ready");
} else if ("Midfielder".equalsIgnoreCase(position)) {
newPlayer = new PlayersInfo("Sena", "Midfielder", "Ready");
}
if (newPlayer != null) {
players.add(newPlayer);
rosterTextArea.append("\n" + newPlayer + " has been added to the team");
}
} else if (choice == 1) {
String name = JOptionPane.showInputDialog("Enter the name of the player:");
String position = JOptionPane.showInputDialog("Enter the position of the player:");
PlayersInfo newPlayer = new PlayersInfo(name, position, null);
players.add(newPlayer);
rosterTextArea.append("\n" + newPlayer + " has been added to the team");
}
}
public static FootballSystemGUI getInstance() {
if (instance == null) {
instance = new FootballSystemGUI();
}
return instance;
}
private void writeToFile(String filename, String content) throws IOException {
FileWriter writer = new FileWriter(filename);
writer.write(content);
writer.close();
}
private String readFromFile(String filename) throws FileNotFoundException {
File file = new File(filename);
Scanner scanner = new Scanner(file);
StringBuilder content = new StringBuilder();
while (scanner.hasNextLine()) {
content.append(scanner.nextLine()).append("\n");
}
scanner.close();
return content.toString();
}
public static void main(String[] args) throws SQLException {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
FootballSystemGUI.getInstance(); // Get the instance using the Singleton pattern
}
});
try
{
String databaseURL = "jdbc:derby:D:/Derby/FootballSystem;create=true";
Connection con = DriverManager.getConnection(databaseURL);
} catch(SQLException ex)
{
ex.printStackTrace();
}
}
}
/*The Singleton pattern ensures that
only one instance of the class can
be created and provides a global point
of access to that instance.
By using the Singleton pattern,
you can ensure that there is a single
instance of the FootballSystemGUI class,
allowing consistent access to the GUI functionality
and preventing multiple instances from being created accidentally.*/