Simulating login and registration functions with Java collections

Source: Internet
Author: User

Package com.linkage.login;

Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.Map;
Import Java.util.Scanner;
Import Java.util.Set;

public class Aggregate {
Static map<string, string> Map = new hashmap<> ();
Static String str1;
Static String str2;

@SuppressWarnings ("resource")
public static void Main (string[] args) {

while (true) {
Scanner Scanner = new Scanner (system.in);
Print ();
SYSTEM.OUT.PRINTLN ("Please select function:");
String str = Scanner.next ();
Switch (str) {
Case "1":
Login ();
Break
Case "2":
Register ();
Break
Case "3":
Show (map);
Break
Case "0":
System.exit (0);
Break
Default
SYSTEM.OUT.PRINTLN ("Wrong input!! ");
Break
}
}
}

Interface
public static void print () {
SYSTEM.OUT.PRINTLN ("--------------------* * * * * 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 ("--------------------* * * * * Management system--------------------");
}

Detection
/**
* Repeat returned false is detected, otherwise true
* */
public static Boolean check () {
Input ();
if (Map.containskey (str1)) {
return false;
} else {
return true;
}
}

Login
public static void login () {
Input ();
if (Map.containskey (str1)) {
String value = Map.get (STR1);
if (Value.equals (str2)) {
SYSTEM.OUT.PRINTLN ("Login Successful! ");
} else {
SYSTEM.OUT.PRINTLN ("Password and username are inconsistent, please re-enter!") ");
}
} else {
System.out.println ("User name does not exist! ");
}
}

Registered
public static void register () {
if (check () = = True) {
Map.put (str1, str2);
SYSTEM.OUT.PRINTLN ("Registered successfully! ");
} else {
System.out.println ("User name already exists, please re-register!") ");
}
}

Show
public static void Show (Map<string, string> Map) {
set<string> keys = Map.keyset ();
Iterator<string> iter = Keys.iterator ();
while (Iter.hasnext ()) {
String key = Iter.next ();
String value = Map.get (key);
System.out.println ("key:" + key + ", Value:" + value);
}
}

Input
@SuppressWarnings ("resource")
public static void input () {
Scanner scanner1 = new Scanner (system.in);
System.out.println ("Please enter user name:");
STR1 = Scanner1.next ();
Scanner scanner2 = new Scanner (system.in);
System.out.println ("Please enter password:");
STR2 = Scanner2.next ();
}
}

Simulating login and registration functions with Java collections

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.