Question: Complete the BasicMvcApp so that the Next and New buttons are working. In particular, ... The display must initially show the three data fields for

Complete the BasicMvcApp so that the Next and New buttons are working. In particular, ...

  • The display must initially show the three data fields for John Doe from the data given in the addInitialStudentsToList method in the StudentList class. The rest of the list should contain the data given for students Jane Deere and Sam Spade, as indicated in the addInitialStudentsToList method.
  • The Next button must work without error to show the next Student in the list. When the list shows the information for Sam Spade, if the user clicks the Next button, you can either show a pop-up that indicates the view is already displaying the last element of the list, or you can have the display loop around to show the first element of the list (i.e., the information for John Doe).
  • The gpa value and appropriate parsing must be handled so that there are no errors.
  • When the user clicks the New button, you should add new Student (with new dummy values) to the end of the list.. Don't just copy the currently displaying values for your new Student. You should include a pop-up indicating to the user that a new Student has been added to the end of the list. Then, the display can remain on the current student (where it was when the New button was clicked), or you can advance the display to the end of the list so that it shows the new student you just added.

Need to use this code:

BasicMvcAppDemo.java

package basicmvcappdemo;

public class BasicMvcAppDemo {

public static void main(String[] args) { new StudentCntl(); }

} Student.java

package basicmvcappdemo;

public class Student {

private final String firstName; private final String lastName; private final double gpa;

public Student(String firstName, String lastName, String gpa) { this.firstName = firstName; this.lastName = lastName; this.gpa = Double.parseDouble(gpa); }

// just getters below /** * @return the firstName */ public String getFirstName() { return firstName; }

/** * @return the lastName */ public String getLastName() { return lastName; }

/** * @return the gpa */ public double getGpa() { return gpa; }

@Override public String toString() { return firstName + " " + lastName + "; gpa: " + gpa; }

}

StudentCntl.java

package basicmvcappdemo;

public class StudentCntl {

private static final int STARTING_INDEX_OF_DISPLAY = 0; private final StudentList studentList; private final StudentUI studentUI;

public StudentCntl() { studentList = new StudentList(); studentUI = new StudentUI(this, STARTING_INDEX_OF_DISPLAY); // pass 0 so UI starts at first element in list studentUI.setVisible(true); }

public Student getStudent(int index) { //you should validate the index before using the next line of code return studentList.getStudents().get(index); }

public void addNewStudent() { //the following line might be handy after you add a new Student to the end of the list //studentUI.refreshDisplay(listSize - 1); } public int getListSize() { return studentList.getStudents().size(); } }

StudentList.java

package basicmvcappdemo;

import java.util.ArrayList; import java.util.List;

public class StudentList {

private final List students;

public StudentList() { students = new ArrayList<>(); addInitialStudentsToList(); }

private void addInitialStudentsToList() { // your list of intial students should have just these three students in the order listed // "John", "Doe", "3.1" // "Jane", "Deere", "3.25" // "Sam", "Spade", "2.9" students.add(new Student("Some", "Student", "0")); students.add(new Student("John", "Doe", "3.1")); students.add(new Student("Jane", "Deere", "3.25")); students.add(new Student("Sam", "Spade", "2.9")); }

List getStudents() { return students; }

} StudentUI.java

package basicmvcappdemo;

import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField;

public class StudentUI extends JFrame {

// don't make this final, because you'll want to change it to keep track of currently displayed item private int currentIndex;

private final JTextField firstNameTextField = new JTextField(15); private final JTextField lastNameTextField = new JTextField(15); private final JTextField gpaTextField = new JTextField(15);

private JPanel instrumentPanel; private JPanel buttonPanel;

private final StudentCntl studentCntl;

public StudentUI(StudentCntl studentCntl, int startingIndexOfDisplay) { this.studentCntl = studentCntl; currentIndex = startingIndexOfDisplay; initComponents(); displayFieldValues(); }

private void initComponents() { setTitle("Student Viewer"); setSize(600, 350); setLocationRelativeTo(null); // center the frame setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

instrumentPanel = new JPanel(new GridLayout(5, 1)); instrumentPanel.add(new JLabel("First Name")); instrumentPanel.add(firstNameTextField); instrumentPanel.add(new JLabel("Last Name")); instrumentPanel.add(lastNameTextField); instrumentPanel.add(new JLabel("GPA")); instrumentPanel.add(gpaTextField);

buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

JButton newButton = new JButton("New"); JButton nextButton = new JButton("Next");

newButton.addActionListener(event -> addNew()); nextButton.addActionListener(event -> showNext());

buttonPanel.add(newButton); buttonPanel.add(nextButton);

setContentPane(new JPanel(new BorderLayout())); getContentPane().add(instrumentPanel, BorderLayout.CENTER); getContentPane().add(buttonPanel, BorderLayout.SOUTH);

}

private void addNew() { studentCntl.addNewStudent(); }

private void showNext() { // you may want to know the list size, so you don't go past the end of the list int listSize = studentCntl.getListSize();

}

private void displayFieldValues() { firstNameTextField.setText(studentCntl.getStudent(currentIndex).getFirstName()); lastNameTextField.setText(studentCntl.getStudent(currentIndex).getLastName()); gpaTextField.setText(Double.toString(studentCntl.getStudent(currentIndex).getGpa())); }

// be sure to check for valid index before assigning it to currentIndex public void refreshDisplay(int index) { // ... displayFieldValues(); // ... }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!