SlideShare a Scribd company logo
Java  Assignment  Help
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();
}
}

More Related Content

PDF
Simplifying JavaScript Projects with ReactJS
PDF
4시간만에 따라해보는 Windows 10 앱 개발 샘플코드
DOCX
Ip project
PPTX
Build your own entity with Drupal
PPTX
Jquery plugin development
PPTX
Drupal 9 training ajax
DOCX
Ip project work test your knowledge
PDF
Building Non-shit APIs with JavaScript
Simplifying JavaScript Projects with ReactJS
4시간만에 따라해보는 Windows 10 앱 개발 샘플코드
Ip project
Build your own entity with Drupal
Jquery plugin development
Drupal 9 training ajax
Ip project work test your knowledge
Building Non-shit APIs with JavaScript

What's hot (20)

PPTX
11. delete record
PDF
Aug 3-2012 - StiltSoft - 10 features for JIRA admin
PPTX
10. view one record
PDF
Your Entity, Your Code
PPTX
Chapter 10.2
PPTX
12. edit record
PPTX
20. CodeIgniter edit images
PDF
Top Ten Reasons to Use EntityFieldQuery in Drupal
PDF
Growing jQuery
PDF
Drupal Entities - Emerging Patterns of Usage
PPTX
KEY
WordCamp Denver 2012 - Custom Meta Boxes
PDF
Delivering a Responsive UI
PDF
Hibernate
PPT
Drupal csu-open atriumname
PDF
Taming forms with React
PDF
Entities in drupal 7
KEY
Zf Zend Db by aida
DOC
Martin Roy Portfolio
11. delete record
Aug 3-2012 - StiltSoft - 10 features for JIRA admin
10. view one record
Your Entity, Your Code
Chapter 10.2
12. edit record
20. CodeIgniter edit images
Top Ten Reasons to Use EntityFieldQuery in Drupal
Growing jQuery
Drupal Entities - Emerging Patterns of Usage
WordCamp Denver 2012 - Custom Meta Boxes
Delivering a Responsive UI
Hibernate
Drupal csu-open atriumname
Taming forms with React
Entities in drupal 7
Zf Zend Db by aida
Martin Roy Portfolio
Ad

Viewers also liked (20)

PPT
Chap06
PDF
Migrating 25K lines of Ant scripting to Gradle
PDF
Java Collections
PDF
Java collections-interview-questions
PDF
디자인연구소 건립 현상공모 설계설명서
PDF
Kickoff Connected Car Challenge
PPTX
Administrativo Robótica- PRONIE-MEP-FOD Joseph Masis Valverde
PPT
Treball cartes d_hivern_power2
TXT
format.txt.txt
PDF
Questions you should ask when building your Savings by Sarang Ahuja
PPTX
Geotermikus energia - Toszkánában és Magyarországon
PPTX
PDF
Campbell River Community Meeting June 3, 2013
PDF
Linked hashmap (java platform se 8 )
PPTX
Google Strategic Opportunities in Vietnam
PDF
Thermal method in Well logging and Geothermal Energy
PPTX
Geothermal energy finalby SP
PDF
Geothermal Energy - Electricity Generation
PDF
Hash map (java platform se 8 )
DOCX
Core Java Equals and hash code
Chap06
Migrating 25K lines of Ant scripting to Gradle
Java Collections
Java collections-interview-questions
디자인연구소 건립 현상공모 설계설명서
Kickoff Connected Car Challenge
Administrativo Robótica- PRONIE-MEP-FOD Joseph Masis Valverde
Treball cartes d_hivern_power2
format.txt.txt
Questions you should ask when building your Savings by Sarang Ahuja
Geotermikus energia - Toszkánában és Magyarországon
Campbell River Community Meeting June 3, 2013
Linked hashmap (java platform se 8 )
Google Strategic Opportunities in Vietnam
Thermal method in Well logging and Geothermal Energy
Geothermal energy finalby SP
Geothermal Energy - Electricity Generation
Hash map (java platform se 8 )
Core Java Equals and hash code
Ad

Similar to Java Assignment Help (20)

PDF
Create a Code that will add an Add, Edi, and Delete button to the GU.pdf
DOCX
Net Beans Codes for Student Portal
PDF
public class Person { private String name; private int age;.pdf
PDF
Hello. Im currently working on the last section to my assignment a.pdf
PPTX
College management system.pptx
PDF
29. Code an application program that keeps track of student informat.pdf
PDF
Header #include -string- #include -vector- #include -iostream- using.pdf
PDF
In Java- Create a Graduate class derived from Student- A graduate has.pdf
PDF
Grain final border one
DOCX
DAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docx
DOCX
Create methods to_insert
PDF
DOCX
culadora cientifica en java
PDF
Hafitz_Rizki 201343500823 JMenuBar_JavaMsAccess_JavaGrade
PPTX
jQuery Data Manipulate API - A source code dissecting journey
DOCX
OOP Lab Report.docx
PDF
@author public class Person{   String sname, .pdf
PDF
Program-a. library is importedimport java.awt.; import j.pdf
PPTX
Jsp presentation
Create a Code that will add an Add, Edi, and Delete button to the GU.pdf
Net Beans Codes for Student Portal
public class Person { private String name; private int age;.pdf
Hello. Im currently working on the last section to my assignment a.pdf
College management system.pptx
29. Code an application program that keeps track of student informat.pdf
Header #include -string- #include -vector- #include -iostream- using.pdf
In Java- Create a Graduate class derived from Student- A graduate has.pdf
Grain final border one
DAOFactory.javaDAOFactory.javapublicclassDAOFactory{ this .docx
Create methods to_insert
culadora cientifica en java
Hafitz_Rizki 201343500823 JMenuBar_JavaMsAccess_JavaGrade
jQuery Data Manipulate API - A source code dissecting journey
OOP Lab Report.docx
@author public class Person{   String sname, .pdf
Program-a. library is importedimport java.awt.; import j.pdf
Jsp presentation

More from Programming Homework Help (8)

PDF
C# Assignmet Help
PDF
C# Assignmet Help
PDF
Family tree in java
PDF
Binary tree in java
PDF
Bank account in java
PDF
Mouse and Cat Game In C++
PDF
Word games in c
PDF
Card Games in C++
C# Assignmet Help
C# Assignmet Help
Family tree in java
Binary tree in java
Bank account in java
Mouse and Cat Game In C++
Word games in c
Card Games in C++

Recently uploaded (20)

PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
GDM (1) (1).pptx small presentation for students
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Classroom Observation Tools for Teachers
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Cell Types and Its function , kingdom of life
PPTX
Institutional Correction lecture only . . .
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
01-Introduction-to-Information-Management.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
GDM (1) (1).pptx small presentation for students
TR - Agricultural Crops Production NC III.pdf
Classroom Observation Tools for Teachers
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Cell Types and Its function , kingdom of life
Institutional Correction lecture only . . .
102 student loan defaulters named and shamed – Is someone you know on the list?
Microbial disease of the cardiovascular and lymphatic systems
Renaissance Architecture: A Journey from Faith to Humanism
01-Introduction-to-Information-Management.pdf
Pre independence Education in Inndia.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Insiders guide to clinical Medicine.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Anesthesia in Laparoscopic Surgery in India

Java Assignment Help

  • 2. 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); }
  • 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 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,
  • 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(); } }