Just how verbose you make your code is at least partly a matter of personal choice. Code that is either too verbose ro too terse can be hard to maintain. Generally speaking, experienced programmers would rather deal with code that is reasonably terse. Excess verbosity is only favored by those new to the field.
Assigning a int return value to a new Integer(...) is just wasteful, since the Integer is immediately assigned to a int variable through auto(un)boxing. Computing and assigning totalPrice in the context of if and else could only be meaningful if the value is used once, outside the if/else blocks.
if (time == 0) {
totalPrice = (minutes * dayTimePrice) + openPrice;
}
else if (time == 1) {
totalPrice = (minutes * nightTimePrice) + openPrice;
}
JOptionPane.showMessageDialog(null, "The total cost is " + totalPrice);
I have a major issue with two lines of code seen in both your code and the author's.
if (answer == 0)
:
else if (answer =1)
Is that 'Yes' or 'No'? That JOptionPane.showConfirmDialog returns 0 when the 'Yes' option is selected is an implementation detail of JOptionPane, and any use of the numeric value outside JOptionPane.java is what is called using a 'magic number'. The correct way is to test against the class constant.
if (answer == JOptionPane.YES_OPTION)
:
else if (answer == JOptionPane.NO_OPTION)
db
edit The 'else' after if ... else if is redundant. If the showConfirmDialog returns CANCEL_OPTION, neither 'if' condition will be satisfied and control will fall through to the next statement after the if .. else if blocks, which is exit(0) -- the same as in the last else condition.