Hello. I have created a program that creates and changes data
Hello. I have created a program that creates and changes data for
students that includes their ID, name, major, and GPA. It is supposed to allow the user to change the data of a student in the database, but I get an error and I’m not sure what’s going on. The code and the directions are below.
/** * File: Project4.java * Author: Lutz, Zechariah * Date: 14 December 2019 * Purpose: */ package project4; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.HashMap; public class Project4 extends JFrame implements ActionListener { private String[] operationChoices = {“Add”, “Delete”, “Update”, “Find”}; private JComboBox operationTypes = new JComboBox(operationChoices); private JButton processButton = new JButton(“Process”); private JTextField nameField = new JTextField(); private JTextField idField = new JTextField(); private JTextField majorField = new JTextField(); private HashMap<String, Student> resultsMap = new HashMap<String, Student>(); public Project4() { super(“Project 4”); setLayout(new GridLayout(0, 2)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); operationTypes.addActionListener(this); add(new JLabel(“ID: “)); add(idField); add(new JLabel(“Name: “)); add(nameField); add(new JLabel(“Major: “)); add(majorField); add(new JLabel(“Choose Selection: “)); add(operationTypes); add(processButton); processButton.addActionListener(this); setBoxes(true, true, true); pack(); setVisible(true); } void setBoxes(boolean id, boolean name, boolean major) { idField.setEditable(id); nameField.setEditable(name); majorField.setEditable(major); if(!id) { idField.setText(“”); } if(!name) { nameField.setText(“”); } if(!major) { majorField.setText(“”); } } public static void main(String[] args) { new Project4(); } @Override public void actionPerformed(ActionEvent e) { int command = operationTypes.getSelectedIndex(); if(e.getSource() == operationTypes) { if (command == 0) { setBoxes(true , true , true); } else { setBoxes(true , false , false); } } else if (e.getSource() == processButton) { String strId = getSafeString(idField.getText()); if (strId.length() == 0) { displayMessage(“Enter ID Field”); return; } if (command == 0) { if (resultsMap.containsKey(strId)) { displayMessage(“Student with this ID Field is already added in database”); return; } String strName = getSafeString(nameField.getText()); String strMajor = getSafeString(majorField.getText()); if (strName.length() == 0) { displayMessage(“Enter Name Field”); return; } if (strMajor.length() == 0) { displayMessage(“Enter Major”); return; } resultsMap.put(strId, new Student(strName, strMajor)); displayMessage(“Student Information Added”); } else { if (!resultsMap.containsKey(strId)) { displayMessage(“Student ID Field Not Found in Database”); return; } if (command == 1) { Student s = resultsMap.get(strId); resultsMap.remove(strId); displayMessage(“Student Deleted: ” + s.toString()); } else if (command == 3) { Student s = resultsMap.get(strId); displayMessage(“Student Found: ” + s.toString()); } else if (command == 2) { String[] choices = {“A” , “B” , “C” , “D” , “E” , “F”}; String grade = (String) JOptionPane.showInputDialog(null , “Choose Grade: ” , “Choose Grade” , JOptionPane.QUESTION_MESSAGE , null , choices , choices[0]); choices = new String[]{“3” , “6”}; int credits = Integer.parseInt(toString()); JOptionPane.showInputDialog(null , “Choose Credits: ” , “Choose Credits” , JOptionPane.QUESTION_MESSAGE , null , choices , choices[0]); Student s = resultsMap.get(strId); s.courseCompleted(grade , credits); displayMessage(“Student Information Updated: ” + s.toString()); } } } } public void displayMessage(String s) { JOptionPane.showMessageDialog(null, s); } public String getSafeString(String s) { if (s == null) { return s; } return s.trim(); } public int getIntUsingPane(String msg) { int num = 0; boolean good = true; do { good = true; try { num = Integer.parseInt(JOptionPane.showInputDialog(msg)); if (num <= 0) { good = false; displayMessage(“Enter Positive Numeric Value”); } } catch (Exception e) { good = false; displayMessage(“Enter Positive Numeric Value”); } } while (!good); return num; } }
/** * File: Student.java * Author: Lutz, Zechariah * Date: 14 December 2019 * Purpose: */ package project4; public class Student { private String studentName; private String major; private int credits; private int qualityPoints; private boolean isCompleted = false; public Student(String studentName, String major) { this.studentName = studentName; this.major = major; credits = 0; qualityPoints = 0; } public void courseCompleted(String courseGrade, int creditHours) { int grade = 0; if (null != courseGrade) switch (courseGrade){ case “A”: grade = 4; break; case “B”: grade = 3; break; case “C”: grade = 2; break; case “D&”: grade = 1; break; } credits += creditHours; qualityPoints += (grade * creditHours); isCompleted = true; } @Override public String toString() { if(!isCompleted){ return “Name: ” + studentName + “, Major: ” + major + “, GPA: 4.0”; } double gpa = (qualityPoints/credits); return “Name: ” + studentName + “, Major: ” + major + “, GPA: ” + gpa; } }