Question
I have a code about student list + major, it's about GUI swing and I'm using netbeans. Please help me fix my code, and if
I have a code about student list + major, it's about GUI swing and I'm using netbeans. Please help me fix my code, and if you feel I have not done the right thing with the required topic, please add and correct me. My topic will be below the code. Please do asap because I am in rush!
Here are my code:
My topic: Created a GUI front-ended, persistent back-ended application.
Try to follow the three-tier design pattern - keep the code related to the back end in it's own classes/package (the model), the frames in their own package (the view), and the code to translate between them in it's own package (the Controller). Pass POJOs (Plain Old Java Object) to the View from the controller. Use the controller classes to pull data from the back end and convert it into Java objects. The model will be opened/closed/updated with methods from the Controller. In other words, the Controller classes should be your go-between to send data from the back-end to the front-end. For the Controller classes, follow the books DAO design pattern.
The design / subject of the program is up to you, but it needs to have at least three screens (one main screen that will spawn others) and provide CRUD functionality - the ability to CREATE new data to add to the back end, READ data from the back end, UPDATE existing values, and DELETE values - for at least two different types of objects.
package controller; import model.StudentDAO; import view.AddStudentGui; import view.DeleteStudentGui; import view.MainGui; import view.UpdateStudentGui; public class MainController f public static void main(String[]args) i private StudentDAO studentDAO; private MainGui mainGui; public MainController(StudentDAO studentDAO) \{ this.studentDAO = studentDAO; this.mainGui = new MainGui (this); this.mainGui.setVisible (b: true); \} public void showAddStudentGui () f new AddStudentGui (mainController: this) . setVisible (b: true); \} public void showUpdateStudentGui () i new UpdateStudentGui(this). SetVisible(true): \} public void showDeleteStudentGui () f new DeletestudentGui(this).setVisible(true): \} public void addStudent(String name, String major) f studentDAO.addStudent (name, major); \} this.mainGui.setVisible (b: true); \} public void showAddStudentGui () \{ new AddStudentGui (maincontroller: this). setVisible (b: true); \} public void showUpdateStudentGui () \{ new UpdateStudentGui(this). setvisible (true); \} public void showDeleteStudentGui () \{ new DeleteStudentGui(this).setVisible (true); \} public void addStudent(String name, String major) \{ studentDAO.addStudent (name, major); \} public void updateStudent(String oldName, String newName, String newMajor) studentDAO. updateStudent (oldName, newName, newMajor); \} public void deleteStudent (String name) \{ studentDAO. deleteStudent (name); \} public String getStudentList() \{ return studentDAO.getStudentListAsString (); \} public void refreshMainGui () \{ mainGui. updateViev(); \} package controller; import model.Student; import model.StudentDAO; import view.MainGui; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import view.NameInputGui; public class StudentController implements ActionListener \{ private StudentDAO studentDAO; private MainGui mainGui; public StudentController(StudentDAO studentDAO, MainGui mainGui) \{ this.studentDAO = studentDAO; this.mainGui = mainGui; mainGui .addGetNameButtonListener (1istener: this); updateview (); \} private void updateview() \{ List students = studentDAO. getStudentList (); mainGui.displayStudents (students); \} public void addStudent(Student student) \{ studentDAO. addStudent (student); updateView (); \} public void updatestudent (Student oldStudent, Student newStudent) \{ studentDAO, updateStudent (oldStudent, newStudent); updateview (); public void updateStudent (Student oldStudent, Student newStudent) \{ studentDAO, updateStudent (oldStudent, newStudent); updateView () ; \} public void deletestudent (Student student) \{ studentDAO. deleteStudent (student); updateview (); \} Qaverride public void actionPerformed(ActionEvent e) \{ if (e.getSource () == mainGui.getGetNameButton ()) \{ openNameInputGui () ; \} \} public void openNameInputGui () \{ NameInputGui nameInputGui = new NameInputGui (mainGui, studentController: th nameInputGui. setVisible (b: true); \} package model; import java.io.Serializable; public class Student implements Serializable \{ private String name; private String major; public Student(String name, String major) \{ this.name = name; this.major = major; \} public String getName() \{ return name; \} public String getMajor() \{ return major; \} Qoverride public String toString() \{ return "Name: " + name + ", Major: " + major; \} \} package model; import java.io.Serializable; import java.util.ArrayList; import java.util.List; public class StudentDAO implements Serializable \{ private List studentList = new ArrayList (); public void addStudent(String name, String major) \{ Student student = new Student (name, major); studentist.add (e: student); \} public void updatestudent (String oldName, String newName, String newMajor) \{ Student oldStudent = findStudentByName (name: oldName); if (oldStudent != null) \{ oldStudent = new Student (name: newName, major: newMajor); \} \} public void deletestudent (String name) \{ Student studentToDelete = findStudentByName (name); if (studentToDelete != null) \{ studentList.remove (o: studentToDelete); \} \} public List getStudentList() \{ return new ArrayList (c: studentList); \} private Student findStudentByName (String name) \{ for (Student student : studentList) \{ if (student.getName () . equals (anobject: name)) \{ return student; private Student findStudentByName (String name) \{ for (Student student : studentList) \{ if (student.getName (). equals (anobject: name)) \{ return student; \} \} return null; \} public String getstudentListAsString() \{ StringBuilder stringBuilder = new StringBuilder(); for (Student student : studentList) \{ stringBuilder.append (str:student.toString ()) . append (str: " ") ; \} return stringBuilder.toString(); \} package view; import controller.MainController; import javax.swing. * ; import java.awt.event.ActionListener; import java.util.List; import model.Student; public class MainGui extends javax.swing.JFrame \{ private JButton addButton; private JButton updateButton; private JButton deleteButton; private JTextArea textArea; private MainController mainController; public MainGui (MainController mainController) \{ this.mainController = mainController; initcomponents (); \} private void initComponents () \{ JPanel mainPanel = new JPanel (); textArea = new JTextArea (rows: 10, colums:30); addButton = new JButton (tert: "Add Student"); updateButton = new JButton (tert: "Update Student"); deleteButton = new JButton(tert: "Delete Student"); mainPanel.setLayout (new BoxLayout (target: mainPanel, aris: BoxLayout. Y_AXIS)) mainPanel.add (new JLabel (tert: "Student List:")) ; mainPanel.add (comp: textArea); mainPanel.add (comp: addButton) ; mainPanel.add (comp: updateButton) ; mainPanel.add (comp: deleteButton); add (comp: mainPanel); setTitle (title: "Student List"); setDefaultCloseOperation (operation: JFrame.EXIT_ON_CLOSE) ; pack(); setLocationRelativeTo (c: null); addButton.addActionListener (e > mainController.showAddStudentGui ()); updateButton.addActionListener (e -> mainController. showUpdateStudentGui ()) ; deleteButton.addActionListener (e -> mainController.showDeleteStudentGui ()); updateView () ; \} public void updateview() \{ textArea.setText (t: "n); textArea.append (str:mainController.getStudentList ()); \} public void displayStudents (List students) \{ textArea.setText ( t : " "); for (Student student : students) \{ textArea, append (str:student.toString()).append (" ); \} \} public void addGetNameButtonListener (ActionListener listener) \{ addButton.addActionListener (1: 1istener); \} public JButton getGetNameButton() \{ return addButton; \} \} package view; import controller.MainController; import javax.swing. *; import java.awt. event. ; public class AddStudentGui extends javax.swing.JFrame \{ private JTextField nameField; private JTextField majorField; private JButton submitButton; private MainController mainController; public AddStudentGui (MainController mainController) \{ this.mainController = mainController; initComponents (); \} private void initComponents () \{ JPanel inputPanel = new JPanel (); nameField = new JTextField(colums:20); majorField = new JTextField(colums:20); submitButton = new JButton (tert: "Submit"); inputPanel.setLayout (new BoxLayout (target: inputPanel, aris: BoxLayout.Y_AXIS)); inputPanel.add (new JLabel(tert: "Name:")); inputPanel.add (comp: nameField); inputPanel.add(new JLabel(tert: "Major:")); inputPanel. add (comp: majorField) ; inputPanel.add (comp: submitButton); add (comp: inputPanel); setTitle (title: "Add Student"); setTitle (title: "Add Student"); setDefaultCloseoperation (operation: JFrame.DISPOSE_ON_CLOSE) ; pack(); setLocationRelativeTo(c: null) ; submitButton.addActionListener(e -> submitAddStudentForm()) ; \} private void submitAddStudentForm() \{ String name = nameField.getText () ; String major = majorField.getText (); mainController.addStudent (name, major); mainController.refreshMainGui () ; dispose () ; \} package view; import controller.MainController; import javax.swing. ; import java.awt. event. ActionEvent: import java.awt.event.ActionListener: public class UpdateStudentGui extends javax.swing.JFrame \{ private JTextField oldNameField; private JTextField newNameField;| private JTextField newMajorField; private JButton submitButton; private MainController mainController; public UpdateStudentGui (MainController mainController) \{ this.mainController = mainController; initcomponents () ; \} private void initcomponents () \{ JPanel inputPanel = new JPanel(); oldNameField = new JTextField(colums:20); newNameField = new JTextField (colums:20); newMajorField = new JTextField (colums:20); submitButton = new JButton (tert: "Submit"); inputPanel.setLayout (new BoxLayout (target: inputPanel, aris: BoxLayout. Y_AXIS)); inputPanel.add (new JLabel (tert: "Old Name:")); inputPanel.add (comp: oldNameField); inputPanel.add (new JLabel (text: "New Name:")); inputPanel.add (comp: newNameField) ; inputPanel.add (new JLabel (tert: "New Major:")); inputPanel.add (comp: newMajorField) ; inputPanel.add (new JLabel (tert: "Old Name:")) ; inputPanel.add (comp: oldNameField) ; inputPanel.add (new JLabel (tert: "New Name:")); inputPanel.add (comp: newNameField) ; inputPanel.add (new JLabel (tert: "New Major:")); inputPanel.add (comp: newMajorField) ; inputPanel.add (comp: submitButton) ; add (comp: inputPanel); setTitle (title: "Update Student"); setDefaultCloseOperation (operation: JFrame.DISPOSE_ON_CLOSE) ; pack (); setLocationRelativeTo (c: null); submitButton. addActionListener (e > submitUpdateStudentForm()) ; \} private void submitUpdateStudentForm() \{ String oldName = oldNameField.getText (); String newName = newNameField.getText (); String newMajor = newMajorField.getText (); mainController.updateStudent (oldName, newName, newMajor); mainController.refreshMainGui () ; dispose () ; \} package view; import controller.MainController; import javax.swing. * import java.awt.event. Actionkvent: import java.awt.event.ActionListener: public class DeleteStudentGui extends javax.swing.JFrame private JTextField nameField; private JButton submitButton; private MainController mainController; public DeleteStudentGui (MainController mainController) \{ this.mainController = mainController; initComponents () ; \} private void initComponents () \{ JPanel inputPanel = new JPanel (); nameField = new JTextField(colums:20); submitButton = new JButton(tert: "Submit"); inputPanel.setLayout (new BoxLayout (target: inputPanel, aris: BoxLayout.Y_AXIS)); inputPanel.add (new JLabel (tert: "Name:")); inputPanel.add (comp: nameField); inputPanel.add (comp: submitButton) ; add (comp: inputPanel) ; setTitle(title: "Delete Student"); setDefaultCloseOperation(operation: JFrame.DISPOSE ON CLOSE) ; pack() setTitle (title: "Delete Student"); setDefaultCloseOperation(operation: JFrame.DISPOSE_ON_CLOSE) ; pack(); setLocationRelativeTo (c: null); submitButton.addActionListener (e > submitDeleteStudentForm()); \} private void submitDeleteStudentForm() \{ String name = nameField.getText (); mainController.deleteStudent (name); mainController.refreshMainGui () ; dispose () ; \}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started