Home > Software design >  Inserting and searching for data from a HashMap
Inserting and searching for data from a HashMap

Time:03-06

There's something wrong with my solution and I can't quite figure it out. I would very much appreciate it, if someone could steer me in the right direction and get this working. When I'm calling my methods nothing seems to happen.

Here's the setters and getters for the worker parameters:

public class Worker {

private String id;
private String name;
private String location;

public Flight(String id, String name, String location) {
    this.id = id;
    this.name = name;
    this.location = location;
}

public String getID() {
    return id;
}

public void setID(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}

}

I want to have the worker data on the WorkerDB class with the methods to handle the search function and inserting new workers to the HashMap.

public class WorkerDB {

HashMap<Integer, Worker> workers = new HashMap<Integer, Worker>();

public WorkerDB() {
    workers.put(1, new Worker("W01", "John", "Workshop"));
    workers.put(2, new Worker("W02", "Steve", "Office"));
    workers.put(3, new Worker("W03", "Bob", "Gate"));
}

public String InsertWorkerData(String [] workerDataArray) {
    int sizeOfHashMap = Integer.parseInt(workerDataArray[0]);
    workers.put(sizeOfHashMap, new Worker(workerDataArray[1], workerDataArray[2], workerDataArray[3]);
    return "Worker data inserted successfully";     
}

public String searchWorker(String workerID) {
    String search = "Worker with ID: "   workerID   ", not found";
    for (int i = 1; i < workers.size(); i  ) {
            if(workers.get(i).getID().equals(workerID)) {
                search = workers.get(i).getID()   " "   workers.get(i).getName()   " "   workers.get(i).getLocation();
            }
        }
    return search;
}

}

Here I'm just calling the methods from the WorkerDB class.

public class Interface {

public static void main(String[] args) {
    
    WorkerDB workers = new WorkerDB(); 
    String[] testArray = {"4", "W04", "George", "Garage"};
    workers.searchWorker("W01");    
}

}

CodePudding user response:

I think you just have a few typos in your code:

First of all, in your Worker class, you need to define the constructor so that it is public Worker() and not public Flight():

public Worker(String id, String name, String location) {
    this.id = id;
    this.name = name;
    this.location = location;
}

Additionally, you had a misplaced parenthesis in your WorkerDB class:

workers.put(sizeOfHashMap, new Worker(workerDataArray[1], workerDataArray[2], workerDataArray[3]));

Also, since you are using a return statement to return the String, we need to use a print statement to output it in our main method:

System.out.println(workers.searchWorker("W03"));   

Result:

W01 John Workshop

Additionally, you should be going up until (and including) the length of the HashMap since you start at 0, so use <=:

for (int i = 1; i <= workers.size(); i  ) {

I hope this helped answer your question! Please let me know if you need any further details or clarification :)

  • Related