Which data structure should I use in Java?

Michal Dobrzycki

I'm doing Car Rental app in Java.

  1. I have class Car with Strings RegNo, producer, model and boolean isCarRented.
  2. List of cars I'm keeping in:

    Collection<Car> carList = new HashSet<Car>();

Everything works fine.

Now what I need to do is history/statistic module for whole rental company:

  • history of all car rentals

  • rental history for each car separately

My idea is to:

  1. Create class CarHistory with:

    private static List<String> rentalDates = new ArrayList<String>();

  2. Keeping there dates which I'm gonna add every time the car is rented.

  3. Create data structure to remember each car rental history like this:

    static Map<Car, CarHistory> rentalList = new HashMap<Car, CarHistory>();

Is it a good way? I do have troubles with constructor for single CarHistory in this solution. Not really sure what it should return. Should I create it after first rental? And should create empty List rentalDates for each car to create HashMap?

Simon

What you are trying to do is to implement a one-to-one relationship, because a Car has only one CarHistory and one CarHistory concerns only one Car. That is why, the correct way of doing it would be to add field CarHistory carHistory to the class of Car.

In the beginning, the list of CarHistory would be empty. With each reservation, you would simply add one record to the list. The car history would be easily accessible, and the model would match the reality in the most accurate way.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Which data structure should I use to handle multi value data?

From Dev

Which data structure should I use to search a string from CSV?

From Dev

Which data structure should I use for maintaining this information?

From Dev

Which data structure in Java or Python should I use when I am trying to test requests to PHP-based server which expects array with String indexes?

From Dev

Which data structure should I use for having sorted data, where the key can be used on multiple entries

From Dev

Which data structure should I use to have fast deletion/insertion and max (or min) search?

From Dev

Which data structure should I use to support key-value mappings, reverse iteration and insertion ordering?

From Java

Which Java Collection should I use?

From Dev

Which version of Java should I use for Minecraft?

From Dev

Which version of Java should I use for Minecraft?

From Dev

Which java database should I use

From Dev

What data structure should I use to mimic "order by counter" in Cassandra?

From Dev

What data structure should I use for sentiment analysis?

From Dev

What data structure should I use for a music play queue?

From Dev

What data structure should I use to represent this board?

From Dev

What data structure should I use for sentiment analysis?

From Dev

What data structure should I use to mimic "order by counter" in Cassandra?

From Dev

What data structure I should use for my dictionary?

From Dev

Displaying and manipulating data in a windows form, Which control should I use?

From Dev

which library should I use for big data project

From Dev

which class should I use to collect and store different data types?

From Dev

Which method should I use to send data to Apple Watch and back?

From Dev

which class should I use to collect and store different data types?

From Dev

Which method should I use to send data to Apple Watch and back?

From Dev

Which clustering algorithm should I use for frequency data?

From Dev

Which data type should i use to store YouTube video durations?

From Dev

When to use which Data Structure?

From Dev

Not sure which data structure to use

From Dev

Which version of Java should I use for Clojure (performance)?

Related Related

  1. 1

    Which data structure should I use to handle multi value data?

  2. 2

    Which data structure should I use to search a string from CSV?

  3. 3

    Which data structure should I use for maintaining this information?

  4. 4

    Which data structure in Java or Python should I use when I am trying to test requests to PHP-based server which expects array with String indexes?

  5. 5

    Which data structure should I use for having sorted data, where the key can be used on multiple entries

  6. 6

    Which data structure should I use to have fast deletion/insertion and max (or min) search?

  7. 7

    Which data structure should I use to support key-value mappings, reverse iteration and insertion ordering?

  8. 8

    Which Java Collection should I use?

  9. 9

    Which version of Java should I use for Minecraft?

  10. 10

    Which version of Java should I use for Minecraft?

  11. 11

    Which java database should I use

  12. 12

    What data structure should I use to mimic "order by counter" in Cassandra?

  13. 13

    What data structure should I use for sentiment analysis?

  14. 14

    What data structure should I use for a music play queue?

  15. 15

    What data structure should I use to represent this board?

  16. 16

    What data structure should I use for sentiment analysis?

  17. 17

    What data structure should I use to mimic "order by counter" in Cassandra?

  18. 18

    What data structure I should use for my dictionary?

  19. 19

    Displaying and manipulating data in a windows form, Which control should I use?

  20. 20

    which library should I use for big data project

  21. 21

    which class should I use to collect and store different data types?

  22. 22

    Which method should I use to send data to Apple Watch and back?

  23. 23

    which class should I use to collect and store different data types?

  24. 24

    Which method should I use to send data to Apple Watch and back?

  25. 25

    Which clustering algorithm should I use for frequency data?

  26. 26

    Which data type should i use to store YouTube video durations?

  27. 27

    When to use which Data Structure?

  28. 28

    Not sure which data structure to use

  29. 29

    Which version of Java should I use for Clojure (performance)?

HotTag

Archive