Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Train Ticket Calculator You will be writing a program to calculate the cost of a

ID: 3667240 • Letter: T

Question

Train Ticket Calculator

You will be writing a program to calculate the cost of a train ticket from Menlo Park to either New York, Philadelphia or Boston. The cost for a one way ticket is listed below. Peak hours are 5AM-8AM (M-F) and 3PM to 7PM (M-f). You will receive a 10% discount if you purchase a round trip ticket (the cost times 2 minus the 10%). Senior Citizens will receive a 20% discount off of the total per ticket price cost. Student tickets get a 5% discount off of the total per ticket price. If you purchase in a quantity of 10 or more you receive $10.00 off of the total cost of the tickets (you may assume all tickets are same in regards to Senior, peak roundtrip etc).

City Off Peak Hours Peak Hours

New York 14.90 17.90

Philadelphia 23.85 25.85

Boston 41.60 46.60

Your Train calculator will ask the user:

• Where they are going.

• Round Trip or One Way

• Peak or Off Peak hours.

• Number of tickets.

• Are they a Senior, Student.

Explanation / Answer

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class TrainTicketCalc {
   private static Map<String, Double> ticketFare = new HashMap<>();
   static {
       ticketFare.put("New_York_Peek", 17.90);
       ticketFare.put("New York__Peek_Off", 14.90);
       ticketFare.put("Philadelphia_Peek", 25.85);
       ticketFare.put("Philadelphia_Peek_Off", 23.85);
       ticketFare.put("Boston_Peek", 46.60);
       ticketFare.put("Boston_Peek_Off", 41.60);
   }

   public static void main(String[] args) throws Exception {
       BufferedReader reader = new BufferedReader(new InputStreamReader(
               System.in));
       String[] dest = new String[] { "New_York", "Philadelphia", "Boston" };
       String[] travelType = new String[] { "Round", "One_Way" };
       String[] timeOfTravel = new String[] { "Peek", "Peek_Off" };
       String[] passengerType = new String[] { "Senior", "Student", "Other" };

       System.out
               .println("Destination : 1.New York 2.Philadelphia 3.Boston : ");
       int destOption = Integer.parseInt(reader.readLine()) - 1;

       System.out.println("Round Trip or One Way 1.Round 2.One way : ");
       int wayOption = Integer.parseInt(reader.readLine()) - 1;

       System.out.println("Peak or Off Peak hours. 1.peek 2.peek off : ");
       int timeOption = Integer.parseInt(reader.readLine()) - 1;

       System.out.println("Number of tickets : ");
       int ticketCount = Integer.parseInt(reader.readLine());

       System.out.println("Passenger Type. 1.Senior 2.Student 3.Others");
       int passengerTypeOpt = Integer.parseInt(reader.readLine()) - 1;

       double discountedPrice = calculateTicketPrice(dest[destOption],
               travelType[wayOption], timeOfTravel[timeOption],
               passengerType[passengerTypeOpt], ticketCount);

       System.out.println("Discounted Total Price from " + "Menlo Park to"
               + dest[destOption] + " : $" + round(discountedPrice, 2)
               * ticketCount);
   }

   private static double calculateTicketPrice(String dest, String travelType,
           String timeOfTravel, String passengerType, int ticktCounts) {
       double ticketPrice = ticketFare.get(dest + "_" + timeOfTravel);
       double discount = 0.0;
       if ("Round".equalsIgnoreCase(travelType)) {
           discount += ticketPrice * (10.0f / 100.0f);
       }
       if ("Senior".equalsIgnoreCase(passengerType)) {
           discount += ticketPrice * (20.0f / 100.0);
       }
       if ("Student".equalsIgnoreCase(passengerType)) {
           discount += ticketPrice * (5.0f / 100.0f);
       }
       if (ticktCounts >= 10) {
           discount += 10.0;
       }

       ticketPrice = ticketPrice - discount;

       return ticketPrice;
   }

   private static double round(double value, int places) {
       if (places < 0)
           throw new IllegalArgumentException();

       long factor = (long) Math.pow(10, places);
       value = value * factor;
       long tmp = Math.round(value);
       return (double) tmp / factor;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote