Below is a simplified example using Java Swing for the GUI and a simple data structure for storing runner information. Note that this is a basic mockup that I made but I still need to improve on. import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class RunnerGUI extends JFrame { private List runners = new ArrayList<>(); private JTextField nameField, distanceField, timeField; private DefaultListModel listModel; public RunnerGUI() { // Set up JFrame setTitle("Runner Information"); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create components nameField = new JTextField(20); distanceField = new JTextField(10); timeField = new JTextField(10); listModel = new DefaultListModel<>(); JList listView = new JList<>(listModel); JButton addButton = new JButton("Add Runner"); JButton sortButton = new JButton("Sort List"); JButton saveButton = new JButton("Save to CSV"); // Add components to the frame setLayout(new BorderLayout()); JPanel inputPanel = new JPanel(); inputPanel.add(new JLabel("Name:")); inputPanel.add(nameField); inputPanel.add(new JLabel("Distance:")); inputPanel.add(distanceField); inputPanel.add(new JLabel("Time:")); inputPanel.add(timeField); inputPanel.add(addButton); inputPanel.add(sortButton); inputPanel.add(saveButton); add(inputPanel, BorderLayout.NORTH); add(new JScrollPane(listView), BorderLayout.CENTER); // Add ActionListener for buttons addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { addRunner(); } }); sortButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { sortList(); } }); saveButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { saveToCSV(); } }); } private void addRunner() { String name = nameField.getText(); double distance = Double.parseDouble(distanceField.getText()); double time = Double.parseDouble(timeField.getText()); double pace = time / distance; Runner runner = new Runner(name, distance, time, pace); runners.add(runner); updateListView(); clearFields(); } private void sortList() { // Implement sorting based on radio button selection // For simplicity, let's sort by name in ascending order Collections.sort(runners, Comparator.comparing(Runner::getName)); updateListView(); } private void saveToCSV() { try (BufferedWriter writer = new BufferedWriter(new FileWriter("finalProjectRunners.txt"))) { for (Runner runner : runners) { String line = String.format("%s,%.2f,%.2f,%.2f", runner.getName(), runner.getDistance(), runner.getTime(), runner.getPace()); writer.write(line); writer.newLine(); } JOptionPane.showMessageDialog(this, "Data saved to CSV file."); } catch (IOException e) { e.printStackTrace(); JOptionPane.showMessageDialog(this, "Error saving to CSV file."); } } private void updateListView() { listModel.clear(); for (Runner runner : runners) { String displayText = String.format("Name: %s, Distance: %.2f, Time: %.2f, Pace: %.2f", runner.getName(), runner.getDistance(), runner.getTime(), runner.getPace()); listModel.addElement(displayText); } } private void clearFields() { nameField.setText(""); distanceField.setText(""); timeField.setText(""); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new RunnerGUI().setVisible(true); } }); } } class Runner { private String name;
Below is a simplified example using Java Swing for the GUI and a simple data structure for storing runner information. Note that this is a basic mockup that I made but I still need to improve on.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; public class RunnerGUI extends JFrame {
private List<Runner> runners = new ArrayList<>();
private JTextField nameField, distanceField, timeField;
private DefaultListModel<String> listModel;
public RunnerGUI() {
// Set up JFrame setTitle("Runner Information");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create components nameField = new JTextField(20);
distanceField = new JTextField(10);
timeField = new JTextField(10); listModel = new DefaultListModel<>();
JList<String> listView = new JList<>(listModel);
JButton addButton = new JButton("Add Runner");
JButton sortButton = new JButton("Sort List");
JButton saveButton = new JButton("Save to CSV");
// Add components to the frame setLayout(new BorderLayout());
JPanel inputPanel = new JPanel(); inputPanel.add(new JLabel("Name:"));
inputPanel.add(nameField);
inputPanel.add(new JLabel("Distance:"));
inputPanel.add(distanceField);
inputPanel.add(new JLabel("Time:"));
inputPanel.add(timeField);
inputPanel.add(addButton); inputPanel.add(sortButton);
inputPanel.add(saveButton);
add(inputPanel, BorderLayout.NORTH);
add(new JScrollPane(listView), BorderLayout.CENTER);
// Add ActionListener for buttons
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) { addRunner();
}
});
sortButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) { sortList();
}
});
saveButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
saveToCSV();
}
});
}
private void addRunner()
{
String name = nameField.getText();
double distance = Double.parseDouble(distanceField.getText());
double time = Double.parseDouble(timeField.getText());
double pace = time / distance;
Runner runner = new Runner(name, distance, time, pace);
runners.add(runner);
updateListView();
clearFields(); }
private void sortList()
{
// Implement sorting based on radio button selection
// For simplicity, let's sort by name in ascending order Collections.sort(runners, Comparator.comparing(Runner::getName));
updateListView();
}
private void saveToCSV() { try (BufferedWriter writer = new BufferedWriter(new FileWriter("finalProjectRunners.txt")))
{
for (Runner runner : runners) { String line = String.format("%s,%.2f,%.2f,%.2f", runner.getName(), runner.getDistance(), runner.getTime(), runner.getPace());
writer.write(line);
writer.newLine(); } JOptionPane.showMessageDialog(this, "Data saved to CSV file."); } catch (IOException e) { e.printStackTrace(); JOptionPane.showMessageDialog(this, "Error saving to CSV file."); } } private void updateListView() { listModel.clear();
for (Runner runner : runners) { String displayText = String.format("Name: %s, Distance: %.2f, Time: %.2f, Pace: %.2f", runner.getName(), runner.getDistance(), runner.getTime(), runner.getPace()); listModel.addElement(displayText); } } private void clearFields() { nameField.setText(""); distanceField.setText(""); timeField.setText("");
}
public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new RunnerGUI().setVisible(true);
}
});
}
}
class Runner
{
private String name;
private double distance;
private double time;
private double pace; public Runner(String name, double distance, double time, double pace) { this.name = name; this.distance = distance;
this.time = time; this.pace = pace;
}
public String getName()
{
return name;
}
public double getDistance() {
return distance;
}
public double getTime() {
return time;
}
public double getPace() {
return pace;
}
}
With this code, can you please improve on it and see what can be fixed. Thank you!



Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images









