Skip to content
Advertisement

Java – HashMap for booking system

I have been given an exercise to develop an appropriate data structure to implement a tennis court booking system.

A hashmap is what is required for this and I was given a diagram as a visual representation of the data structure.

From looking at the diagram, I am unclear on how the Key and Value should be defined in the HashMap. Should ‘Court’ be a class, which is the Key?

For the value, it shows a time, and a players name. How would the value be defined, since it has two values of time and player name?

diagram link

Advertisement

Answer

Say we have a HashMap named as “Date1”. Well, now i can put a value with a key in this “Date1” HashMap & later can retrieve that value with the key -> Date1.get("key")

According to your diagram, the keys will be court1, court2, ….

So that, when i will ask for Date1.get("court1"), i can get this ArrayList

But, in this arraylist, each row consist of two values. If our list had one string values in each row, we could say that this is a arraylist of string type.

So lets make our own class (like a type) which can store two values (like “9:00”, “player”)

public class EntryInfo{
    String time, player;
    
    public EntryInfo(String time, String player) {
        this.time = time;
        this.player = player;
    }
}

So if we create an object of “EntryInfo” class, that object will be able to store two values – and each row of our arraylist is of this type.

so, lets sum up :

our hashmap key will be “court1, court2…” and each key will hold values of ArrayList<EntryInfo> type.

So our hashmap will be like HashMap< String, ArrayList<EntryInfo> >


Now, lets imagine the scenario according to your diagram.

first, lets talk about usage scenario. Say, there are several courts in your school. They are named as court1, court2, court3, …

well, I want today’s entry-log for court1. So, i will just ask for feb9.get("court1"), and it will return a list (ArrayList), and i can traverse that array to see the log like following:

for(EntryInfo entryinfo : feb9.get("court1") ) {
    System.out.println(entryinfo.time + " : " + entryinfo.player);
}
    

Yeah, thats it. If i want to get the log for court2, i will retrieve the value using key “court2”

Now, lets discuss how to make this.

well today is feb9, lets make a hashmap to store today’s entry log. Lets name the hashmap “feb9”

HashMap<String, ArrayList<EntryInfo>> feb9 = new HashMap<String, ArrayList<EntryInfo>>();

your school has 3 courts. Lets add 3 different keys to this hashmap so that it can hold 3 separate and corresponding entry-logs for each key(court)

feb9.put("court1", new ArrayList<EntryInfo>());
feb9.put("court2", new ArrayList<EntryInfo>());
feb9.put("court3", new ArrayList<EntryInfo>());

You can relate this with creating 3 new files named court1, court2 & court3. Each file consists of an ArrayList object, which is empty for now.

Say, now at 9:00 am, player “zineryt” enters court1. Lets entry the log into our hashmap

feb9.get("court1").add(new EntryInfo("9:00", "zineryt"));

Here feb9.get("court1") will return an ArrayList, and we used add() method to insert elements into the ArrayList.

say, at 9:50am, player “bigboy” enters court1. We can entry the log like below

feb9.get("court1").add(new EntryInfo("9:50", "bigboy"));

and it goes like this.

For tomorrow’s information, we will create another new HashMap named feb10 and will follow the same steps.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement