patternjavaMinor
"Merchants Guide to Galaxy" challenge
Viewed 0 times
challengegalaxymerchantsguide
Problem
I have solved the classic "Merchants Guide to Galaxy" problem:
A merchant buys and sells items in the galaxy. Buying and selling over
the galaxy requires you to convert numbers and units. The numbers
used for intergalactic transactions follows similar convention to the
roman numerals. Roman numerals are based on seven symbols:
Numbers are formed by combining symbols together and adding the
values. For example, MMVI is 1000 + 1000 + 5 + 1 = 2006. Generally,
symbols are placed in order of value, starting with the largest
values. When smaller values precede larger values, the smaller values
are subtracted from the larger values, and the result is added to the
total. For example MCMXLIV = 1000 + (1000 − 100) + (50 − 10) + (5 − 1)
= 1944.
The symbols "I", "X", "C", and "M" can be repeated three times in
succession, but no more. (They may appear four times if the third and
fourth are separated by a smaller value, such as XXXIX.) "D", "L", and
"V" can never be repeated. "I" can be subtracted from "V" and "X"
only. "X" can be subtracted from "L" and "C" only. "C" can be
subtracted from "D" and "M" only. "V", "L", and "D" can never be
subtracted. Only one small-value symbol may be subtracted from any
large-value symbol. A number written in [16]Arabic numerals can be
broken into digits. For example, 1903 is composed of 1, 9, 0, and 3.
To write the Roman numeral, each of the non-zero digits should be
treated separately. Inthe above example, 1,000 = M, 900 = CM, and 3 =
III. Therefore, 1903 = MCMIII. Input to your program consists of
lines of text detailing your notes on the conversion between
intergalactic units and roman numerals. You are expected to handle
invalid queries appropriately.
Test input
`glob is I
prok is V
pish is X
tegj is L
glob glob Silver is 34 Credits
glob prok Gold is 57800 Credits
pish pish Iron is 3910 Credits
how much is pish tegj glob glo
A merchant buys and sells items in the galaxy. Buying and selling over
the galaxy requires you to convert numbers and units. The numbers
used for intergalactic transactions follows similar convention to the
roman numerals. Roman numerals are based on seven symbols:
I 1
V 5
X 10
L 50
C 100
D 500
m 1000
Numbers are formed by combining symbols together and adding the
values. For example, MMVI is 1000 + 1000 + 5 + 1 = 2006. Generally,
symbols are placed in order of value, starting with the largest
values. When smaller values precede larger values, the smaller values
are subtracted from the larger values, and the result is added to the
total. For example MCMXLIV = 1000 + (1000 − 100) + (50 − 10) + (5 − 1)
= 1944.
The symbols "I", "X", "C", and "M" can be repeated three times in
succession, but no more. (They may appear four times if the third and
fourth are separated by a smaller value, such as XXXIX.) "D", "L", and
"V" can never be repeated. "I" can be subtracted from "V" and "X"
only. "X" can be subtracted from "L" and "C" only. "C" can be
subtracted from "D" and "M" only. "V", "L", and "D" can never be
subtracted. Only one small-value symbol may be subtracted from any
large-value symbol. A number written in [16]Arabic numerals can be
broken into digits. For example, 1903 is composed of 1, 9, 0, and 3.
To write the Roman numeral, each of the non-zero digits should be
treated separately. Inthe above example, 1,000 = M, 900 = CM, and 3 =
III. Therefore, 1903 = MCMIII. Input to your program consists of
lines of text detailing your notes on the conversion between
intergalactic units and roman numerals. You are expected to handle
invalid queries appropriately.
Test input
`glob is I
prok is V
pish is X
tegj is L
glob glob Silver is 34 Credits
glob prok Gold is 57800 Credits
pish pish Iron is 3910 Credits
how much is pish tegj glob glo
Solution
Preface: I did not address everything that feels off. I don't think I have the skill to do so. But I hope that this already helps.
The biggest design problem that I see is that the
It is good that you use constants in
Some smaller things:
When an error happens, you print an error message, and e. If you would also do
While I know that you don't need a block for an
You use
Your methods should never just
The biggest design problem that I see is that the
ReadInputAndProcess class and the DisplayOutput class communicate only with the help of two public lists in DisplayOutput. This hides the communication between your classes in a way you don't want. In this case I would make the lists parameters of the processOutput() method. The readFileAndProcess() method would then return them. This way you can also easier understand the main method. When I read it at first I had no idea about the communication between those two classes.It is good that you use constants in
DisplayOutput for the text you print, but why are you not using constants for the text to compare to in ReadInputAndProcess?Some smaller things:
When an error happens, you print an error message, and e. If you would also do
e.printStackTrace(), you would get way more information.While I know that you don't need a block for an
if-statement, it makes the code harder to read. It also makes it harder to maintain. If I want to add another line there, I need to be careful to also add a block, otherwise it will behave weird.You use
Double.valueOf(intInAString).intValue() to parse an int. Integer.parseInt(intInAString) is more readable and will also fail on "12.5", as it should.Your methods should never just
throw Exception. This gives no information about where those might be thrown, and why. Throw more specific exceptions, and write a comment about when they might be thrown.Context
StackExchange Code Review Q#142409, answer score: 2
Revisions (0)
No revisions yet.