1

I'm writing a simple Java program.

First, the program asks user to input some info like name and id of the student and uses radio button for asking whether the student is present or absent. After finishing the inputs, then program validate whether predefined student names and inputs are match or not. And check id number again and spit out if input is over 4. Lastly check radio button is true or false. If one of the above two rules get error then program will quit without executing next method.

I have three .java files. One for UI. One for validation and one for storing data.

UI.java

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UI extends JFrame {
    JTextField name = new JTextField("Name", 10);
    JTextField id = new JTextField("Id", 10);
    JRadioButton attendance = new JRadioButton("Present");
    JButton JB = new JButton("Check");

    public UI() {
        super("Test");
        JPanel JP = new JPanel();
        JP.add(name);
        JP.add(id);
        JP.add(attendance);
        JP.add(JB);
        add(JP);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void buttonAction(){
        UI UIbutton = new UI();
        UIbutton.JB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == UIbutton.JB) {
                    String nameInput = UIbutton.name.getText();
                    int idInt = Integer.parseInt(UIbutton.id.getText());
                    boolean attInput = UIbutton.attendance.isSelected();
                    Validate.nameChk(nameInput);
                    Validate.idChk(idInt);
                    Validate.attChk(attInput);
                    Student studentObj = new Student(UIbutton.name.getText(), idInt, UIbutton.attendance.isSelected());
                    System.out.println(studentObj.name + "'s ID number is : " + studentObj.id + ".");
                    System.out.println(studentObj.name + " is present: " + studentObj.attendance);
                    System.exit(0);
                }}});
    }

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

Validate.java

public class Validate {
    public static void nameChk (String nameInput) {
        String n1 = "Matthew";
        String n2 = "John";
        String n3 = "Mark";
        String n4 = "Luke";
        if ((nameInput.equalsIgnoreCase(n1))||
                (nameInput.equalsIgnoreCase(n2))||
                (nameInput.equalsIgnoreCase(n3))||
                (nameInput.equalsIgnoreCase(n4))){
            System.out.println("Your data is okay.");
        }
        else {
            System.out.println("Error, wrong student name.");
            System.exit(0);
        }
    }
    public static void idChk (int idInt) {
        if (idInt > 4) {
            System.out.println("Your id is not correct.");
            System.exit(0);
        }
        else {
            System.out.println("Your id is correct.");
        }
    }
    public static void attChk (boolean attInput) {
        if (attInput) {
            System.out.println("The student is present.");
        } else {
            System.out.println("The student is absent.");
        }
    }
}

Student.java

public class Student {
    String name;
    int id;
    boolean attendance;

    Student(String name, int id, boolean attendance) {
        this.name = name;
        this.id = id;
        this.attendance = attendance;
    }
}

What I want to know is how can I reuse output of that actionlister method somewhere else. Let say I would create foo.java class and use that studentObj variable to give grades like

System.out.println(studentObj.name+"has B+.");

Sort of. How can I do that? How to turn that variable into global?

1 Answer 1

1

This can be achieved in different ways. Quite simple, but not a good practice would be to create a Singleton. It would contain Students objects and you'll be able to access them from anywhere. Here is example with eager singleton, but you can implement much better versions (check about singleton implementations i.e. here https://www.journaldev.com/1377/java-singleton-design-pattern-best-practices-examples)

public class StudentsSingleton {

    private Map<Integer, Student> students = new HashMap<>();
    public Student getStudent(int id) { return students.get(id);}
    public void addStudent(Student s) { students.put(s.id, s);}

    private static final StudentsSingleton instance = new StudentsSingleton();

    //private constructor to avoid client applications to use constructor
    private StudentsSingleton(){}

    public static StudentsSingleton getInstance(){
        return instance;
    }
}

In that case, you can access it from anywhere by getting the instance :

StudentsSingleton.getInstance().getStudent(id);

A much better solution and a good practice would be to use some Dependency Injection framework i.e. Spring. In that case, you would create a Bean and inject it whenever it is needed to use.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.