用java集合模拟登录和注册功能
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("请选择功能:");
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("输入有误!!");
break;
}
}
}
// 界面
public static void print() {
System.out.println("--------------------*****管理系统--------------------");
System.out
.println(" 1.登录功能 (login) ");
System.out
.println(" 2.注册功能(register) ");
System.out
.println(" 3.查看(show) ");
System.out
.println(" 0.退出(exit) ");
System.out.println("--------------------*****管理系统--------------------");
}
// 检测
/**
* 检测到重复返回false,否则返回true
* */
public static boolean check() {
input();
if (map.containsKey(str1)) {
return false;
} else {
return true;
}
}
// 登录
public static void login() {
input();
if (map.containsKey(str1)) {
String value = map.get(str1);
if (value.equals(str2)) {
System.out.println("登录成功!");
} else {
System.out.println("密码和用户名不一致,请重新输入!");
}
} else {
System.out.println("用户名不存在!");
}
}
// 注册
public static void register() {
if (check() == true) {
map.put(str1, str2);
System.out.println("注册成功!");
} else {
System.out.println("用户名已存在,请重新注册!");
}
}
// 显示
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);
}
}
// 输入
@SuppressWarnings("resource")
public static void input() {
Scanner scanner1 = new Scanner(System.in);
System.out.println("请输入用户名:");
str1 = scanner1.next();
Scanner scanner2 = new Scanner(System.in);
System.out.println("请输入密码:");
str2 = scanner2.next();
}
}