0

I'm making a dispatching (dispatcher?) program. While i managed to create "addReport" method i have issues with displaying all the reports (itering through the map). I think that every time i'm trying to add new elements they are replaced because identificator (UUID) is the same. What do you think, or maybe it is something different?

public class Dispatching {
    private String identificator;
    private Map<String, Report> reportMap;

    public Dispatching() {
        this.identificator = UUID.randomUUID().toString();
        this.reportMap = new HashMap<>();
    }

    void addReport(String message, ReportType type) {
        reportMap.put(identificator, new Report(type, message, LocalTime.now()));
    }

    void showReports() {
        for (Map.Entry element : reportMap.entrySet()) {
            System.out.println("uuid: " + element.getKey().toString()
                    + " " + element.getValue().toString());
        }
    }

}

public class Report {
    ReportType reportType;
    String reportMessage;
    LocalTime reportTime;


    public Report(ReportType reportType, String reportMessage, LocalTime reportTime) {
        this.reportType = reportType;
        this.reportMessage = reportMessage;
        this.reportTime = reportTime;

    }

    @Override
    public String toString() {
        return "Report{" +
                "reportType=" + reportType +
                ", reportMessage='" + reportMessage + '\'' +
                ", reportTime=" + reportTime +
                '}';
    }
}

public class Main {

    public static void main(String[] args) {
        Dispatching dispatching = new Dispatching();

        dispatching.addReport("heeeeelp",ReportType.AMBULANCE);
        dispatching.addReport("poliiiice",ReportType.POLICE);
        dispatching.addReport("treeee",ReportType.OTHER);

        dispatching.showReports();

    }



}

public enum ReportType {
    AMBULANCE,
    POLICE,
    FIRE_BRIGADE,
    ACCIDENT,
    OTHER
}
santana011
  • 119
  • 10

2 Answers2

5

You are generating the UUID only once in the constructor and reusing it inside addReport and eventually, map will only keep the last entry for the same key so generate a new ID using

void addReport(String message, ReportType type) {
        reportMap.put(UUID.randomUUID().toString(), new Report(type, message, LocalTime.now()));
    }
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • You are right, i tried it and it worked. But i would prefer if constructor of Report made custom identificator. Unfortunately i have no idea how to put is as a key in addReport method. How would you do it? – santana011 Jan 21 '20 at 17:34
  • @santana011 There are few ways, either use [static AtomicLong](https://stackoverflow.com/a/8939049/4936904) object to generate and use unique IDs or you can create a String ID field in the `Report` class and assign value to it inside constructor using `UUID.randomUUID().toString()` and use it's value as key for your hash map key. You can also [override hash code](https://stackoverflow.com/questions/2265503/why-do-i-need-to-override-the-equals-and-hashcode-methods-in-java) to generate unique id using `LocalTime` field and use it as key – Pavneet_Singh Jan 21 '20 at 17:42
  • you can create a String ID field in the Report class and assign value to it inside constructor using UUID.randomUUID().toString() and use it's value as key for your hash map key **<-----this** i dont know how to pass the reference to addReport method. i tried Report.getIdentificator but it doesnt even show up. How to do it? ` **void addReport(String message, ReportType type) { reportMap.put(, new Report(type, message, LocalTime.now())); }** `` What do i put here as a key? PS: Do you know how to make newlines in here? My comments are really messy :/ – santana011 Jan 21 '20 at 17:52
  • 1
    @santana011 A) Add field and initialise `class Report{ String ID; public Report(...){ID = } UUID.randomUUID().toString(); // other code...}`. B) use it `void addReport(String message, ReportType type) { Report report = new Report(type, message, LocalTime.now()); reportMap.put(report.ID, report); // create a getter for ID }` – Pavneet_Singh Jan 21 '20 at 17:57
  • OH THANK YOU. It worked, i hadnt thought of creating report variable in that method.... Silly me, thanks again. – santana011 Jan 21 '20 at 18:03
  • I am glad that I could help, Happy coding!! – Pavneet_Singh Jan 21 '20 at 18:07
2

Yes. You are right.

void addReport(String message, ReportType type) {
    reportMap.put(identificator, new Report(type, message, LocalTime.now()));
}

Your identificator value is the same for each report you are adding. Hence, it will overwrite the entry in the map.

You must use a different id for each report you add. The choice of this id depends on your use case. Maybe, you can create a UUID each time you insert into the map. Or, you can make each Report to have its own id.

It depends.. Should calling addReport for the same message and report type create two entries in reportMap? If no, then you need to have an id for a Report.

Thiyagu
  • 17,362
  • 5
  • 42
  • 79
  • Thank you for your time. I tried something like this: `void addReport(String message, ReportType type) { reportMap.put(UUID.randomUUID().toString(), new Report(type, message, LocalTime.now())); } ` And it worked. I think if constructor of Report would generate random UUID's it will be just better. But i dont know how to get that specific identificator to put as a key map in addReport method. How would you do it? – santana011 Jan 21 '20 at 17:27