0% found this document useful (0 votes)
16 views4 pages

JDDDD2

The document outlines key issues in a grading system program, including missing DAO implementations, incorrect grade entry placement, improper GUI layout, and potential null pointer errors. It provides fixed code that includes implementations for StudentDAO and GradeDAO, ensuring students exist before adding grades, and improves the GUI layout and error handling. The improvements ensure the program functions correctly and is user-friendly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

JDDDD2

The document outlines key issues in a grading system program, including missing DAO implementations, incorrect grade entry placement, improper GUI layout, and potential null pointer errors. It provides fixed code that includes implementations for StudentDAO and GradeDAO, ensuring students exist before adding grades, and improves the GUI layout and error handling. The improvements ensure the program functions correctly and is user-friendly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Here are the key issues in your program and how to fix them:

Issues:

1. Missing StudentDAO and GradeDAO Implementation:


o You reference studentDAO and gradeDAO, but they aren't defined in
your code.
2. Incorrect Placement of Grade Entry:
o Grades are added based on the studentIdField, which should be
searched first to ensure the student exists.
3. Improper GUI Layout:
o The UI could be structured better for clarity.
4. Possible Null Pointer Errors:
o Searching for a student when no data exists may result in
exceptions.

Fixed Code:

This version includes a basic StudentDAO and GradeDAO to ensure functionality.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

class StudentDAO {
private Map<String, String> students = new HashMap<>();

public void addStudent(String id, String name, String section) {


students.put(id, "ID: " + id + ", Name: " + name + ", Section: " +
section);
}

public List<String> searchStudent(String query) {


List<String> results = new ArrayList<>();
for (String student : students.values()) {
if (student.toLowerCase().contains(query.toLowerCase())) {
results.add(student);
}
}
return results;
}
}

class GradeDAO {
private Map<String, List<String>> grades = new HashMap<>();

public void addGrade(String studentId, String subject, double grade) {


grades.computeIfAbsent(studentId, k -> new ArrayList<>())
.add("Subject: " + subject + ", Grade: " + grade);
}

public List<String> getGrades(String studentId) {


return grades.getOrDefault(studentId, new ArrayList<>());
}
}

public class GradingSystemGUI {


private JFrame frame;
private JTextField studentIdField, nameField, sectionField, subjectField,
gradeField, searchField;
private JTextArea resultArea;
private StudentDAO studentDAO = new StudentDAO();
private GradeDAO gradeDAO = new GradeDAO();

public GradingSystemGUI() {
frame = new JFrame("Grading System");
frame.setSize(700, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());

// Student Panel
JPanel studentPanel = new JPanel(new FlowLayout());
studentIdField = new JTextField(10);
nameField = new JTextField(10);
sectionField = new JTextField(10);
JButton addStudentBtn = new JButton("Add Student");

studentPanel.add(new JLabel("Student ID:"));


studentPanel.add(studentIdField);
studentPanel.add(new JLabel("Name:"));
studentPanel.add(nameField);
studentPanel.add(new JLabel("Section:"));
studentPanel.add(sectionField);
studentPanel.add(addStudentBtn);

// Grade Panel
JPanel gradePanel = new JPanel(new FlowLayout());
subjectField = new JTextField(10);
gradeField = new JTextField(5);
JButton addGradeBtn = new JButton("Add Grade");

gradePanel.add(new JLabel("Subject:"));
gradePanel.add(subjectField);
gradePanel.add(new JLabel("Grade:"));
gradePanel.add(gradeField);
gradePanel.add(addGradeBtn);

// Search Panel
JPanel searchPanel = new JPanel(new FlowLayout());
searchField = new JTextField(20);
JButton searchBtn = new JButton("Search");
resultArea = new JTextArea(15, 50);
resultArea.setEditable(false);
searchPanel.add(new JLabel("Search:"));
searchPanel.add(searchField);
searchPanel.add(searchBtn);

JPanel resultPanel = new JPanel(new BorderLayout());


resultPanel.add(searchPanel, BorderLayout.NORTH);
resultPanel.add(new JScrollPane(resultArea), BorderLayout.CENTER);

// Adding Panels to Frame


frame.add(studentPanel, BorderLayout.NORTH);
frame.add(gradePanel, BorderLayout.CENTER);
frame.add(resultPanel, BorderLayout.SOUTH);

// Action Listeners
addStudentBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String id = studentIdField.getText().trim();
String name = nameField.getText().trim();
String section = sectionField.getText().trim();

if (id.isEmpty() || name.isEmpty() || section.isEmpty()) {


JOptionPane.showMessageDialog(frame, "All fields are
required!", "Error", JOptionPane.ERROR_MESSAGE);
return;
}

studentDAO.addStudent(id, name, section);


JOptionPane.showMessageDialog(frame, "Student Added!");
studentIdField.setText("");
nameField.setText("");
sectionField.setText("");
}
});

addGradeBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String id = studentIdField.getText().trim();
String subject = subjectField.getText().trim();
double grade =
Double.parseDouble(gradeField.getText().trim());

if (id.isEmpty() || subject.isEmpty()) {
JOptionPane.showMessageDialog(frame, "Student ID and
Subject are required!", "Error", JOptionPane.ERROR_MESSAGE);
return;
}

// Ensure student exists before adding a grade


List<String> student = studentDAO.searchStudent(id);
if (student.isEmpty()) {
JOptionPane.showMessageDialog(frame, "Student not
found! Add the student first.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}

gradeDAO.addGrade(id, subject, grade);


JOptionPane.showMessageDialog(frame, "Grade Added!");
subjectField.setText("");
gradeField.setText("");
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(frame, "Invalid grade! Enter
a numeric value.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});

searchBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
resultArea.setText("");
String query = searchField.getText().trim();
List<String> students = studentDAO.searchStudent(query);
List<String> grades = gradeDAO.getGrades(query);

if (students.isEmpty() && grades.isEmpty()) {


resultArea.append("No records found.\n");
} else {
for (String s : students) resultArea.append(s + "\n");
for (String g : grades) resultArea.append(g + "\n");
}
}
});

frame.setVisible(true);
}

public static void main(String[] args) {


new GradingSystemGUI();
}
}

Improvements & Fixes:

✔ Added StudentDAO and GradeDAO Implementations


✔ Ensured Student Exists Before Adding a Grade
✔ More Organized GUI with Better Panel Layouts
✔ Fixed Empty Student Search Handling
✔ Improved Error Handling for Invalid Inputs

This should now work correctly. Let me know if you need modifications!

You might also like