Loading...
Loading...
Loading...
Tickets print class codes, not Java names. Declare enum TrainClass with a private final code and a fare perKm in rupees: SLEEPER("SL", 1), AC_3_TIER("3A", 3), AC_2_TIER("2A", 4), AC_FIRST("1A", 7).
Write static TrainClass fromCode(String code) that throws IllegalArgumentException when no class has that code. For each code, print the class and the fare for the trip, or that the code is invalid.
CODE CLASS Rs. fare or CODE invalid.Example
Input
500 3 3A SL 2S
Output
3A AC_3_TIER Rs. 1500 SL SLEEPER Rs. 500 2S invalid
2S (second sitting) is not in this enum.
Try the problem first. Open one hint at a time.
fromCode loops over values() and compares t.code.equals(code).
Call it inside try; print invalid in the catch block.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor