getting all keys belonging to one object in a hashmap

James :

Below is my user Class. I also have a Card class which is the parent class of a Single currency card class and a multi card currency class.

I am fairly new to coding and have trouble understanding some concepts. What I need to do is return the cards that the user owns if the username and password match. This is in the getCards method. After this, i need to add the card to the Hashmap list if the username and password match. Any tips or other sites that would really help as I struggle a lot with the HashMap concept.


public class User {
String username;
String password;
User user;

HashMap<String,Card> userHash = new HashMap <String, Card>(); //key is the cardID

public User(String username, String password)
{
    this.username = username;
    this.password = password;
}    

public String toString()
{
    return "User ---------" + "\n" + "Username: " + username + "\n" + "Password: " + password;
}

public String getUsername()
{
    return username;
}

public String getPassword()
{
    return password;
}

public boolean userValidate(String username, String password)
{
    if (username.contains(user.getUsername()) && password.contains(user.getPassword()))
    {
        System.out.println("User accepted");
        return true;
    }else
        System.out.println("Access denied");
    return false;
}

public HashMap<String, Card> getCards(String username, String password)
{
    for(String value : userHash.keySet())
        if (user.userValidate(username, password) == true)
        {
            //return user's cards
            return true;
        }else
            return null; 
        return null;

}


public boolean addCard(Card card, String username, String password)
{
    if(user.userValidate(username, password) == true)
    {
        user.getCards(username, password);

    }
    return false;
}

The card which belongs to the user
tommybee :

Here is my design concept.

In this case, the user class can have many cards and each card has an id. The validation check must be saved every time when creation user.

The userValidate method can be checked the arguments with its own values then save it to isvalid variable(boolean).

public boolean userValidate(String username, String password)
{
    if (username.contains(getUsername()) && password.contains(getPassword()))
    {
        System.out.println("User accepted");
        return isvalid = true;
    }else
        System.out.println("Access denied");
    return isvalid = false;
}

The getCards method is simple because it checks that the isvalid is true or not.

public HashMap<String, Card> getCards() {
    if (isvalid) {
        // return user's cards
        return userHash;
    } else
        return null;

}

The addCard method, I can just save the card with card id, if user validation is success or nothing...

public boolean addCard(Card card, String username, String password)
{
    if(userValidate(username, password))
    {
        userHash.put(card.getCardId(), card);
        return true;

    }
    return false;
}

So, come together with all these codes.

import java.util.HashMap;

class Card 
{
    private String cardId;

    public String getCardId() {
        return cardId;
    }

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

}

public class User {
    private String username;
    private String password;
    private boolean isvalid;


    HashMap<String, Card> userHash = new HashMap<String, Card>(); // key is the
                                                                    // cardID

    public User(String username, String password) {
        this.username = username;
        this.password = password;

    }


    public boolean userValidate(String username, String password)
    {
        if (username.contains(getUsername()) && password.contains(getPassword()))
        {
            System.out.println("User accepted");
            return isvalid = true;
        }else
            System.out.println("Access denied");
        return isvalid = false;
    }

    public String toString() {
        return "User ---------" + "\n" + "Username: " + username + "\n" + "Password: " + password;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public HashMap<String, Card> getCards() {
        if (isvalid) {
            // return user's cards
            return userHash;
        } else
            return null;

    }

    public boolean addCard(Card card, String username, String password)
    {
        if(userValidate(username, password))
        {
            userHash.put(card.getCardId(), card);
            return true;

        }
        return false;
    }


}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=109036&siteId=1