This is a simple program that I\'m struggling with. Java is not my forte... It\'
ID: 3770748 • Letter: T
Question
This is a simple program that I'm struggling with. Java is not my forte... It's way too verbose for my liking. Anyways... The criteria for the prog is as follows:
No issues with the GUI, I can do this on my own, but for a reference see 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 JOptionPanewindow 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. The first class should define the GUI and handle the database interactions. It should be hand-coded and not generated by a GUI generator. 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 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.
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.
The third method should override toString and return a labeled string containing the student name, major and GPA.
Be sure that all instance and class variables are declared as private. Also any exceptions thrown by nonnumeric inputs should be properly handled. Finally when a student has not yet completed any course, the GPA should be displayed as 4.0.
Explanation / Answer
Program:
/**StdDBGUI.java**/
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import javax.swing.*;//JButton;JComboBox;JFrame;JLabel;JPanel;JTextField;
public class StdDBGUI extends JFrame implements ActionListener
{
JLabel id,name,major,chooseselection;
JTextField idBox,nameBox,majorBox;
JComboBox selectionBox;
JButton process;
JPanel jp;
HashMap<Integer,Student> sdtDB;
@SuppressWarnings("unchecked")
StdDBGUI()
{
sdtDB=new HashMap<Integer,Student>();
jp=new JPanel(new GridLayout(6,2));//
id=new JLabel("Id:");
jp.add(id);
idBox=new JTextField(20);
jp.add(idBox);
name=new JLabel("Name:");
jp.add(name);
nameBox=new JTextField(20);
jp.add(nameBox);
major=new JLabel("Major:");
jp.add(major);
majorBox=new JTextField(20);
jp.add(majorBox);
chooseselection=new JLabel("Choose Selection:");
jp.add(chooseselection);
String course[] = {"Insert","Delete","Find","Update"};
selectionBox=new JComboBox(course);
//selectionBox.addActionListener(this);
jp.add(selectionBox);
process=new JButton("Process Request");
process.addActionListener(this);
jp.add(process);
add(jp);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Project 4");
}
public static void main(String arg[])
{
StdDBGUI sdg=new StdDBGUI();
sdg.setVisible(true);
sdg.setSize(350, 200);
}
public void actionPerformed(ActionEvent e)
{
String selection=(String) selectionBox.getSelectedItem();
try
{
if(selection.equals("Insert"))
{
Student s=new Student(nameBox.getText(),majorBox.getText());
if(!Exist(Integer.parseInt(idBox.getText())))
{
sdtDB.put(Integer.parseInt(idBox.getText()),s);
JOptionPane.showMessageDialog(null, "Successfully inserted");
idBox.setText("");
nameBox.setText("");
majorBox.setText("");
}
else
{
JOptionPane.showMessageDialog(null, "Sorry! record is already exist in the data base");
idBox.setText("");
nameBox.setText("");
majorBox.setText("");
}
}
else if(selection.equals("Delete"))
{
if(Exist(Integer.parseInt(idBox.getText())))
{
sdtDB.remove(Integer.parseInt(idBox.getText()));
idBox.setText("");
nameBox.setText("");
majorBox.setText("");
}
else
{
JOptionPane.showMessageDialog(null, "Sorry! record is not exist in the data base");
idBox.setText("");
nameBox.setText("");
majorBox.setText("");
}
}
else if(selection.equals("Find"))
{
if(Exist(Integer.parseInt(idBox.getText())))
{
Student obj=sdtDB.get(Integer.parseInt(idBox.getText()));
JOptionPane.showMessageDialog(null, obj.toString(),"Student Info",JOptionPane.INFORMATION_MESSAGE);
idBox.setText("");
nameBox.setText("");
majorBox.setText("");
}
else
{
JOptionPane.showMessageDialog(null, "Sorry! record is not exist in the data base");
idBox.setText("");
nameBox.setText("");
majorBox.setText("");
}
}
else if(selection.equals("Update"))
{
if(Exist(Integer.parseInt(idBox.getText())))
{
Student obj=sdtDB.get(Integer.parseInt(idBox.getText()));
Object[] grades = {"A", "B", "C","D","F"};
String g = (String)JOptionPane.showInputDialog(null,"Chose grade:","Grade Dialog",JOptionPane.PLAIN_MESSAGE,null,grades,"A");
Object[] credits = {"3", "6"};
String c = (String)JOptionPane.showInputDialog(null,"Chose grade:","Credit Dialog",JOptionPane.PLAIN_MESSAGE,null,credits,"A");
int grd=0;
if(g.equals("A")){ grd=4; }
else if(g.equals("B")){grd=3; }
else if(g.equals("C")){ grd=2;}
else if(g.equals("D")){ grd=1;}
else if(g.equals("F")){ grd=0;}
obj.courseCompleted(grd*6,Integer.parseInt(c));
JOptionPane.showMessageDialog(null, obj.toString(),"Student Info",JOptionPane.INFORMATION_MESSAGE);
idBox.setText("");
nameBox.setText("");
majorBox.setText("");
}
else
{
JOptionPane.showMessageDialog(null, "Sorry! record is not exist in the data base");
idBox.setText("");
nameBox.setText("");
majorBox.setText("");
}
}
}
catch(Exception e1)
{
JOptionPane.showMessageDialog(null, e1.getMessage(),"Input error",JOptionPane.ERROR_MESSAGE);
}
}
//to find a record
public boolean Exist(int id)
{
Set keys = sdtDB.keySet();
Iterator itr = (Iterator) keys.iterator();
int key;
Student record=null;
while( itr.hasNext() )
{
key = (int) itr.next();
if(key==id)
record = (Student)sdtDB.get(key);
}
if(record==null)
{
return false;
}
else
{
return true;
}
}
}
/**Student.java**/
public class Student
{
private String sname, smajor;
private int totalCredits,qualityPoints;
private double GPA;
Student(String name,String major)
{
sname=name;
smajor=major;
totalCredits=0;
qualityPoints=0;
GPA= 4.0;
}
//invoked when update request is made
public void courseCompleted(int grade,int hours)
{
totalCredits=grade;
qualityPoints=hours;//grade * hours
}
public String toString()
{
if(totalCredits==5)
return " "+sname+" "+smajor+" "+4.0;
else
return " Name: "+sname+" Major : "+smajor+" GPA: "+(totalCredits/qualityPoints);
}
}
Code to copy:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.