Using Java arrays to simulate login and registration functions

Source: Internet
Author: User
Tags array length

Package com.linkage.login;

Import Java.util.Scanner;

public class User {
Store user name and password
public static String arr1[] = new STRING[3];
public static String arr2[] = new STRING[3];
static int index = 0;

@SuppressWarnings ("resource")
public static void Main (string[] args) {
Arr1[0] = "Zhangsan";
Arr2[0] = "Zhang";
while (true) {
Print ();
Scanner scan = new Scanner (system.in);
SYSTEM.OUT.PRINTLN ("Please select function:");
String str = Scan.next ();
if (Str.equals ("1")) {
Login ();
} else if (Str.equals ("2")) {
Register ();
} else if (Str.equals ("0")) {
System.exit (0);
Break
} else if (Str.equals ("3")) {
Show ();
} else {
SYSTEM.OUT.PRINTLN ("input error");
}
}
}

Login function
@SuppressWarnings ("resource")
public static void login () {

for (int i = 0; i < 3; i++) {
Scanner scan1 = new Scanner (system.in);
System.out.println ("Please enter user name:");
String str1 = Scan1.next ();
Scanner scan2 = new Scanner (system.in);
System.out.println ("Please enter password:");
String str2 = Scan2.next ();
for (int i1 = 0; i1 < arr2.length; i1++) {
if (Str1.equals (Arr1[i1]) && str2.equals (Arr2[i1])) {
SYSTEM.OUT.PRINTLN ("Login Successful! ");
Return
}
}
SYSTEM.OUT.PRINTLN ("Login failed, please re-enter!") ");
}
}

Registration function
@SuppressWarnings ("resource")
public static void register () {
Scanner scan1 = new Scanner (system.in);
System.out.println ("Please enter user name:");
String str1 = Scan1.next ();
Scanner scan2 = new Scanner (system.in);
System.out.println ("Please enter password:");
String str2 = Scan2.next ();

Boolean flag = false;
for (int i = 0; i < arr1.length; i++) {
if (Str1.equals (Arr1[i])) {
System.out.println ("User name already exists");
Flag = true;
}
}
if (flag = = False) {
index++;
if (Index < arr1.length) {
Arr1[index] = str1;
Arr2[index] = str2;
SYSTEM.OUT.PRINTLN ("Registered successfully! ");
} else {
Add ();
Arr1[index] = str1;
Arr2[index] = str2;
SYSTEM.OUT.PRINTLN ("Registered successfully! ");

}
}
}

Display user Information
public static void Show () {
for (int i = 0; i < arr1.length; i++) {

if (arr1[i]! = null) {
System.out.println ("User name:" + arr1[i] + "," + "Password:" + arr2[i]
+ "\ n");
}
}
}

Increase array length
public static void Add () {
String newarray1[] = new String[arr1.length + 3];
String newarray2[] = new String[arr1.length + 3];
System.arraycopy (arr1, 0, newarray1, 0, arr1.length);
System.arraycopy (arr2, 0, newarray2, 0, arr1.length);
arr1 = Newarray1;
ARR2 = Newarray2;
Show ();
}

Input content
public static void print () {
System.out.println ("--------------------small fat Fish Management System--------------------");
System.out
. println ("1. Login function (login)");
System.out
. println ("2. Registration function (register)");
System.out
. println ("3. View (show)");
System.out
. println ("0. Exit");
System.out.println ("--------------------small fat Fish Management System--------------------");
}
}

Using Java arrays to simulate login and registration functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.