Storing Names in ArrayList and Using it to Login

I have a task to do which involves asking the user to input their last name and giving the user an account number to login to the program. I have listed the steps below which might make more sense.

1) User creates an account

2) User enters their last name (Stores into the arraylist)

3) User is given an account number (Stores into the arraylist)

4) User can then login using their last name and account number (checks arraylist for lastname and accountnumber, if it matches then login message, if it doesnt then error message)

A user enters their last name and they are given an account number which they then use to login to deposit, withdraw and check balance.

How do i create a programe to do this without the use of database?

Account Class

private static int number = 500;

    Account(){
        accountNumber = number++;
    }

Create Account

public void createAccount(){

String firstName;

System.out.print("Please Enter Last Name: ");
lastName = scanner.nextLine();
System.out.println("This is your Account Number to log into: " + _______ );
}

public void logIn(){

    System.out.println("Please enter your last name: ");

    System.out.println("Please enter your account number: ");

}

I would like to suggest another method using xml to store credentials follow the steps below

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
      {
            string username;
            string pwd;
            string CurrentUser = "";
            string CurrentPwd = "";
            bool LoginStatus = false;
            username = Login1.UserName;
            pwd = Login1.Password;
            XmlDocument xmxdoc = new XmlDocument();
            xmxdoc.Load(Server.MapPath("Register.xml"));
            XmlNodeList xmlnodelist = xmxdoc.GetElementsByTagName("user");
            foreach (XmlNode xn in xmlnodelist)
            {
                  XmlNodeList xmlnl = xn.ChildNodes;
                  foreach (XmlNode xmln in xmlnl)
                  {
                        if (xmln.Name == "Name")
                        {
                              if (xmln.InnerText == username)
                              {
                                    CurrentUser = username;
                              }
                        }
                        if (xmln.Name == "Password")
                        {
                              if (xmln.InnerText == pwd)
                              {
                                    CurrentPwd = pwd;
                              }
                        }
                  }
                  if ((CurrentUser != "") & (CurrentPwd != ""))
                  {
                        LoginStatus = true;
                  }
            }
            if (LoginStatus == true)
            {
                  Session["UserAuthentication"] = username;
                  Session.Timeout = 1;
                  Response.Redirect("welcome.aspx");
            }
            else
            {
                  Session["UserAuthentication"] = "";
            }
      }

in your xml file

<user>
  <Name>smashcode</Name>
  <Password>smashcode</Password>
</user>

I guess this would be better approach than a arraylist approach

if you want to try in using arraylist follow steps

step1: username_list{uesr1,user2,user3}

  password_List{pass1,pass2,pass3}

step:check all entries with entered userid and password in a loop as follows

     int flag = 0;
     while(username_list.get(i)!=null)
       {

           if((username_list.get(i).equals(enteredusername))&&((password_list.get(i).equals(enteredpassword)))
             {
                flag = 1;
             }
       }

if(flag==1)
 {
    System.out.println("login successful ");
    Response.Redirect("welcome.aspx");
 }
   I had written second code implementation in cut short 

Hope my work will be helpful.Keep coding


Not a full answer here but a few suggestions....

You could create a "bank" class... It might hold the arraylist of accounts, also holding
createAccount()

delAccount()

findAccount()...

So on and so forth

Having posted this I now see it is an answer, my bad guys


I assume you need to be able to keep this information after the execution is complete, which means you need to store the information somewhere besides the running program.

Of the top of my head, you can use a file to store this store of information, where each line of the file would equal a match of last name - account. When opening the program, you read the file. Try reading:

  • http://www.tutorialspoint.com/java/java_files_io.htm or
  • https://docs.oracle.com/javase/tutorial/essential/io/file.html
  • The solution is similar to using a database, so I don't know if it will do or not. Hope it does.

    链接地址: http://www.djcxy.com/p/40792.html

    上一篇: Java:深层克隆/复制实例的推荐解决方案

    下一篇: 在ArrayList中存储名称并使用它登录