Hi I am having trouble creating a system to log in to a system.

If you look at a picture its like this. User selects thier user name from a dropdown box, and then select thier Picture(instead of typing in a password to login.

There can I get more info on how to make something like this?

Attachments Logindesign.JPG 18.27 KB

Do I store the usernames in an array lisst and pictures inb and array list and then somehow match them together to allow the user a succesful login>?

Do I store the usernames in an array lisst and pictures inb and array list and then somehow match them together to allow the user a succesful login>?

Now I saw the specific question. In my opinion you don't really need to "store the pictures". You can assign some ID to each picture and store the association of Username vs this ID. Picture could be just shown on the UI and when it's selected appropriate ID along with the username can be given to server for verification. (Point is you don't compare the picture with picture but ID with ID).
The ID can be an int or string or...
Storage of the list of Username vs ID most probably has to be done persistantly. I would recommend not to use DB if this is the only reason you want to use it. Rather go for a simple flat/txt file would be easier.

Storage of the list of Username vs ID most probably has to be done persistantly. I would recommend not to use DB if this is the only reason you want to use it. Rather go for a simple flat/txt file would be easier.

I don't know why you feel storing it in a flatfile is better. That requires that you load the entire list while running, or that you write your own search routines to search for the user/id combination. With a DB you simply set of a query with the username as the search string and return the "id". No fuss, no muss. No loading the entire list into main memory, and no self-rigged search routines. And storing new user/id combos is just as easy as finding one. A single statement, and you're done. And this includes the possibility of a new user attempting to use the same name as a current user, when the username is the primary key, as the insert will fail with a primary key validation. With that, you no longer even have to check yourself whether a new username exists or not. Simply try to add it.

That requires that you load the entire list while running, or that you write your own search routines to search for the user/id combination.

True but I feel that would be easier ! :)

With a DB you simply set of a query with the username as the search string and return the "id".

As someone quoted earlier you've deceptively missed a few other things:
1. Installation of DB?
2. Creation of DB table?
3. Code to create the DB connection?
4. Readup how to do step 1, 2 and 3 or ask someone???
I sure agree that once you're done with all this you'll "simply set of a query with the username as the search string and return the id".

No fuss, no muss. No loading the entire list into main memory, and no self-rigged search routines. And storing new user/id combos is just as easy as finding one. A single statement, and you're done.

Here is the fuss/muss we'll have to do if we use flat file (see the code at end). Self-regged search routines? std::map gives it. map::find().
Abt the "single stmt and you're done part" as I said earlier, it's not really true.

And this includes the possibility of a new user attempting to use the same name as a current user, when the username is the primary key, as the insert will fail with a primary key validation. With that, you no longer even have to check yourself whether a new username exists or not. Simply try to add it.

See the return type of std::map::insert(). It does this for you. Again you don't write no code.

#define MAX_LINE_LEN 100

int main()
{
    FILE * pFile = fopen ("user_info.txt", "w+");
    if( ! pFile )
    {
        cout << "Could not open file" << endl ;
        return 1 ;
    }

    for( int i = 0; i < 10; i++ )
    {
        char dummy[50] ;
        itoa(i, dummy, 10) ;
        fprintf (pFile, "%s %d\n", dummy, i);
    }

    rewind (pFile);

    map< string, int > usrInfMap ;
    char str [80];
    int j;
    while( EOF != fscanf (pFile, "%s %d", str, &j) )
    {
        usrInfMap[str] = j ;
    };

    fclose (pFile);

    map< string, int >::iterator it = usrInfMap.begin();
    for( ; it != usrInfMap.end(); it++ )
        cout << it->first.c_str() << " = " << it->second << endl ;

    return 0;
}

Now finally remember that I said earlier:
I would recommend not to use DB if this is the only reason you want to use it (=>DB).

Comments
C++ is not JAVA, don't put C++ solution in JAVA section

True but I feel that would be easier ! :)


As someone quoted earlier you've deceptively missed a few other things:
1. Installation of DB?
2. Creation of DB table?
3. Code to create the DB connection?
4. Readup how to do step 1, 2 and 3 or ask someone???
I sure agree that once you're done with all this you'll "simply set of a query with the username as the search string and return the id".


Here is the fuss/muss we'll have to do if we use flat file (see the code at end). Self-regged search routines? std::map gives it. map::find().
Abt the "single stmt and you're done part" as I said earlier, it's not really true.


See the return type of std::map::insert(). It does this for you. Again you don't write no code.

#define MAX_LINE_LEN 100

int main()
{
    FILE * pFile = fopen ("user_info.txt", "w+");
    if( ! pFile )
    {
        cout << "Could not open file" << endl ;
        return 1 ;
    }

    for( int i = 0; i < 10; i++ )
    {
        char dummy[50] ;
        itoa(i, dummy, 10) ;
        fprintf (pFile, "%s %d\n", dummy, i);
    }

    rewind (pFile);

    map< string, int > usrInfMap ;
    char str [80];
    int j;
    while( EOF != fscanf (pFile, "%s %d", str, &j) )
    {
        usrInfMap[str] = j ;
    };

    fclose (pFile);

    map< string, int >::iterator it = usrInfMap.begin();
    for( ; it != usrInfMap.end(); it++ )
        cout << it->first.c_str() << " = " << it->second << endl ;

    return 0;
}

Now finally remember that I said earlier:
I would recommend not to use DB if this is the only reason you want to use it (=>DB).

I did not miss any of those posts, but not asingle one of them is hard, and yna software you get will contain the instructions for doing them, so no, you would not need to "ask someone".

Everything else you wrote is c++ not Java. Go crawl back under the rock you came from.

And, flat file system will never give you the type of security you want for a password system unless you encrypt them as well. Then you also have to self-rigg that encryption system, and even though the "password" here is simply a picture "id" it should still be encrypted. Nearly all mainstream dbs provide an easy way to encrypt entries, so even that is "done for you".

So remember, I say again, why do you think a flat file system is better?

Wow is that coencidence that you both have avatars created by matt groening.

Ok, thnx for the replies aye. To be honest this doesnt even need security, all it really needs is different files stored for different userz.

I don't want to create a database so I like the flat txt file idea:-| .

Ill read through your comments and see if I can code this.

This is what I have so far. Btw im using J builder.

I've created the GUI and now I need to store the data when you Create Account.

//createAccountBtn pressed
    createAccountBtn.addActionListener(new ActionListener(){                                 
      public void actionPerformed(ActionEvent e) {
        userName = userNameInput.getText();
        userBox.addItem(userName);
      }
    });

So far all I managed was to make the comboBox contain the username.

I understand I somehow have to store Username and Picture to a file? But then how will I be able to call the file in the login screen?

Do I need 3 Arraylists? One for user one for picture one for ID or can I just have one for ID that stores the relation with the picture and username

public class AccountInfo {
  //private String userName;
  private int pictureInput;
  private int userID;
  private GUI gui;
  private ArrayList userName;
  private ArrayList password;

  public AccountInfo()
  {
    gui = new GUI();
    userName = new ArrayList();
    password = new ArrayList();
  }
  public void setUsername()
  {
    userName.add(gui.getName());
  }
  public void setPassword()
  {
    password.add(gui.getName());
  }
}

Heres my AccountInfo class

Heres some more of my improved Gui.
'
I still am struggling to do this all I gave picture id though.

//createAccountBtn pressed
    createAccountBtn.addActionListener(new ActionListener(){                                 
      public void actionPerformed(ActionEvent e) {
        userName = userNameInput.getText();
        userID = userName + passWord;
        userBox.addItem(userName);
      }
    });
    
    //accountPicture1 pressed
    accountPicture1.addActionListener(new ActionListener(){                                 
      public void actionPerformed(ActionEvent e) {
        passWord = "1";
      }
    });
    
    //accountPicture2 pressed
    accountPicture2.addActionListener(new ActionListener(){                                 
      public void actionPerformed(ActionEvent e) {
        passWord = "2";
      }
    });
    //accountPicture3 pressed
    accountPicture3.addActionListener(new ActionListener(){                                 
      public void actionPerformed(ActionEvent e) {
        passWord = "3";
      }
    });
    //accountPicture4 pressed
    accountPicture4.addActionListener(new ActionListener(){                                 
      public void actionPerformed(ActionEvent e) {
        passWord = "4";
      }
    });
    //accountPicture5 pressed
    accountPicture5.addActionListener(new ActionListener(){                                 
      public void actionPerformed(ActionEvent e) {
        passWord = "5";
      }
    });
    
    //loginBtn pressed
    loginBtn.addActionListener(new ActionListener(){                                 
      public void actionPerformed(ActionEvent e) {
        loginPanel.setVisible(false);
        workSpacePanel.setVisible(true);
      }
    });

Wow is that coencidence that you both have avatars created by matt groening.

Ok, thnx for the replies aye. To be honest this doesnt even need security, all it really needs is different files stored for different userz.

I don't want to create a database so I like the flat txt file idea:-| .

Ill read through your comments and see if I can code this.

1. Great, just use java.util.Properties class and things would be even easier than C++. Store the data as "username=pictID" in file.
2. For storing the username vs. ID mapping use HashTable/Map.
3. The pictures: I don't know why you need to store any information abt them at all. See my previous posts. They're static info and can be just kept to the UI component. Don't de/serialize them.

I really appreciate you helping me. I really got to research how to store stuff in files. Im reading documentation on properties at the moment.

So I should use store?

and then load to login?

Once your login page comes, the user would type a username in the control u're using for username, also he'll select/click the picture he wants, on this click you set passWord (as you've coded).
On click of the create account button you just add this username password combination to your HashMap object.
When you're exiting from the process, you serialize (write) the HashMap to a flat file using Properties class (just copy all data from HashMap to Properties class and use it's method to write to file).
At next startup of your process you'll have this process so in main() or somewhere during initialization you create Properties object using name of the the file to which you had serialized, Properties class will read all data.
Now you copy this data from Properties file to HashMap and you're back in business.

Your AccountInfo class would not be required. HashMap that I refer to in my previous post is the one that'll hold username-password combination instead of AccountInfo.

;) Wow you make it seem so easy. Just add Username and Password to hashmap aye.

private HashMap userInformation;
 
 
//createAccountBtn pressed
    createAccountBtn.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        userName = userNameInput.getText();
        
 
//userNameInput.setText(userName);
        
        userBox.addItem(userName);
        
        
        //AccountInfo.aUserID.add(UserID);
        
        //userID = userName + passWord;
        //userIDs.add(userID);
        
        userInformation.put(userName,passWord);
        //userName = null;
        //passWord = null;
        
      }
    });

Thanks for you help once again. I don't thing you can explain it any simpler. I will make sure to listen to what you said but I right now I really gotta understand hashmaps better :D .

>> So I should use store?
>> and then load to login?
Yes.
Overall flow would be:

  • From main() you create your UI elements.
  • Add all actionListners().
  • Create the Properties object and call load() with name of the file that you used for storing the data (in last run).
  • Copy all data from this Properties object to a HashMap.
  • On click of one of the pictures you set the password variable (as you're doing).
  • On click of create account button, pickup the username/password and see if it's already existing in the HashMap. If yes, it's an error.
  • On click of Login button pickup the username/password and see if user name is already existing in the HashMap. If not it's an error, if yes see that password (ID) matches.
  • At the end before exiting from main(), copy all data from HashMap to a new Properties object and call store() with the file name.
public class User {
  private String userName;
  private String password;
  private String accountInfo;

  public User()
  {
     //accountInfo = new ArrayList();
  }
  public void setPassword(String pass)
  {
    password = pass;
  }
  public void setUsername(String user)
  {
    userName = user;
  }
  public void setAccountInfo()
  {
    accountInfo = password + userName;
  }

  public void saveAccountInfo()
  {
    try
        {
          File dir = new File("H:/SDI/MileStone3/" + userName);
          dir.mkdir();
          FileOutputStream fOut = new FileOutputStream("H:/SDI/MileStone3/" + userName + "/accountInfo.dat");
          ObjectOutputStream oOut = new ObjectOutputStream(fOut);
          oOut.writeObject(accountInfo);
          oOut.close();
          System.out.println("Account info saved");
        }
        catch(Exception e)
        {
          System.out.println("Error! High Scores not saved");
          e.printStackTrace();
        }
  }
  public void checkInfo()
  {
/**
    //attempt to restore the high scores
    try
    {
        FileInputStream fIn = new FileInputStream("accountInfo.dat");
        ObjectInputStream oIn = new ObjectInputStream(fIn);
        accountInfo = (String) oIn.readObject();
        System.out.println("High Scores restored successfully!");
        oIn.close();
        userName.compareTo(password);
    }
    catch(FileNotFoundException e)
    {
        System.out.println("No file found");
    }
    catch(ClassNotFoundException e)
    {
        System.out.println("Class not found!");
        e.printStackTrace();
    }
    catch(IOException e)
    {
        System.out.println("IO Error!");
        e.printStackTrace();
    }
 }
 **/
    userName.compareTo(password);

  }

  }
//GoCreateAccount button pressed
    goCreateAccount.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        createAccountPanel.setVisible(true);
        loginPanel.setVisible(false);
      }
    });
    //GoLogin button pressed
    goLogin.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        createAccountPanel.setVisible(false);
        loginPanel.setVisible(true);
      }
    });
 
    //accountPicture1 pressed
    accountPicture1.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        info.setPassword("1");

      }
    });
    //accountPicture2 pressed
    accountPicture2.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        info.setPassword("2");
      }
    });
    //accountPicture3 pressed
    accountPicture3.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        info.setPassword("3");
      }
    });
    //accountPicture4 pressed
    accountPicture4.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        info.setPassword("4");
      }
    });
    //accountPicture5 pressed
    accountPicture5.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        info.setPassword("5");

      }
    });
    //loginBtn pressed
    loginBtn.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
 
        loginPanel.setVisible(false);
        workSpacePanel.setVisible(true);
      }
    });

    //loginPicture1 pressed
    loginPicture1.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        info.setPassword("1");

      }
    });

    //loginPicture2 pressed
    loginPicture2.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        info.setPassword("2");
      }
    });
    //loginPicture3 pressed
    loginPicture3.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        info.setPassword("3");
      }
    });
    //loginPicture4 pressed
    loginPicture4.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        info.setPassword("4");
      }
    });
    //loginPicture5 pressed
    loginPicture5.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e)
      {
        info.setPassword("5");
      }
    });

    //createAccountBtn pressed
    createAccountBtn.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        info.setUsername(userNameInput.getText());
        userBox.addItem(userNameInput.getText());
        info.setPassword(pass);
        info.setAccountInfo();
        info.saveAccountInfo();
        createAccountPanel.setVisible(false);
        loginPanel.setVisible(true);
      }
    });
 
 
    //loginBtn pressed
    loginBtn.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        pass = (String) userBox.getSelectedItem();
        info.setUsername(pass);
        info.checkInfo();

      }
    });
 
    //logoutBtn pressed
    logoutBtn.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {

        loginPanel.setVisible(true);
        workSpacePanel.setVisible(false);
      }
    });
 

  }
}

This is all I got, I dont know why it still isnt workin :sad:

Can you post your main as well.. ? Place where you create all your controls (like goCreateAccount, goLogin, accountPicture1...)

A few things I can see:
1. checkInfo() doesn't return anything. Also the handler for loginBtn doesn't do anything else than call checkInfo() so in effect when you press the button nothing happens on the GUI.
2. In checkInfo() you are not using the file to which you had written in saveAccountInfo(). Of course you're also not using Properties class but I assume you've thought abt it and decided to serialize the object itself, which is also okay..
3. Lastly I'm unable to make out the overall flow in the GUI part, when you click login btn what happens? when you click the create a/c btn what happens... ?....

This article has been dead for over six months. Start a new discussion instead.