I have a list with "Booking" Objects . "Booking" has these attributes: - Long roomId. Date fecha. Long order. Double price.
On a certain moment, this list could be:
1 -- 22/07/2016 -- 15
1 -- 23/07/2016 -- 15
1 -- 24/07/2016 -- 15
4 -- 01/08/2016 -- 25
4 -- 02/08/2016 -- 25
4 -- 03/08/2016 -- 25
4 -- 04/08/2016 -- 25
It means, there is a Booking between July 22 and 24 for room 1, total value 45. There is a Booking between August 1 and 4 for room 4, value 100.
I want to make a new list of "OrderDetail" objects. "OrderDetail" object would have these attributes:
- roomId, InitialDate, FinalDate, price
So, with my list, It would create two OrderDetail objects and it would add to an OrderDetail list. These objects would be:
Could someone help me? I think it´s not a difficult code but I do not usually program so I´m having some problems to make it work.
This is my shitty code:
L1 = (this is a database query)
L2 = (this is the same query) (so I have two identical lists)
List<OrderDetail> L3 = new ArrayList<OrderDetail>();
Long roomId = null;
Date InititalDate;
Date FinalDate;
double price = 0;
for (int i = 0; i < L1.size(); i++) {
InititalDate = null;
Booking current = L1.get(i);
roomId = current.getRoomId();
InititalDate = current.getFecha();
Iterator<Booking> it = L2.iterator();
while (it.hasNext()) {
Booking current2 = it.next();
if (current2.getRoomId.equals(roomId)) {
precio = precio + current2.getPrecio();
FinalDate = current2.getFecha();
i++;
}
}
OrderDetail = new OrderDetail(roomId, InitialDate, FinalDate, precio);
L3.add(OrderDetail);
}
return L3;
}
I am learning Java8
, just tried to implement what you have asked, with streams
Booking b1 = new Booking(1L, LocalDate.of(2016, 7, 22), 15d);
Booking b2 = new Booking(1L, LocalDate.of(2016, 7, 23), 15d);
Booking b3 = new Booking(1L, LocalDate.of(2016, 7, 24), 15d);
Booking b4 = new Booking(4L, LocalDate.of(2016, 8, 1), 25d);
Booking b5 = new Booking(4L, LocalDate.of(2016, 8, 2), 25d);
Booking b6 = new Booking(4L, LocalDate.of(2016, 8, 3), 25d);
Booking b7 = new Booking(4L, LocalDate.of(2016, 8, 4), 25d);
List<Booking> bookings = Arrays.asList(b1, b2, b3, b4, b5, b6, b7);
List<OrderDetail> orderDetails = bookings
.stream().collect(Collectors.groupingBy(Booking::getRoomId)).values().stream().map(i -> new OrderDetail(i.get(0).getRoomId(),
i.get(0).getBookedDate(), i.get(i.size() - 1).getBookedDate(), i.stream().collect(Collectors.summingDouble(Booking::getPrice))))
.collect(Collectors.toList());
System.out.println(orderDetails);
output
[OrderDetail [roomId=1, startDate=2016-07-22, endDate=2016-07-24, totalPrice=45.0], OrderDetail [roomId=4, startDate=2016-08-01, endDate=2016-08-04, totalPrice=100.0]]
Please note : I believe there might be a better way of implementing this, please add your answer happy to learn from that
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.