Problem Statement
We have a bunch of old client data, and we want to convert it to XML to make it easier to work with in the future. Each of our clients has a name, an address, and an optional sales number. Each client is represented by the 3 elements of the inputs name, address, and sales with the same indices. An entry of -1 in sales indicates that this information is missing about that particular client. You are to convert each client to XML as follows, assuming the client's name is "NAME", address is "ADDRESS", and sales is 555:
Your task is to convert each client into a single line XML record as described above. You should then return a String[], the ith element of which corresponds to the XML record of the ith client. Notice that there is no need to actually generate the pretty version of the XML with the extra whitespace, you can make the compressed version directly.
<client>
<name>
NAME
</name>
<address>
ADDRESS
</address>
<sales>
555
</sales>
</client>
If the sales entry for a client is missing (-1), then do not include that entry in the XML. For example, had sales been -1 in the above example, you would have converted to XML as:
<client>
<name>
NAME
</name>
<address>
ADDRESS
</address>
</client>
To ensure consistency in our XML, you must always put the name entry before the address entry, and the address entry before the sales entry, if it exists. Furthermore, to make our XML files smaller, we are going to generate them without the extra whitespace, using a single line per customer. For example, the above two clients would be converted to the following (respectively):
<client><name>NAME</name><address>ADDRESS</address><sales>555</sales></client> <client><name>NAME</name><address>ADDRESS</address></client>Note that there may be spaces within a client's name or address, and these should be preserved, but you should not add any additional whitespace.
Your task is to convert each client into a single line XML record as described above. You should then return a String[], the ith element of which corresponds to the XML record of the ith client. Notice that there is no need to actually generate the pretty version of the XML with the extra whitespace, you can make the compressed version directly.
Definition
- Class:
- ClientToXML
- Method:
- convert
- Parameters:
- String[], String[], int[]
- Returns:
- String[]
- Method signature:
- String[] convert(String[] name, String[] address, int[] sales)
- (be sure your method is public)
Notes
- sales entries in the return should be integers and should not have any extra leading 0's.
- Your return must follow the format described exactly. Even if your return has the same XML semantics, it will be judged wrong unless it follows the specification precisely (including capitalization).
Constraints
- name will contain between 1 and 50 elements, inclusive.
- address and sales will each have the same number of elements as name.
- Each element of name and address will contain between 1 and 50 characters, inclusive, and will have no leading or trailing spaces.
- Each element of sales will be between -1 and 1,000,000, inclusive.
- Each character in name and address will have ASCII value between 32 and 126, inclusive.
Examples
{"John P. Doe the 3rd", "A B"}{"555 1st St.", "C D"}{-1, 435}Returns: { "<client><name>John P. Doe the 3rd</name><address>555 1st St.</address></client>", "<client><name>A B</name><address>C D</address><sales>435</sales></client>" }{"Jenny","Bobby","Peg","A B C"}{"1 1st Ave.","555 55th st.","Rt 3","NY"}{-1,0,534,83911}Returns: { "<client><name>Jenny</name><address>1 1st Ave.</address></client>", "<client><name>Bobby</name><address>555 55th st.</address><sales>0</sales></client>", "<client><name>Peg</name><address>Rt 3</address><sales>534</sales></client>", "<client><name>A B C</name><address>NY</address><sales>83911</sales></client>" }{"name"}{"address"}{0}Returns: { "<client><name>name</name><address>address</address><sales>0</sales></client>" }