Designing and implementing a "student" class that has the capacity to continually accept input and create multiple student objects

StackOverflow https://stackoverflow.com/questions/21891257

Frage

The goal of this project is to create two classes: a student class and a GUI class. The Student class contains name, address, balance, and major. I have the Student and GUI classes all created my problem is with the arrays and loops. I need to have the Student class array and associated counter variable be a static variable and every time the user enters the information for the Student class(name, address, balance, major) it adds another Student object to the JTextArea. In essence everytime the JButton is pressed the JTextArea updates with the new student's info without deleting the old info. Any help with making this work would be greatly appreciated!

My code:

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

class Student {

    private String name;
    private String address;
    private String balance;
    private String major;

    // Constructs fields
    public Student(String name, String address, String balance, String major) {
        this.name = name;
        this.address = address;
        this.balance = balance;
        this.major = major;
    }

    public String setName(String Name) {
        this.name = name;
        return name;
    }

    public String setAddress(String address) {
        this.address = address;
        return address;
    }

    public String setBalance(String balance) {
        this.balance = balance;
        return balance;
    }

    public String setMajor(String major) {
        this.major = major;
        return major;

    }

    public String toString() {
        return ("Name: " + this.name + " Address: " + this.address
                + " Balance: " + this.balance + " Major: " + this.major);
    }
}

public class SecondAssignment extends JFrame implements ActionListener {
    public SecondAssignment() {
        setLayout(new GridLayout(6, 1, 1, 1));

        // Creates TextField, TextArea, and button components
        name = new JTextField();
        address = new JTextField();
        balance = new JTextField();
        major = new JTextField();
        JButton jbtSubmit = new JButton("Submit");
        echoStudent = new JTextArea();

        // Add TextField, TextArea, and button components to the frame
        add(new JLabel("Name: "));
        add(name);
        add(new JLabel("Address: "));
        add(address);
        add(new JLabel("Balance: "));
        add(balance);
        add(new JLabel("Major: "));
        add(major);
        add(new JLabel("Submit Button: "));
        add(jbtSubmit);
        jbtSubmit.addActionListener(this);
        add(echoStudent);
        echoStudent.setEditable(false);

    }

    // TextFields
    private JTextField name;
    private JTextField address;
    private JTextField balance;
    private JTextField major;

    // Echo TextArea
    private JTextArea echoStudent;

    // Listener
    public void actionPerformed(ActionEvent a) {
        Student student1 = new Student(name.getText(), address.getText(),
                balance.getText(), major.getText());
        echoStudent.setText(student1.toString());
    }

    public static void main(String[] args) {
        SecondAssignment frame = new SecondAssignment();
        frame.setTitle("Student Interface");
        frame.setSize(500, 700);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setVisible(true);

    }

}
War es hilfreich?

Lösung

To store your students, you can do it either statically (using a array with a fixed size) or dynamically (using a collection such as a List).

In both ways, your SecondAssignment class will need to store this element and fill in with the created students.

Static way

If you need a static array of size 50, simply declare it with:

private Student[] myStudents = new Student[50];
private int currentIndice = 0;

Then in action performed, you can save your student:

public void actionPerformed(ActionEvent a) {
    Student student1 = new Student(name.getText(), address.getText(),
            balance.getText(), major.getText());

    //check that we have still room in the array
    if (currentIndice < 50) {
        myStudents[currentIndice] = student1;
        // increase indice to the next available element
        ++currentIndice;
    }
    //otherwise handle error
    else {
        //some error management
    }
    echoStudent.setText(student1.toString());
}

Major drawback of this solution is that your array has a fixed size and thus you cannot handle more than 50 students.

Dynamic way

Instead, you can add element to a collection that will increase from time to time. First import packages:

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

Then declare your List:

private List<Student> myStudents = new ArrayList<>;

And in your actionPerformed method:

public void actionPerformed(ActionEvent a) {
    Student student1 = new Student(name.getText(), address.getText(),
            balance.getText(), major.getText());
    //simply add the new student to the list
    myStudents.add(student1);

    echoStudent.setText(student1.toString());
}

You can retrieve the number of students with myStudents.size(), and benefit from collection features (remove from list, search in list, etc.).

Edit

As Nick Rippe stated, if echoStudent label must handle the list of all students, the simpler way would be to keep the current text instead of resetting each time: replacing echoStudent.setText(student1.toString()); with echoStudent.setText(echoStudent.getText() + "\n" + student1.toString()); would do the job for example (\n is escape sequence for a new line).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top