Hashmap

时间:2015-10-23 02:08:50

标签: java

作为家庭作业,我应该能够接受用户名和密码,然后查找已经形成的Hashmap(来自FakePersonDatbase文本文档),看看是否用户名和密码存在于Hashmap中; Hashmap的Key是一个String,它是一个用户名,值是一个名为User的对象,它是由FakePersonDatabase构成的。

这是我的尝试,我开始创建用于在Database类中创建Hashmap的相同方法,除了将对象中的用户名和密码更改为在开头输入的字符串,然后从那里我迷路了,并且不知道如何将新的User对象与user_map进行比较。

这是在Main和Database类中都使用的User类/对象

public class User 
{
    public String first_name, last_name, email, country, username, password, ip; 

    public User(String[] parts)
    {
        first_name = parts[0];
        last_name = parts[1];
        email = parts[2];
        country = parts[3];
        username = parts[4];
        password = parts[5];
    }
}

这是数据库,用于创建user_map Hashmap。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;


public class Database 
{
    public static Map<String, User> load()
    {
        Map<String, User> user_map = new HashMap<String, User>();
        try 
        {
            Scanner data_store = new Scanner(new File("fake-people-db.txt"));           

            while (data_store.hasNextLine())
            {
                String[] split_string = data_store.nextLine().split(",");
                User u = new User(split_string);
                user_map.put(u.username, u);
            }                   
        } 

        catch (FileNotFoundException e) 
        {
            System.out.println(e.getMessage());
        }
        return user_map;
    }
}

这是主要的,这是我迄今为止的尝试。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class Gatekeeper 
{   
    public static void main(String[] args) 
    {
        /* parse the user database */
        Map<String, User> user_map = Database.load();
        /* You now have a map full of users.
         * The key is the username and the value is the user object.
         * How can you check to see if the given username/password is correct? */
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter username");
        String username = keyboard.nextLine();

        if(user_map.containsKey(username))
        {
            System.out.println("Enter password");
            String password = keyboard.nextLine();

            Map<String, User> user_compare = new HashMap<String, User>();

            try 
            {
                Scanner data_store = new Scanner(new File("fake-people-db.txt"));

                while (data_store.hasNextLine())
                {
                    String[] split_string = data_store.nextLine().split(",");
                    split_string [4] = username;
                    split_string [5] = password;
                    User u = new User(split_string);
                        user_compare.put(u.username, u);
                        /* I have a feeling that this is where the issue is occuring, 
                         * I don't know how to compare the object created above to the user_map Hashmap.*/
                        if(u.equals(user_map.get(u.username)))
                        {
                            System.out.println("Hello");
                    }
                }
                System.out.println("Incorrect password");
            } 

            catch (FileNotFoundException e)
            {
                System.out.println(e.getMessage());
            }           
        }
        else
        {
            System.out.println("That username does not exist.");
        }
    }
}

对于看起来像二年级学生的代码表示道歉,我是一名CCC学生,几乎没有学习过绳索。任何有关这方面的帮助将非常感激!

2 个答案:

答案 0 :(得分:1)

您不需要比较整个User对象,您只想验证密码是否是该用户的有效密码,对吗?

您可以向User班级添加方法:

 public boolean doesPasswordEqual(String password) {
     return this.password.equals(password);
 }

然后在你的主要部分,你可以这样做:

...
System.out.println("Enter username");
String username = keyboard.nextLine();

if(user_map.containsKey(username))
{
   System.out.println("Enter password");
   String password = keyboard.nextLine();

   if (user_map.get(username).doesPasswordEqual(password)) {
       System.out.println("Hello");
   } else {
       System.out.println("Incorrect password");
   }
}

答案 1 :(得分:0)

import java.util.*;
import java.util.stream.Collectors;
public class Gatekeeper 
{           
    public static void main(String[] args) 
    {
        Map<String, User> user_map = Database.load();

        Scanner keyboard = new Scanner(System.in);
        String username,password;

        System.out.println("Enter username");
        username = keyboard.nextLine();

        if (!user_map.containsKey(username))
        {
            System.out.println("That username does not exist.");
            System.exit(0);
        }   

        System.out.println("Enter password");
        password = keyboard.nextLine();

        User un = user_map.get(username);

            if((un.password).equals(password))
            {
                System.out.println("Successfully logged in.");
                System.out.println("Welcome " + un.first_name + " "+ un.last_name);
            }
            else
                System.out.println("Incorrect password");
    }   
}