Hash Map implementation in Java
This program is showing the concept of a GUI implementation for
student database.
import java.util.HashMap;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
// the GUI for student database management
public class GUI extends JPanel {
private JTextField idField, nameField, majorField;
private JComboBox<String> opBox; /* the operation */
private JButton processButton;
private HashMap<String, Student> students;
public GUI() {
this.students = new HashMap<String, Student>();
// create the fields and add into the GUI
this.idField = new JTextField();
this.nameField = new JTextField();
this.majorField = new JTextField();
this.opBox = new JComboBox<String>(
new String[] {"Insert", "Delete", "Find", "Update"});
this.processButton = new JButton("Process Request");
JLabel[] labels = {
new JLabel("Id:"), new JLabel("Name: "),
new JLabel("Major:"), new JLabel("Choose Selection: ")
};
this.setLayout(new GridLayout(5, 2));
this.add(labels[0]);
this.add(this.idField);
this.add(labels[1]);
this.add(this.nameField);
this.add(labels[2]);
this.add(this.majorField);
this.add(labels[3]);
this.add(this.opBox);
this.add(processButton);
this.setPreferredSize(new Dimension(300, 180));
// add action listener
this.processButton.addActionListener(handler);
}
private ActionListener handler = new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
String op = (String) opBox.getSelectedItem();
// call the corresponding function to handle the operation
if ("Insert".equals(op)) {
insert();
} else if ("Delete".equals(op)) {
delete();
} else if ("Find".equals(op)) {
find();
} else if ("Update".equals(op)) {
update();
}
}
};
/* the four functions to manage student database */
private void insert() {
/* validate the input fields */
String ID = this.idField.getText().trim();
if (ID.length() == 0) {
JOptionPane.showMessageDialog(this, "The ID field is
empty!");
return;
}
if (students.containsKey(ID)) {
JOptionPane.showMessageDialog(this, "Student ID(" + ID +
") already exists!");
return;
}
/* make sure name/major are not empty */
String name = this.nameField.getText().trim();
String major = this.majorField.getText().trim();
if (name.length() == 0) {
JOptionPane.showMessageDialog(this, "The name field is
empty!");
return;
}
if (major.length() == 0) {
JOptionPane.showMessageDialog(this, "The major field is
empty!");
return;
}
/* add into the dababase */
Student student = new Student(name, major);
this.students.put(ID, student);
JOptionPane.showMessageDialog(this,
"This student has been added into the database!");
}
private void delete() {
/* validate the input fields */
String ID = this.idField.getText().trim();
if (ID.length() == 0) {
JOptionPane.showMessageDialog(this, "The ID field is
empty!");
return;
}
if (!students.containsKey(ID)) {
JOptionPane.showMessageDialog(this,
"Student ID(" + ID + ") does not exist!");
return;
}
/* delete from the database */
this.students.remove(ID);
JOptionPane.showMessageDialog(this,
"This student has been deleted from the database!");
}
private void find() {
/* validate the input fields */
String ID = this.idField.getText().trim();
if (ID.length() == 0) {
JOptionPane.showMessageDialog(this, "The ID field is
empty!");
return;
}
if (!students.containsKey(ID)) {
JOptionPane.showMessageDialog(this,
"Student ID(" + ID + ") does not exist!");
return;
}
/* display the student information */
Student student = students.get(ID);
JOptionPane.showMessageDialog(this, student.toString());
}
private void update() {
/* validate the input fields */
String ID = this.idField.getText().trim();
if (ID.length() == 0) {
JOptionPane.showMessageDialog(this, "The ID field is
empty!");
return;
}
if (!students.containsKey(ID)) {
JOptionPane.showMessageDialog(this,
"Student ID(" + ID + ") does not exist!");
return;
}
/* input the credit and grade for the student */
Student student = students.get(ID);
/* letter grade */
JComboBox<String> grades = new JComboBox<String>(
new String[] {"A", "B", "C", "D", "F"});
JOptionPane.showMessageDialog(this,
grades, "Choose grade:", JOptionPane.QUESTION_MESSAGE);
String grade = (String) grades.getSelectedItem();
/* credits */
JComboBox<Integer> credits = new JComboBox<Integer>(
new Integer[] {3, 6});
JOptionPane.showMessageDialog(this,
credits, "Choose credits:", JOptionPane.QUESTION_MESSAGE);
Integer credit = (Integer) credits.getSelectedItem();
/* add to the student */
student.courseCompleted(grade, credit);
JOptionPane.showMessageDialog(this,
"This student has been updated in the database!");
}
// display in a frame
public void display() {
JFrame frame = new JFrame();
frame.setTitle("Project 4");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(this);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new GUI().display();
}
}

Java Assignment Help

  • 2.
    Hash Map implementationin Java This program is showing the concept of a GUI implementation for student database. import java.util.HashMap; import javax.swing.*; import java.awt.event.*; import java.awt.*; // the GUI for student database management public class GUI extends JPanel { private JTextField idField, nameField, majorField; private JComboBox<String> opBox; /* the operation */ private JButton processButton; private HashMap<String, Student> students; public GUI() { this.students = new HashMap<String, Student>(); // create the fields and add into the GUI this.idField = new JTextField(); this.nameField = new JTextField(); this.majorField = new JTextField(); this.opBox = new JComboBox<String>( new String[] {"Insert", "Delete", "Find", "Update"}); this.processButton = new JButton("Process Request"); JLabel[] labels = { new JLabel("Id:"), new JLabel("Name: "), new JLabel("Major:"), new JLabel("Choose Selection: ") }; this.setLayout(new GridLayout(5, 2)); this.add(labels[0]); this.add(this.idField); this.add(labels[1]); this.add(this.nameField); this.add(labels[2]); this.add(this.majorField); this.add(labels[3]); this.add(this.opBox); this.add(processButton); this.setPreferredSize(new Dimension(300, 180)); // add action listener this.processButton.addActionListener(handler); }
  • 3.
    private ActionListener handler= new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { String op = (String) opBox.getSelectedItem(); // call the corresponding function to handle the operation if ("Insert".equals(op)) { insert(); } else if ("Delete".equals(op)) { delete(); } else if ("Find".equals(op)) { find(); } else if ("Update".equals(op)) { update(); } } }; /* the four functions to manage student database */ private void insert() { /* validate the input fields */ String ID = this.idField.getText().trim(); if (ID.length() == 0) { JOptionPane.showMessageDialog(this, "The ID field is empty!"); return; } if (students.containsKey(ID)) { JOptionPane.showMessageDialog(this, "Student ID(" + ID + ") already exists!"); return; } /* make sure name/major are not empty */ String name = this.nameField.getText().trim(); String major = this.majorField.getText().trim(); if (name.length() == 0) { JOptionPane.showMessageDialog(this, "The name field is empty!"); return; } if (major.length() == 0) { JOptionPane.showMessageDialog(this, "The major field is empty!"); return; } /* add into the dababase */ Student student = new Student(name, major); this.students.put(ID, student); JOptionPane.showMessageDialog(this,
  • 4.
    "This student hasbeen added into the database!"); } private void delete() { /* validate the input fields */ String ID = this.idField.getText().trim(); if (ID.length() == 0) { JOptionPane.showMessageDialog(this, "The ID field is empty!"); return; } if (!students.containsKey(ID)) { JOptionPane.showMessageDialog(this, "Student ID(" + ID + ") does not exist!"); return; } /* delete from the database */ this.students.remove(ID); JOptionPane.showMessageDialog(this, "This student has been deleted from the database!"); } private void find() { /* validate the input fields */ String ID = this.idField.getText().trim(); if (ID.length() == 0) { JOptionPane.showMessageDialog(this, "The ID field is empty!"); return; } if (!students.containsKey(ID)) { JOptionPane.showMessageDialog(this, "Student ID(" + ID + ") does not exist!"); return; } /* display the student information */ Student student = students.get(ID); JOptionPane.showMessageDialog(this, student.toString()); } private void update() { /* validate the input fields */ String ID = this.idField.getText().trim(); if (ID.length() == 0) { JOptionPane.showMessageDialog(this, "The ID field is empty!"); return; } if (!students.containsKey(ID)) { JOptionPane.showMessageDialog(this,
  • 5.
    "Student ID(" +ID + ") does not exist!"); return; } /* input the credit and grade for the student */ Student student = students.get(ID); /* letter grade */ JComboBox<String> grades = new JComboBox<String>( new String[] {"A", "B", "C", "D", "F"}); JOptionPane.showMessageDialog(this, grades, "Choose grade:", JOptionPane.QUESTION_MESSAGE); String grade = (String) grades.getSelectedItem(); /* credits */ JComboBox<Integer> credits = new JComboBox<Integer>( new Integer[] {3, 6}); JOptionPane.showMessageDialog(this, credits, "Choose credits:", JOptionPane.QUESTION_MESSAGE); Integer credit = (Integer) credits.getSelectedItem(); /* add to the student */ student.courseCompleted(grade, credit); JOptionPane.showMessageDialog(this, "This student has been updated in the database!"); } // display in a frame public void display() { JFrame frame = new JFrame(); frame.setTitle("Project 4"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(this); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { new GUI().display(); } }