Write a java program to manage a student database. The interface to the program
ID: 3548299 • Letter: W
Question
Write a java program to manage a student database. The interface to the program should be a GUI that looks similar to the following:
A combo box should allow the user to select one of the three database actions shown. The database should be implemented as a HashMap, with the ID field as the key and a student record consisting of a name and GPA as the value. The operation should be performed when the user clicks the Process Request button. If the user attempts to insert a key that is already in the database an error message should be displayed using a JOptionPane message dialog box. If the user attempts to delete or find a record that is not in the database, a message should also be displayed.
This program must consist of two classes, one for handling the database interactions and a second class that defines the student record.
Write a java program to manage a student database. The interface to the program should be a GUI that looks similar to the following: A combo box should allow the user to select one of the three database actions shown. The database should be implemented as a HashMap, with the ID field as the key and a student record consisting of a name and GPA as the value. The operation should be performed when the user clicks the Process Request button. If the user attempts to insert a key that is already in the database an error message should be displayed using a J Option Pane message dialog box. If the user attempts to delete or find a record that is not in the database, a message should also be displayed. This program must consist of two classes, one for handling the database interactions and a second class that defines the student record.Explanation / Answer
//StudentDatabase.javaimport java.util.HashMap;public class StudentDatabase { private final HashMap db; public StudentDatabase() { this.db = new HashMap(); } public boolean add(int id, String name, double gpa) { if (db.containsKey(id)) return false; db.put(id, new StudentRecord(name, gpa)); return true; } public boolean remove(int id) { if (!db.containsKey(id)) return false; db.remove(id); return true; } public StudentRecord get(int id) { return db.get(id); }}//StudentRecord.javapublic class StudentRecord { private String name; private double gpa; public StudentRecord(String name, double gpa) { this.name = name; this.gpa = gpa; } public String getName() { return name; } public double getGpa() { return gpa; } public void setName(String newName) { name = newName; } public void setGpa(double newGpa) { gpa = newGpa; }}//StudentDbGui.javaimport javax.swing.*;import java.awt.GridLayout;import java.awt.event.*;public class StudentDbGui { private StudentDatabase db; private JTextField idField; private JTextField nameField; private JTextField gpaField; private JComboBox selectionBox; private JButton processBtn; private JFrame frame; private class ProcessRequest implements ActionListener { public void actionPerformed(ActionEvent event) { int id = Integer.parseInt(idField.getText()); String selection = (String) selectionBox.getSelectedItem(); if (selection == "Insert") insert(id); else if (selection == "Delete") remove(id); else if (selection == "Find") find(id); } private void insert(int id) { String name = nameField.getText(); double gpa = Double.parseDouble(gpaField.getText()); if (!db.add(id, name, gpa)) { String msg = "Student with id "+id+" is already in database"; JOptionPane.showMessageDialog(frame, msg, "Insert Error", JOptionPane.ERROR_MESSAGE); } } private void remove(int id) { if (!db.remove(id)) { String msg = "Database doesn't contain any student with id "+id; JOptionPane.showMessageDialog(frame, msg, "Delete Error", JOptionPane.ERROR_MESSAGE); } } private void find(int id) { StudentRecord rec = db.get(id); if (rec == null) { String msg = "Cannot find any student with id " + id; JOptionPane.showMessageDialog(frame, msg, "Find Error", JOptionPane.ERROR_MESSAGE); } else { nameField.setText(rec.getName()); gpaField.setText(""+rec.getGpa()); } } } public StudentDbGui(String title) { db = new StudentDatabase(); idField = new JTextField(); nameField = new JTextField(); gpaField = new JTextField(); selectionBox = new JComboBox(); selectionBox.addItem("Insert"); selectionBox.addItem("Delete"); selectionBox.addItem("Find"); processBtn = new JButton("Process Request"); processBtn.addActionListener(new ProcessRequest()); frame = new JFrame(title); frame.setLayout(new GridLayout(6, 2, 16, 8)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new JLabel("Id:")); frame.add(idField); frame.add(new JLabel("Name:")); frame.add(nameField); frame.add(new JLabel("GPA:")); frame.add(gpaField); frame.add(new JLabel("Choose Selection:")); frame.add(selectionBox); frame.add(processBtn); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { new StudentDbGui("Project 4"); }}Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.