n Monday and Wednesday, we will be adding Exceptions to the Cars and GasPumps ex
ID: 3701362 • Letter: N
Question
n Monday and Wednesday, we will be adding Exceptions to the Cars and GasPumps example:
Creating Exceptions
Triggering Exceptions
Handling Exceptions
On Monday, we will create new Exception classes (We will do two together in class - you will do two on your own).
On Wednesday, we will use the new Exceptions and add try/catch blocks to our Driver. Again, we will do some in class, then you will do some on your own.
You will be able to see my completed solution once yours is graded.
------------------------------------------------
We will be creating 4 exceptions:
NegativeDataException: This Exception should be triggered/thrown whenever negative data is provided for values which cannot have negative values. Locations to update:Car:
Car() full constructor
setNumberDoors()
setCurFuelAmt()
setTankCapacity()
setCurSpeed()
GasPump:
GasPump() full constructor
setCurAmtFuel()
setAmtLastDispensed()
setPricePerGallon()
pumpGas()
reduceFuelAmount()
fillTank()
VehicleInMotionException: This Exception should be triggered/thrown whenever an attempt is made to pump gas while a vehicle is moving. Locations to update:GasPump:
pumpGas()
fillTank()
InsufficientCapacityException: This Exception should be triggered/thrown whenever there isn't enough capacity in the tank to add gas. Locations to update:GasPump:
pumpGas()
fillTank()
InsufficientGasException: This Exception should be triggered/thrown whenever an attempt is made to when there isn't enough gas in the fuel pump. Locations to update:GasPump:
pumpGas()
fillTank()
The classes are the following
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package carsandpumps;
/**
* This is a Car Object
* @author Lisa Minogue
*/
public class Car {
private int numberDoors;
private double curAmtFuel;
private double tankCapacity;
private int fuelType;
private int curSpeed;
private String model;
private int year;
/**
* Default constructor for Car. Sets up some initial values.
*/
public Car() {
model = "Default";
year = 1999;
curSpeed = 0;
fuelType = 1;
tankCapacity = 20;
curAmtFuel = 20;
numberDoors = 4;
}
/**
* Full Constructor for Car
* @param inModel a String representing the model of the Car
* @param inYear an int representing the year of the Car
* @param inCurSpeed an int representing the current speed of the Car
* @param inFuelType an int representing the kind of fuel the Car needs
* @param inCapacity a double representing the tank capacity of the Car
* @param inCurAmtFuel a double representing how much gas is currently in the Car
* @param inNumDoors an int representing how many doors the car has
*/
public Car(String inModel, int inYear, int inCurSpeed, int inFuelType, double inCapacity, double inCurAmtFuel, int inNumDoors) {
setModel (inModel);
setYear (inYear);
setCurSpeed (inCurSpeed);
setFuelType (inFuelType);
setTankCapacity (inCapacity);
setCurAmtFuel (inCurAmtFuel);
setNumberDoors (inNumDoors);
}
/**
* Retrieves the number of doors of the Car
* @return Number of doors for the Car as an int
*/
public int getNumberDoors() {
return numberDoors;
}
/**
* Sets the number of doors for the Car
* @param inNumberDoors New value for the Car's number of doors
*/
public void setNumberDoors(int inNumberDoors) {
this.numberDoors = inNumberDoors;
}
/**
* Retrieves the current amount of fuel for the Car
* @return current amount of fuel for the Car as a double
*/
public double getCurAmtFuel() {
return curAmtFuel;
}
/**
* Sets the current amount of fuel for the Car
* @param inCurAmtFuel New current amount of fuel for the Car
*/
public void setCurAmtFuel(double inCurAmtFuel) {
this.curAmtFuel = inCurAmtFuel;
}
/**
* Retrieves the gas tank capacity for the Car
* @return gas tank capacity for the Car as a double
*/
public double getTankCapacity() {
return tankCapacity;
}
/**
* Sets the gas tank capacity for the Car
* @param inTankCapacity New tank capacity for the Car
*/
public void setTankCapacity(double inTankCapacity) {
this.tankCapacity = inTankCapacity;
}
/**
* Retrieves the fuel type that the car takes
* @return the fuel type that the car takes as an int
*/
public int getFuelType() {
return fuelType;
}
/**
* Sets the fuel type that the car takes
* @param inFuelType New value for the fuel type for the Car
*/
public void setFuelType(int inFuelType) {
this.fuelType = inFuelType;
}
/**
* Retrieves the current speed that the Car is going
* @return the current speed of the car as an int
*/
public int getCurSpeed() {
return curSpeed;
}
/**
* Sets the current speed of the Car
* @param inCurSpeed New value for the current speed of the Car
*/
public void setCurSpeed(int inCurSpeed) {
this.curSpeed = inCurSpeed;
}
/**
* Retrieves the model of the Car
* @return the model of the Car as a String
*/
public String getModel() {
return model;
}
/**
* Sets the inModel of the Car
* @param inModel New value for the Car's inModel
*/
public void setModel(String inModel) {
this.model = inModel;
}
/**
* Retrieves the year of the Car
* @return the year of the Car as an int
*/
public int getYear() {
return year;
}
/**
* Sets the inYear of the Car
* @param inYear The new inYear for the Car
*/
public void setYear(int inYear) {
this.year = inYear;
}
/**
* Creates a String describing the Car
* @return A String which describes the Car
*/
@Override
public String toString() {
return "Car{" + "model=" + model + ", year=" + year + ", curAmtFuel=" + curAmtFuel + ", tankCapacity=" + tankCapacity + ", numberDoors=" + numberDoors + ", fuelType=" + fuelType + ", curSpeed=" + curSpeed + '}';
}
}
package carsandpumps;
import java.util.Random;
/**
* Runs our Car and GasPump simulation.
* Sets up data with hardcoded or randomized values. Uses arrays to hold the Cars and GasPumps.
* @author Lisa
*/
public class Driver {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Random rand = new Random();
// Create some GasPumps
GasPump[] gp = new GasPump[5];
for (int i = 0; i < 5; i++) {
// Get a random number between 100 and 200
int capacity = rand.nextInt (101) + 100;
gp[i] = new GasPump("Pump 1", 1, capacity, false);
}
// Print them
System.out.println ("** Initial setup of Gas Pumps **");
for (int i = 0; i < 5; i++) {
System.out.println (gp[i]);
}
System.out.println ("Total Fuel = " + GasPump.getTotalFuel());
// Create some Cars
Car[] car = new Car[3];
String[] initialCarModels = {"Toyota Camry", "Honda Civic", "Ford F=150"};
for (int i = 0; i < 3; i++) {
int year = rand.nextInt(19) + 2000;
int doors = (rand.nextInt(2) + 1) * 2; // Either 2 or 4 doors
double capacity = (rand.nextDouble() * 10) + 5; // Tank size of at least 5
int curAmt = (int)capacity - 2;
car[i] = new Car (initialCarModels[i], year, 0, 1, capacity, curAmt, doors);
}
System.out.println (" ** Initial setup of Cars **");
for (int i = 0; i < 3; i++) {
System.out.println (car[i]);
}
// Make car[1] moving. First, purposely set it to negative. Then set to 40.
car[1].setCurSpeed (-10);
car[1].setCurSpeed (40);
// Fill the cars with gas
for (int i = 0; i < 3; i++) {
gp[i].fillTank(car[i]);
}
System.out.println (" ** After filling all cars **");
System.out.println (gp[0]);
for (int i = 0; i < 3; i++) {
System.out.println (car[i]);
}
System.out.println ("Total Fuel = " + GasPump.getTotalFuel());
System.out.println (" ** Fill first car again **");
gp[1].fillTank (car[0]);
System.out.println (gp[1]);
System.out.println (car[0]);
System.out.println ("Total Fuel = " + GasPump.getTotalFuel());
System.out.println (" ** Attempt to fill car 3 with too much gas");
gp[2].pumpGas(car[2], car[2].getTankCapacity() + 5);
System.out.println (gp[2]);
System.out.println (car[2]);
System.out.println ("Total Fuel = " + GasPump.getTotalFuel());
System.out.println (" **Empty and refill car several times**");
// Empty car 3 gas tank and fill it several times
while (gp[2].getCurAmtFuel() > car[2].getCurAmtFuel()) {
car[2].setCurAmtFuel(0);
gp[2].fillTank(car[2]);
}
System.out.println (gp[2] + " " + car[2]);
System.out.println (" **One more time**");
car[2].setCurAmtFuel(0);
gp[2].fillTank(car[2]);
System.out.println (" **One last time**");
// Using NetBeans automagic insertion of catching Exceptions with Logger
car[2].setCurAmtFuel(0);
gp[2].fillTank(car[2]);
System.out.println (gp[2] + " " + car[2]);
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package carsandpumps;
/**
*
* @author Lisa
*/
public class GasPump {
private static double totalFuel;
private static int pricePerGallon; // Price in pennies
private int fuelType;
private boolean isDispensing;
private String pumpID;
private double curAmtFuel;
private boolean allowsCreditCard;
double amtLastDispensed;
/**
* Default Contructor. Sets up some initial values for GasPump.
*/
public GasPump() {
fuelType = 1;
isDispensing = false;
pumpID = "Default Pump";
curAmtFuel = 100;
allowsCreditCard = false;
amtLastDispensed = 0;
totalFuel += curAmtFuel;
}
/**
* Constructor for GasPump.
* New GasPumps can't be in the process of dispensing fuel and have never dispensed before,
* so the value amtLastDispensed is 0 and the value for isDispensing is false.
* @param id String value for the pump ID
* @param fuelType int value to identify the fuel type
* @param curAmtFuel double value indicating how much fuel is in the pump
* @param allowsCC boolean value denoting whether the pump accepts credit cards
*/
public GasPump (String id, int fuelType, double curAmtFuel, boolean allowsCC) {
pumpID = id;
setFuelType(fuelType);
setCurAmtFuel(curAmtFuel);
setAllowsCreditCard(allowsCC);
setIsDispensing (false);
setAmtLastDispensed(0);
totalFuel += curAmtFuel;
}
/**
* Retrieves the fuel type of the GasPump
* @return the fuel type of the GasPump as an int
*/
public int getFuelType() {
return fuelType;
}
/**
* Retrieves whether the GasPump is currently dispensing
* @return whether the GasPump is currently dispensing as a boolean
*/
public boolean isIsDispensing() {
return isDispensing;
}
/**
* Retrieves the ID of the GasPump
* @return the ID of the GasPump as a String
*/
public String getPumpID() {
return pumpID;
}
/**
* Retrieves the current amount of fuel in the GasPump
* @return the current amount of fuel in the GasPump as a double
*/
public double getCurAmtFuel() {
return curAmtFuel;
}
/**
* Retrieves whether the GasPump allows credit cards for payment
* @return whether the GasPump allows credit cards for payment as a boolean
*/
public boolean isAllowsCreditCard() {
return allowsCreditCard;
}
/**
* Retrieves the last amount of fuel that was dispensed from the GasPump
* @return the last amount of fuel that was dispensed from the GasPump as a double
*/
public double getAmtLastDispensed() {
return amtLastDispensed;
}
/**
* Retrieves the price per gallon of gas at this GasPump
* @return the price per gallon of gas at the GasPump as an int. The price is in pennies. (i.e. $2.53 would be 253)
*/
public static int getPricePerGallon() {
return pricePerGallon;
}
/**
* Updates the fuel type of the GasPump
* @param inFuelType the fuel type the GasPump dispenses
*/
public void setFuelType(int inFuelType) {
this.fuelType = inFuelType;
}
/**
* Updates whether the GasPump is currently dispensing fuel
* @param inIsDispensing new boolean value for whether the GasPump is currently dispensing fuel
*/
public void setIsDispensing(boolean inIsDispensing) {
this.isDispensing = inIsDispensing;
}
/**
* Updates the Identifier of the GasPump
* @param inPumpID new Identifier for the GasPump
*/
public void setPumpID(String inPumpID) {
this.pumpID = inPumpID;
}
/**
* Updates the current amount of fuel in the GasPump
* @param inCurAmtFuel the new value for the amount of fuel in the GasPump
*/
public void setCurAmtFuel(double inCurAmtFuel) {
this.curAmtFuel = inCurAmtFuel;
}
/**
* Updates whether the GasPump allows credit cards
* @param inAllowsCreditCard the new boolean value for whether the GasPump allows credit cards
*/
public void setAllowsCreditCard(boolean inAllowsCreditCard) {
this.allowsCreditCard = inAllowsCreditCard;
}
/**
* Updates the amount of gas last dispensed by the GasPump
* @param inAmtLastDispensed the new amount of gas last dispensed by the GasPump
*/
public void setAmtLastDispensed(double inAmtLastDispensed) {
this.amtLastDispensed = inAmtLastDispensed;
}
/**
* Updates the price per gallon of gas at the GasPump
* @param inPricePerGallon the new price per gallon of gas for the GasPump. The price is given in pennies (i.e. $2.53 would be 253)
*/
public static void setPricePerGallon(int inPricePerGallon) {
pricePerGallon = inPricePerGallon;
}
/**
* Returns the total amount of fuel in all the gas pumps
* @return the total amount of fuel in all gas pumps
*/
public static double getTotalFuel () {
return totalFuel;
}
/**
* Creates a String describing the GasPump
* @return A String which describes the GasPump
*/
@Override
public String toString() {
return "GasPump{ pumpID=" + pumpID + ", curAmtFuel=" + curAmtFuel + ", fuelType=" + fuelType + ", isDispensing=" + isDispensing + ", allowsCreditCard=" + allowsCreditCard + ", amtLastDispensed=" + amtLastDispensed + ", pricePerGallon=" + pricePerGallon + '}';
}
/**
* Reduces the total fuel for all gas pumps by the given amount
* @param fillAmt
*/
private static void reduceFuelAmount (double fillAmt) {
totalFuel -= fillAmt;
}
/**
* Adds the amount of fuel to the Car and decreases it from GasPump.
* @param inCar the car to pump fuel into
* @param inAmtOfFuel the amount of fuel to pump into the Car
* @return the amount of fuel that was pumped from the GasPump into the Car
* @author Lisa Minogue
*/
public double pumpGas (Car inCar, double inAmtOfFuel) {
// First check that car isn't moving
if (inCar.getCurSpeed() > 0) {
System.out.println ("Can't pump gas while car is moving");
return 0;
}
// Check to make sure there is room in the Car's gas tank
double carAmtToFull = inCar.getTankCapacity() - inCar.getCurAmtFuel();
double fillAmt = inAmtOfFuel;
if (carAmtToFull < inAmtOfFuel) {
System.out.println ("Not enough capacity in the tank to add " + inAmtOfFuel + " fuel. Adding " + carAmtToFull + " instead.");
fillAmt = carAmtToFull;
}
// Check to make sure there is enough gas in the fuel pump
if (getCurAmtFuel() < carAmtToFull) {
System.out.println ("Not enough gas in the fuel pump.");
fillAmt = getCurAmtFuel();
}
setCurAmtFuel(getCurAmtFuel() - fillAmt);
inCar.setCurAmtFuel(inCar.getCurAmtFuel() + fillAmt);
reduceFuelAmount(fillAmt);
return fillAmt;
}
/**
* Fills the tank of the Car. This method must determine how much gas is needed to fill the tank of the car.
* If there is any reason why the tank can't be filled, this method should not pump any fuel.
* @param inCar the Car whose tank is to be filled with fuel
* @return how much gas was pumped
*/
public double fillTank (Car inCar) {
// How much gas is needed?
double amtToFill = inCar.getTankCapacity() - inCar.getCurAmtFuel();
return pumpGas (inCar, amtToFill);
}
}
Explanation / Answer
/******* Re Written all three java files with exceptions . Expetions classes are written in GasPump.java *******/
public class Car {
private int numberDoors;
private double curAmtFuel;
private double tankCapacity;
private int fuelType;
private int curSpeed;
private String model;
private int year;
/**
* Default constructor for Car. Sets up some initial values.
*/
public Car() {
model = "Default";
year = 1999;
curSpeed = 0;
fuelType = 1;
tankCapacity = 20;
curAmtFuel = 20;
numberDoors = 4;
}
/**
* Full Constructor for Car
* @param inModel a String representing the model of the Car
* @param inYear an int representing the year of the Car
* @param inCurSpeed an int representing the current speed of the Car
* @param inFuelType an int representing the kind of fuel the Car needs
* @param inCapacity a double representing the tank capacity of the Car
* @param inCurAmtFuel a double representing how much gas is currently in the Car
* @param inNumDoors an int representing how many doors the car has
* @throws NegativeDataException
*/
public Car(String inModel, int inYear, int inCurSpeed, int inFuelType, double inCapacity, double inCurAmtFuel, int inNumDoors) throws NegativeDataException {
setModel (inModel);
setYear (inYear);
setCurSpeed (inCurSpeed);
setTankCapacity (inCapacity);
setCurAmtFuel (inCurAmtFuel);
setNumberDoors (inNumDoors);
setFuelType (inFuelType);
}
/**
* Retrieves the number of doors of the Car
* @return Number of doors for the Car as an int
*/
public int getNumberDoors() {
return numberDoors;
}
/**
* Sets the number of doors for the Car
* @param inNumberDoors New value for the Car's number of doors
* @throws NegativeDataException
*/
public void setNumberDoors(int inNumberDoors) throws NegativeDataException {
if(inNumberDoors < 0){
throw new NegativeDataException("Negative number of Doors");
}
this.numberDoors = inNumberDoors;
}
/**
* Retrieves the current amount of fuel for the Car
* @return current amount of fuel for the Car as a double
*/
public double getCurAmtFuel() {
return curAmtFuel;
}
/**
* Sets the current amount of fuel for the Car
* @param inCurAmtFuel New current amount of fuel for the Car
* @throws NegativeDataException
*/
public void setCurAmtFuel(double inCurAmtFuel) throws NegativeDataException {
if(inCurAmtFuel < 0){
throw new NegativeDataException("negative current amount Fuel");
}
this.curAmtFuel = inCurAmtFuel;
}
/**
* Retrieves the gas tank capacity for the Car
* @return gas tank capacity for the Car as a double
*/
public double getTankCapacity() {
return tankCapacity;
}
/**
* Sets the gas tank capacity for the Car
* @param inTankCapacity New tank capacity for the Car
* @throws NegativeDataException
*/
public void setTankCapacity(double inTankCapacity) throws NegativeDataException {
if(inTankCapacity < 0){
throw new NegativeDataException("Negative Tank Capacity");
}
this.tankCapacity = inTankCapacity;
}
/**
* Retrieves the fuel type that the car takes
* @return the fuel type that the car takes as an int
*/
public int getFuelType() {
return fuelType;
}
/**
* Sets the fuel type that the car takes
* @param inFuelType New value for the fuel type for the Car
*/
public void setFuelType(int inFuelType) {
this.fuelType = inFuelType;
}
/**
* Retrieves the current speed that the Car is going
* @return the current speed of the car as an int
*/
public int getCurSpeed() {
return curSpeed;
}
/**
* Sets the current speed of the Car
* @param inCurSpeed New value for the current speed of the Car
* @throws NegativeDataException
*/
public void setCurSpeed(int inCurSpeed) throws NegativeDataException {
if(inCurSpeed < 0){
throw new NegativeDataException("Negative current speed");
}
this.curSpeed = inCurSpeed;
}
/**
* Retrieves the model of the Car
* @return the model of the Car as a String
*/
public String getModel() {
return model;
}
/**
* Sets the inModel of the Car
* @param inModel New value for the Car's inModel
*/
public void setModel(String inModel) {
this.model = inModel;
}
/**
* Retrieves the year of the Car
* @return the year of the Car as an int
*/
public int getYear() {
return year;
}
/**
* Sets the inYear of the Car
* @param inYear The new inYear for the Car
*/
public void setYear(int inYear) {
this.year = inYear;
}
/**
* Creates a String describing the Car
* @return A String which describes the Car
*/
@Override
public String toString() {
return "Car{" + "model=" + model + ", year=" + year + ", curAmtFuel=" + curAmtFuel + ", tankCapacity=" + tankCapacity + ", numberDoors=" + numberDoors + ", fuelType=" + fuelType + ", curSpeed=" + curSpeed + '}';
}
}
/**** Gas Pump ****/
public class GasPump {
private static double totalFuel;
private static int pricePerGallon; // Price in pennies
private int fuelType;
private boolean isDispensing;
private String pumpID;
private double curAmtFuel;
private boolean allowsCreditCard;
double amtLastDispensed;
/**
* Default Contructor. Sets up some initial values for GasPump.
*/
public GasPump() {
fuelType = 1;
isDispensing = false;
pumpID = "Default Pump";
curAmtFuel = 100;
allowsCreditCard = false;
amtLastDispensed = 0;
totalFuel += curAmtFuel;
}
/**
* Constructor for GasPump.
* New GasPumps can't be in the process of dispensing fuel and have never dispensed before,
* so the value amtLastDispensed is 0 and the value for isDispensing is false.
* @param id String value for the pump ID
* @param fuelType int value to identify the fuel type
* @param curAmtFuel double value indicating how much fuel is in the pump
* @param allowsCC boolean value denoting whether the pump accepts credit cards
* @throws NegativeDataException
*/
public GasPump (String id, int fuelType, double curAmtFuel, boolean allowsCC) throws NegativeDataException {
pumpID = id;
setFuelType(fuelType);
setCurAmtFuel(curAmtFuel);
setAmtLastDispensed(0);
setAllowsCreditCard(allowsCC);
setIsDispensing (false);
totalFuel += curAmtFuel;
}
/**
* Retrieves the fuel type of the GasPump
* @return the fuel type of the GasPump as an int
*/
public int getFuelType() {
return fuelType;
}
/**
* Retrieves whether the GasPump is currently dispensing
* @return whether the GasPump is currently dispensing as a boolean
*/
public boolean isIsDispensing() {
return isDispensing;
}
/**
* Retrieves the ID of the GasPump
* @return the ID of the GasPump as a String
*/
public String getPumpID() {
return pumpID;
}
/**
* Retrieves the current amount of fuel in the GasPump
* @return the current amount of fuel in the GasPump as a double
*/
public double getCurAmtFuel() {
return curAmtFuel;
}
/**
* Retrieves whether the GasPump allows credit cards for payment
* @return whether the GasPump allows credit cards for payment as a boolean
*/
public boolean isAllowsCreditCard() {
return allowsCreditCard;
}
/**
* Retrieves the last amount of fuel that was dispensed from the GasPump
* @return the last amount of fuel that was dispensed from the GasPump as a double
*/
public double getAmtLastDispensed() {
return amtLastDispensed;
}
/**
* Retrieves the price per gallon of gas at this GasPump
* @return the price per gallon of gas at the GasPump as an int. The price is in pennies. (i.e. $2.53 would be 253)
*/
public static int getPricePerGallon() {
return pricePerGallon;
}
/**
* Updates the fuel type of the GasPump
* @param inFuelType the fuel type the GasPump dispenses
*/
public void setFuelType(int inFuelType) {
this.fuelType = inFuelType;
}
/**
* Updates whether the GasPump is currently dispensing fuel
* @param inIsDispensing new boolean value for whether the GasPump is currently dispensing fuel
*/
public void setIsDispensing(boolean inIsDispensing) {
this.isDispensing = inIsDispensing;
}
/**
* Updates the Identifier of the GasPump
* @param inPumpID new Identifier for the GasPump
*/
public void setPumpID(String inPumpID) {
this.pumpID = inPumpID;
}
/**
* Updates the current amount of fuel in the GasPump
* @param inCurAmtFuel the new value for the amount of fuel in the GasPump
* @throws NegativeDataException
*/
public void setCurAmtFuel(double inCurAmtFuel) throws NegativeDataException {
if(inCurAmtFuel < 0){
throw new NegativeDataException("Negative current fuel amount");
}
this.curAmtFuel = inCurAmtFuel;
}
/**
* Updates whether the GasPump allows credit cards
* @param inAllowsCreditCard the new boolean value for whether the GasPump allows credit cards
*/
public void setAllowsCreditCard(boolean inAllowsCreditCard) {
this.allowsCreditCard = inAllowsCreditCard;
}
/**
* Updates the amount of gas last dispensed by the GasPump
* @param inAmtLastDispensed the new amount of gas last dispensed by the GasPump
* @throws NegativeDataException
*/
public void setAmtLastDispensed(double inAmtLastDispensed) throws NegativeDataException {
if(inAmtLastDispensed < 0){
throw new NegativeDataException("Negative Amount last dispensed");
}
this.amtLastDispensed = inAmtLastDispensed;
}
/**
* Updates the price per gallon of gas at the GasPump
* @param inPricePerGallon the new price per gallon of gas for the GasPump. The price is given in pennies (i.e. $2.53 would be 253)
* @throws NegativeDataException
*/
public static void setPricePerGallon(int inPricePerGallon) throws NegativeDataException {
if(inPricePerGallon < 0){
throw new NegativeDataException("Negative Price per Gallon");
}
pricePerGallon = inPricePerGallon;
}
/**
* Returns the total amount of fuel in all the gas pumps
* @return the total amount of fuel in all gas pumps
*/
public static double getTotalFuel () {
return totalFuel;
}
/**
* Creates a String describing the GasPump
* @return A String which describes the GasPump
*/
@Override
public String toString() {
return "GasPump{ pumpID=" + pumpID + ", curAmtFuel=" + curAmtFuel + ", fuelType=" + fuelType + ", isDispensing=" + isDispensing + ", allowsCreditCard=" + allowsCreditCard + ", amtLastDispensed=" + amtLastDispensed + ", pricePerGallon=" + pricePerGallon + '}';
}
/**
* Reduces the total fuel for all gas pumps by the given amount
* @param fillAmt
* @throws NegativeDataException
*/
private static void reduceFuelAmount (double fillAmt) throws NegativeDataException {
if(fillAmt < 0){
throw new NegativeDataException("Negative fill Amount in reduceFuelAmount");
}
totalFuel -= fillAmt;
}
/**
* Adds the amount of fuel to the Car and decreases it from GasPump.
* @param inCar the car to pump fuel into
* @param inAmtOfFuel the amount of fuel to pump into the Car
* @return the amount of fuel that was pumped from the GasPump into the Car
* @author Lisa Minogue
* @throws NegativeDataException
* @throws VehicleInMotionException
* @throws InsufficientGasException
* @throws InsufficientCapacityException
*/
public double pumpGas (Car inCar, double inAmtOfFuel) throws NegativeDataException, VehicleInMotionException, InsufficientGasException, InsufficientCapacityException {
if(inAmtOfFuel < 0){
throw new NegativeDataException("Negative Amount of Fuel while Pumping Gas");
}
// First check that car isn't moving
if (inCar.getCurSpeed() > 0) {
throw new VehicleInMotionException("Can't pump gas while car is moving");
}
// Check to make sure there is room in the Car's gas tank
double carAmtToFull = inCar.getTankCapacity() - inCar.getCurAmtFuel();
double fillAmt = inAmtOfFuel;
if (carAmtToFull < inAmtOfFuel) {
fillAmt = carAmtToFull;
throw new InsufficientCapacityException("Not enough capacity in the tank to add " + inAmtOfFuel + " fuel. Adding " + carAmtToFull + " instead.");
}
// Check to make sure there is enough gas in the fuel pump
if (getCurAmtFuel() < carAmtToFull) {
fillAmt = getCurAmtFuel();
throw new InsufficientGasException("Not enough gas in the fuel pump.");
}
setCurAmtFuel(getCurAmtFuel() - fillAmt);
inCar.setCurAmtFuel(inCar.getCurAmtFuel() + fillAmt);
reduceFuelAmount(fillAmt);
return fillAmt;
}
/**
* Fills the tank of the Car. This method must determine how much gas is needed to fill the tank of the car.
* If there is any reason why the tank can't be filled, this method should not pump any fuel.
* @param inCar the Car whose tank is to be filled with fuel
* @return how much gas was pumped
* @throws NegativeDataException
* @throws VehicleInMotionException
* @throws InsufficientCapacityException
* @throws InsufficientGasException
*/
public double fillTank (Car inCar) throws NegativeDataException, VehicleInMotionException, InsufficientGasException, InsufficientCapacityException {
// How much gas is needed?
double amtToFill = inCar.getTankCapacity() - inCar.getCurAmtFuel();
return pumpGas (inCar, amtToFill);
}
}
class NegativeDataException extends Exception{
public NegativeDataException(String s){
super(s);
}
}
class VehicleInMotionException extends Exception{
public VehicleInMotionException(String s){
super(s);
}
}
class InsufficientCapacityException extends Exception{
public InsufficientCapacityException(String s){
super(s);
}
}
class InsufficientGasException extends Exception{
public InsufficientGasException(String s){
super(s);
}
}
/******* Driver.java*****/
import java.util.Random;
/**
* Runs our Car and GasPump simulation.
* Sets up data with hardcoded or randomized values. Uses arrays to hold the Cars and GasPumps.
* @author Lisa
*/
public class Driver {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Random rand = new Random();
// Create some GasPumps
try{
GasPump[] gp = new GasPump[5];
for (int i = 0; i < 5; i++) {
// Get a random number between 100 and 200
int capacity = rand.nextInt (101) + 100;
gp[i] = new GasPump("Pump 1", 1, capacity, false);
}
// Print them
System.out.println ("** Initial setup of Gas Pumps **");
for (int i = 0; i < 5; i++) {
System.out.println (gp[i]);
}
System.out.println ("Total Fuel = " + GasPump.getTotalFuel());
// Create some Cars
Car[] car = new Car[3];
String[] initialCarModels = {"Toyota Camry", "Honda Civic", "Ford F=150"};
for (int i = 0; i < 3; i++) {
int year = rand.nextInt(19) + 2000;
int doors = (rand.nextInt(2) + 1) * 2; // Either 2 or 4 doors
double capacity = (rand.nextDouble() * 10) + 5; // Tank size of at least 5
int curAmt = (int)capacity - 2;
car[i] = new Car (initialCarModels[i], year, 0, 1, capacity, curAmt, doors);
}
System.out.println (" ** Initial setup of Cars **");
for (int i = 0; i < 3; i++) {
System.out.println (car[i]);
}
// Make car[1] moving. First, purposely set it to negative. Then set to 40.
car[1].setCurSpeed (-10);
car[1].setCurSpeed (40);
// Fill the cars with gas
for (int i = 0; i < 3; i++) {
gp[i].fillTank(car[i]);
}
System.out.println (" ** After filling all cars **");
System.out.println (gp[0]);
for (int i = 0; i < 3; i++) {
System.out.println (car[i]);
}
System.out.println ("Total Fuel = " + GasPump.getTotalFuel());
System.out.println (" ** Fill first car again **");
gp[1].fillTank (car[0]);
System.out.println (gp[1]);
System.out.println (car[0]);
System.out.println ("Total Fuel = " + GasPump.getTotalFuel());
System.out.println (" ** Attempt to fill car 3 with too much gas");
gp[2].pumpGas(car[2], car[2].getTankCapacity() + 5);
System.out.println (gp[2]);
System.out.println (car[2]);
System.out.println ("Total Fuel = " + GasPump.getTotalFuel());
System.out.println (" **Empty and refill car several times**");
// Empty car 3 gas tank and fill it several times
while (gp[2].getCurAmtFuel() > car[2].getCurAmtFuel()) {
car[2].setCurAmtFuel(0);
gp[2].fillTank(car[2]);
}
System.out.println (gp[2] + " " + car[2]);
System.out.println (" **One more time**");
car[2].setCurAmtFuel(0);
gp[2].fillTank(car[2]);
System.out.println (" **One last time**");
// Using NetBeans automagic insertion of catching Exceptions with Logger
car[2].setCurAmtFuel(0);
gp[2].fillTank(car[2]);
System.out.println (gp[2] + " " + car[2]);
} catch(InsufficientCapacityException| NegativeDataException|InsufficientGasException|VehicleInMotionException e){
e.printStackTrace();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.