1. 程式人生 > >用java集合模擬登錄和註冊功能

用java集合模擬登錄和註冊功能

out ngs java集合 管理系 AR scan return pack bre

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();
}
}

用java集合模擬登錄和註冊功能