0

I'm struggling to figure out how to return the most popular hotel rooms. Basically I have an ArrayList of booking objects that include the room name and length of booking which was read in from a csv file. I need to figure out how to return the most popular rooms which I have 0 clue how to do. I need to somehow add up all the individual lengthofbookings for each room.

this.roomName = roomName;
this.date = date;
this.time = time;
this.lengthOfBooking = lengthOfBooking;


 public String[] getMostPopularRooms(int n){
    String[] popularRooms = new String[n];

    return popularRooms;

 } 
fuentesj
  • 154
  • 7
void45
  • 7
  • 5
  • May be you can use a data structure like a map... something like map ... – Eduardo Pascual Aseff Mar 24 '20 at 15:52
  • By what criteria do you measure the popularity of a room? – Abra Mar 24 '20 at 15:53
  • which room has the longest length of booking – void45 Mar 24 '20 at 15:54
  • So you want to sum the days each room was booked and then display the list of rooms sorted by total days booked - in descending order? – Abra Mar 24 '20 at 15:56
  • no I would just want to sum the length of booking the date or time doesn't matter sorry I should've been more specific. e.g. room 1 has 2 different bookings the length of booking for booking 1: is 10 and booking 2: is 5 so I want to figure out how to sum up all the length of bookings for each room and then sort it by the total length of booking – void45 Mar 24 '20 at 16:01

1 Answers1

0

You make a Booking class that carries member variables such as length of the stay. Similar to what you show at the top of your code example. (Your getMostPopularRooms method does not belong on this class. It should go elsewhere.)

You collect Booking objects, apparently read from a CSV file according to your Question. Each row in the CSV is used to produce one Booking object. Tip: Use a library such as Apache Commons CSV to assist with the chore of reading the CSV file. You will produce a List or Set of Booking objects.

Instantiate a Map to hold room names, for each of which you will store a number of days totaled across all the booking objects. Probably TreeMap to keep the room names in order. From your Question, I deduce that you are tracking each room name using String.

Tip: If you were to add the ThreeTen-Extra library to your project, you could use Days type here instead of Integer. For your homework assignment rather than real work, use Integer.

For efficiency, you could set the initial capacity to the known number of rooms.

Map< String , Integer > roomPopularity = new TreeMap<>( numberOfRooms ) ;

Now loop your collection of Booking objects. For each one, look up the matching key in the Map. If not already present, add the key to map. Then retrieve the Integer object for that room, and add that booking’s number of days to that number.

When finished, examine the values of the map to see which room is booked the most. This Question may help (not sure, didn't study).

All these pieces have been covered many times already on Stack Overflow. To learn more, search existing Questions and Answers.

You could probably use streams to good effect here. But as you are just starting with Java, use conventional syntax.

Your teacher may be intending for you to use simple arrays rather than collections. If so, the logic is similar. Create a pair of arrays, one for room name, and one for total days. As you read each CSV, look for the room name in the first array. If not found, add the room. Then look the same row number in the second array, for the number of days total. Add the incoming row's number of days to the total days. When done with the CSV, loop the elements of the total-days array, looking for the biggest number. Then bounce of to the array of array names to retrieve the name in the matching row.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154