Java / Other Minor Java Work / EF_RoadTrip.java
EF_RoadTrip.java
Raw
/**
 * Program Name: EF_RoadTrip.java
 * Purpose: Calculates the total trip time using legs and their information provided by the user.
 * Coder: Essam Fahmy
 * Date: Apr 18, 2022
 */

import java.util.Scanner;

public class EF_RoadTrip
{

	public static void main(String[] args)
	{
		
		//Creating Scanner and Variable to Gain User Input
		
		Scanner input = new Scanner (System.in);
		int legs;
		
		//Outputting Program Title and Information and Gaining User Input
		
		System.out.println("Road Trip Time Estimator\n\nThis program will estimate the time required to complete a road trip.\n");
		System.out.print("Enter the number of 'legs' for your road trip: ");
		legs = input.nextInt();
		System.out.println();
		
		//Declaring Required Arrays
		
		double[] distancesArray = new double[legs];
		int[] speedsArray = new int[legs];
		double[] timesArray = new double[legs];
		
		//Gaining User Input for Each Leg for Distances and Speed Limit and Validating the Data Before Assigning it to the Appropriate Arrays
		
		for(int i = 0; i < legs; i++)
		{
			System.out.print("Enter the distance in km for leg " + (i + 1) + ": ");
			distancesArray[i] = input.nextDouble();
			System.out.print("Enter the speed limit in km/h for leg " + (i + 1) + ": ");
			speedsArray[i] = input.nextInt();

			while(speedsArray[i] > 110)
			{
				System.out.print("The speed limit can not be greater than 110. Please enter another number: ");
				speedsArray[i] = input.nextInt();
			}
			System.out.println();
		}
		
		//Closing the Scanner
		
		input.close();
		
		//Calculating the Time for Each leg and Assigning it to the timesArray Array
		
		for(int i = 0; i < legs; i++)
		{
			timesArray[i] = distancesArray[i] / speedsArray[i];
		}
		
		//Outputting the report for each leg
		
		for(int i = 0; i < legs; i++)
		{
			System.out.println("Leg " + (i + 1) + ": " + distancesArray[i] + " km at " + speedsArray[i] + " km/h will take " + formatTime(timesArray[i]));
		}
		
		System.out.println();
		
	  //Outputting the final report while calling the getArrayTotal and formatTime methods to do some calculations and formating
		
		System.out.println("The entire journey of " + getArrayTotal(distancesArray) + " km will take " + formatTime(getArrayTotal(timesArray)));
	}
	//end main
	
	/**
	 * Method Name: formatTime()
	 * Purpose: a public method that formats time from double into string form.
	 * Accepts: 1 double
	 * Returns: A String
	 * Date: Apr 18, 2022 
	*/
	
	public static String formatTime(double time)
	{
		int hours = (int)time;
		double temp = (time - hours) * 60;
		int minutes = (int) temp;
		String report;
		report = hours + " hours and " + minutes + " minutes.";
		return report;
	}
	
	/**
	 * Method Name: getArrayTotal()
	 * Purpose: a public method that returns the sum of all elements in a double array
	 * Accepts: 1 double array
	 * Returns: A double
	 * Date: Apr 18, 2022 
	*/
	
	public static double getArrayTotal(double[] addArray)
	{
		double sum = 0;
		
		for(int i = 0; i < addArray.length; i++)
		{
			sum += addArray[i];
		}
		
		return sum;
	}
	
}
//end class