100% found this document useful (1 vote)
61 views

CS1102 - Unit 7 Programming

Uploaded by

marlenalomeli
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
100% found this document useful (1 vote)
61 views

CS1102 - Unit 7 Programming

Uploaded by

marlenalomeli
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/ 13

Programming Unit 7

First, this is the code in Java.


//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* A GUI application for managing student records, course enrollment, and
grades.
*/
public class StudentManagementSystemGUI extends JFrame {
private ArrayList<Student> students; // List of students
private JComboBox<String> courseDropdown; // Dropdown menu for selecting
courses
private JTextArea studentDetailsTextArea; // Text area to display student
details
private Map<Student, Map<String, String>> studentCourseGrades; // Map to
store studentcourse-grade associations
/**
* Constructs the Student Management System GUI.
*/
public StudentManagementSystemGUI() {
// Initialize data structures
students = new ArrayList<>();
studentCourseGrades = new HashMap<>();
// Set up the main frame
setTitle("Student Management System");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// Create and add components
JPanel topPanel = new JPanel();
JLabel courseLabel = new JLabel("Select Course:");
courseDropdown = new JComboBox<>(new String[]{"Math", "Science",
"History"}); //
//Example courses;
JButton enrollButton = new JButton("Enroll Student");
JButton gradeButton = new JButton("Assign Grade");
topPanel.add(courseLabel);
topPanel.add(courseDropdown);
topPanel.add(enrollButton);
topPanel.add(gradeButton);
add(topPanel, BorderLayout.NORTH);
JPanel centerPanel = new JPanel(new GridLayout(1, 2));
studentDetailsTextArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(studentDetailsTextArea);
centerPanel.add(scrollPane);
add(centerPanel, BorderLayout.CENTER);
// Add action listeners
enrollButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
enrollStudent();
}
});
gradeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
assignGrade();
}
});
}
/**
* Enrolls a student in the selected course.
*/
private void enrollStudent() {
String selectedCourse = (String) courseDropdown.getSelectedItem();
if (selectedCourse != null) {
// Display input dialogs to enter student information
String name = JOptionPane.showInputDialog("Enter student name:");
String ageStr = JOptionPane.showInputDialog("Enter student age:");
try {
int age = Integer.parseInt(ageStr);
// Create a new student and add to the list
Student student = new Student(name, age);
students.add(student);
// Update display
updateStudentDetailsTextArea();
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Invalid age. Please enter
a valid integer.");
}
} else {
JOptionPane.showMessageDialog(this, "Please select a course
first.");
}
}
/**
* Assigns a grade to the selected student for the selected course.
*/
private void assignGrade() {
// Check if there are students available
if (students.isEmpty()) {
JOptionPane.showMessageDialog(this, "No students enrolled yet.");
return;
}

// Display a dialog to select a student


String[] studentNames = new String[students.size()];
for (int i = 0; i < students.size(); i++) {
studentNames[i] = students.get(i).getName();
}
String selectedStudentName = (String) JOptionPane.showInputDialog(
this,
"Select a student:",
"Assign Grade",
JOptionPane.PLAIN_MESSAGE,
null,
studentNames,
studentNames[0]);

// Find the selected student


Student selectedStudent = null;
for (Student student : students) {
if (student.getName().equals(selectedStudentName)) {
selectedStudent = student;
break;
}
}

// Check if a student is selected


if (selectedStudent != null) {
String selectedCourse = (String) courseDropdown.getSelectedItem();
if (selectedCourse != null) {
// Display input dialog to enter grade
String grade = JOptionPane.showInputDialog("Assign grade for "
+
selectedStudent.getName() + " in " + selectedCourse +
":");

// Update student-course-grade association


Map<String, String> courseGrades =
studentCourseGrades.getOrDefault(selectedStudent, new
HashMap<>());
courseGrades.put(selectedCourse, grade);
studentCourseGrades.put(selectedStudent, courseGrades);

// Update display
updateStudentDetailsTextArea();
} else {
JOptionPane.showMessageDialog(this, "Please select a course
first.");
}
} else {
JOptionPane.showMessageDialog(this, "Please select a student.");
}
}

/**
* Updates the student details text area with the current list of
students.
*/
private void updateStudentDetailsTextArea() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
sb.append("Student ").append(i + 1).append(":
").append(student.getName()).append(", Age:
").append(student.getAge()).append("\n");
// Display enrolled courses and grades for the student
Map<String, String> courseGrades =
studentCourseGrades.getOrDefault(student, new
HashMap<>());
for (Map.Entry<String, String> entry : courseGrades.entrySet()) {
StringBuilder append = sb.append("\tCourse:
").append(entry.getKey()).append(",
Grade:").append(entry.getValue()).append("\n");
}
}
studentDetailsTextArea.setText(sb.toString());
}
/**
* Represents a Student with a name and age.
*/
private class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
/**
* Main method to start the application.
*
* @param args command-line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new StudentManagementSystemGUI().setVisible(true);
}
});
}
}

This is the end result, after enrolling several students in 3 different courses, and assigning them
each a grade.
Assign Grade menu:

Input for Student Name upon selecting ‘Enroll Student”


And in entering no integer or an invalid integer:
And my code snippets:

You might also like