'How can I get Output like this in JAVA?

I need output like this :-

ID :- 1
    Hotel :- Hotel 1
    Packages :- 
            Package 1 = 10$
            Package 2 = 20$
            Package 3 = 30$
    
    ID :- 2
    Hotel :- Hotel 2
    Packages :- 
            Package 1 = 10$
            Package 2 = 20$
            Package 3 = 30$
    
    
    
    ID :- 3
    Hotel :- Hotel 3
    Packages :- 
            Package 1 = 10$
            Package 2 = 20$
            Package 3 = 30$

After that I want to get input as hotel ID, relevant package, and how many packages customers want. then I need to calculate the full amount.

This is my code;-

But this code's output never fullfill my requirement

import java.util.Arrays;
import java.util.LinkedHashMap;

public class HotelChainServicePublishImpl implements HotelChainServicePublish {
    
    LinkedHashMap<Integer, String[]> hotelList = new LinkedHashMap<Integer, String[]>();
    Double subTotal = 0.0;
    @Override
    public void getHotels() {
        hotelList.put(1, new String[] {"Hotel 1" , "100000.00"} );
        hotelList.put(2, new String[] {"Hotel 2" , "200000.00"} );
        hotelList.put(3, new String[] {"Hotel 3" , "300000.00"} );
    
        hotelList.forEach((k, v)  -> {System.out.println(" ");
        System.out.println(k + ")");
        Arrays.asList(v).stream().forEach(s -> System.out.println(s));
});
    }
}


Solution 1:[1]

Assuming you mean the nested indentations whith the phrase "output like this":

you can use the tab character \t in order to indent text. For nested indentation you will need to keep track of the indentation level as with each newline the indentation gets reset.

You can then compute a prefix for each line you print with

String prefix = "";
for (int i = 0; i < indentation_level; i++) {
    prefix += "\t";
}

You may need to pass the indentation level to other functions and/or increase it as needed.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Oliver Leenders