reading and writing to a file to make a user

edited March 2016 in Questions about Code

Hi guys. So far I have a log in that only allows users from the actual code. I would like to change it to be reading and writing to a file? have you guys any advice how id do this?

import static javax.swing.JOptionPane.*;
import javax.swing.JPasswordField;

final StringDict accounts = new StringDict(
  new String[] {
  "baptiste", "tanguy", "alexis"
  }
  , new String[] {
    "bap", "guy", "ex"
  }
  );

final JPasswordField pwd = new JPasswordField();

{
  println(accounts);
}

void draw() {
  if (frameCount == 1)  frame.setVisible(false);

  String user = askUser();

  if (user == null)           confirmQuit();
  else if (!"".equals(user))  askPass(user);
}

String askUser() {
  String id = showInputDialog("Please enter user:");

  if (id == null)
    showMessageDialog(null, "You've canceled login operation!"
      , "Alert", ERROR_MESSAGE);

  else if ("".equals(id))
    showMessageDialog(null, "Empty user input!"
      , "Alert", ERROR_MESSAGE);

  else if (!accounts.hasKey(id = id.toLowerCase()))
    showMessageDialog(null, "Unknown \"" + id + "\" user!" + (id = "")
      , "Alert", ERROR_MESSAGE);

  return id;
}

boolean askPass(String id) {
  boolean isLogged = false;
  pwd.setText("");

  int action = showConfirmDialog(null, pwd
    , "Now enter password:", OK_CANCEL_OPTION);

  if (action != OK_OPTION) {
    showMessageDialog(null, "Password input canceled!"
      , "Alert", ERROR_MESSAGE);

    return false;
  }

  String phrase = pwd.getText();
  //String phrase = new String(pwd.getPassword());

  if ("".equals(phrase))
    showMessageDialog(null, "Empty password input!"
      , "Alert", ERROR_MESSAGE);

  else if (accounts.get(id).equals(phrase)) {
    showMessageDialog(null, "Welcome \"" + id + "\"!\nYou're logged in!"
      , "Info", INFORMATION_MESSAGE);

    isLogged = true;
  } else
    showMessageDialog(null, "Password \"" + phrase + "\" mismatch!"
      , "Alert", ERROR_MESSAGE);

  return isLogged;
}

void confirmQuit() {
  if (showConfirmDialog(null, "Wanna quit then?", "Exit"
    , OK_CANCEL_OPTION) == OK_OPTION)  exit();
}

Answers

Sign In or Register to comment.