This programming project involves writing a program to manage a student database
ID: 3856777 • Letter: T
Question
This programming project involves writing a 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 four 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 major 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, find or update a record that is not in the database, a message should also be displayed. After each successful operation is completed a JOptionPane window should be displayed confirming the success. In the case of a successful Find request, a window should pop up containing the student's ID, name, major and current GPA. When the user selects the Update request, the following JOptionPane windows should be displayed to gather information about a course that has just been completed:
This program must consist of two classes.
1. The first class should define the GUI and handle the database interactions.
2. The second class named Student, should define the student record. It must have instance variables for the student name, major and two variables that are used to compute the GPA. A variable that contains the total number of credits completed and a second variable that contains the total quality points, which are the numeric value of the grade received in a course times the number of credit hours. It should not contain the student ID. The class should have the following three methods:
a. A constructor that is used when new student records are created. It should accept the name and major as parameters and initialize the fields that are used to compute the GPA to zero.
b. The second method courseCompleted should accept the course grade and credit hours and update the variables used to compute the GPA. It will be called when an Update request is made.
c. The third method should override toString and return a labeled string containing the student name, major and GPA.
Finally when a student has not yet completed any course, the GPA should be displayed as 4.0.
Test cases should be supplied in the form of table with columns indicating the input values, expected output, actual output and if the test case passed or failed. This table should contain 4 columns with appropriate labels and a row for each test case. Note that the actual output should be the actual results you receive when running your program and applying the input for the test record. Be sure to select enough different scenarios to completely test the program.
Project 4- ld: Name: Major: Choose Selection: Insert Insert Delete Find Update Process RequestExplanation / Answer
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
class Frame1 extends Frame implements ActionListener
{
String msg="";
Button btnNew,btnSubmit,btnView;
Label lblName,lblAge,lblAddr,lblGender,lblQua;
TextField txtName,txtAge;
TextArea txtAddr,txtAns;
CheckboxGroup ChkGrp;
Checkbox chkMale,chkFemale;
Checkbox chkMca,chkBca,chkBba,chkMba;
Frame1(String name)
{
super(name);
setLayout(new GridLayout(3,2));
lblName = new Label("Name: ");
lblAge = new Label("Age: ");
lblAddr = new Label("Address : ");
lblGender = new Label("Gender: ");
lblQua = new Label("Qualification: ");
txtName = new TextField(20);
txtAge = new TextField(20);
txtAddr = new TextArea();
ChkGrp = new CheckboxGroup();
chkMale = new Checkbox("Male",ChkGrp,false);
chkFemale = new Checkbox("Female",ChkGrp,false);
chkMca = new Checkbox("MCA");
chkBca = new Checkbox("BCA");
chkMba = new Checkbox("MBA");
chkBba = new Checkbox("BBA");
btnNew = new Button("NEW");
btnSubmit = new Button("SUBMIT");
btnView = new Button("VIEW");
btnNew.addActionListener(this);
btnSubmit.addActionListener(this);
btnView.addActionListener(this);
add(lblName);
add(txtName);
add(lblAge);
add(txtAge);
add(lblAddr);
add(txtAddr);
add(lblGender);
add(chkMale);
add(chkFemale);
add(lblQua);
add(chkBca);
add(chkBba);
add(chkMca);
add(chkMba);
add(btnNew);
add(btnSubmit);
add(btnView);
txtAns = new TextArea();
add(txtAns);
}
publicvoid actionPerformed(ActionEvent ae)
{
String s="";
boolean b;
FileInputStream Fin;
DataInputStream dis;
FileOutputStream Fout;
DataOutputStream dos;
try
{
Fout = new FileOutputStream("Biodata.txt",true);
dos = new DataOutputStream(Fout);
String str = ae.getActionCommand();
if(str.equals("SUBMIT"))
{
s=txtName.getText().trim();
dos.writeUTF(s);
dos.writeInt(Integer.parseInt(txtAge.getText()));
s=txtAddr.getText();
dos.writeUTF(s);
if(chkMale.getState())
dos.writeUTF("Male ");
if(chkFemale.getState())
dos.writeUTF("Female ");
s="";
if(chkMca.getState())
s="MCA ";
if(chkBca.getState())
s+="BCA ";
if(chkBba.getState())
s+="BBA ";
if(chkMba.getState())
s+="MBA ";
s+="!";
dos.writeUTF(s);
Fout.close();
}
if(str.equals("VIEW"))
{
String tmp,name,addr,gender,qual;
int age;
Fin = new FileInputStream("Biodata.txt");
dis = new DataInputStream(Fin);
int i=0,j;
while(Fin.available()>0)
{
name = dis.readUTF();
age = dis.readInt();
addr = dis.readUTF();
gender = dis.readUTF();
qual = dis.readUTF();
if(name.equals(txtName.getText().trim()))
{
txtAge.setText(age+"");
txtAddr.setText(addr);
if(gender.equals("Male "))
chkMale.setState(true);
else
chkFemale.setState(true);
while(qual.charAt(i)!='!')
{
j=qual.indexOf(' ');
tmp = qual.substring(i,j);
if(tmp.equals("MCA"))
chkMca.setState(true);
if(tmp.equals("BCA"))
chkBca.setState(true);
if(tmp.equals("BBA"))
chkBba.setState(true);
if(tmp.equals("MBA"))
chkMba.setState(true);
i=j+1;
}
break;
}
}
Fin.close();
}
if(str.equals("NEW"))
{
txtName.setText("");
txtAge.setText("");
txtAddr.setText("");
chkMale.setState(false);
chkFemale.setState(false);
chkMca.setState(false);
chkBca.setState(false);
chkBba.setState(false);
chkMba.setState(false);
}
}
catch(Exception e)
{
System.out.println("The Exception Is : " +e);
}
}
}
class Bio2
{
publicstaticvoid main(String args[])
{
try{
Frame1 F = new Frame1("Biodata");
F.setSize(400,400);
F.show();
}catch(Exception e)
{
System.out.println(e);
}
}
}
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
public class MainGUI extends JFrame {
String[] columnNames = {"GENDER", "NAME", "COURSE", "YEAR", "ID"};
Object[][] data = new Object[25][5];
// table
JTable table = new JTable(data, columnNames) {
public boolean isCellEditable(int row, int column) {
return false; // returns false, cannot be edited
}
};
// frames
JFrame frame, frame1;
// panels
JPanel buttonPanel, buttonPanel2, tablePanel, addPanel, editPanel;
// labels
JLabel labelGender, labelName, labelCourse, labelYear, labelID;
// text fields
JTextField txtGender, txtName, txtCourse, txtYear, txtID;
// buttons
JButton btnAdd, btnEdit, btnDelete, btnSort, btnSave, btnAddInput, btnCancel;
// additionals
int keyCode, rowIndex, rowNumber, noOfStudents;
// button handler
ButtonHandler bh = new ButtonHandler();
public MainGUI() {
// setting/modifying table components
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getSelectionModel().addListSelectionListener(new RowListener());
table.getColumnModel().getColumn(1).setPreferredWidth(200);
table.getColumnModel().getColumn(3).setPreferredWidth(50);
table.getTableHeader().setResizingAllowed(false);
table.getTableHeader().setReorderingAllowed(false);
JScrollPane scrollPane = new JScrollPane(table);
// main buttons
btnAdd = new JButton("ADD");
btnAdd.addActionListener(bh);
btnEdit = new JButton("EDIT");
btnEdit.addActionListener(bh);
btnEdit.setEnabled(false); // disables the component
btnDelete = new JButton("DELETE");
btnDelete.addActionListener(bh);
btnDelete.setEnabled(false); // disables the component
btnSort = new JButton("SORT");
btnSort.addActionListener(bh);
btnSave = new JButton("SAVE");
btnSave.addActionListener(bh);
// with button Listeners
// sub buttons
btnAddInput = new JButton("Add");
btnAddInput.addActionListener(bh);
btnCancel = new JButton("Cancel");
btnCancel.addActionListener(bh);
// set label names
labelGender = new JLabel("GENDER");
labelName = new JLabel("NAME");
labelCourse = new JLabel("COURSE");
labelYear = new JLabel("YEAR");
labelID = new JLabel("ID");
// set text fields width
txtGender = new JTextField(20);
txtName = new JTextField(20);
txtCourse = new JTextField(20);
txtYear = new JTextField(20);
txtYear.setDocument(new JTextFieldLimit(1)); // limits the length of
// input: max of 1
txtYear.addKeyListener(keyListener); // accepts only numerals
txtID = new JTextField(20);
txtID.setDocument(new JTextFieldLimit(7)); // limits the length of input:
// max of 7
txtID.addKeyListener(keyListener); // accepts only numerals
// main frame
// panel for the table
tablePanel = new JPanel();
tablePanel.setLayout(new BoxLayout(tablePanel, BoxLayout.PAGE_AXIS));
tablePanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 10));
tablePanel.add(table.getTableHeader());
tablePanel.add(table);
// panel for the main buttons
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// positions the main buttons
c.gridx = 0;
c.gridy = 0;
c.ipady = 20;
c.insets = new Insets(10, 10, 10, 10);
c.fill = GridBagConstraints.HORIZONTAL;
buttonPanel.add(btnAdd, c);
c.gridx = 0;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20;
c.insets = new Insets(10, 10, 10, 10);
buttonPanel.add(btnEdit, c);
c.gridx = 0;
c.gridy = 2;
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20;
c.insets = new Insets(10, 10, 10, 10);
buttonPanel.add(btnDelete, c);
c.gridx = 0;
c.gridy = 3;
c.ipady = 20;
c.insets = new Insets(10, 10, 10, 10);
c.fill = GridBagConstraints.HORIZONTAL;
buttonPanel.add(btnSort, c);
c.gridx = 0;
c.gridy = 4;
c.ipady = 20;
c.insets = new Insets(10, 10, 10, 10);
c.fill = GridBagConstraints.HORIZONTAL;
buttonPanel.add(btnSave, c);
frame = new JFrame("Student Database");
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.add(tablePanel, BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.EAST);
frame.pack();
// ADD frame
// panel for adding
addPanel = new JPanel();
addPanel.setLayout(new GridBagLayout());
// positions the components for adding
// labels
c.insets = new Insets(1, 0, 1, 1);
c.gridx = 0;
c.gridy = 0;
addPanel.add(labelID, c);
c.gridy = 1;
addPanel.add(labelName, c);
c.gridy = 2;
addPanel.add(labelCourse, c);
c.gridy = 3;
addPanel.add(labelYear, c);
c.gridy = 4;
addPanel.add(labelGender, c);
// text fields
c.gridx = 1;
c.gridy = 0;
c.ipady = 1;
addPanel.add(txtID, c);
c.gridy = 1;
c.ipady = 1;
addPanel.add(txtName, c);
c.gridy = 2;
c.ipady = 1;
addPanel.add(txtCourse, c);
c.gridy = 3;
c.ipady = 1;
addPanel.add(txtYear, c);
c.gridy = 4;
c.ipady = 1;
addPanel.add(txtGender, c);
// panel for other necessary buttons
buttonPanel2 = new JPanel();
buttonPanel2.setLayout(new GridLayout(1, 1));
buttonPanel2.add(btnAddInput);
buttonPanel2.add(btnCancel);
frame1 = new JFrame("Student Database");
frame1.setVisible(false);
frame1.setResizable(false);
frame1.setDefaultCloseOperation(HIDE_ON_CLOSE);
frame1.add(addPanel, BorderLayout.CENTER);
frame1.add(buttonPanel2, BorderLayout.PAGE_END);
frame1.pack();
}// end
KeyListener keyListener = new KeyListener() {
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
keyCode = e.getKeyCode();
if (!(keyCode >= 48 && keyCode <= 57) && !(keyCode >= 96 && keyCode <= 105)
&& !(keyCode >= 37 && keyCode <= 40) && !(keyCode == 127 || keyCode == 8)) {
txtID.setEditable(false);
}
}
public void keyReleased(KeyEvent e) {
txtID.setEditable(true);
}
};
class RowListener implements ListSelectionListener {
public void valueChanged(ListSelectionEvent event) {
if (event.getValueIsAdjusting()) {
rowIndex = table.getSelectedRow();
if (data[rowIndex][0] == null || data[rowIndex][0] == "") {
btnEdit.setEnabled(false);
btnDelete.setEnabled(false);
} else {
btnEdit.setEnabled(true);
btnDelete.setEnabled(true);
}
}
}
}// end
class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
if (e.getActionCommand().equals("ADD")) {
// text fields for Student input data
txtGender.setText("");
txtName.setText("");
txtCourse.setText("");
txtYear.setText("");
txtID.setText("");
frame1.setTitle("Add Student data"); // title bar name for add
btnAddInput.setActionCommand("Add2");
btnAddInput.setText("ADD");
frame1.setVisible(true); // sets the visibility of frame1
} else if (e.getActionCommand().equals("EDIT")) {
txtID.setText(data[rowIndex][4] + ""); // will preview the ID
// input during Add
txtName.setText(data[rowIndex][1] + ""); // will preview the Name
// input during Add
txtCourse.setText(data[rowIndex][2] + ""); // will preview the
// Course input during
// Add
txtYear.setText(data[rowIndex][3] + ""); // will preview the Year
// input during Add
txtGender.setText(data[rowIndex][0] + ""); // will preview the
// Gender input during
// Add
txtID.setEditable(false); // forbids the user to edit the entered
// ID number
frame1.setTitle("Edit Student data"); // title bar name for edit
btnAddInput.setActionCommand("Edit2");
btnAddInput.setText("ACCEPT");
frame1.setVisible(true); // sets the visibility of frame1
} else if (e.getActionCommand().equals("DELETE")) {
int confirm = JOptionPane.showConfirmDialog(frame, "ARE YOU SURE?", "CONFIRM",
JOptionPane.YES_NO_OPTION);
if (confirm == 0) {
rowIndex = table.getSelectedRow();
rowNumber = 0;
noOfStudents--;
for (int i = 0; i <= 10; i++) {
if (rowIndex != i && i <= noOfStudents) {
data[rowNumber][4] = data[i][4];
data[rowNumber][1] = data[i][1];
data[rowNumber][2] = data[i][2];
data[rowNumber][3] = data[i][3];
data[rowNumber][0] = data[i][0];
rowNumber++;
} else if (rowIndex != i && i > noOfStudents) {
data[rowNumber][4] = "";
data[rowNumber][1] = "";
data[rowNumber][2] = "";
data[rowNumber][3] = "";
data[rowNumber][0] = "";
rowNumber++;
}
}
if (noOfStudents == 50)
btnAdd.setEnabled(false);
else
btnAdd.setEnabled(true);
if (noOfStudents == 0) {
btnDelete.setEnabled(false);
btnEdit.setEnabled(false);
} else {
btnDelete.setEnabled(true);
btnEdit.setEnabled(true);
}
rowIndex = table.getSelectedRow();
if (data[rowIndex][0] == null || data[rowIndex][0] == "") {
btnEdit.setEnabled(false);
btnDelete.setEnabled(false);
} else {
btnEdit.setEnabled(true);
btnDelete.setEnabled(true);
}
table.updateUI();
}
} else if (e.getActionCommand().equals("Add2")) {
if (txtID.getText().isEmpty() || txtName.getText().isEmpty()
|| txtCourse.getText().isEmpty()// /
|| txtYear.getText().isEmpty() || txtGender.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "PLEASE FILL IN THE BLANKS.", "ERROR!",
JOptionPane.ERROR_MESSAGE);
} else {
int dup = 0;
for (int i = 0; i < 10; i++) {
if (txtID.getText().equals(data[i][0])) {
JOptionPane.showMessageDialog(null, "ID NUMBER ALREADY EXISTS.", "ERROR!",
JOptionPane.ERROR_MESSAGE);
dup++;
break;
}
}
if (dup == 0) {
rowIndex = table.getSelectedRow();
data[noOfStudents][4] = txtID.getText();
data[noOfStudents][1] = txtName.getText();
data[noOfStudents][2] = txtCourse.getText();
data[noOfStudents][3] = txtYear.getText();
data[noOfStudents][0] = txtGender.getText();
table.updateUI();
frame1.dispose();
noOfStudents++;
if (noOfStudents == 50)
btnAdd.setEnabled(false);
else
btnAdd.setEnabled(true);
if (data[rowIndex][0] == null) {
btnEdit.setEnabled(false);
btnDelete.setEnabled(false);
} else {
btnEdit.setEnabled(true);
btnDelete.setEnabled(true);
}
}
}
table.updateUI();
} else if (e.getActionCommand().equals("Edit2")) {
if (txtID.getText().isEmpty() || txtName.getText().isEmpty()
|| txtCourse.getText().isEmpty() || txtYear.getText().isEmpty()
|| txtGender.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "INCOMPLETE INPUT.", "ERROR!",
JOptionPane.ERROR_MESSAGE);
} else {
data[rowIndex][4] = txtGender.getText();
data[rowIndex][1] = txtName.getText();
data[rowIndex][2] = txtCourse.getText();
data[rowIndex][3] = txtYear.getText();
data[rowIndex][0] = txtID.getText();
frame1.dispose();
}
table.updateUI();
} else if (e.getActionCommand().equals("Cancel")) {
frame1.dispose();
}
} catch (Exception error) {
}
}
}// end
class JTextFieldLimit extends PlainDocument {
private int limit;
JTextFieldLimit(int limit) {
super();
this.limit = limit;
}
JTextFieldLimit(int limit, boolean upper) {
super();
this.limit = limit;
}
public void insertString(int offset, String str, AttributeSet attr)
throws BadLocationException {
if (str == null)
return;
if ((getLength() + str.length()) <= limit) {
super.insertString(offset, str, attr);
}
}
}// end
public static void main(String[] args) {
new MainGUI();
}
}
else if( e.getActionCommand().equals( "SAVE" ) ){
try{
FileWriter fw= new FileWriter("output.dat",true);
FileWriter fw2= new FileWriter("log.txt",true);
for (int r=0; r<data.length; r++){
String data1="";
for (int c=0; c<data[r].length; c++){
if(data[r][c] != null){
data1 = data1+ (String)data[r][c]+"-";
}
}
if(data1.trim().length() > 0){
fw.write(data1+System.getProperty("line.separator" ));
fw2.write(data1+System.getProperty("line.separator "));
}
}
fw.flush();
fw2.flush();
fw.close();
fw2.close();
}catch(IOException ioe){
System.out.println(ioe.getMessage());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.