简体   繁体   中英

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));
});
    }
}

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM