I have to translate a given text to another format.
For example, it is the given text:
#The number of cars that you can put in garage = 4
t1n1b2p2t1m1b2a2
v1j1t1c1b3t1b1a1m1j1b4
I have to change it to:
#The number of cars that you can put in garage = 4
Tesla Nissan BMW | Porsche Tesla Maserati | BMW Audi |
Volvo Jaguar Tesla Cadillac | BMW Tesla | BMW Audi Maserati Jaguar | BMW |
But there is an other situation:
#The number of cars that you can put in garage is different
^3
t1b1t1b3
^2
m2a1t1
^4
a4v2p2
I have to change it to:
#The number of cars that you can put in garage is different
#Number of cars = 3
Tesla BMW Tesla | BMW |
#Number of cars = 2
Maserati | Audi Tesla |
#Number of cars = 4
Audi | Volvo Porsche |
CodePudding user response:
First the code. Explanations after the code.
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Garages {
private static final Map<Character, String> CAR_MAKES = Map.of('a', "Audi",
'b', "BMW",
'c', "Cadillac",
'j', "Jaguar",
'm', "Maserati",
'n', "Nissan",
'p', "Porsche",
't', "Tesla",
'v', "Volvo");
private static final Pattern REGEX = Pattern.compile("\\d $");
private static String handleLine(String line, int limit) {
StringBuilder result = new StringBuilder();
int total = 0;
char[] letters = line.toCharArray();
for (int j = 0; j < letters.length; j ) {
if (j % 2 == 0) {
// should be a letter
String make = CAR_MAKES.get(letters[j]);
result.append(make);
}
else {
// should be a number
int amount = letters[j] - '0';
for (int k = 1; k < amount; k ) {
result.append(' ');
}
result.append(' ');
total = amount;
if (total >= limit) {
result.append("| ");
total = 0;
}
}
}
return result.toString();
}
private static String handleLines(String[] lines) {
StringBuilder result = new StringBuilder();
result.append(lines[0]);
result.append(System.lineSeparator());
Matcher matcher = REGEX.matcher(lines[0]);
int limit = 0;
boolean hasLimit = matcher.find();
if (hasLimit) {
limit = Integer.parseInt(matcher.group());
}
for (int i = 1; i < lines.length; i ) {
if (hasLimit) {
result.append(handleLine(lines[i], limit));
}
else {
if (i % 2 == 1) {
// amount line
limit = Integer.parseInt(lines[i].substring(1));
result.append("#Number of cars = " limit);
}
else {
result.append(handleLine(lines[i], limit));
}
}
result.append(System.lineSeparator());
}
return result.toString();
}
private static String handleInput(String input) {
return handleLines(input.split("\n"));
}
public static void main(String[] args) {
String first = """
#The number of cars that you can put in garage = 4
t1n1b2p2t1m1b2a2
v1j1t1c1b3t1b1a1m1j1b4
""";
System.out.println(handleInput(first));
String second = """
#The number of cars that you can put in garage is different
^3
t1b1t1b3
^2
m2a1t1
^4
a4v2p2
""";
System.out.println("====================================================================");
System.out.println(handleInput(second));
}
}
- The above code is based solely on the data in your question and does not check for invalid data.
- The code uses text blocks that were first added in JDK 13. (Refer to variables
first
andsecond
in methodmain
in above code.) In text blocks the line separator is\n
. - Static method
of
was added to Map interface in JDK 9. I populated the map according to the data in your question. - The
Map
key type must be a class (and not a primitive). Hence the key type isjava.lang.Character
(and notchar
). Autoboxing automatically converts thechar
literal to aCharacter
object. - The first line of the input determines how the rest of the input should be parsed and handled. I use a regular expression for this. I check to see whether the first line of the input ends in a number.
- After the first line, I assume that each subsequent line of the input is either
^
character followed by a number or a line where every odd character (i.e. first, third, fifth, etc) is a letter and every even character is a digit. Hence the code will probably throw an exception – or give incorrect results – if the input does not follow this pattern.
Hopefully, the rest of the code is clear and self-explanatory.
This is the result I get when I run the above code.
#The number of cars that you can put in garage = 4
Tesla Nissan BMW | Porsche Tesla Maserati | BMW Audi |
Volvo Jaguar Tesla Cadillac | BMW Tesla | BMW Audi Maserati Jaguar | BMW |
====================================================================
#The number of cars that you can put in garage is different
#Number of cars = 3
Tesla BMW Tesla | BMW |
#Number of cars = 2
Maserati | Audi Tesla |
#Number of cars = 4
Audi | Volvo Porsche |