0

So let's say I have 3 files.

  • Accounts.java (Account class)

  • loginGUI.java (JForm)

  • displayAccountGUI.java (JForm)

when I login I input CustomerID, Pin and Account Number. Then the program checks if the 3 values I have inputted are in accounts.txt.

I also have Account a = new Account(); in both of the Jframes.

In displayAccount.java I want to display Account Number from loginGUI.java that is being stored in Account.java using a.setId(ID); but because I have Account a = new Account(); in displayAccount.java the value set before is being removed due to creation new 'Account'.

Is there any way I can access this value in some other way?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
hDDen
  • 145
  • 1
  • 2
  • 13
  • 1
    Create some kind of session object which encapsulates the information you need to be shared between your classes, pass this to your classes so they are making use of it and not creating new instances of objects which have no association with what's actually going on – MadProgrammer Apr 18 '17 at 02:19
  • 1
    This is a relatively basic programming concept, consider having a look at [Passing Information to a Method or a Constructor](https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html) – MadProgrammer Apr 18 '17 at 02:20
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Apr 18 '17 at 19:50

3 Answers3

1

Once you've validated the user credentials, create a "session" object, which encapsulates the information you need to share. It could be the "customer ID" and "account name" information or it could be an instance of the Account class.

Pass this to your displayAccount class (removing the Account a = new Account(); and using the passed reference)

How you achieve all this will depend on how you've structured your code. Me personally, I'd have some kind of controller which displayed a login dialog. When the dialog is dismissed, I'd validate the credentials, displaying an error message if they failed. If they succeeded, I'd create a new instance of the Account class (assuming it holds enough information about the client), I'd create the "account view", passing the instance of Account to it

This is a relatively basic programming concept, consider having a look at Passing Information to a Method or a Constructor

lazy
  • 77
  • 10
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

I belive you can create a new constructor for displayAccountGUI and pass Account as a parameter. It makes sense because in the future you may need more information from the account in displayAccountGUI, like a photo or something else.

Sillas Camara
  • 76
  • 1
  • 5
1

Since I don't have the privilege to add comments yet, I'll just post it here. I only know two of many ways to accomplish what you want to do to get the desired result.

  1. pass the value via a constructor

    public displayAccount(Account a){this.a = a;}

  2. use setter method

    public void setAccount(Account a){this.a = a;}

lazy
  • 77
  • 10