// Arup Guha
// 11/4/2017
// Edit of class Car shown in Junior Knights.
// Idea was to add a "racing" component where cars go a distance and fill up their
// tank over and over again until they finish the that distance and we calculate 
// the total time, including stops.

import java.util.*;

public class Car2 {

	// Instance variables that comprise a Car object.
	private int fuelTankSize;
	private int mpg;
	private double currentFuel;
	private double odometer;

	/*** Added for Car2 class from Car class. ***/
	private int refuelMinutes;
	private double maxMPH;

	// Constructor - builds a car2 object
	public Car2(int tankSize, int fuelEff, int minutesToFuel, double maxSpeedMPH) {

		// My job is to give values to the instance variables.
		fuelTankSize = tankSize;
		mpg = fuelEff;
		currentFuel = tankSize;
		odometer = 0;
		refuelMinutes = minutesToFuel;
		maxMPH = maxSpeedMPH;
	}

	// From Car class...
	public double milesToEmpty() {
		return currentFuel*mpg;
	}

	// Just fills up the tank.
	public void fillTank() {
		currentFuel = fuelTankSize;
	}

	// From Car class...
	public void addFuel(double numGallons) {

		// You're trying to put in too much, we'll just give you a full tank.
		if (numGallons+currentFuel > fuelTankSize)
			currentFuel = fuelTankSize;

		// Add the gas...
		else if (numGallons > 0)
			currentFuel = currentFuel + numGallons;
	}

	// Old drive method.
	public void drive(int distance) {

		double canDrive = milesToEmpty();

		// You want to drive to far...I'll make you stuck after driving
		// canDrive miles.
		if (distance > canDrive) {
			currentFuel = 0;
			odometer = odometer + canDrive;
		}

		// Okay, this is reasonable.
		else if (distance > 0) {
			currentFuel = currentFuel - (double)distance/mpg;
			odometer = odometer + distance;
		}
	}

	// This drives distance miles and returns how minutes it took to do so.
	// Automatically fills tank every time we get to empty.
	public double driveInRace(double distance) {

		double totTime = 0;

		// Keep on driving until I have enough in the tank to finish the race.
		while (distance > milesToEmpty()) {

			// Calculate how far I can go to empty and how long it will take.
			double curDrive = milesToEmpty();
			double curHours = curDrive/maxMPH;
			double curMinutes = curHours*60;

			// Add up time for drive and refueling.
			totTime = totTime + curMinutes + refuelMinutes;

			// New odometer reading.
			odometer = odometer + curDrive;

			// In a race we automatically fill the tank.
			fillTank();

			// Update distance left to drive!!!
			distance = distance - curDrive;
		}

		// We might have to drive some more, but not refuel...

		// Calculate time of remaining segment.
		double curHours = distance/maxMPH;
		double curMinutes = curHours*60;
		
		// Update Time, Odometer and Fuel.
		totTime = totTime + curMinutes;
		odometer = odometer + distance;
		currentFuel = currentFuel - distance/mpg;

		// Must return how long we drove.
		return totTime;
	}

	public String toString() {
		return "Odometer: "+odometer+" Fuel Left: "+currentFuel;
	}

	public static void main(String[] args) {

		// Test a race between 2 cars!
		Car2 myPrius = new Car2(12, 40, 3, 100);
		Car2 badGasGuzzler = new Car2(30, 8, 20, 110);

		Scanner stdin = new Scanner(System.in);
		System.out.println("How long is your race in miles?");
		int miles = stdin.nextInt();

		double totMinPrius = myPrius.driveInRace(miles);
		double totMinGasGuzzler = badGasGuzzler.driveInRace(miles);

		System.out.println("The Prius took "+totMinPrius+" minutes to finish the race.");
		System.out.println("The Gas Guzzler took "+totMinGasGuzzler+" minutes to finish the race.");

		if (totMinPrius < totMinGasGuzzler)
			System.out.println("The Prius wins!!!");
		else
			System.out.println("The Gas Guzzler wins, boo :(");
	}

}