2

I am creating a staff management system and i would like to know how i can pass the details of the staff from the login frame to other frames when they successfully login.

My current method is to retrieve their data when the successfully login and pass it through the constructor.

if(checkPassword.equals(password)&&checkStaffId.equals(staffId)){
                    close();
                    String name = temp.getName();
                    String position = temp.getPosition();
                    String imageSrc = temp.getImageSRC();
                    String email = temp.getEmail();
                    Home page = new Home(staffId,name,position,imageSrc,email);
                    page.setVisible(true);
                    MainInterface menu = new MainInterface(staffId,name,position,imageSrc,email);
                    form b = new form(staffId,name,position,imageSrc,email);
                    Patients a = new Patients(staffId,name,position,imageSrc,email);
                    AdminMenu admin = new AdminMenu(staffId,name,position,imageSrc,email);
                    MainRpt r = new MainRpt(staffId,name,position,imageSrc,email);
                    viewSchedule s = new viewSchedule(staffId,name,position,imageSrc,email);
                }

It works but I would like to know if there is other ways of doing it.

Rahman
  • 3,755
  • 3
  • 26
  • 43
alan
  • 86
  • 6

4 Answers4

2

It's okay to pass your arguments through the constructor but It's more flexible to create an Object Staff to encapsulate all the fields you need to pass from on class to another.

public class Staff {
    private String name;
    private String position;
    private String imageSrc;
    private String email;

    public Staff(Object temp){ // Change object here to the "temp" type
       this.name = name;
       //...
    }
}

Then change the constuctor of the receiving classes to something like

public Home(Staff staff){

}

With this you will create Home this way :

Staff staff = new Staff(temp); 
Home page = new Home(staff);
Guillaume Barré
  • 4,168
  • 2
  • 27
  • 50
1

When working with frames usually you have a bigger class, a controller, that contains the views and connects them with the model (check MVC).

Long story short, you should (you don't have to, but in my opinion it's the best approach) do something like this:

NOTE: this has nothing to do to your code, is just made to show an example of MVC.

class Model {
    int firstNumber;
    int secondNumber;

    public int add () {
        return firstNumber + secondNumber;
    }
}

class View extends JFrame {

    JTextArea first;
    JTextArea second;

    JLabel result;

    // do necessary stuff to show the JFrame, and add this TextAreas and the label
}

class Controller {
    Model model;
    View view;

    // Initialize both in the constructor

    public void add() { // This can be called when you press certain button, for example
        model.firstNumber = view.first;
        model.secondNumber = view.second; // THIS IS PSEUDO CODE, you have to convert it and stuff

        view.result = model.add();
    }
}

You just should do something like this in your program, and everything will be easier.

If you don't want to do this, then probably the best solution for you is just what you are doing or something similar, but this is easier to modify afterwards, and easier to understand.

Community
  • 1
  • 1
dquijada
  • 1,697
  • 3
  • 14
  • 19
1

How about this way?. You do not need to pass many parameter again and again .I just suggest to you but i have not tested yet.

if(checkPassword.equals(password)&&checkStaffId.equals(staffId)){
    close();
    StaffInfo staffInfo =  new StaffInfo();
    staffInfo.setName(temp.getName());
    staffInfo.setPosition(temp.getPosition());
    staffInfo.imageSrc(temp.getImageSRC());
    staffInfo.setEmail(temp.getEmail());

    Home page = new Home(staffInfo);
    page.setVisible(true);
    MainInterface menu = new MainInterface(staffInfo);

    form b = new form(staffInfo);
    Patients a = new Patients(staffInfo);
    AdminMenu admin = new AdminMenu(staffInfo);
    MainRpt r = new MainRpt(staffInfo);
    viewSchedule s = new viewSchedule(staffInfo);
}         

public class StaffInfo {

    private String name ;
    private String position;
    private String imageSrc;
    private String email;

    // Getters and setters
}
dquijada
  • 1,697
  • 3
  • 14
  • 19
Jar Yit
  • 955
  • 11
  • 22
0

If you need to pass too many parameters to a method (or constructor), it would be better to wrap them in a new class and pass the instance of that class to the method.

Areca
  • 1,292
  • 4
  • 11
  • 21