/**
* This class simulates an automobile's odometer and
* fuel guage operations. A person can drive the car,
* add fuel to the car.
*
* @author Chuck Bates
* @version 02.01.2010
*/
public class Auto
{
// instance variables - replace the example below with your own
private int odometer;
private double fuelGuage;
private int tankCapacity;
private int mpg;
private int initMiles;
private double fuelInTank;
private int distance;
private int trip;
/**
* Constructor for objects of class Auto
*/
public Auto()
{
// initialise instance variables
odometer = 0;
fuelGuage = 0;
tankCapacity = 15;
mpg = 25;
System.out.println("A simple automobile for Chuck has been initialized ");
}
/**
* constructs an automobile with values of 25mpg
* however much gas the person starts with as long
* as this is less than or equal to the tanks limit
*/
public Auto(int initMiles, double initFuel)
{
mpg = 25;
tankCapacity = 15;
if(initFuel <= tankCapacity)
{
fuelInTank = initFuel;
fuelGuage = initFuel;
odometer = initMiles;
fuelGuage = initFuel;
mpg = 25;
System.out.println("A custom automobile has been initialized");
}else{
System.out.println("Instance not created initFuel exceeds the tank capacity");
}
}
public double getfuelGuage(){
return fuelGuage;
}
public void addFuel(double amtToAdd)
{
if (tankCapacity >= amtToAdd + fuelGuage ){
fuelGuage = amtToAdd + fuelGuage;
System.out.println("you added " + amtToAdd + " gallons to your tank \n and now have "
+ fuelGuage + " gallons in your tank");
}else{
System.out.println("Can't add " + amtToAdd + " gallons, Your tank will overflow");
}
}
public void drive(int distance)
{
if(distance / mpg < fuelGuage){
System.out.println("You drove " + distance + " miles");
trip = distance;
fuelGuage = fuelGuage - distance / mpg;
odometer = odometer + distance;
}else{
System.out.println("You don't have enough fuel to go " + distance + " miles");
}
}
public void printCurrentStatus()
{
System.out.println("Current Status");
System.out.println("===========================================================");
System.out.println("odometer \t : " + odometer );
System.out.println("fuel guage \t : " + fuelGuage);
System.out.println("===========================================================");
}
/**
* test cases here
*/
public static void testing()
{
Auto myCar = new Auto(); // instantiate an automobile object named myCar
//
myCar.addFuel(12);
myCar.drive(100);
myCar.printCurrentStatus();
myCar.drive(100);
myCar.printCurrentStatus();
myCar.drive(150);
myCar.printCurrentStatus();
myCar.addFuel(13);
myCar.printCurrentStatus();
myCar.addFuel(10);
myCar.printCurrentStatus();
//
Auto yourCar = new Auto(1000, 16); // instantiate an automobile object named yourCar
Auto herCar = new Auto(1000, 10); // instantiate an automobile object named herCar
herCar.printCurrentStatus();
}
}