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 \{ public static void main(String[]args) \{ 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 () \{ 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 (listener: 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 deleteStudent(Student student) \{ studentDAO. deleteStudent (student) ; updateView (); \} Qoverride public void actionPerformed(ActionEvent e ) \{ if (e.getSource () == mainGui.getGetNameButton()) \{ openNameInputGui () ; \} \} public void openNameInputGui () \{ NameInputGui nameInputGui = new NameInputGui (mainGui, studentController: this) nameInputGui.setVisible (b: true); \} \} 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); studentList.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; \} \} 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, awis: BoxLayout.Y_AXIS) ); mainPanel.add (new JLabel (text: "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: "n); 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, axis: 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"); 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.Actionkvent; 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 (tert: "New Name:")); inputPanel. add (comp: newNameField); inputPanel.add (new JLabel (teut: "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 (); 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